- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/OS-2.3.5/ApplicationTemplates/instanceGenerator/OSInstanceGenerator.cpp

Go to the documentation of this file.

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