/home/coin/SVN-release/OS-2.3.2/ApplicationTemplates/cglCuts/OSCglCuts.cpp

Go to the documentation of this file.
00001 /* $Id: OSAddCuts.cpp 2710 2009-06-10 21:13:43Z kmartin $ */
00018 #include <cppad/cppad.hpp> 
00019 #include "OSConfig.h"
00020 #include "OSCoinSolver.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 "OSFileUtil.h"   
00028 #include "OSDefaultSolver.h"  
00029 #include "OSWSUtil.h" 
00030 #include "OSSolverAgent.h"   
00031 #include "OShL.h"     
00032 #include "OSErrorClass.h"
00033 #include "OSmps2osil.h"   
00034 #include "OSBase64.h"
00035 #include "OSErrorClass.h"
00036 #include "CglGomory.hpp"
00037 #include "CglSimpleRounding.hpp"
00038 #include "CglKnapsackCover.hpp"
00039 #include "OSMathUtil.h"
00040 #include "CbcModel.hpp"
00041 
00042 #include<iostream> 
00043 using std::cout;   
00044 using std::endl;
00045 
00046 int main( ){
00047         WindowsErrorPopupBlocker();
00048 // test OS code samples here
00049         FileUtil *fileUtil = NULL; 
00050         fileUtil = new FileUtil();
00051         const char dirsep =  CoinFindDirSeparator();
00052         // Set directory containing mps data files.
00053         std::string dataDir;
00054    dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
00055         cout << "Start Building the Model" << endl;
00056         try{
00057                 // get the p0033 problem
00058                 std::string osilFileName;
00059                 osilFileName =  dataDir  +   "p0033.osil";
00060                 std::cout << "Try to read a sample file" << std::endl;
00061                 std::cout << "The file is: " ;
00062                 std::cout <<  osilFileName << std::endl;
00063                 std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
00064                 OSiLReader *osilreader = NULL;
00065                 osilreader = new OSiLReader(); 
00066                 OSInstance *osinstance;
00067                 osinstance = osilreader->readOSiL( osil);
00068                 // done writing the model
00069                 cout << "Done writing the Model" << endl;
00070                 // now solve the model
00071                 CoinSolver *solver  = NULL;
00072                 solver = new CoinSolver();  
00073                 solver->sSolverName ="cbc"; 
00074                 solver->osinstance = osinstance;
00075                 solver->buildSolverInstance();
00076                 solver->osiSolver->setHintParam(OsiDoReducePrint, true, OsiHintTry);
00077                 solver->osiSolver->initialSolve();
00078                 cout << "Here is the initial objective value "  << solver->osiSolver->getObjValue()  << endl;
00079                 
00080                 CglKnapsackCover cover;
00081             CglSimpleRounding round;  
00082                 CglGomory gomory;
00083                 CbcModel *model = new CbcModel( *solver->osiSolver);
00084 
00085                 //model->setBestObjectiveValue(4000);
00086                 model->setMaximumNodes(100000);
00087                 //
00088                 model->addCutGenerator(&gomory, 1, "Gomory");
00089                 model->addCutGenerator(&cover, 1, "Cover");
00090                 model->addCutGenerator(&round, 1, "Round");
00091                 model->branchAndBound();        
00092                 // now create a result object
00093                 OSResult *osresult = new OSResult();
00094                 // if we are throw an exception if the problem is nonlinear
00095                 double *x = NULL;
00096                 double *y = NULL;
00097                 double *z = NULL;
00098                 //int i = 0;
00099                 std::string *rcost = NULL;
00100                 // resultHeader infomration
00101                 if(osresult->setServiceName("Solved with Coin Solver: " + solver->sSolverName) != true)
00102                         throw ErrorClass("OSResult error: setServiceName");
00103                 if(osresult->setInstanceName(  solver->osinstance->getInstanceName()) != true)
00104                         throw ErrorClass("OSResult error: setInstanceName");
00105                 if(osresult->setVariableNumber( solver->osinstance->getVariableNumber()) != true)
00106                         throw ErrorClass("OSResult error: setVariableNumer");
00107                 if(osresult->setObjectiveNumber( 1) != true)
00108                         throw ErrorClass("OSResult error: setObjectiveNumber");
00109                 if(osresult->setConstraintNumber( solver->osinstance->getConstraintNumber()) != true)
00110                         throw ErrorClass("OSResult error: setConstraintNumber");
00111                 if(osresult->setSolutionNumber(  1) != true)
00112                         throw ErrorClass("OSResult error: setSolutionNumer");   
00113                 int solIdx = 0;
00114                 std::string description = "";
00115                 osresult->setGeneralStatusType("success");
00116                 std::cout << "PROVEN OPTIMAL " << model->isProvenOptimal() << std::endl;
00117                 int i;
00118                 if (model->isProvenOptimal() == 1){
00119                         osresult->setSolutionStatus(solIdx, "optimal", description);
00120                         /* Retrieve the solution */
00121                         x = new double[solver->osinstance->getVariableNumber() ];
00122                         y = new double[solver->osinstance->getConstraintNumber() ];
00123                         z = new double[1];
00124                         rcost = new std::string[ solver->osinstance->getVariableNumber()];
00125                         //
00126                         *(z + 0)  =  model->getObjValue();
00127                         osresult->setObjectiveValuesDense(solIdx, z);
00128                         for(i=0; i < solver->osinstance->getVariableNumber(); i++){
00129                                 *(x + i) = model->getColSolution()[i];
00130                         }
00131                         osresult->setPrimalVariableValuesDense(solIdx, x );
00132                         //if( solver->sSolverName.find( "symphony") == std::string::npos){
00133                         for(i=0; i <  solver->osinstance->getConstraintNumber(); i++){
00134                                 *(y + i) = model->getRowPrice()[ i];
00135                         }
00136                         osresult->setDualVariableValuesDense(solIdx, y);
00137                         //
00138                         // now put the reduced costs into the osrl
00139                         int numberOfOtherVariableResult = 1;
00140                         int otherIdx = 0;
00141                         // first set the number of Other Variable Results
00142                         osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResult);
00143                         
00144                         ostringstream outStr;
00145                         int numberOfVar =  solver->osinstance->getVariableNumber();
00146                         for(i=0; i < numberOfVar; i++){
00147                                 outStr << model->getReducedCost()[ i]; 
00148                                 rcost[ i] = outStr.str();
00149                                 outStr.str("");
00150                         }
00151                         osresult->setAnOtherVariableResultDense(solIdx, otherIdx, "reduced costs", "", "the variable reduced costs",  
00152                                 rcost);                 
00153                         // end of settiing reduced costs                        
00154                 }
00155                 else{ 
00156                         if(solver->osiSolver->isProvenPrimalInfeasible() == true) 
00157                                 osresult->setSolutionStatus(solIdx, "infeasible", description);
00158                         else
00159                                 if(solver->osiSolver->isProvenDualInfeasible() == true) 
00160                                         osresult->setSolutionStatus(solIdx, "dualinfeasible", description);
00161                                 else
00162                                         osresult->setSolutionStatus(solIdx, "other", description);
00163                 }
00164                 OSrLWriter *osrlwriter = new OSrLWriter();
00165                 std::cout <<  osrlwriter->writeOSrL( osresult) << std::endl;
00166                 if(solver->osinstance->getVariableNumber() > 0){
00167                         delete[] x;
00168                         x = NULL;
00169                 }
00170                 if(solver->osinstance->getConstraintNumber()) delete[] y;
00171                 y = NULL;
00172                 delete[] z;     
00173                 z = NULL;
00174                 if(solver->osinstance->getVariableNumber() > 0){
00175                         delete[] rcost;
00176                         rcost = NULL;
00177                 }
00178         // do garbage collection
00179                 delete osresult;
00180                 osresult = NULL;
00181                 delete osrlwriter;
00182                 osrlwriter = NULL;
00183                 delete solver;
00184                 solver = NULL;
00185                 delete osilreader;
00186                 osilreader = NULL;
00187                 delete fileUtil;
00188                 fileUtil  = NULL;
00189                 delete model;
00190                 model = NULL;
00191                 cout << "Done with garbage collection" << endl;
00192                 return 0;
00193         }
00194         catch(const ErrorClass& eclass){
00195                 delete fileUtil;
00196                 std::cout << eclass.errormsg <<  std::endl;
00197                 return 0;
00198         } 
00199 }// end main
00200 

Generated on Fri Jan 7 03:24:38 2011 by  doxygen 1.4.7