10 #include "GAMSlinksConfig.h"
11 #include "GamsNLinstr.h"
13 #include "OSInstance.h"
14 #include "CoinHelperFunctions.hpp"
22 : gev(gmo_ ? (gevHandle_t)gmoEnvironment(gmo_) : NULL), gmo(gmo_),
osinstance(NULL)
31 initGMO( gamsControlFile.c_str() ) ;
54 if (!gmoCreate(&
gmo, msg,
sizeof(msg)))
58 osrl = osrlwriter->writeOSrL(
osresult);
62 if (!gevCreate(&
gev, msg,
sizeof(msg)))
66 osrl = osrlwriter->writeOSrL(
osresult);
71 gmoIdentSet(
gmo,
"OS link object");
74 if ((rc = gevInitEnvironmentLegacy(
gev, datfile)))
77 outStr <<
"Could not initialize GAMS environment: " << datfile <<
" Rc = " << rc << std::endl;
80 osrl = osrlwriter->writeOSrL(
osresult);
86 if ((rc = gmoRegisterEnvironment(
gmo,
gev, msg)))
90 osrl = osrlwriter->writeOSrL(
osresult);
96 if ((rc = gmoLoadDataLegacy(
gmo, msg)))
100 osrl = osrlwriter->writeOSrL(
osresult);
108 gmoObjReformSet(
gmo, 1);
109 gmoObjStyleSet(
gmo, ObjType_Fun);
110 gmoIndexBaseSet(
gmo, 0);
128 char* vartypes =
new char[gmoN(
gmo)];
129 std::string* varnames =
new std::string[gmoN(
gmo)];
130 for(i = 0; i < gmoN(
gmo); ++i)
132 switch (gmoGetVarTypeOne(
gmo, i))
146 gevLogStat(
gev,
"Error: Unsupported variable type.");
150 gmoGetVarNameOne(
gmo, i, buffer);
151 varnames[i] = buffer;
154 double* varlow =
new double[gmoN(
gmo)];
155 double* varup =
new double[gmoN(
gmo)];
156 gmoGetVarLower(
gmo, varlow);
157 gmoGetVarUpper(
gmo, varup);
167 if (gmoModelType(
gmo) == Proc_cns)
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)];
182 if (gmoObjNZ(
gmo)) nlflag[0] = 0;
183 gmoGetObjSparse(
gmo, colidx, val, nlflag, dummy, dummy);
184 for (i = 0, j = 0; i < gmoObjNZ(
gmo); ++i)
186 if (nlflag[i])
continue;
187 objectiveCoefficients->
indexes[
j] = colidx[i];
188 objectiveCoefficients->
values[
j] = val[i];
190 assert(j <= gmoObjNZ(
gmo) - gmoObjNLNZ(
gmo));
192 assert(j == gmoObjNZ(
gmo) - gmoObjNLNZ(
gmo));
199 std::string objname =
"objective";
202 delete objectiveCoefficients;
205 delete objectiveCoefficients;
211 for (i = 0; i < gmoM(
gmo); ++i)
213 switch (gmoGetEquTypeOne(
gmo, i))
216 lb = ub = gmoGetRhsOne(
gmo, i);
220 ub = gmoGetRhsOne(
gmo, i);
223 lb = gmoGetRhsOne(
gmo, i);
231 gevLogStat(
gev,
"Error: Unknown row type. Exiting ...");
235 gmoGetEquNameOne(
gmo, i, buffer);
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];
247 gmoGetMatrixCol(
gmo, colstarts, rowindexes, values, nlflags);
250 colstarts[gmoN(
gmo)] = nz;
253 for (
int col = 0; col < gmoN(
gmo); ++col)
255 colstarts[col+1] -= shift;
256 int k = colstarts[col];
257 while (k < colstarts[col+1])
259 values[
k] = values[k+shift];
260 rowindexes[
k] = rowindexes[k+shift];
261 if (nlflags[k+shift])
277 colstarts, 0, gmoN(
gmo)))
286 if (!gmoObjNLNZ(
gmo) && !gmoNLNZ(
gmo))
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);
302 std::clog <<
"parsing nonlinear objective instructions" << std::endl;
303 gmoDirtyGetObjFNLInstr(
gmo, &codelen, opcodes, fields);
306 if (!nl)
return false;
308 double objjacval = gmoObjJacVal(
gmo);
309 std::clog <<
"obj jac val: " << objjacval << std::endl;
316 else if (objjacval != -1.)
319 numbernode->
value = -1/objjacval;
325 assert(iNLidx < osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions);
333 for (i = 0; i < gmoM(
gmo); ++i)
335 if (gmoDirtyGetRowFNLInstr(
gmo, i, &codelen, opcodes, fields))
337 std::clog <<
"got nonzero return at constraint " << i << std::endl;
339 if (!codelen)
continue;
342 if (!nl)
return false;
343 assert(iNLidx < osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions);
364 std::vector<OSnLNode*> nlNodeVec;
366 const bool debugoutput =
false;
371 nlNodeVec.reserve(codelen);
373 for (
int i=0; i<codelen; ++i)
375 GamsOpCode opcode = (GamsOpCode)opcodes[i];
376 int address = fields[i]-1;
378 if (debugoutput) std::clog <<
'\t' << GamsOpCodeName[opcode] <<
": ";
387 if (debugoutput) std::clog <<
"ignored" << std::endl;
392 address = gmoGetjSolver(
gmo, address);
396 nlNodeVec.push_back( nlNode );
401 if (debugoutput) std::clog <<
"push constant " << constants[address] << std::endl;
403 nlNode->
value = constants[address];
404 nlNodeVec.push_back( nlNode );
409 if (debugoutput) std::clog <<
"ignored" << std::endl;
414 if (debugoutput) std::clog <<
"add" << std::endl;
420 address = gmoGetjSolver(
gmo, address);
424 nlNodeVec.push_back( nlNode );
430 if (debugoutput) std::clog <<
"add constant " << constants[address] << std::endl;
432 nlNode->
value = constants[address];
433 nlNodeVec.push_back( nlNode );
439 if (debugoutput) std::clog <<
"minus" << std::endl;
445 address = gmoGetjSolver(
gmo, address);
449 nlNodeVec.push_back( nlNode );
455 if (debugoutput) std::clog <<
"substract constant " << constants[address] << std::endl;
457 nlNode->
value = constants[address];
458 nlNodeVec.push_back( nlNode );
464 if (debugoutput) std::clog <<
"multiply" << std::endl;
470 address = gmoGetjSolver(
gmo, address);
474 nlNodeVec.push_back( nlNode );
480 if (debugoutput) std::clog <<
"multiply constant " << constants[address] << std::endl;
482 nlNode->
value = constants[address];
483 nlNodeVec.push_back( nlNode );
489 if (debugoutput) std::clog <<
"divide" << std::endl;
495 address = gmoGetjSolver(
gmo, address);
499 nlNodeVec.push_back( nlNode );
505 if (debugoutput) std::clog <<
"divide constant " << constants[address] << std::endl;
507 nlNode->
value = constants[address];
508 nlNodeVec.push_back( nlNode );
514 if (debugoutput) std::clog <<
"negate" << std::endl;
520 address = gmoGetjSolver(
gmo, address);
523 nlNode->
idx = address;
525 nlNodeVec.push_back( nlNode );
532 if (debugoutput) std::clog <<
"call function ";
533 GamsFuncCode func = GamsFuncCode(address+1);
538 if (debugoutput) std::clog <<
"min" << std::endl;
544 if (debugoutput) std::clog <<
"max" << std::endl;
550 if (debugoutput) std::clog <<
"square" << std::endl;
558 if (debugoutput) std::clog <<
"exp" << std::endl;
564 if (debugoutput) std::clog <<
"ln" << std::endl;
572 if (debugoutput) std::clog <<
"log10 = ln * 1/ln(10)" << std::endl;
576 nlNodeVec.push_back( nlNode );
582 if (debugoutput) std::clog <<
"log2 = ln * 1/ln(2)" << std::endl;
586 nlNodeVec.push_back( nlNode );
592 if (debugoutput) std::clog <<
"sqrt" << std::endl;
598 if (debugoutput) std::clog <<
"abs" << std::endl;
604 if (debugoutput) std::clog <<
"cos" << std::endl;
610 if (debugoutput) std::clog <<
"sin" << std::endl;
619 if (debugoutput) std::clog <<
"power" << std::endl;
625 if (debugoutput) std::clog <<
"pi" << std::endl;
638 if (debugoutput) std::clog <<
"divide" << std::endl;
642 nlNodeVec.push_back( nlNode );
700 if (debugoutput) std::cerr <<
"nr. " << func <<
" - unsupported. Error." << std::endl;
708 if (debugoutput) std::clog <<
"multiply constant " << constants[address] <<
" and add " << std::endl;
710 nlNode->
value = constants[address];
711 nlNodeVec.push_back( nlNode );
718 if (debugoutput) std::clog <<
"ignored" << std::endl;
723 if (debugoutput) std::clog <<
"ignored" << std::endl;
728 if (debugoutput) std::clog <<
"ignored" << std::endl;
733 if (debugoutput) std::clog <<
"push constant zero" << std::endl;
739 if (debugoutput) std::clog <<
"ignored" << std::endl;
759 std::cerr <<
"not supported - Error." << std::endl;
765 if (!nlNodeVec.size())
return NULL;
767 return nlNodeVec[0]->createExpressionTreeFromPostfix(nlNodeVec);
double coef
coef is an option coefficient on the variable, the default value is 1.0
OSnLNode * parseGamsInstructions(int codelen, int *opcodes, int *fields, int constantlen, double *constants)
NonlinearExpressions * nonlinearExpressions
nonlinearExpressions is a pointer to a NonlinearExpressions object
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
int idx
idx is the index of the variable
OSInstance * takeOverOSInstance()
Gives OSInstance and ownership to calling function.
std::string * getVariableNames()
Get variable names.
bool createOSInstance()
Creates an OSInstance from the GAMS smag instance representation.
The OSnLNodeNumber Class.
Used to hold part of the instance in memory.
The OSnLNodeNegate Class.
bool setConstraintNumber(int number)
set the number of constraints.
The OSnLNodeSquare Class.
Nl ** nl
nl is pointer to an array of Nl object pointers
The OSnLNodeVariable Class.
bool setObjectiveNumber(int number)
set the number of objectives.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
double value
value is the value of the number
a sparse vector data structure
The OSnLNodeDivide Class.
OSnLNode ** m_mChildren
m_mChildren holds all the operands, that is, nodes that the current node operates on...
bool setGeneralMessage(std::string message)
Set the general message.
bool setVariables(int number, std::string *names, double *lowerBounds, double *upperBounds, char *types)
set all the variable related elements.
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
OSnLNode * m_treeRoot
m_treeRoot holds the root node (of OSnLNode type) of the expression tree.
InstanceData * instanceData
A pointer to an InstanceData object.
The in-memory representation of the <nl> element.
int idx
idx holds the row index of the nonlinear expression
bool setInstanceDescription(std::string description)
set the instance description.
ScalarExpressionTree * osExpressionTree
osExpressionTree contains the root of the ScalarExpressionTree
double * values
values holds a double array of nonzero values.
int numberOfNonlinearExpressions
numberOfNonlinearExpressions is the number of <nl> elements in the <nonlinearExpressions> element...
bool initGMO(const char *datfile)
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero. ...
OSgams2osil(struct gmoRec *gmo_=NULL)
bool setVariableNumber(int number)
set the number of variables.
The in-memory representation of an OSiL instance..
The OSnLNode Class for nonlinear expressions.
Used to hold the instance in memory.
std::string * getConstraintNames()
Get constraint names.
used for throwing exceptions.
CouExpr & log(CouExpr &e)