/home/coin/SVN-release/OS-1.1.0/OS/examples/osTestCode/OSTestCode.cpp

Go to the documentation of this file.
00001 
00018 #include <cppad/cppad.hpp> 
00019 #include "OSConfig.h"
00020 #include "OSCoinSolver.h"
00021 
00022 #ifdef COIN_HAS_IPOPT  
00023         #ifndef COIN_HAS_ASL
00024                 #include "OSIpoptSolver.h"
00025                 #undef COIN_HAS_ASL
00026         #else
00027                 #include "OSIpoptSolver.h"
00028         #endif
00029 #endif
00030 
00031 #include "OSResult.h" 
00032 #include "OSiLReader.h"        
00033 #include "OSiLWriter.h"   
00034 #include "OSrLReader.h"          
00035 #include "OSrLWriter.h"      
00036 #include "OSInstance.h"  
00037 #include "OSFileUtil.h"   
00038 #include "OSDefaultSolver.h"  
00039 //#include "OSWSUtil.h" 
00040 //#include "OSSolverAgent.h"   
00041 #include "OShL.h"     
00042 #include "OSErrorClass.h"
00043 #include "OSmps2osil.h"   
00044 #include "OSBase64.h"
00045 #include "OSCommonUtil.h"
00046 #include "OSErrorClass.h"
00047 #include "OSMathUtil.h"
00048 
00049 #include<iostream> 
00050 using std::cout;   
00051 using std::endl;
00052 
00053 //int main(int argC, char* argV[]){
00054 int main( ){
00055 // test OS code samples here
00056         FileUtil *fileUtil = NULL; 
00057         fileUtil = new FileUtil();
00058         cout << "Start Building the Model" << endl;
00059         try{
00060                 OSInstance *osinstance = new OSInstance();
00061 
00062                 // put in some of the OSInstance <instanceHeader> information
00063                 osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
00064                 osinstance->setInstanceDescription("The Par Inc. Problem");
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", 0, OSDBL_MAX, 'C', OSNAN, "");
00073                 osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C', OSNAN, "");
00074                 //
00075                 // now add the objective function
00076                 osinstance->setObjectiveNumber( 1);
00077                 // now the coefficient
00078                 SparseVector *objcoeff = new SparseVector(2);   
00079                 objcoeff->indexes[ 0] = 0;
00080                 objcoeff->values[ 0] = 10;
00081                 objcoeff->indexes[ 1] = 1;
00082                 objcoeff->values[ 1] = 9;
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( 4); 
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, 630, 0); 
00093                 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
00094                 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
00095                 osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0); 
00096                 /*
00097                 now add the <linearConstraintCoefficients>
00098                 first do column major
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[ 8];
00104                 int *indexes = new int[ 8];
00105                 int *starts = new int[ 3];  
00106                 values[ 0] = .7;
00107                 values[ 1] = .5;
00108                 values[ 2] = 1.0;
00109                 values[ 3] = .1;
00110                 values[ 4] = 1.0;
00111                 values[ 5] = 5./6.;
00112                 values[ 6] = 2./3.;
00113                 values[ 7] = .25;
00114                 indexes[ 0] = 0;
00115                 indexes[ 1] = 1;
00116                 indexes[ 2] = 2;
00117                 indexes[ 3] = 3;
00118                 indexes[ 4] = 0;
00119                 indexes[ 5] = 1;
00120                 indexes[ 6] = 2;  
00121                 indexes[ 7] = 3;
00122                 starts[ 0] = 0;
00123                 starts[ 1] = 4;
00124                 starts[ 2] = 8; 
00125                 cout << "Call setLinearConstraintCoefficients" << endl;
00126                 osinstance->setLinearConstraintCoefficients(8, true, values, 0, 7, 
00127                         indexes, 0, 7, starts, 0, 2);   
00128                 
00129                 now row major
00130                 */
00131                 double *values = new double[ 8];
00132                 int *indexes = new int[ 8];
00133                 int *starts = new int[ 5];  
00134                 values[ 0] = .7;
00135                 values[ 1] = 1;
00136                 values[ 2] = .5;
00137                 values[ 3] = 5./6.;
00138                 values[ 4] = 1.0;
00139                 values[ 5] = 2./3.;
00140                 values[ 6] = .1;
00141                 values[ 7] = .25;
00142                 indexes[ 0] = 0;
00143                 indexes[ 1] = 1;
00144                 indexes[ 2] = 0;
00145                 indexes[ 3] = 1;
00146                 indexes[ 4] = 0;
00147                 indexes[ 5] = 1;
00148                 indexes[ 6] = 0;  
00149                 indexes[ 7] = 1;
00150                 starts[ 0] = 0;
00151                 starts[ 1] = 2;
00152                 starts[ 2] = 4;
00153                 starts[ 3] = 6;
00154                 starts[ 4] = 8;
00155                 cout << "Call setLinearConstraintCoefficients" << endl;
00156                 osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7, 
00157                         indexes, 0, 7, starts, 0, 4);
00158                 cout << "End Building the Model" << endl; 
00159                 // Write out the model
00160                 OSiLWriter *osilwriter; 
00161                 osilwriter = new OSiLWriter();
00162                 cout << osilwriter->writeOSiL( osinstance);
00163                 // done writing the model
00164                 cout << "Done writing the Model" << endl;
00165                 // now solve the model
00166                 CoinSolver *solver = new CoinSolver();
00167                 //IpoptSolver *solver = new IpoptSolver();
00168                 solver->osinstance = osinstance;
00169                 solver->sSolverName ="clp"; 
00170                 cout << "call the COIN - Clp Solver" << endl;
00171                 solver->buildSolverInstance();
00172                 solver->solve();
00173                 std::cout << solver->osrl << std::endl;
00174  
00175                 // do garbage collection
00176                 delete osinstance;
00177                 osinstance = NULL;
00178                 delete osilwriter;
00179                 osilwriter = NULL;
00180                 delete solver;
00181                 solver = NULL;
00182                 delete fileUtil;
00183                 fileUtil = NULL;
00184                 cout << "Done with garbage collection" << endl;
00185                 return 0;
00186                 //
00187         }
00188         catch(const ErrorClass& eclass){
00189                 delete fileUtil;
00190                 std::cout << eclass.errormsg <<  std::endl;
00191                 return 0;
00192         } 
00193 }// end main
00194 

Generated on Fri May 16 19:01:52 2008 by  doxygen 1.4.7