/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OS/OS/examples/instanceGenerator/OSInstanceGenerator.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 "OSErrorClass.h"
00045 
00046 
00047 
00048  
00049 
00050 
00051  
00052 
00053 using std::cout;
00054 using std::endl;
00055 int  main(){    
00056 
00057         cout << "Start Building the Model" << endl;
00058         try{
00059                 OSInstance *osinstance;
00060                 osinstance = new OSInstance();
00061                 //
00062                 // put in some of the OSInstance <instanceHeader> information
00063                 osinstance->setInstanceSource("An example from the LINDO API samples directory");
00064                 osinstance->setInstanceDescription("A good example of a hard nonlinear program");
00065                 //
00066                 // now put in the OSInstance <instanceData> information
00067                 // 
00068                 // first the variables
00069                 osinstance->setVariableNumber( 2);   
00070                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00071                 // we could use setVariables() and add all the variable with one method call -- below is easier
00072                 osinstance->addVariable(0, "x0", -100, 100, 'C', OSNAN, "");
00073                 osinstance->addVariable(1, "x1", 0, 1, 'B', OSNAN, "");
00074                 //
00075                 // now add the objective function
00076                 osinstance->setObjectiveNumber( 1);
00077                 // now the coefficient
00078                 SparseVector *objcoeff;
00079                 objcoeff = new SparseVector(1);   
00080                 objcoeff->indexes[ 0] = 1;
00081                 objcoeff->values[ 0] = .4;
00082                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00083                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00084                 objcoeff->bDeleteArrays = true;
00085                 delete objcoeff;
00086                 //
00087                 // now the constraints
00088                 osinstance->setConstraintNumber( 6); 
00089                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00090                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
00091                 osinstance->addConstraint(0, "row0", -OSDBL_MAX, 4, 0); 
00092                 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 6, 0);
00093                 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 0, 0);
00094                 osinstance->addConstraint(3, "row3", 0 , OSDBL_MAX, 0); 
00095                 osinstance->addConstraint(4, "row4", -OSDBL_MAX, 0, 0);
00096                 osinstance->addConstraint(5, "row5", -OSDBL_MAX, 0, 0);
00097                 //
00098                 //
00099                 // now add the <linearConstraintCoefficients>
00100                 //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
00101                 //double* values, int valuesBegin, int valuesEnd, 
00102                 //int* indexes, int indexesBegin, int indexesEnd,                       
00103                 //int* starts, int startsBegin, int startsEnd); 
00104                 double *values = new double[ 3];
00105                 int *indexes = new int[ 3];
00106                 int *starts = new int[ 3];  
00107                 values[ 0] = 1.0;
00108                 values[ 1] = 1.0;
00109                 values[ 2] = 1.0;
00110                 indexes[ 0] = 0;
00111                 indexes[ 1] = 0;
00112                 indexes[ 2] = 1;
00113                 starts[ 0] = 0;
00114                 starts[ 1] = 2;
00115                 starts[ 2] = 3; 
00116                 osinstance->setLinearConstraintCoefficients(3, true, values, 0, 2, 
00117                         indexes, 0, 2, starts, 0, 2);   
00118                 //
00119                 // finally the nonlinear part, not as nice since we don't have any set() methods
00120                 // yet, we must work directly with the data structures
00121                 //
00122                 // we have 6 nonlinear expressions
00123                 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = 6;
00124                 osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 6 ];
00125                 // define the vectors
00126                 OSnLNode *nlNodePoint;
00127                 OSnLNodeVariable *nlNodeVariablePoint;
00128                 OSnLNodeNumber *nlNodeNumberPoint;
00129                 OSnLNodeMax *nlNodeMaxPoint;
00130                 std::vector<OSnLNode*> nlNodeVec;
00131                 //
00132                 //
00133                 // the objective function nonlinear term abs( x0 + 1)
00134                 osinstance->instanceData->nonlinearExpressions->nl[ 0] = new Nl();
00135                 osinstance->instanceData->nonlinearExpressions->nl[ 0]->idx = -1;
00136                 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree = new OSExpressionTree();
00137                 // create a variable nl node for x0
00138                 nlNodeVariablePoint = new OSnLNodeVariable();
00139                 nlNodeVariablePoint->idx=0;
00140                 nlNodeVec.push_back( nlNodeVariablePoint);
00141                 // create the nl node for number 1
00142                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00143                 nlNodeNumberPoint->value = 1.0;
00144                 nlNodeVec.push_back( nlNodeNumberPoint);
00145                 // create the nl node for +
00146                 nlNodePoint = new OSnLNodePlus();
00147                 nlNodeVec.push_back( nlNodePoint);
00148                 // create the nl node for max
00149                 nlNodePoint = new OSnLNodeAbs();
00150                 nlNodeVec.push_back( nlNodePoint);
00151                 // the vectors are in postfix format
00152                 // now the expression tree
00153                 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree->m_treeRoot =
00154                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00155                 nlNodeVec.clear();
00156                 //
00157                 //
00158                 // constraint 0 has no nonlinear terms
00159                 // generate the x0*x1 term in constraint 1
00160                 //
00161                 osinstance->instanceData->nonlinearExpressions->nl[ 1] = new Nl();
00162                 osinstance->instanceData->nonlinearExpressions->nl[ 1]->idx = 1;
00163                 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree = new OSExpressionTree();
00164                 // create a variable nl node for x0
00165                 nlNodeVariablePoint = new OSnLNodeVariable();
00166                 nlNodeVariablePoint->idx=0;
00167                 nlNodeVec.push_back( nlNodeVariablePoint);
00168                 // create the nl node for x1
00169                 nlNodeVariablePoint = new OSnLNodeVariable();
00170                 nlNodeVariablePoint->idx=1;
00171                 nlNodeVec.push_back( nlNodeVariablePoint);
00172                 // create the nl node for *
00173                 nlNodePoint = new OSnLNodeTimes();
00174                 nlNodeVec.push_back( nlNodePoint);
00175                 // the vectors are in postfix format
00176                 // now the expression tree
00177                 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree->m_treeRoot =
00178                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00179                 nlNodeVec.clear();
00180                 // 
00181                 //
00182                 // generate the x0*x1 term in constraint 2
00183                 osinstance->instanceData->nonlinearExpressions->nl[ 2] = new Nl();
00184                 osinstance->instanceData->nonlinearExpressions->nl[ 2]->idx = 2;
00185                 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree = new OSExpressionTree();
00186                 // create a variable nl node for x0
00187                 nlNodeVariablePoint = new OSnLNodeVariable();
00188                 nlNodeVariablePoint->idx=0;
00189                 nlNodeVec.push_back( nlNodeVariablePoint);
00190                 // create the nl node for x0
00191                 nlNodeVariablePoint = new OSnLNodeVariable(); 
00192                 nlNodeVariablePoint->idx=1;
00193                 nlNodeVec.push_back( nlNodeVariablePoint);
00194                 // create the nl node for *
00195                 nlNodePoint = new OSnLNodeTimes();
00196                 nlNodeVec.push_back( nlNodePoint);
00197                 // the vectors are in postfix format
00198                 // now the expression tree
00199                 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree->m_treeRoot =
00200                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00201                 nlNodeVec.clear();
00202                 //
00203                 //
00204                 //
00205                 // generate the max(x0 , x1 + 1) term in constraint 3
00206                 osinstance->instanceData->nonlinearExpressions->nl[ 3] = new Nl();
00207                 osinstance->instanceData->nonlinearExpressions->nl[ 3]->idx = 3;
00208                 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree = new OSExpressionTree();
00209                 // create a variable nl node for x1
00210                 nlNodeVariablePoint = new OSnLNodeVariable();
00211                 nlNodeVariablePoint->idx=1;
00212                 nlNodeVec.push_back( nlNodeVariablePoint);
00213                 // create the nl node for number 1
00214                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00215                 nlNodeNumberPoint->value = 1.0;
00216                 nlNodeVec.push_back( nlNodeNumberPoint);
00217                 // create the nl node for +
00218                 nlNodePoint = new OSnLNodePlus();
00219                 nlNodeVec.push_back( nlNodePoint);
00220                 // now push x0 to the stack
00221                 nlNodeVariablePoint = new OSnLNodeVariable();
00222                 nlNodeVariablePoint->idx=0;
00223                 nlNodeVec.push_back( nlNodeVariablePoint);
00224                 // create the nl node for max
00225                 nlNodeMaxPoint = new OSnLNodeMax();
00226                 nlNodeMaxPoint->inumberOfChildren = 2;
00227                 nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
00228                 nlNodeVec.push_back( nlNodeMaxPoint);
00229                 // the vectors are in postfix format
00230                 // now the expression tree
00231                 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree->m_treeRoot =
00232                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00233                 nlNodeVec.clear();
00234                 //
00235                 //
00236                 //
00237                 // generate the if(x1, 1, x1) term in constraint 4
00238                 osinstance->instanceData->nonlinearExpressions->nl[ 4] = new Nl();
00239                 osinstance->instanceData->nonlinearExpressions->nl[ 4]->idx = 4;
00240                 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree = new OSExpressionTree();
00241                 // create a variable nl node for x1
00242                 nlNodeVariablePoint = new OSnLNodeVariable();
00243                 nlNodeVariablePoint->idx=1;
00244                 nlNodeVec.push_back( nlNodeVariablePoint);
00245                 // create the nl node for number 1
00246                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00247                 nlNodeNumberPoint->value = 1.0;
00248                 nlNodeVec.push_back( nlNodeNumberPoint);
00249                 // now push x1 to the stack
00250                 nlNodeVariablePoint = new OSnLNodeVariable();
00251                 nlNodeVariablePoint->idx=1;
00252                 nlNodeVec.push_back( nlNodeVariablePoint);
00253                 // create the nl node for If
00254                 nlNodePoint = new OSnLNodeIf();
00255                 nlNodeVec.push_back( nlNodePoint);
00256                 // the vectors are in postfix format
00257                 // now the expression tree
00258                 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree->m_treeRoot =
00259                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00260                 nlNodeVec.clear();
00261                 //
00262                 //
00263                 //
00264                 // generate the (x1 * 2 * x1  -  x1) * x0 term in constraint 5
00265                 osinstance->instanceData->nonlinearExpressions->nl[ 5] = new Nl();
00266                 osinstance->instanceData->nonlinearExpressions->nl[ 5]->idx = 5;
00267                 osinstance->instanceData->nonlinearExpressions->nl[ 5]->osExpressionTree = new OSExpressionTree();
00268                 // create a variable nl node for x1
00269                 nlNodeVariablePoint = new OSnLNodeVariable();
00270                 nlNodeVariablePoint->idx=1;
00271                 nlNodeVec.push_back( nlNodeVariablePoint);
00272                 // create the nl node for number 1
00273                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00274                 nlNodeNumberPoint->value = 2.0;
00275                 nlNodeVec.push_back( nlNodeNumberPoint);
00276                 // create an nl node for *
00277                 nlNodePoint = new OSnLNodeTimes(); 
00278                 nlNodeVec.push_back( nlNodePoint);
00279                 // now push x1 to the stack
00280                 nlNodeVariablePoint = new OSnLNodeVariable();
00281                 nlNodeVariablePoint->idx=1;
00282                 nlNodeVec.push_back( nlNodeVariablePoint);
00283                 // create an nl node for *
00284                 nlNodePoint = new OSnLNodeTimes(); 
00285                 nlNodeVec.push_back( nlNodePoint);
00286                 // create a variable nl node for x1
00287                 nlNodeVariablePoint = new OSnLNodeVariable();
00288                 nlNodeVariablePoint->idx=1;
00289                 nlNodeVec.push_back( nlNodeVariablePoint);
00290                 // create an nl node for -
00291                 nlNodePoint = new OSnLNodeMinus(); 
00292                 nlNodeVec.push_back( nlNodePoint);
00293                 // create a variable nl node for x0
00294                 nlNodeVariablePoint = new OSnLNodeVariable();
00295                 nlNodeVariablePoint->idx=0;
00296                 nlNodeVec.push_back( nlNodeVariablePoint);
00297                 // create an nl node for *
00298                 nlNodePoint = new OSnLNodeTimes(); 
00299                 nlNodeVec.push_back( nlNodePoint);
00300                 // the vectors are in postfix format
00301                 // now the expression tree
00302                 osinstance->instanceData->nonlinearExpressions->nl[ 5]->osExpressionTree->m_treeRoot =
00303                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00304                 nlNodeVec.clear();
00305                 //
00306                 //
00307                 // 
00308                 cout << "End Building the Model" << endl; 
00309                 // Write out the model
00310                 OSiLWriter *osilwriter;
00311                 osilwriter = new OSiLWriter();
00312                 cout << osilwriter->writeOSiL( osinstance);
00313                 // done writing the model
00314                 cout << "Done writing the Model" << endl;
00315                 delete osinstance;
00316                 osinstance = NULL;
00317                 delete osilwriter;
00318                 osilwriter = NULL;
00319                 cout << "Done with garbage collection" << endl;
00320                 return 0;
00321         }
00322         catch(const ErrorClass& eclass){
00323                 cout << eclass.errormsg <<  endl;
00324         }               
00325 }
00326 

Generated on Sat Mar 29 22:38:01 2008 by  doxygen 1.5.3