/home/coin/SVN-release/OS-2.4.0/ApplicationTemplates/osModificationDemo/OSModificationDemo.cpp

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

Generated on Thu Sep 22 03:05:50 2011 by  doxygen 1.4.7