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