/home/coin/SVN-release/OS-2.4.1/examples/osModificationDemo/OSModificationDemo.cpp

Go to the documentation of this file.
00001 /* $Id: OSModificationDemo.cpp 2831 2009-07-21 07:34:44Z kmartin $ */
00016 #include "OSConfig.h"
00017 #include "OSCoinSolver.h"
00018 #include "OSIpoptSolver.h"
00019 #include "OSResult.h" 
00020 #include "OSiLReader.h"        
00021 #include "OSiLWriter.h"   
00022 #include "OSrLReader.h"          
00023 #include "OSrLWriter.h"      
00024 #include "OSInstance.h"  
00025 #include "OSoLWriter.h"
00026 #include "OSFileUtil.h"   
00027 #include "OSDefaultSolver.h"  
00028 //#include "OSWSUtil.h" 
00029 //#include "OSSolverAgent.h"   
00030 #include "OShL.h"     
00031 #include "OSErrorClass.h"
00032 #include "OSmps2osil.h"   
00033 #include "OSBase64.h"
00034 #include "OSErrorClass.h"
00035 #include "OSMathUtil.h"
00036 
00037 #include "OsiClpSolverInterface.hpp"
00038 #include "OsiCbcSolverInterface.hpp"
00039 #include "CbcModel.hpp"
00040 
00041 #include<iostream> 
00042 using std::cout;   
00043 using std::endl;
00044 
00045 //int main(int argC, char* argV[]){
00046 int main( ){
00047 // test OS code samples here
00048 
00049         FileUtil *fileUtil = NULL; 
00050         fileUtil = new FileUtil();
00051         cout << "Start Building the Model" << endl;
00052         try{
00053                 OSInstance *osinstance = new OSInstance();
00054 
00055                 // put in some of the OSInstance <instanceHeader> information
00056                 osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
00057                 osinstance->setInstanceDescription("The Par Inc. Problem");
00058                 //
00059                 // now put in the OSInstance <instanceData> information
00060                 // 
00061                 // first the variables
00062                 osinstance->setVariableNumber( 2);   
00063                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00064                 // we could use setVariables() and add all the variable with one method call -- below is easier
00065                 osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C');
00066                 osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C');
00067                 //
00068                 // now add the objective function
00069                 osinstance->setObjectiveNumber( 1);
00070                 // now the coefficient
00071                 SparseVector *objcoeff = new SparseVector(2);   
00072                 objcoeff->indexes[ 0] = 0;
00073                 objcoeff->values[ 0] = 10;
00074                 objcoeff->indexes[ 1] = 1;
00075                 objcoeff->values[ 1] = 9;
00076                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00077                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00078                 objcoeff->bDeleteArrays = true;
00079                 delete objcoeff;
00080                 //
00081                 // now the constraints
00082                 osinstance->setConstraintNumber( 4); 
00083                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00084                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
00085                 osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0); 
00086                 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
00087                 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
00088                 osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0); 
00089                 /*
00090                 now add the <linearConstraintCoefficients>
00091                 first do column major
00092                 bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
00093                 double* values, int valuesBegin, int valuesEnd, 
00094                 int* indexes, int indexesBegin, int indexesEnd,                         
00095                 int* starts, int startsBegin, int startsEnd);   
00096                 double *values = new double[ 8];
00097                 int *indexes = new int[ 8];
00098                 int *starts = new int[ 3];  
00099                 values[ 0] = .7;
00100                 values[ 1] = .5;
00101                 values[ 2] = 1.0;
00102                 values[ 3] = .1;
00103                 values[ 4] = 1.0;
00104                 values[ 5] = 5./6.;
00105                 values[ 6] = 2./3.;
00106                 values[ 7] = .25;
00107                 indexes[ 0] = 0;
00108                 indexes[ 1] = 1;
00109                 indexes[ 2] = 2;
00110                 indexes[ 3] = 3;
00111                 indexes[ 4] = 0;
00112                 indexes[ 5] = 1;
00113                 indexes[ 6] = 2;  
00114                 indexes[ 7] = 3;
00115                 starts[ 0] = 0;
00116                 starts[ 1] = 4;
00117                 starts[ 2] = 8; 
00118                 cout << "Call setLinearConstraintCoefficients" << endl;
00119                 osinstance->setLinearConstraintCoefficients(8, true, values, 0, 7, 
00120                         indexes, 0, 7, starts, 0, 2);   
00121                 
00122                 now row major
00123                 */
00124                 double *values = new double[ 8];
00125                 int *indexes = new int[ 8];
00126                 int *starts = new int[ 5];  
00127                 values[ 0] = .7;
00128                 values[ 1] = 1;
00129                 values[ 2] = .5;
00130                 values[ 3] = 5./6.;
00131                 values[ 4] = 1.0;
00132                 values[ 5] = 2./3.;
00133                 values[ 6] = .1;
00134                 values[ 7] = .25;
00135                 indexes[ 0] = 0;
00136                 indexes[ 1] = 1;
00137                 indexes[ 2] = 0;
00138                 indexes[ 3] = 1;
00139                 indexes[ 4] = 0;
00140                 indexes[ 5] = 1;
00141                 indexes[ 6] = 0;  
00142                 indexes[ 7] = 1;
00143                 starts[ 0] = 0;
00144                 starts[ 1] = 2;
00145                 starts[ 2] = 4;
00146                 starts[ 3] = 6;
00147                 starts[ 4] = 8;
00148                 cout << "Call setLinearConstraintCoefficients" << endl;
00149                 osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7, 
00150                         indexes, 0, 7, starts, 0, 4);
00151                 cout << "End Building the Model" << endl; 
00152                 // Write out the model
00153                 OSiLWriter *osilwriter; 
00154                 osilwriter = new OSiLWriter();
00155                 cout << osilwriter->writeOSiL( osinstance);
00156                 // done writing the model
00157                 cout << "Done writing the Model" << endl;
00158                 // now solve the model
00159                 CoinSolver *solver = new CoinSolver();
00160                 solver->osinstance = osinstance;
00161                 solver->sSolverName ="clp"; 
00162                 solver->buildSolverInstance();          
00163                 solver->solve();
00164                 std::cout << solver->osrl << std::endl;
00165                 // write the answer to a file
00166                 //fileUtil->writeFileFromString("../result.xml", solver->osrl);
00167                 // work with the OSResult object
00168                 //OSResult *result = NULL;
00169                 //result = solver->osresult;
00170                 //std::cout << result->resultData->optimization->solution[0]->objectives->values->obj[0]->value << std::endl;
00171                 // do garbage collection
00172                 
00173                 // illustrate changing an objective function coefficient
00174                 delete solver;
00175                 osinstance->bObjectivesModified = true;
00176                 osinstance->instanceData->objectives->obj[0]->coef[0]->value = 5;
00177                 solver = new CoinSolver();
00178                 solver->osinstance = osinstance;
00179                 solver->sSolverName ="clp"; 
00180                 solver->buildSolverInstance();
00181                 solver->solve();
00182                 std::cout << solver->osrl << std::endl;
00183                 delete solver;
00184                 // change objective function coefficient a second time
00185                 osinstance->bObjectivesModified = true;
00186                 osinstance->instanceData->objectives->obj[0]->coef[0]->value = 0;       
00187                 // make the upper bound of the second variable 500
00188                 // first print out the current value -- should be "infinity"
00189                 std::cout << "Variable upper bound = "<< osinstance->instanceData->variables->var[1]->ub << std::endl;
00190                 // now change it to 500;
00191                 osinstance->bVariablesModified = true;
00192                 osinstance->instanceData->variables->var[1]->ub = 500;
00193                 std::cout << "Variable upper bound = "<< osinstance->instanceData->variables->var[1]->ub << std::endl;
00194                 solver = new CoinSolver();
00195                 solver->osinstance = osinstance;
00196                 solver->sSolverName ="clp";     
00197                 solver->buildSolverInstance();
00198                 solver->solve();
00199                 std::cout << solver->osrl << std::endl;         
00200                 std::cout << "Obj value =  " << solver->osresult->optimization->solution[0]->objectives->values->obj[0]->value << endl; 
00201                 std::cout << "Obj value =  " << solver->osresult->getOptimalObjValue( -1, 0) << endl;  // use a get             
00202                 delete osinstance;
00203                 osinstance = NULL;
00204                 delete osilwriter;
00205                 osilwriter = NULL;
00206                 delete solver;
00207                 solver = NULL;
00208                 delete fileUtil;
00209                 fileUtil = NULL;
00210                 cout << "Done with garbage collection" << endl;
00211                 return 0;
00212                 //
00213         }
00214         catch(const ErrorClass& eclass){
00215                 delete fileUtil;
00216                 std::cout << eclass.errormsg <<  std::endl;
00217                 return 0;
00218         } 
00219 }// end main
00220 

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