/home/coin/SVN-release/OS-1.0.0/OS/examples/instanceGenerator/instanceGenerator.cpp

Go to the documentation of this file.
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                 // put in some of the OSInstance <instanceHeader> information
00067                 osinstance->setInstanceSource("An example from the LINDO API samples directory");
00068                 osinstance->setInstanceDescription("A good example of a hard nonlinear program");
00069                 //
00070                 // now put in the OSInstance <instanceData> information
00071                 // 
00072                 // first the variables
00073                 osinstance->setVariableNumber( 2);   
00074                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00075                 // we could use setVariables() and add all the variable with one method call -- below is easier
00076                 osinstance->addVariable(0, "x0", -100, 100, 'C', OSNAN, "");
00077                 osinstance->addVariable(1, "x1", 0, 1, 'B', OSNAN, "");
00078                 //
00079                 // now add the objective function
00080                 osinstance->setObjectiveNumber( 1);
00081                 // now the coefficient
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                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00089                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00090                 //
00091                 // now the constraints
00092                 osinstance->setConstraintNumber( 6); 
00093                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00094                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
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                 // now add the <linearConstraintCoefficients>
00104                 //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
00105                 //double* values, int valuesBegin, int valuesEnd, 
00106                 //int* indexes, int indexesBegin, int indexesEnd,                       
00107                 //int* starts, int startsBegin, int startsEnd); 
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                 // finally the nonlinear part, not as nice since we don't have any set() methods
00124                 // yet, we must work directly with the data structures
00125                 //
00126                 // we have 6 nonlinear expressions
00127                 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = 6;
00128                 osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 6 ];
00129                 // define the vectors
00130                 OSnLNode *nlNodePoint;
00131                 OSnLNodeVariable *nlNodeVariablePoint;
00132                 OSnLNodeNumber *nlNodeNumberPoint;
00133                 OSnLNodeMax *nlNodeMaxPoint;
00134                 std::vector<OSnLNode*> nlNodeVec;
00135                 //
00136                 //
00137                 // the objective function nonlinear term abs( x0 + 1)
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                 // create a variable nl node for x0
00142                 nlNodeVariablePoint = new OSnLNodeVariable();
00143                 nlNodeVariablePoint->idx=0;
00144                 nlNodeVec.push_back( nlNodeVariablePoint);
00145                 // create the nl node for number 1
00146                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00147                 nlNodeNumberPoint->value = 1.0;
00148                 nlNodeVec.push_back( nlNodeNumberPoint);
00149                 // create the nl node for +
00150                 nlNodePoint = new OSnLNodePlus();
00151                 nlNodeVec.push_back( nlNodePoint);
00152                 // create the nl node for max
00153                 nlNodePoint = new OSnLNodeAbs();
00154                 nlNodeVec.push_back( nlNodePoint);
00155                 // the vectors are in postfix format
00156                 // now the expression tree
00157                 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree->m_treeRoot =
00158                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00159                 nlNodeVec.clear();
00160                 //
00161                 //
00162                 // constraint 0 has no nonlinear terms
00163                 // generate the x0*x1 term in constraint 1
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                 // create a variable nl node for x0
00169                 nlNodeVariablePoint = new OSnLNodeVariable();
00170                 nlNodeVariablePoint->idx=0;
00171                 nlNodeVec.push_back( nlNodeVariablePoint);
00172                 // create the nl node for x1
00173                 nlNodeVariablePoint = new OSnLNodeVariable();
00174                 nlNodeVariablePoint->idx=1;
00175                 nlNodeVec.push_back( nlNodeVariablePoint);
00176                 // create the nl node for *
00177                 nlNodePoint = new OSnLNodeTimes();
00178                 nlNodeVec.push_back( nlNodePoint);
00179                 // the vectors are in postfix format
00180                 // now the expression tree
00181                 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree->m_treeRoot =
00182                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00183                 nlNodeVec.clear();
00184                 // 
00185                 //
00186                 // generate the x0*x1 term in constraint 2
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                 // create a variable nl node for x0
00191                 nlNodeVariablePoint = new OSnLNodeVariable();
00192                 nlNodeVariablePoint->idx=0;
00193                 nlNodeVec.push_back( nlNodeVariablePoint);
00194                 // create the nl node for x0
00195                 nlNodeVariablePoint = new OSnLNodeVariable(); 
00196                 nlNodeVariablePoint->idx=1;
00197                 nlNodeVec.push_back( nlNodeVariablePoint);
00198                 // create the nl node for *
00199                 nlNodePoint = new OSnLNodeTimes();
00200                 nlNodeVec.push_back( nlNodePoint);
00201                 // the vectors are in postfix format
00202                 // now the expression tree
00203                 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree->m_treeRoot =
00204                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00205                 nlNodeVec.clear();
00206                 //
00207                 //
00208                 //
00209                 // generate the max(x0 , x1 + 1) term in constraint 3
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                 // create a variable nl node for x1
00214                 nlNodeVariablePoint = new OSnLNodeVariable();
00215                 nlNodeVariablePoint->idx=1;
00216                 nlNodeVec.push_back( nlNodeVariablePoint);
00217                 // create the nl node for number 1
00218                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00219                 nlNodeNumberPoint->value = 1.0;
00220                 nlNodeVec.push_back( nlNodeNumberPoint);
00221                 // create the nl node for +
00222                 nlNodePoint = new OSnLNodePlus();
00223                 nlNodeVec.push_back( nlNodePoint);
00224                 // now push x0 to the stack
00225                 nlNodeVariablePoint = new OSnLNodeVariable();
00226                 nlNodeVariablePoint->idx=0;
00227                 nlNodeVec.push_back( nlNodeVariablePoint);
00228                 // create the nl node for max
00229                 nlNodeMaxPoint = new OSnLNodeMax();
00230                 nlNodeMaxPoint->inumberOfChildren = 2;
00231                 nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
00232                 nlNodeVec.push_back( nlNodeMaxPoint);
00233                 // the vectors are in postfix format
00234                 // now the expression tree
00235                 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree->m_treeRoot =
00236                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00237                 nlNodeVec.clear();
00238                 //
00239                 //
00240                 //
00241                 // generate the if(x1, 1, x1) term in constraint 4
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                 // create a variable nl node for x1
00246                 nlNodeVariablePoint = new OSnLNodeVariable();
00247                 nlNodeVariablePoint->idx=1;
00248                 nlNodeVec.push_back( nlNodeVariablePoint);
00249                 // create the nl node for number 1
00250                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00251                 nlNodeNumberPoint->value = 1.0;
00252                 nlNodeVec.push_back( nlNodeNumberPoint);
00253                 // now push x1 to the stack
00254                 nlNodeVariablePoint = new OSnLNodeVariable();
00255                 nlNodeVariablePoint->idx=1;
00256                 nlNodeVec.push_back( nlNodeVariablePoint);
00257                 // create the nl node for If
00258                 nlNodePoint = new OSnLNodeIf();
00259                 nlNodeVec.push_back( nlNodePoint);
00260                 // the vectors are in postfix format
00261                 // now the expression tree
00262                 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree->m_treeRoot =
00263                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00264                 nlNodeVec.clear();
00265                 //
00266                 //
00267                 //
00268                 // generate the (x1 * 2 * x1  -  x1) * x0 term in constraint 5
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                 // create a variable nl node for x1
00273                 nlNodeVariablePoint = new OSnLNodeVariable();
00274                 nlNodeVariablePoint->idx=1;
00275                 nlNodeVec.push_back( nlNodeVariablePoint);
00276                 // create the nl node for number 1
00277                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00278                 nlNodeNumberPoint->value = 2.0;
00279                 nlNodeVec.push_back( nlNodeNumberPoint);
00280                 // create an nl node for *
00281                 nlNodePoint = new OSnLNodeTimes(); 
00282                 nlNodeVec.push_back( nlNodePoint);
00283                 // now push x1 to the stack
00284                 nlNodeVariablePoint = new OSnLNodeVariable();
00285                 nlNodeVariablePoint->idx=1;
00286                 nlNodeVec.push_back( nlNodeVariablePoint);
00287                 // create an nl node for *
00288                 nlNodePoint = new OSnLNodeTimes(); 
00289                 nlNodeVec.push_back( nlNodePoint);
00290                 // create a variable nl node for x1
00291                 nlNodeVariablePoint = new OSnLNodeVariable();
00292                 nlNodeVariablePoint->idx=1;
00293                 nlNodeVec.push_back( nlNodeVariablePoint);
00294                 // create an nl node for -
00295                 nlNodePoint = new OSnLNodeMinus(); 
00296                 nlNodeVec.push_back( nlNodePoint);
00297                 // create a variable nl node for x0
00298                 nlNodeVariablePoint = new OSnLNodeVariable();
00299                 nlNodeVariablePoint->idx=0;
00300                 nlNodeVec.push_back( nlNodeVariablePoint);
00301                 // create an nl node for *
00302                 nlNodePoint = new OSnLNodeTimes(); 
00303                 nlNodeVec.push_back( nlNodePoint);
00304                 // the vectors are in postfix format
00305                 // now the expression tree
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                 // Write out the model
00314                 OSiLWriter *osilwriter;
00315                 osilwriter = new OSiLWriter();
00316                 cout << osilwriter->writeOSiL( osinstance);
00317                 // done writing the model
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                 // do the garbage collection 
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 

Generated on Thu May 15 22:15:04 2008 by  doxygen 1.4.7