00001
00035 #include<iostream>
00036 #include <vector>
00037
00038
00039 #include "OSConfig.h"
00040 #include "OSInstance.h"
00041 #include "OSiLWriter.h"
00042 #include "OSParameters.h"
00043 #include "OSnLNode.h"
00044 #include "ErrorClass.h"
00045
00046
00047 #ifdef COIN_HAS_LINDO
00048 #include "LindoSolver.h"
00049 #endif
00050
00051
00052
00053
00054
00055
00056
00057 using std::cout;
00058 using std::endl;
00059 int main(){
00060
00061 cout << "Start Building the Model" << endl;
00062 try{
00063 OSInstance *osinstance;
00064 osinstance = new OSInstance();
00065
00066
00067 osinstance->setInstanceSource("An example from the LINDO API samples directory");
00068 osinstance->setInstanceDescription("A good example of a hard nonlinear program");
00069
00070
00071
00072
00073 osinstance->setVariableNumber( 2);
00074
00075
00076 osinstance->addVariable(0, "x0", -100, 100, 'C', OSNAN, "");
00077 osinstance->addVariable(1, "x1", 0, 1, 'B', OSNAN, "");
00078
00079
00080 osinstance->setObjectiveNumber( 1);
00081
00082 SparseVector *objcoeff;
00083 objcoeff = new SparseVector(1);
00084 objcoeff->indexes = new int[1];
00085 objcoeff->values = new double[1];
00086 objcoeff->indexes[ 0] = 1;
00087 objcoeff->values[ 0] = .4;
00088
00089 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00090
00091
00092 osinstance->setConstraintNumber( 6);
00093
00094
00095 osinstance->addConstraint(0, "row0", -OSINFINITY, 4, 0);
00096 osinstance->addConstraint(1, "row1", -OSINFINITY, 6, 0);
00097 osinstance->addConstraint(2, "row2", -OSINFINITY, 0, 0);
00098 osinstance->addConstraint(3, "row3", 0 , OSINFINITY, 0);
00099 osinstance->addConstraint(4, "row4", -OSINFINITY, 0, 0);
00100 osinstance->addConstraint(5, "row5", -OSINFINITY, 0, 0);
00101
00102
00103
00104
00105
00106
00107
00108 double *values = new double[ 3];
00109 int *indexes = new int[ 3];
00110 int *starts = new int[ 3];
00111 values[ 0] = 1.0;
00112 values[ 1] = 1.0;
00113 values[ 2] = 1.0;
00114 indexes[ 0] = 0;
00115 indexes[ 1] = 0;
00116 indexes[ 2] = 1;
00117 starts[ 0] = 0;
00118 starts[ 1] = 2;
00119 starts[ 2] = 3;
00120 osinstance->setLinearConstraintCoefficients(3, true, values, 0, 2,
00121 indexes, 0, 2, starts, 0, 2);
00122
00123
00124
00125
00126
00127 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = 6;
00128 osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 6 ];
00129
00130 OSnLNode *nlNodePoint;
00131 OSnLNodeVariable *nlNodeVariablePoint;
00132 OSnLNodeNumber *nlNodeNumberPoint;
00133 OSnLNodeMax *nlNodeMaxPoint;
00134 std::vector<OSnLNode*> nlNodeVec;
00135
00136
00137
00138 osinstance->instanceData->nonlinearExpressions->nl[ 0] = new Nl();
00139 osinstance->instanceData->nonlinearExpressions->nl[ 0]->idx = -1;
00140 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree = new OSExpressionTree();
00141
00142 nlNodeVariablePoint = new OSnLNodeVariable();
00143 nlNodeVariablePoint->idx=0;
00144 nlNodeVec.push_back( nlNodeVariablePoint);
00145
00146 nlNodeNumberPoint = new OSnLNodeNumber();
00147 nlNodeNumberPoint->value = 1.0;
00148 nlNodeVec.push_back( nlNodeNumberPoint);
00149
00150 nlNodePoint = new OSnLNodePlus();
00151 nlNodeVec.push_back( nlNodePoint);
00152
00153 nlNodePoint = new OSnLNodeAbs();
00154 nlNodeVec.push_back( nlNodePoint);
00155
00156
00157 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree->m_treeRoot =
00158 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00159 nlNodeVec.clear();
00160
00161
00162
00163
00164
00165 osinstance->instanceData->nonlinearExpressions->nl[ 1] = new Nl();
00166 osinstance->instanceData->nonlinearExpressions->nl[ 1]->idx = 1;
00167 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree = new OSExpressionTree();
00168
00169 nlNodeVariablePoint = new OSnLNodeVariable();
00170 nlNodeVariablePoint->idx=0;
00171 nlNodeVec.push_back( nlNodeVariablePoint);
00172
00173 nlNodeVariablePoint = new OSnLNodeVariable();
00174 nlNodeVariablePoint->idx=1;
00175 nlNodeVec.push_back( nlNodeVariablePoint);
00176
00177 nlNodePoint = new OSnLNodeTimes();
00178 nlNodeVec.push_back( nlNodePoint);
00179
00180
00181 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree->m_treeRoot =
00182 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00183 nlNodeVec.clear();
00184
00185
00186
00187 osinstance->instanceData->nonlinearExpressions->nl[ 2] = new Nl();
00188 osinstance->instanceData->nonlinearExpressions->nl[ 2]->idx = 2;
00189 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree = new OSExpressionTree();
00190
00191 nlNodeVariablePoint = new OSnLNodeVariable();
00192 nlNodeVariablePoint->idx=0;
00193 nlNodeVec.push_back( nlNodeVariablePoint);
00194
00195 nlNodeVariablePoint = new OSnLNodeVariable();
00196 nlNodeVariablePoint->idx=1;
00197 nlNodeVec.push_back( nlNodeVariablePoint);
00198
00199 nlNodePoint = new OSnLNodeTimes();
00200 nlNodeVec.push_back( nlNodePoint);
00201
00202
00203 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree->m_treeRoot =
00204 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00205 nlNodeVec.clear();
00206
00207
00208
00209
00210 osinstance->instanceData->nonlinearExpressions->nl[ 3] = new Nl();
00211 osinstance->instanceData->nonlinearExpressions->nl[ 3]->idx = 3;
00212 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree = new OSExpressionTree();
00213
00214 nlNodeVariablePoint = new OSnLNodeVariable();
00215 nlNodeVariablePoint->idx=1;
00216 nlNodeVec.push_back( nlNodeVariablePoint);
00217
00218 nlNodeNumberPoint = new OSnLNodeNumber();
00219 nlNodeNumberPoint->value = 1.0;
00220 nlNodeVec.push_back( nlNodeNumberPoint);
00221
00222 nlNodePoint = new OSnLNodePlus();
00223 nlNodeVec.push_back( nlNodePoint);
00224
00225 nlNodeVariablePoint = new OSnLNodeVariable();
00226 nlNodeVariablePoint->idx=0;
00227 nlNodeVec.push_back( nlNodeVariablePoint);
00228
00229 nlNodeMaxPoint = new OSnLNodeMax();
00230 nlNodeMaxPoint->inumberOfChildren = 2;
00231 nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
00232 nlNodeVec.push_back( nlNodeMaxPoint);
00233
00234
00235 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree->m_treeRoot =
00236 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00237 nlNodeVec.clear();
00238
00239
00240
00241
00242 osinstance->instanceData->nonlinearExpressions->nl[ 4] = new Nl();
00243 osinstance->instanceData->nonlinearExpressions->nl[ 4]->idx = 4;
00244 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree = new OSExpressionTree();
00245
00246 nlNodeVariablePoint = new OSnLNodeVariable();
00247 nlNodeVariablePoint->idx=1;
00248 nlNodeVec.push_back( nlNodeVariablePoint);
00249
00250 nlNodeNumberPoint = new OSnLNodeNumber();
00251 nlNodeNumberPoint->value = 1.0;
00252 nlNodeVec.push_back( nlNodeNumberPoint);
00253
00254 nlNodeVariablePoint = new OSnLNodeVariable();
00255 nlNodeVariablePoint->idx=1;
00256 nlNodeVec.push_back( nlNodeVariablePoint);
00257
00258 nlNodePoint = new OSnLNodeIf();
00259 nlNodeVec.push_back( nlNodePoint);
00260
00261
00262 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree->m_treeRoot =
00263 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00264 nlNodeVec.clear();
00265
00266
00267
00268
00269 osinstance->instanceData->nonlinearExpressions->nl[ 5] = new Nl();
00270 osinstance->instanceData->nonlinearExpressions->nl[ 5]->idx = 5;
00271 osinstance->instanceData->nonlinearExpressions->nl[ 5]->osExpressionTree = new OSExpressionTree();
00272
00273 nlNodeVariablePoint = new OSnLNodeVariable();
00274 nlNodeVariablePoint->idx=1;
00275 nlNodeVec.push_back( nlNodeVariablePoint);
00276
00277 nlNodeNumberPoint = new OSnLNodeNumber();
00278 nlNodeNumberPoint->value = 2.0;
00279 nlNodeVec.push_back( nlNodeNumberPoint);
00280
00281 nlNodePoint = new OSnLNodeTimes();
00282 nlNodeVec.push_back( nlNodePoint);
00283
00284 nlNodeVariablePoint = new OSnLNodeVariable();
00285 nlNodeVariablePoint->idx=1;
00286 nlNodeVec.push_back( nlNodeVariablePoint);
00287
00288 nlNodePoint = new OSnLNodeTimes();
00289 nlNodeVec.push_back( nlNodePoint);
00290
00291 nlNodeVariablePoint = new OSnLNodeVariable();
00292 nlNodeVariablePoint->idx=1;
00293 nlNodeVec.push_back( nlNodeVariablePoint);
00294
00295 nlNodePoint = new OSnLNodeMinus();
00296 nlNodeVec.push_back( nlNodePoint);
00297
00298 nlNodeVariablePoint = new OSnLNodeVariable();
00299 nlNodeVariablePoint->idx=0;
00300 nlNodeVec.push_back( nlNodeVariablePoint);
00301
00302 nlNodePoint = new OSnLNodeTimes();
00303 nlNodeVec.push_back( nlNodePoint);
00304
00305
00306 osinstance->instanceData->nonlinearExpressions->nl[ 5]->osExpressionTree->m_treeRoot =
00307 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00308 nlNodeVec.clear();
00309
00310
00311
00312 cout << "End Building the Model" << endl;
00313
00314 OSiLWriter *osilwriter;
00315 osilwriter = new OSiLWriter();
00316 cout << osilwriter->writeOSiL( osinstance);
00317
00318 cout << "Done writing the Model" << endl;
00319 #ifdef COIN_HAS_LINDO
00320 cout << "Now Solve with LINDO" << endl;
00321 cout << "create a new LINDO Solver for OSiL string solution" << endl;
00322 LindoSolver *lindo;
00323 lindo = new LindoSolver();
00324 lindo->osinstance = osinstance;
00325 cout << "call the LINDO Solver" << endl;
00326 lindo->solve();
00327 cout << "Here is the LINDO solver solution" << endl;
00328 cout << lindo->osrl << endl;
00329
00330 lindo->osinstance = NULL;
00331 delete lindo;
00332 lindo = NULL;
00333 #endif
00334 delete osinstance;
00335 osinstance = NULL;
00336 delete osilwriter;
00337 osilwriter = NULL;
00338 cout << "Done with garbage collection" << endl;
00339 return 0;
00340 }
00341 catch(const ErrorClass& eclass){
00342 cout << eclass.errormsg << endl;
00343 }
00344 }
00345