/home/coin/SVN-release/OS-2.0.0/OS/CoinAllExamples/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 
00050 
00051 #include<iostream> 
00052 using std::cout;   
00053 using std::endl;
00054 
00055 //int main(int argC, char* argV[]){
00056 int main( ){
00057         WindowsErrorPopupBlocker();
00058 // test OS code samples here
00059         FileUtil *fileUtil = NULL; 
00060         fileUtil = new FileUtil();
00061         
00062         try{
00063                 
00064                 OSInstance *osinstance = new OSInstance();
00065 
00066                 // put in some of the OSInstance <instanceHeader> information
00067                 osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
00068                 osinstance->setInstanceDescription("The Par Inc. Problem");
00069                 /* Here is the model:
00070                  * Max 10*x0 + 9*x1
00071                  * s.t.
00072                  * .7*x0 + x1 <= 630
00073                  * .5*x0 + (5/6)*x1 <= 600
00074                  *  x0 + (2/3)*x1 <= 708
00075                  * .1*x0 + .25*x1 <= 135
00076                  * 
00077                  */
00078                 //
00079                 // now put in the OSInstance <instanceData> information
00080                 // 
00081                 // first the variables
00082                 osinstance->setVariableNumber( 2);   
00083                 //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
00084                 // we could use setVariables() and add all the variable with one method call -- below is easier
00085                 osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C', OSNAN, "");
00086                 osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C', OSNAN, "");
00087                 //
00088                 // now add the objective function
00089                 osinstance->setObjectiveNumber( 1);
00090                 // now the coefficient
00091                 SparseVector *objcoeff = new SparseVector(2);   
00092                 objcoeff->indexes[ 0] = 0;
00093                 objcoeff->values[ 0] = 10;
00094                 objcoeff->indexes[ 1] = 1;
00095                 objcoeff->values[ 1] = 9;
00096                 //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
00097                 osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
00098                 objcoeff->bDeleteArrays = true;
00099                 delete objcoeff;
00100                 //
00101                 // now the constraints
00102                 osinstance->setConstraintNumber( 4); 
00103                 //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
00104                 // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
00105                 osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0); 
00106                 osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
00107                 osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
00108                 osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0); 
00109                 /*
00110                 now add the <linearConstraintCoefficients>
00111                 first do column major
00112                 bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
00113                 double* values, int valuesBegin, int valuesEnd, 
00114                 int* indexes, int indexesBegin, int indexesEnd,                         
00115                 int* starts, int startsBegin, int startsEnd);   
00116                 double *values = new double[ 8];
00117                 int *indexes = new int[ 8];
00118                 int *starts = new int[ 3];  
00119                 values[ 0] = .7;
00120                 values[ 1] = .5;
00121                 values[ 2] = 1.0;
00122                 values[ 3] = .1;
00123                 values[ 4] = 1.0;
00124                 values[ 5] = 5./6.;
00125                 values[ 6] = 2./3.;
00126                 values[ 7] = .25;
00127                 indexes[ 0] = 0;
00128                 indexes[ 1] = 1;
00129                 indexes[ 2] = 2;
00130                 indexes[ 3] = 3;
00131                 indexes[ 4] = 0;
00132                 indexes[ 5] = 1;
00133                 indexes[ 6] = 2;  
00134                 indexes[ 7] = 3;
00135                 starts[ 0] = 0;
00136                 starts[ 1] = 4;
00137                 starts[ 2] = 8; 
00138                 cout << "Call setLinearConstraintCoefficients" << endl;
00139                 osinstance->setLinearConstraintCoefficients(8, true, values, 0, 7, 
00140                         indexes, 0, 7, starts, 0, 2);   
00141                 
00142                 now row major
00143                 */
00144                 double *values = new double[ 8];
00145                 int *indexes = new int[ 8];
00146                 int *starts = new int[ 5];  
00147                 values[ 0] = .7;
00148                 values[ 1] = 1;
00149                 values[ 2] = .5;
00150                 values[ 3] = 5./6.;
00151                 values[ 4] = 1.0;
00152                 values[ 5] = 2./3.;
00153                 values[ 6] = .1;
00154                 values[ 7] = .25;
00155                 indexes[ 0] = 0;
00156                 indexes[ 1] = 1;
00157                 indexes[ 2] = 0;
00158                 indexes[ 3] = 1;
00159                 indexes[ 4] = 0;
00160                 indexes[ 5] = 1;
00161                 indexes[ 6] = 0;  
00162                 indexes[ 7] = 1;
00163                 starts[ 0] = 0;
00164                 starts[ 1] = 2;
00165                 starts[ 2] = 4;
00166                 starts[ 3] = 6;
00167                 starts[ 4] = 8;
00168                 cout << "Call setLinearConstraintCoefficients" << endl;
00169                 osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7, 
00170                         indexes, 0, 7, starts, 0, 4);
00171                 cout << "End Building the Model" << endl; 
00172                 // Write out the model
00173                 OSiLWriter *osilwriter; 
00174                 osilwriter = new OSiLWriter();
00175                 std::string osil = osilwriter->writeOSiL( osinstance);
00176                 cout << osil;
00177                 // done writing the model
00178                 cout << "Done writing the Model" << endl;
00179                 // now solve the model
00180                 CoinSolver *solver = new CoinSolver();
00181                 //IpoptSolver *solver = new IpoptSolver();
00182                 solver->osinstance = osinstance;
00183                 solver->sSolverName ="clp"; 
00184                 cout << "call the COIN - Clp Solver" << endl;
00185                 solver->buildSolverInstance();
00186                 solver->solve();
00187                 std::cout << solver->osrl << std::endl;
00188                 
00189                 
00190                 // now solve remotely
00191                 /*
00192                 OSSolverAgent* osagent = NULL;
00193                 osagent = new OSSolverAgent("gsbkip.chicagogsb.edu/os/OSSolverService.jws");    
00194                 
00195                 std::string osol = "<osol></osol>";
00196                 cout << "osil:" << endl << endl;
00197                 cout << osil  << endl << endl;
00198                 std::string osrl = osagent->solve(osil, osol);
00199                 cout << "osrl result from osagent:" << endl << endl;
00200                 cout << osrl  << endl << endl;
00201                 delete osagent
00202                 */
00203                 // do garbage collection
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 
00213                 cout << "Done with garbage collection" << endl;
00214                 return 0;
00215                 //
00216         }
00217         catch(const ErrorClass& eclass){
00218                 delete fileUtil;
00219                 std::cout << eclass.errormsg <<  std::endl;
00220                 return 0;
00221         } 
00222 }// end main
00223 

Generated on Mon Aug 3 03:02:21 2009 by  doxygen 1.4.7