/home/coin/SVN-release/OS-2.4.1/examples/instanceGenerator/OSInstanceGenerator.cpp

Go to the documentation of this file.
00001 /* $Id: OSInstanceGenerator.cpp 2698 2009-06-09 04:14:07Z kmartin $ */
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                 // put in some of the OSInstance <instanceHeader> information
00062                 osinstance->setInstanceSource("An example from the LINDO API samples directory");
00063                 osinstance->setInstanceDescription("A good example of a hard nonlinear program");
00064                 //
00065                 // now put in the OSInstance <instanceData> information
00066                 // 
00067                 // first the variables
00068                 osinstance->setVariableNumber( 2);   
00069                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00070                 // we could use setVariables() and add all the variable with one method call -- below is easier
00071                 osinstance->addVariable(0, "x0", -100, 100, 'C');
00072                 osinstance->addVariable(1, "x1", 0, 1, 'B');
00073                 //
00074                 // now add the objective function
00075                 osinstance->setObjectiveNumber( 1);
00076                 // now the coefficient
00077                 SparseVector *objcoeff;
00078                 objcoeff = new SparseVector(1);   
00079                 objcoeff->indexes[ 0] = 1;
00080                 objcoeff->values[ 0] = .4;
00081                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00082                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00083                 objcoeff->bDeleteArrays = true;
00084                 delete objcoeff;
00085                 //
00086                 // now the constraints
00087                 osinstance->setConstraintNumber( 6); 
00088                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00089                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
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                 // now add the <linearConstraintCoefficients>
00099                 //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
00100                 //double* values, int valuesBegin, int valuesEnd, 
00101                 //int* indexes, int indexesBegin, int indexesEnd,                       
00102                 //int* starts, int startsBegin, int startsEnd); 
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                 // finally the nonlinear part, not as nice since we don't have any set() methods
00119                 // yet, we must work directly with the data structures
00120                 //
00121                 // we have 6 nonlinear expressions
00122                 osinstance->instanceData->nonlinearExpressions->numberOfNonlinearExpressions = 6;
00123                 osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 6 ];
00124                 // define the vectors
00125                 OSnLNode *nlNodePoint;
00126                 OSnLNodeVariable *nlNodeVariablePoint;
00127                 OSnLNodeNumber *nlNodeNumberPoint;
00128                 OSnLNodeMax *nlNodeMaxPoint;
00129                 std::vector<OSnLNode*> nlNodeVec;
00130                 //
00131                 //
00132                 // the objective function nonlinear term abs( x0 + 1)
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                 // create a variable nl node for x0
00137                 nlNodeVariablePoint = new OSnLNodeVariable();
00138                 nlNodeVariablePoint->idx=0;
00139                 nlNodeVec.push_back( nlNodeVariablePoint);
00140                 // create the nl node for number 1
00141                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00142                 nlNodeNumberPoint->value = 1.0;
00143                 nlNodeVec.push_back( nlNodeNumberPoint);
00144                 // create the nl node for +
00145                 nlNodePoint = new OSnLNodePlus();
00146                 nlNodeVec.push_back( nlNodePoint);
00147                 // create the nl node for max
00148                 nlNodePoint = new OSnLNodeAbs();
00149                 nlNodeVec.push_back( nlNodePoint);
00150                 // the vectors are in postfix format
00151                 // now the expression tree
00152                 osinstance->instanceData->nonlinearExpressions->nl[ 0]->osExpressionTree->m_treeRoot =
00153                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00154                 nlNodeVec.clear();
00155                 //
00156                 //
00157                 // constraint 0 has no nonlinear terms
00158                 // generate the x0*x1 term in constraint 1
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                 // create a variable nl node for x0
00164                 nlNodeVariablePoint = new OSnLNodeVariable();
00165                 nlNodeVariablePoint->idx=0;
00166                 nlNodeVec.push_back( nlNodeVariablePoint);
00167                 // create the nl node for x1
00168                 nlNodeVariablePoint = new OSnLNodeVariable();
00169                 nlNodeVariablePoint->idx=1;
00170                 nlNodeVec.push_back( nlNodeVariablePoint);
00171                 // create the nl node for *
00172                 nlNodePoint = new OSnLNodeTimes();
00173                 nlNodeVec.push_back( nlNodePoint);
00174                 // the vectors are in postfix format
00175                 // now the expression tree
00176                 osinstance->instanceData->nonlinearExpressions->nl[ 1]->osExpressionTree->m_treeRoot =
00177                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00178                 nlNodeVec.clear();
00179                 // 
00180                 //
00181                 // generate the x0*x1 term in constraint 2
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                 // create a variable nl node for x0
00186                 nlNodeVariablePoint = new OSnLNodeVariable();
00187                 nlNodeVariablePoint->idx=0;
00188                 nlNodeVec.push_back( nlNodeVariablePoint);
00189                 // create the nl node for x0
00190                 nlNodeVariablePoint = new OSnLNodeVariable(); 
00191                 nlNodeVariablePoint->idx=1;
00192                 nlNodeVec.push_back( nlNodeVariablePoint);
00193                 // create the nl node for *
00194                 nlNodePoint = new OSnLNodeTimes();
00195                 nlNodeVec.push_back( nlNodePoint);
00196                 // the vectors are in postfix format
00197                 // now the expression tree
00198                 osinstance->instanceData->nonlinearExpressions->nl[ 2]->osExpressionTree->m_treeRoot =
00199                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00200                 nlNodeVec.clear();
00201                 //
00202                 //
00203                 //
00204                 // generate the max(x0 , x1 + 1) term in constraint 3
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                 // create a variable nl node for x1
00209                 nlNodeVariablePoint = new OSnLNodeVariable();
00210                 nlNodeVariablePoint->idx=1;
00211                 nlNodeVec.push_back( nlNodeVariablePoint);
00212                 // create the nl node for number 1
00213                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00214                 nlNodeNumberPoint->value = 1.0;
00215                 nlNodeVec.push_back( nlNodeNumberPoint);
00216                 // create the nl node for +
00217                 nlNodePoint = new OSnLNodePlus();
00218                 nlNodeVec.push_back( nlNodePoint);
00219                 // now push x0 to the stack
00220                 nlNodeVariablePoint = new OSnLNodeVariable();
00221                 nlNodeVariablePoint->idx=0;
00222                 nlNodeVec.push_back( nlNodeVariablePoint);
00223                 // create the nl node for max
00224                 nlNodeMaxPoint = new OSnLNodeMax();
00225                 nlNodeMaxPoint->inumberOfChildren = 2;
00226                 nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
00227                 nlNodeVec.push_back( nlNodeMaxPoint);
00228                 // the vectors are in postfix format
00229                 // now the expression tree
00230                 osinstance->instanceData->nonlinearExpressions->nl[ 3]->osExpressionTree->m_treeRoot =
00231                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00232                 nlNodeVec.clear();
00233                 //
00234                 //
00235                 //
00236                 // generate the if(x1, 1, x1) term in constraint 4
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                 // create a variable nl node for x1
00241                 nlNodeVariablePoint = new OSnLNodeVariable();
00242                 nlNodeVariablePoint->idx=1;
00243                 nlNodeVec.push_back( nlNodeVariablePoint);
00244                 // create the nl node for number 1
00245                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00246                 nlNodeNumberPoint->value = 1.0;
00247                 nlNodeVec.push_back( nlNodeNumberPoint);
00248                 // now push x1 to the stack
00249                 nlNodeVariablePoint = new OSnLNodeVariable();
00250                 nlNodeVariablePoint->idx=1;
00251                 nlNodeVec.push_back( nlNodeVariablePoint);
00252                 // create the nl node for If
00253                 nlNodePoint = new OSnLNodeIf();
00254                 nlNodeVec.push_back( nlNodePoint);
00255                 // the vectors are in postfix format
00256                 // now the expression tree
00257                 osinstance->instanceData->nonlinearExpressions->nl[ 4]->osExpressionTree->m_treeRoot =
00258                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
00259                 nlNodeVec.clear();
00260                 //
00261                 //
00262                 //
00263                 // generate the (x1 * 2 * x1  -  x1) * x0 term in constraint 5
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                 // create a variable nl node for x1
00268                 nlNodeVariablePoint = new OSnLNodeVariable();
00269                 nlNodeVariablePoint->idx=1;
00270                 nlNodeVec.push_back( nlNodeVariablePoint);
00271                 // create the nl node for number 1
00272                 nlNodeNumberPoint = new OSnLNodeNumber(); 
00273                 nlNodeNumberPoint->value = 2.0;
00274                 nlNodeVec.push_back( nlNodeNumberPoint);
00275                 // create an nl node for *
00276                 nlNodePoint = new OSnLNodeTimes(); 
00277                 nlNodeVec.push_back( nlNodePoint);
00278                 // now push x1 to the stack
00279                 nlNodeVariablePoint = new OSnLNodeVariable();
00280                 nlNodeVariablePoint->idx=1;
00281                 nlNodeVec.push_back( nlNodeVariablePoint);
00282                 // create an nl node for *
00283                 nlNodePoint = new OSnLNodeTimes(); 
00284                 nlNodeVec.push_back( nlNodePoint);
00285                 // create a variable nl node for x1
00286                 nlNodeVariablePoint = new OSnLNodeVariable();
00287                 nlNodeVariablePoint->idx=1;
00288                 nlNodeVec.push_back( nlNodeVariablePoint);
00289                 // create an nl node for -
00290                 nlNodePoint = new OSnLNodeMinus(); 
00291                 nlNodeVec.push_back( nlNodePoint);
00292                 // create a variable nl node for x0
00293                 nlNodeVariablePoint = new OSnLNodeVariable();
00294                 nlNodeVariablePoint->idx=0;
00295                 nlNodeVec.push_back( nlNodeVariablePoint);
00296                 // create an nl node for *
00297                 nlNodePoint = new OSnLNodeTimes(); 
00298                 nlNodeVec.push_back( nlNodePoint);
00299                 // the vectors are in postfix format
00300                 // now the expression tree
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                 // Write out the model
00309                 OSiLWriter *osilwriter;
00310                 osilwriter = new OSiLWriter();
00311                 cout << osilwriter->writeOSiL( osinstance);
00312                 std::cout << osinstance->printModel( ) << std::endl;
00313                 // done writing the model
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 

Generated on Thu Nov 10 03:05:46 2011 by  doxygen 1.4.7