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