/home/coin/SVN-release/OS-2.2.0/OS/CoinAllExamples/addCuts/OSAddCuts.cpp

Go to the documentation of this file.
00001 
00017 #include <cppad/cppad.hpp> 
00018 #include "OSConfig.h"
00019 #include "OSCoinSolver.h"
00020 #include "OSResult.h" 
00021 #include "OSiLReader.h"        
00022 #include "OSiLWriter.h"   
00023 #include "OSrLReader.h"          
00024 #include "OSrLWriter.h"      
00025 #include "OSInstance.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 "OSCommonUtil.h"
00035 #include "OSErrorClass.h"
00036 #include "CglGomory.hpp"
00037 #include "CglSimpleRounding.hpp"
00038 #include "CglKnapsackCover.hpp"
00039 #include "OSMathUtil.h"
00040 
00041 #include<iostream> 
00042 using std::cout;   
00043 using std::endl;
00044 
00045 int main( ){
00046         WindowsErrorPopupBlocker();
00047 // test OS code samples here
00048         FileUtil *fileUtil = NULL; 
00049         fileUtil = new FileUtil();
00050         const char dirsep =  CoinFindDirSeparator();
00051         // Set directory containing mps data files.
00052         std::string dataDir;
00053    dataDir = dirsep == '/' ? "../../data/" : "..\\..\\data\\";
00054         cout << "Start Building the Model" << endl;
00055         try{
00056                 // get the p0033 problem
00057                 std::string osilFileName;
00058                 //osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00059                 osilFileName =  "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 ="clp"; 
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                 CglKnapsackCover cover;
00080             CglSimpleRounding round;  
00081                 CglGomory gomory;
00082                 CbcModel *model = new CbcModel( *solver->osiSolver);
00083 
00084                 //model->setBestObjectiveValue(4000);
00085                 model->setMaximumNodes(100000);
00086                 //
00087                 model->addCutGenerator(&gomory, 1, "Gomory");
00088                 model->addCutGenerator(&cover, 1, "Cover");
00089                 model->addCutGenerator(&round, 1, "Round");
00090                 model->branchAndBound();        
00091                 // now create a result object
00092                 OSResult *osresult = new OSResult();
00093                 // if we are throw an exception if the problem is nonlinear
00094                 double *x = NULL;
00095                 double *y = NULL;
00096                 double *z = NULL;
00097                 //int i = 0;
00098                 std::string *rcost = NULL;
00099                 // resultHeader infomration
00100                 if(osresult->setServiceName("Solved with Coin Solver: " + solver->sSolverName) != true)
00101                         throw ErrorClass("OSResult error: setServiceName");
00102                 if(osresult->setInstanceName(  solver->osinstance->getInstanceName()) != true)
00103                         throw ErrorClass("OSResult error: setInstanceName");
00104                 if(osresult->setVariableNumber( solver->osinstance->getVariableNumber()) != true)
00105                         throw ErrorClass("OSResult error: setVariableNumer");
00106                 if(osresult->setObjectiveNumber( 1) != true)
00107                         throw ErrorClass("OSResult error: setObjectiveNumber");
00108                 if(osresult->setConstraintNumber( solver->osinstance->getConstraintNumber()) != true)
00109                         throw ErrorClass("OSResult error: setConstraintNumber");
00110                 if(osresult->setSolutionNumber(  1) != true)
00111                         throw ErrorClass("OSResult error: setSolutionNumer");   
00112                 int solIdx = 0;
00113                 std::string description = "";
00114                 osresult->setGeneralStatusType("success");
00115                 std::cout << "PROVEN OPTIMAL " << model->isProvenOptimal() << std::endl;
00116                 int i;
00117                 if (model->isProvenOptimal() == 1){
00118                         osresult->setSolutionStatus(solIdx, "optimal", description);
00119                         /* Retrieve the solution */
00120                         x = new double[solver->osinstance->getVariableNumber() ];
00121                         y = new double[solver->osinstance->getConstraintNumber() ];
00122                         z = new double[1];
00123                         rcost = new std::string[ solver->osinstance->getVariableNumber()];
00124                         //
00125                         *(z + 0)  =  model->getObjValue();
00126                         osresult->setObjectiveValues(solIdx, z);
00127                         for(i=0; i < solver->osinstance->getVariableNumber(); i++){
00128                                 *(x + i) = model->getColSolution()[i];
00129                         }
00130                         osresult->setPrimalVariableValues(solIdx, x);
00131                         //if( solver->sSolverName.find( "symphony") == std::string::npos){
00132                         for(i=0; i <  solver->osinstance->getConstraintNumber(); i++){
00133                                 *(y + i) = model->getRowPrice()[ i];
00134                         }
00135                         osresult->setDualVariableValues(solIdx, y);
00136                         //
00137                         // now put the reduced costs into the osrl
00138                         int numberOfOtherVariableResult = 1;
00139                         int otherIdx = 0;
00140                         // first set the number of Other Variable Results
00141                         osresult->setNumberOfOtherVariableResult(solIdx, numberOfOtherVariableResult);
00142                         ostringstream outStr;
00143                         int numberOfVar =  solver->osinstance->getVariableNumber();
00144                         for(i=0; i < numberOfVar; i++){
00145                                 outStr << model->getReducedCost()[ i]; 
00146                                 rcost[ i] = outStr.str();
00147                                 outStr.str("");
00148                         }
00149                         osresult->setAnOtherVariableResult(solIdx, otherIdx, "reduced costs", "the variable reduced costs", rcost);                     
00150                         // end of settiing reduced costs                        
00151                 }
00152                 else{ 
00153                         if(solver->osiSolver->isProvenPrimalInfeasible() == true) 
00154                                 osresult->setSolutionStatus(solIdx, "infeasible", description);
00155                         else
00156                                 if(solver->osiSolver->isProvenDualInfeasible() == true) 
00157                                         osresult->setSolutionStatus(solIdx, "dualinfeasible", description);
00158                                 else
00159                                         osresult->setSolutionStatus(solIdx, "other", description);
00160                 }
00161                 OSrLWriter *osrlwriter = new OSrLWriter();
00162                 std::cout <<  osrlwriter->writeOSrL( osresult) << std::endl;
00163                 if(solver->osinstance->getVariableNumber() > 0) delete[] x;
00164                 x = NULL;
00165                 if(solver->osinstance->getConstraintNumber()) delete[] y;
00166                 y = NULL;
00167                 delete[] z;     
00168                 z = NULL;
00169                 if(solver->osinstance->getVariableNumber() > 0){
00170                         delete[] rcost;
00171                         rcost = NULL;
00172                 }
00173         // do garbage collection
00174                 delete osresult;
00175                 osresult = NULL;
00176                 delete osrlwriter;
00177                 osrlwriter = NULL;
00178                 delete solver;
00179                 solver = NULL;
00180                 delete osilreader;
00181                 osilreader = NULL;
00182                 delete fileUtil;
00183                 fileUtil  = NULL;
00184                 cout << "Done with garbage collection" << endl;
00185                 return 0;
00186         }
00187         catch(const ErrorClass& eclass){
00188                 delete fileUtil;
00189                 std::cout << eclass.errormsg <<  std::endl;
00190                 return 0;
00191         } 
00192 }// end main
00193 

Generated on Thu Aug 5 03:02:58 2010 by  doxygen 1.4.7