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