My Project
OSgams2osil.cpp
Go to the documentation of this file.
1// Copyright (C) GAMS Development and others 2007-2009
2// All Rights Reserved.
3// This code is published under the Eclipse Public License.
4//
5// $Id$
6//
7// Author: Stefan Vigerske
8
9#include "OSgams2osil.hpp"
10#include "GAMSlinksConfig.h"
11#include "GamsNLinstr.h"
12
13#include "OSInstance.h"
14#include "CoinHelperFunctions.hpp"
15
16#include "gmomcc.h"
17#include "gevmcc.h"
18
19#include <sstream>
20
21OSgams2osil::OSgams2osil(gmoHandle_t gmo_)
22 : gev(gmo_ ? (gevHandle_t)gmoEnvironment(gmo_) : NULL), gmo(gmo_), osinstance(NULL)
23{
24 //assert(gev_ == NULL); // TODO better
25 assert(gmo_ == NULL); // TODO better
26}
27
28OSgams2osil::OSgams2osil(std::string gamsControlFile)
29 : gev(NULL), gmo(NULL), osinstance(NULL)
30{
31 initGMO( gamsControlFile.c_str() ) ;
32}
33
35{
36 delete osinstance;
37
38 //close output channels
39// gmoCloseGms(gmo);
40 gmoFree(&gmo);
41 gevFree(&gev);
42 gmoLibraryUnload();
43 gevLibraryUnload();
44}
45
46bool OSgams2osil::initGMO(const char* datfile)
47{
48 assert(gmo == NULL);
49 assert(gev == NULL);
50
51 char msg[1024];
52 int rc;
53
54 if (!gmoCreate(&gmo, msg, sizeof(msg)))
55 {
58 osrl = osrlwriter->writeOSrL( osresult);
59 throw ErrorClass( osrl);
60 }
61
62 if (!gevCreate(&gev, msg, sizeof(msg)))
63 {
66 osrl = osrlwriter->writeOSrL( osresult);
67 throw ErrorClass( osrl);
68 }
69
70
71 gmoIdentSet(gmo, "OS link object");
72
73 // load control file
74 if ((rc = gevInitEnvironmentLegacy(gev, datfile)))
75 {
76 ostringstream outStr;
77 outStr << "Could not initialize GAMS environment: " << datfile << " Rc = " << rc << std::endl;
78 osresult->setGeneralMessage(outStr.str());
80 osrl = osrlwriter->writeOSrL( osresult);
81 gmoFree(&gmo);
82 gevFree(&gev);
83 throw ErrorClass( osrl);
84 }
85
86 if ((rc = gmoRegisterEnvironment(gmo, gev, msg)))
87 {
88 osresult->setGeneralMessage("Could not register GAMS environment.");
90 osrl = osrlwriter->writeOSrL( osresult);
91 gmoFree(&gmo);
92 gevFree(&gev);
93 throw ErrorClass( osrl);
94 }
95
96 if ((rc = gmoLoadDataLegacy(gmo, msg)))
97 {
98 osresult->setGeneralMessage("Could not load model data.");
100 osrl = osrlwriter->writeOSrL( osresult);
101 gmoFree(&gmo);
102 gevFree(&gev);
103 throw ErrorClass( osrl);
104 }
105
106 gmoMinfSet(gmo, -OSDBL_MAX);
107 gmoPinfSet(gmo, OSDBL_MAX);
108 gmoObjReformSet(gmo, 1);
109 gmoObjStyleSet(gmo, ObjType_Fun);
110 gmoIndexBaseSet(gmo, 0);
111
112 return true;
113}
114
116{
117 assert(gmo != NULL);
118 assert(gev != NULL);
119
120 osinstance = new OSInstance();
121 int i, j;
122 char buffer[255];
123
124 // unfortunately, we do not know the model name
125 osinstance->setInstanceDescription("Generated from GAMS modeling object");
127
128 char* vartypes = new char[gmoN(gmo)];
129 std::string* varnames = new std::string[gmoN(gmo)];
130 for(i = 0; i < gmoN(gmo); ++i)
131 {
132 switch (gmoGetVarTypeOne(gmo, i))
133 {
134 case var_X:
135 vartypes[i] = 'C';
136 break;
137 case var_B:
138 vartypes[i] = 'B';
139 break;
140 case var_I:
141 vartypes[i] = 'I';
142 break;
143 default :
144 {
145 // TODO: how to represent semicontinuous var. and SOS in OSiL ?
146 gevLogStat(gev, "Error: Unsupported variable type.");
147 return false;
148 }
149 }
150 gmoGetVarNameOne(gmo, i, buffer);
151 varnames[i] = buffer;
152 }
153
154 double* varlow = new double[gmoN(gmo)];
155 double* varup = new double[gmoN(gmo)];
156 gmoGetVarLower(gmo, varlow);
157 gmoGetVarUpper(gmo, varup);
158
159 if (!osinstance->setVariables(gmoN(gmo), varnames, varlow, varup, vartypes))
160 return false;
161
162 delete[] vartypes;
163 delete[] varnames;
164 delete[] varlow;
165 delete[] varup;
166
167 if (gmoModelType(gmo) == Proc_cns) // no objective in constraint satisfaction models
168 {
170 }
171 else // setup objective
172 {
174
175 SparseVector* objectiveCoefficients = new SparseVector(gmoObjNZ(gmo) - gmoObjNLNZ(gmo));
176
177 int* colidx = new int[gmoObjNZ(gmo)];
178 double* val = new double[gmoObjNZ(gmo)];
179 int* nlflag = new int[gmoObjNZ(gmo)];
180 int* dummy = new int[gmoObjNZ(gmo)];
181
182 if (gmoObjNZ(gmo)) nlflag[0] = 0; // workaround for gmo bug
183 gmoGetObjSparse(gmo, colidx, val, nlflag, dummy, dummy);
184 for (i = 0, j = 0; i < gmoObjNZ(gmo); ++i)
185 {
186 if (nlflag[i]) continue;
187 objectiveCoefficients->indexes[j] = colidx[i];
188 objectiveCoefficients->values[j] = val[i];
189 j++;
190 assert(j <= gmoObjNZ(gmo) - gmoObjNLNZ(gmo));
191 }
192 assert(j == gmoObjNZ(gmo) - gmoObjNLNZ(gmo));
193
194 delete[] colidx;
195 delete[] val;
196 delete[] nlflag;
197 delete[] dummy;
198
199 std::string objname = "objective"; //TODO
200 if (!osinstance->addObjective(-1, objname, gmoSense(gmo) == Obj_Min ? "min" : "max", gmoObjConst(gmo), 1., objectiveCoefficients))
201 {
202 delete objectiveCoefficients;
203 return false;
204 }
205 delete objectiveCoefficients;
206 }
207
209
210 double lb, ub;
211 for (i = 0; i < gmoM(gmo); ++i)
212 {
213 switch (gmoGetEquTypeOne(gmo, i))
214 {
215 case equ_E:
216 lb = ub = gmoGetRhsOne(gmo, i);
217 break;
218 case equ_L:
219 lb = -OSDBL_MAX;
220 ub = gmoGetRhsOne(gmo, i);
221 break;
222 case equ_G:
223 lb = gmoGetRhsOne(gmo, i);
224 ub = OSDBL_MAX;
225 break;
226 case equ_N:
227 lb = -OSDBL_MAX;
228 ub = OSDBL_MAX;
229 break;
230 default:
231 gevLogStat(gev, "Error: Unknown row type. Exiting ...");
232 return false;
233 }
234 std::string conname;
235 gmoGetEquNameOne(gmo, i, buffer);
236 conname = buffer;
237 if (!osinstance->addConstraint(i, conname, lb, ub, 0.))
238 return false;
239 }
240
241 int nz = gmoNZ(gmo);
242 double* values = new double[nz];
243 int* colstarts = new int[gmoN(gmo)+1];
244 int* rowindexes = new int[nz];
245 int* nlflags = new int[nz];
246
247 gmoGetMatrixCol(gmo, colstarts, rowindexes, values, nlflags);
248// for (i = 0; i < gmoNZ(gmo); ++i)
249// if (nlflags[i]) values[i] = 0.;
250 colstarts[gmoN(gmo)] = nz;
251
252 int shift = 0;
253 for (int col = 0; col < gmoN(gmo); ++col)
254 {
255 colstarts[col+1] -= shift;
256 int k = colstarts[col];
257 while (k < colstarts[col+1])
258 {
259 values[k] = values[k+shift];
260 rowindexes[k] = rowindexes[k+shift];
261 if (nlflags[k+shift])
262 {
263 ++shift;
264 --colstarts[col+1];
265 }
266 else
267 {
268 ++k;
269 }
270 }
271 }
272 nz -= shift;
273
275 values, 0, nz-1,
276 rowindexes, 0, nz-1,
277 colstarts, 0, gmoN(gmo)))
278 {
279 delete[] nlflags;
280 return false;
281 }
282
283 // values, colstarts, rowindexes are deleted by OSInstance
284 delete[] nlflags;
285
286 if (!gmoObjNLNZ(gmo) && !gmoNLNZ(gmo)) // everything linear -> finished
287 return true;
288
291 int iNLidx = 0;
292
293 int* opcodes = new int[gmoMaxSingleFNL(gmo)+1];
294 int* fields = new int[gmoMaxSingleFNL(gmo)+1];
295 int constantlen = gmoNLConst(gmo);
296 double* constants = (double*)gmoPPool(gmo);
297 int codelen;
298
299 OSnLNode* nl;
300 if (gmoObjNLNZ(gmo))
301 {
302 std::clog << "parsing nonlinear objective instructions" << std::endl;
303 gmoDirtyGetObjFNLInstr(gmo, &codelen, opcodes, fields);
304
305 nl = parseGamsInstructions(codelen, opcodes, fields, constantlen, constants);
306 if (!nl) return false;
307
308 double objjacval = gmoObjJacVal(gmo);
309 std::clog << "obj jac val: " << objjacval << std::endl;
310 if (objjacval == 1.) // scale by -1/objjacval = negate
311 {
312 OSnLNode* negnode = new OSnLNodeNegate;
313 negnode->m_mChildren[0] = nl;
314 nl = negnode;
315 }
316 else if (objjacval != -1.) // scale by -1/objjacval
317 {
318 OSnLNodeNumber* numbernode = new OSnLNodeNumber();
319 numbernode->value = -1/objjacval;
320 OSnLNodeTimes* timesnode = new OSnLNodeTimes();
321 timesnode->m_mChildren[0] = nl;
322 timesnode->m_mChildren[1] = numbernode;
323 nl = timesnode;
324 }
325 assert(iNLidx < osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions);
330 ++iNLidx;
331 }
332
333 for (i = 0; i < gmoM(gmo); ++i)
334 {
335 if (gmoDirtyGetRowFNLInstr(gmo, i, &codelen, opcodes, fields))
336 {
337 std::clog << "got nonzero return at constraint " << i << std::endl;
338 }
339 if (!codelen) continue;
340 std::clog << "parsing " << codelen << " nonlinear instructions of constraint " << osinstance->getConstraintNames()[i] << std::endl;
341 nl = parseGamsInstructions(codelen, opcodes, fields, constantlen, constants);
342 if (!nl) return false;
343 assert(iNLidx < osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions);
345 osinstance->instanceData->nonlinearExpressions->nl[iNLidx]->idx = i; // correct that this is the con. number?
348 ++iNLidx;
349 }
351
352 return true;
353}
354
356{
357 OSInstance* osinst = osinstance;
358 osinstance = NULL;
359 return osinst;
360}
361
362OSnLNode* OSgams2osil::parseGamsInstructions(int codelen, int* opcodes, int* fields, int constantlen, double* constants)
363{
364 std::vector<OSnLNode*> nlNodeVec;
365
366 const bool debugoutput = false;
367
368// for (int i=0; i<codelen; ++i)
369// std::clog << i << '\t' << GamsOpCodeName[opcodes[i+1]] << '\t' << fields[i+1] << std::endl;
370
371 nlNodeVec.reserve(codelen);
372
373 for (int i=0; i<codelen; ++i)
374 {
375 GamsOpCode opcode = (GamsOpCode)opcodes[i];
376 int address = fields[i]-1;
377
378 if (debugoutput) std::clog << '\t' << GamsOpCodeName[opcode] << ": ";
379// if (opcode == nlStore) {
380// std::clog << "stop" << std::endl;
381// break;
382// }
383 switch(opcode)
384 {
385 case nlNoOp : // no operation
386 {
387 if (debugoutput) std::clog << "ignored" << std::endl;
388 }
389 break;
390 case nlPushV : // push variable
391 {
392 address = gmoGetjSolver(gmo, address);
393 if (debugoutput) std::clog << "push variable " << osinstance->getVariableNames()[address] << std::endl;
394 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
395 nlNode->idx=address;
396 nlNodeVec.push_back( nlNode );
397 }
398 break;
399 case nlPushI : // push constant
400 {
401 if (debugoutput) std::clog << "push constant " << constants[address] << std::endl;
402 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
403 nlNode->value = constants[address];
404 nlNodeVec.push_back( nlNode );
405 }
406 break;
407 case nlStore: // store row
408 {
409 if (debugoutput) std::clog << "ignored" << std::endl;
410 }
411 break;
412 case nlAdd : // add
413 {
414 if (debugoutput) std::clog << "add" << std::endl;
415 nlNodeVec.push_back( new OSnLNodePlus() );
416 }
417 break;
418 case nlAddV: // add variable
419 {
420 address = gmoGetjSolver(gmo, address);
421 if (debugoutput) std::clog << "add variable " << osinstance->getVariableNames()[address] << std::endl;
422 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
423 nlNode->idx=address;
424 nlNodeVec.push_back( nlNode );
425 nlNodeVec.push_back( new OSnLNodePlus() );
426 }
427 break;
428 case nlAddI: // add immediate
429 {
430 if (debugoutput) std::clog << "add constant " << constants[address] << std::endl;
431 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
432 nlNode->value = constants[address];
433 nlNodeVec.push_back( nlNode );
434 nlNodeVec.push_back( new OSnLNodePlus() );
435 }
436 break;
437 case nlSub: // minus
438 {
439 if (debugoutput) std::clog << "minus" << std::endl;
440 nlNodeVec.push_back( new OSnLNodeMinus() );
441 }
442 break;
443 case nlSubV: // subtract variable
444 {
445 address = gmoGetjSolver(gmo, address);
446 if (debugoutput) std::clog << "substract variable " << osinstance->getVariableNames()[address] << std::endl;
447 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
448 nlNode->idx=address;
449 nlNodeVec.push_back( nlNode );
450 nlNodeVec.push_back( new OSnLNodeMinus() );
451 }
452 break;
453 case nlSubI: // subtract immediate
454 {
455 if (debugoutput) std::clog << "substract constant " << constants[address] << std::endl;
456 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
457 nlNode->value = constants[address];
458 nlNodeVec.push_back( nlNode );
459 nlNodeVec.push_back( new OSnLNodeMinus() );
460 }
461 break;
462 case nlMul: // multiply
463 {
464 if (debugoutput) std::clog << "multiply" << std::endl;
465 nlNodeVec.push_back( new OSnLNodeTimes() );
466 }
467 break;
468 case nlMulV: // multiply variable
469 {
470 address = gmoGetjSolver(gmo, address);
471 if (debugoutput) std::clog << "multiply variable " << osinstance->getVariableNames()[address] << std::endl;
472 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
473 nlNode->idx=address;
474 nlNodeVec.push_back( nlNode );
475 nlNodeVec.push_back( new OSnLNodeTimes() );
476 }
477 break;
478 case nlMulI: // multiply immediate
479 {
480 if (debugoutput) std::clog << "multiply constant " << constants[address] << std::endl;
481 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
482 nlNode->value = constants[address];
483 nlNodeVec.push_back( nlNode );
484 nlNodeVec.push_back( new OSnLNodeTimes() );
485 }
486 break;
487 case nlDiv: // divide
488 {
489 if (debugoutput) std::clog << "divide" << std::endl;
490 nlNodeVec.push_back( new OSnLNodeDivide() );
491 }
492 break;
493 case nlDivV: // divide variable
494 {
495 address = gmoGetjSolver(gmo, address);
496 if (debugoutput) std::clog << "divide variable " << osinstance->getVariableNames()[address] << std::endl;
497 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
498 nlNode->idx=address;
499 nlNodeVec.push_back( nlNode );
500 nlNodeVec.push_back( new OSnLNodeDivide() );
501 }
502 break;
503 case nlDivI: // divide immediate
504 {
505 if (debugoutput) std::clog << "divide constant " << constants[address] << std::endl;
506 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
507 nlNode->value = constants[address];
508 nlNodeVec.push_back( nlNode );
509 nlNodeVec.push_back( new OSnLNodeDivide() );
510 }
511 break;
512 case nlUMin: // unary minus
513 {
514 if (debugoutput) std::clog << "negate" << std::endl;
515 nlNodeVec.push_back( new OSnLNodeNegate() );
516 }
517 break;
518 case nlUMinV: // unary minus variable
519 {
520 address = gmoGetjSolver(gmo, address);
521 if (debugoutput) std::clog << "push negated variable " << osinstance->getVariableNames()[address] << std::endl;
522 OSnLNodeVariable *nlNode = new OSnLNodeVariable();
523 nlNode->idx = address;
524 nlNode->coef = -1.;
525 nlNodeVec.push_back( nlNode );
526 }
527 break;
528 case nlCallArg1 :
529 case nlCallArg2 :
530 case nlCallArgN :
531 {
532 if (debugoutput) std::clog << "call function ";
533 GamsFuncCode func = GamsFuncCode(address+1); // here the shift by one was not a good idea
534 switch (func)
535 {
536 case fnmin :
537 {
538 if (debugoutput) std::clog << "min" << std::endl;
539 nlNodeVec.push_back( new OSnLNodeMin() );
540 }
541 break;
542 case fnmax :
543 {
544 if (debugoutput) std::clog << "max" << std::endl;
545 nlNodeVec.push_back( new OSnLNodeMax() );
546 }
547 break;
548 case fnsqr :
549 {
550 if (debugoutput) std::clog << "square" << std::endl;
551 nlNodeVec.push_back( new OSnLNodeSquare() );
552 }
553 break;
554 case fnexp:
555 case fnslexp:
556 case fnsqexp:
557 {
558 if (debugoutput) std::clog << "exp" << std::endl;
559 nlNodeVec.push_back( new OSnLNodeExp() );
560 }
561 break;
562 case fnlog :
563 {
564 if (debugoutput) std::clog << "ln" << std::endl;
565 nlNodeVec.push_back( new OSnLNodeLn() );
566 }
567 break;
568 case fnlog10:
569 case fnsllog10:
570 case fnsqlog10:
571 {
572 if (debugoutput) std::clog << "log10 = ln * 1/ln(10)" << std::endl;
573 nlNodeVec.push_back( new OSnLNodeLn() );
574 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
575 nlNode->value = 1./log(10.);
576 nlNodeVec.push_back( nlNode );
577 nlNodeVec.push_back( new OSnLNodeTimes() );
578 }
579 break;
580 case fnlog2 :
581 {
582 if (debugoutput) std::clog << "log2 = ln * 1/ln(2)" << std::endl;
583 nlNodeVec.push_back( new OSnLNodeLn() );
584 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
585 nlNode->value = 1./log(2.);
586 nlNodeVec.push_back( nlNode );
587 nlNodeVec.push_back( new OSnLNodeTimes() );
588 }
589 break;
590 case fnsqrt:
591 {
592 if (debugoutput) std::clog << "sqrt" << std::endl;
593 nlNodeVec.push_back( new OSnLNodeSqrt() );
594 }
595 break;
596 case fnabs:
597 {
598 if (debugoutput) std::clog << "abs" << std::endl;
599 nlNodeVec.push_back( new OSnLNodeAbs() );
600 }
601 break;
602 case fncos:
603 {
604 if (debugoutput) std::clog << "cos" << std::endl;
605 nlNodeVec.push_back( new OSnLNodeCos() );
606 }
607 break;
608 case fnsin:
609 {
610 if (debugoutput) std::clog << "sin" << std::endl;
611 nlNodeVec.push_back( new OSnLNodeSin() );
612 }
613 break;
614 case fnpower:
615 case fnrpower: // x ^ y
616 case fncvpower: // constant ^ x
617 case fnvcpower: // x ^ constant {
618 {
619 if (debugoutput) std::clog << "power" << std::endl;
620 nlNodeVec.push_back( new OSnLNodePower() );
621 }
622 break;
623 case fnpi:
624 {
625 if (debugoutput) std::clog << "pi" << std::endl;
626 nlNodeVec.push_back( new OSnLNodePI() );
627 }
628 break;
629 case fndiv:
630 case fndiv0:
631 {
632 nlNodeVec.push_back( new OSnLNodeDivide() );
633 }
634 break;
635 case fnslrec: // 1/x
636 case fnsqrec: // 1/x
637 {
638 if (debugoutput) std::clog << "divide" << std::endl;
639 nlNodeVec.push_back( new OSnLNodeLn() );
640 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
641 nlNode->value = 1.;
642 nlNodeVec.push_back( nlNode );
643 nlNodeVec.push_back( new OSnLNodeDivide() );
644 }
645 break;
646 case fnceil:
647 case fnfloor:
648 case fnround:
649 case fnmod:
650 case fntrunc:
651 case fnsign:
652 case fnarctan:
653 case fnerrf:
654 case fndunfm:
655 case fndnorm:
656 case fnerror:
657 case fnfrac:
658 case fnerrorl:
659 case fnfact /* factorial */:
660 case fnunfmi /* uniform random number */:
661 case fnncpf /* fischer: sqrt(x1^2+x2^2+2*x3) */:
662 case fnncpcm /* chen-mangasarian: x1-x3*ln(1+exp((x1-x2)/x3))*/:
663 case fnentropy /* x*ln(x) */:
664 case fnsigmoid /* 1/(1+exp(-x)) */:
665 case fnboolnot:
666 case fnbooland:
667 case fnboolor:
668 case fnboolxor:
669 case fnboolimp:
670 case fnbooleqv:
671 case fnrelopeq:
672 case fnrelopgt:
673 case fnrelopge:
674 case fnreloplt:
675 case fnrelople:
676 case fnrelopne:
677 case fnifthen:
678 case fnedist /* euclidian distance */:
679 case fncentropy /* x*ln((x+d)/(y+d))*/:
680 case fngamma:
681 case fnloggamma:
682 case fnbeta:
683 case fnlogbeta:
684 case fngammareg:
685 case fnbetareg:
686 case fnsinh:
687 case fncosh:
688 case fntanh:
689 case fnsignpower /* sign(x)*abs(x)^c */:
690 case fnncpvusin /* veelken-ulbrich */:
691 case fnncpvupow /* veelken-ulbrich */:
692 case fnbinomial:
693 case fntan:
694 case fnarccos:
695 case fnarcsin:
696 case fnarctan2 /* arctan(x2/x1) */:
697 case fnpoly: /* simple polynomial */
698 default :
699 {
700 if (debugoutput) std::cerr << "nr. " << func << " - unsupported. Error." << std::endl;
701 return NULL;
702 }
703 }
704 }
705 break;
706 case nlMulIAdd:
707 {
708 if (debugoutput) std::clog << "multiply constant " << constants[address] << " and add " << std::endl;
709 OSnLNodeNumber *nlNode = new OSnLNodeNumber();
710 nlNode->value = constants[address];
711 nlNodeVec.push_back( nlNode );
712 nlNodeVec.push_back( new OSnLNodeTimes() );
713 nlNodeVec.push_back( new OSnLNodePlus() );
714 }
715 break;
716 case nlFuncArgN :
717 {
718 if (debugoutput) std::clog << "ignored" << std::endl;
719 }
720 break;
721 case nlArg:
722 {
723 if (debugoutput) std::clog << "ignored" << std::endl;
724 }
725 break;
726 case nlHeader: // header
727 {
728 if (debugoutput) std::clog << "ignored" << std::endl;
729 }
730 break;
731 case nlPushZero:
732 {
733 if (debugoutput) std::clog << "push constant zero" << std::endl;
734 nlNodeVec.push_back( new OSnLNodeNumber() );
735 }
736 break;
737 case nlStoreS: // store scaled row
738 {
739 if (debugoutput) std::clog << "ignored" << std::endl;
740 }
741 break;
742 // the following three should have been taken out by reorderInstr above; the remaining ones seem to be unused by now
743 case nlPushS: // duplicate value from address levels down on top of stack
744 case nlPopup: // duplicate value from this level to at address levels down and pop entries in between
745 case nlSwap: // swap two positions on top of stack
746 case nlAddL: // add local
747 case nlSubL: // subtract local
748 case nlMulL: // multiply local
749 case nlDivL: // divide local
750 case nlPushL: // push local
751 case nlPopL: // pop local
752 case nlPopDeriv: // pop derivative
753 case nlUMinL: // push umin local
754 case nlPopDerivS: // store scaled gradient
755 case nlEquScale: // equation scale
756 case nlEnd: // end of instruction list
757 default:
758 {
759 std::cerr << "not supported - Error." << std::endl;
760 return NULL;
761 }
762 }
763 }
764
765 if (!nlNodeVec.size()) return NULL;
766 // the vector is in postfix format - create expression tree and return it
767 return nlNodeVec[0]->createExpressionTreeFromPostfix(nlNodeVec);
768}
used for throwing exceptions.
OSnLNode ** m_mChildren
m_mChildren holds all the operands, that is, nodes that the current node operates on.
Definition OSnLNode.h:84
NonlinearExpressions * nonlinearExpressions
nonlinearExpressions is a pointer to a NonlinearExpressions object
The in-memory representation of the <nl> element.
Definition OSInstance.h:411
int idx
idx holds the row index of the nonlinear expression
Definition OSInstance.h:414
ScalarExpressionTree * osExpressionTree
osExpressionTree contains the root of the ScalarExpressionTree
Definition OSInstance.h:430
int numberOfNonlinearExpressions
numberOfNonlinearExpressions is the number of <nl> elements in the <nonlinearExpressions> element.
Definition OSInstance.h:466
Nl ** nl
nl is pointer to an array of Nl object pointers
Definition OSInstance.h:469
Used to hold the instance in memory.
The in-memory representation of an OSiL instance..
bool setConstraintNumber(int number)
set the number of constraints.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool setVariables(int number, std::string *names, double *lowerBounds, double *upperBounds, char *types)
set all the variable related elements.
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, double *values, int valuesBegin, int valuesEnd, int *indexes, int indexesBegin, int indexesEnd, int *starts, int startsBegin, int startsEnd)
set linear constraint coefficients
InstanceData * instanceData
A pointer to an InstanceData object.
bool setInstanceDescription(std::string description)
set the instance description.
std::string * getVariableNames()
Get variable names.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
std::string * getConstraintNames()
Get constraint names.
bool setObjectiveNumber(int number)
set the number of objectives.
bool setVariableNumber(int number)
set the number of variables.
bool setGeneralMessage(std::string message)
Set the general message.
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
OSnLNode * parseGamsInstructions(int codelen, int *opcodes, int *fields, int constantlen, double *constants)
bool createOSInstance()
Creates an OSInstance from the GAMS smag instance representation.
OSInstance * osinstance
struct gevRec * gev
OSInstance * takeOverOSInstance()
Gives OSInstance and ownership to calling function.
OSgams2osil(struct gmoRec *gmo_=NULL)
bool initGMO(const char *datfile)
struct gmoRec * gmo
The OSnLNodeAbs Class.
Definition OSnLNode.h:1113
The OSnLNodeCos Class.
Definition OSnLNode.h:963
The OSnLNodeDivide Class.
Definition OSnLNode.h:669
The OSnLNodeExp Class.
Definition OSnLNode.h:1063
The OSnLNode Class for nonlinear expressions.
Definition OSnLNode.h:180
OSnLNode * createExpressionTreeFromPostfix(std::vector< ExprNode * > nlNodeVec)
Take a vector of ExprNodes (OSnLNodes and OSnLMNodes) in postfix format and create a scalar-valued OS...
Definition OSnLNode.cpp:413
The OSnLNodeLn Class.
Definition OSnLNode.h:816
The OSnLNodeMax Class.
Definition OSnLNode.h:415
The OSnLNodeMin Class.
Definition OSnLNode.h:464
The OSnLNodeMinus Class.
Definition OSnLNode.h:516
The OSnLNodeNegate Class.
Definition OSnLNode.h:567
The OSnLNodeNumber Class.
Definition OSnLNode.h:1263
double value
value is the value of the number
Definition OSnLNode.h:1266
The OSnLNodePI Class.
Definition OSnLNode.h:1413
The OSnLNodePlus Class.
Definition OSnLNode.h:316
The OSnLNodePower Class.
Definition OSnLNode.h:718
The OSnLNodeSin Class.
Definition OSnLNode.h:1013
The OSnLNodeSqrt Class.
Definition OSnLNode.h:865
The OSnLNodeSquare Class.
Definition OSnLNode.h:913
The OSnLNodeTimes Class.
Definition OSnLNode.h:618
The OSnLNodeVariable Class.
Definition OSnLNode.h:1479
int idx
idx is the index of the variable
Definition OSnLNode.h:1488
double coef
coef is an option coefficient on the variable, the default value is 1.0
Definition OSnLNode.h:1485
Used to hold part of the instance in memory.
OSnLNode * m_treeRoot
m_treeRoot holds the root node (of OSnLNode type) of the expression tree.
a sparse vector data structure
Definition OSGeneral.h:123
double * values
values holds a double array of nonzero values.
Definition OSGeneral.h:164
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero.
Definition OSGeneral.h:159
#define OSDBL_MAX
OSResult * osresult