OSCglCuts.cpp
Go to the documentation of this file.
1 /* $Id: OSAddCuts.cpp 2710 2009-06-10 21:13:43Z kmartin $ */
16 #include <cppad/cppad.hpp>
17 #include "OSConfig.h"
18 #include "OSCoinSolver.h"
19 #include "OSResult.h"
20 #include "OSiLReader.h"
21 #include "OSiLWriter.h"
22 #include "OSrLReader.h"
23 #include "OSrLWriter.h"
24 #include "OSInstance.h"
25 #include "OSFileUtil.h"
26 #include "OSDefaultSolver.h"
27 #include "OSWSUtil.h"
28 #include "OSSolverAgent.h"
29 #include "OShL.h"
30 #include "OSErrorClass.h"
31 #include "OSmps2OS.h"
32 #include "OSBase64.h"
33 #include "OSErrorClass.h"
34 #include "CglGomory.hpp"
35 #include "CglSimpleRounding.hpp"
36 #include "CglKnapsackCover.hpp"
37 #include "OSMathUtil.h"
38 #include "CbcModel.hpp"
39 
40 #include <iostream>
41 using std::cout;
42 using std::endl;
43 
44 int main( ){
45  WindowsErrorPopupBlocker();
46 // test OS code samples here
47  FileUtil *fileUtil = NULL;
48  fileUtil = new FileUtil();
49  const char dirsep = CoinFindDirSeparator();
50  // Set directory containing mps data files.
51  std::string dataDir;
52  dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
53  cout << "Start Building the Model" << endl;
54  try{
55  // get the p0033 problem
56  std::string osilFileName;
57  osilFileName = dataDir + "osilFiles" + dirsep + "p0033.osil";
58  std::cout << "Try to read a sample file" << std::endl;
59  std::cout << "The file is: " ;
60  std::cout << osilFileName << std::endl;
61  std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
62  OSiLReader *osilreader = NULL;
63  osilreader = new OSiLReader();
65  osinstance = osilreader->readOSiL( osil);
66  // done writing the model
67  cout << "Done writing the Model" << endl;
68  // now solve the model
69  CoinSolver *solver = NULL;
70  solver = new CoinSolver();
71  solver->sSolverName ="cbc";
72  solver->osinstance = osinstance;
73  solver->buildSolverInstance();
74  solver->osiSolver->setHintParam(OsiDoReducePrint, true, OsiHintTry);
75  solver->osiSolver->initialSolve();
76  cout << "Here is the initial objective value " << solver->osiSolver->getObjValue() << endl;
77 
78  CglKnapsackCover cover;
79  CglSimpleRounding round;
80  CglGomory gomory;
81  CbcModel *model = new CbcModel( *solver->osiSolver);
82 
83  //model->setBestObjectiveValue(4000);
84  model->setMaximumNodes(100000);
85  //
86  model->addCutGenerator(&gomory, 1, "Gomory");
87  model->addCutGenerator(&cover, 1, "Cover");
88  model->addCutGenerator(&round, 1, "Round");
89  model->branchAndBound();
90  // now create a result object
91  OSResult *osresult = new OSResult();
92  // if we are throw an exception if the problem is nonlinear
93  double *x = NULL;
94  double *y = NULL;
95  double *z = NULL;
96  //int i = 0;
97  std::string *rcost = NULL;
98  // resultHeader infomration
99  if(osresult->setServiceName("Solved with Coin Solver: " + solver->sSolverName) != true)
100  throw ErrorClass("OSResult error: setServiceName");
101  if(osresult->setInstanceName( solver->osinstance->getInstanceName()) != true)
102  throw ErrorClass("OSResult error: setInstanceName");
103  if(osresult->setVariableNumber( solver->osinstance->getVariableNumber()) != true)
104  throw ErrorClass("OSResult error: setVariableNumer");
105  if(osresult->setObjectiveNumber( 1) != true)
106  throw ErrorClass("OSResult error: setObjectiveNumber");
107  if(osresult->setConstraintNumber( solver->osinstance->getConstraintNumber()) != true)
108  throw ErrorClass("OSResult error: setConstraintNumber");
109  if(osresult->setSolutionNumber( 1) != true)
110  throw ErrorClass("OSResult error: setSolutionNumer");
111  int solIdx = 0;
112  std::string description = "";
113  osresult->setGeneralStatusType("success");
114  std::cout << "PROVEN OPTIMAL " << model->isProvenOptimal() << std::endl;
115  int i;
116  if (model->isProvenOptimal() == 1){
117  osresult->setSolutionStatus(solIdx, "optimal", description);
118  /* Retrieve the solution */
119  x = new double[solver->osinstance->getVariableNumber() ];
120  y = new double[solver->osinstance->getConstraintNumber() ];
121  z = new double[1];
122  rcost = new std::string[ solver->osinstance->getVariableNumber()];
123  //
124  *(z + 0) = model->getObjValue();
125  osresult->setObjectiveValuesDense(solIdx, z);
126  for(i=0; i < solver->osinstance->getVariableNumber(); i++){
127  *(x + i) = model->getColSolution()[i];
128  }
129  osresult->setPrimalVariableValuesDense(solIdx, x );
130  //if( solver->sSolverName.find( "symphony") == std::string::npos){
131  for(i=0; i < solver->osinstance->getConstraintNumber(); i++){
132  *(y + i) = model->getRowPrice()[ i];
133  }
134  osresult->setDualVariableValuesDense(solIdx, y);
135  //
136  // now put the reduced costs into the osrl
137  int numberOfOtherVariableResult = 1;
138  int otherIdx = 0;
139  // first set the number of Other Variable Results
140  osresult->setNumberOfOtherVariableResults(solIdx, numberOfOtherVariableResult);
141 
142  std::ostringstream outStr;
143  int numberOfVar = solver->osinstance->getVariableNumber();
144  for(i=0; i < numberOfVar; i++){
145  outStr << model->getReducedCost()[ i];
146  rcost[ i] = outStr.str();
147  outStr.str("");
148  }
149  osresult->setAnOtherVariableResultDense(solIdx, otherIdx, "reduced costs", "", "the variable reduced costs",
150  rcost);
151  // end of settiing reduced costs
152  }
153  else{
154  if(solver->osiSolver->isProvenPrimalInfeasible() == true)
155  osresult->setSolutionStatus(solIdx, "infeasible", description);
156  else
157  if(solver->osiSolver->isProvenDualInfeasible() == true)
158  osresult->setSolutionStatus(solIdx, "dualinfeasible", description);
159  else
160  osresult->setSolutionStatus(solIdx, "other", description);
161  }
162  OSrLWriter *osrlwriter = new OSrLWriter();
163  std::cout << osrlwriter->writeOSrL( osresult) << std::endl;
164  if(solver->osinstance->getVariableNumber() > 0){
165  delete[] x;
166  x = NULL;
167  }
168  if(solver->osinstance->getConstraintNumber()) delete[] y;
169  y = NULL;
170  delete[] z;
171  z = NULL;
172  if(solver->osinstance->getVariableNumber() > 0){
173  delete[] rcost;
174  rcost = NULL;
175  }
176  // do garbage collection
177  delete osresult;
178  osresult = NULL;
179  delete osrlwriter;
180  osrlwriter = NULL;
181  delete solver;
182  solver = NULL;
183  delete osilreader;
184  osilreader = NULL;
185  delete fileUtil;
186  fileUtil = NULL;
187  delete model;
188  model = NULL;
189  cout << "Done with garbage collection" << endl;
190  cout << "Program terminates normally" << endl;
191  return 0;
192  }
193  catch(const ErrorClass& eclass){
194  delete fileUtil;
195  std::cout << eclass.errormsg << std::endl;
196  return 0;
197  }
198 }// end main
199 
bool setSolutionStatus(int solIdx, std::string type, std::string description)
Set the [i]th optimization solution status, where i equals the given solution index.
bool setPrimalVariableValuesDense(int solIdx, double *x)
Set the [i]th optimization solution&#39;s primal variable values, where i equals the given solution index...
Definition: OSResult.cpp:5001
int getVariableNumber()
Get number of variables.
bool setServiceName(std::string serviceName)
Set service name.
bool setVariableNumber(int variableNumber)
Set the variable number.
Definition: OSResult.cpp:4712
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
OsiSolverInterface * osiSolver
osiSolver is the osi solver object – in this case clp, glpk, cbc, cplex, symphony or dylp ...
Definition: OSCoinSolver.h:93
The Result Class.
Definition: OSResult.h:2548
Take an OSResult object and write a string that validates against OSrL.
Definition: OSrLWriter.h:30
bool setDualVariableValuesDense(int solIdx, double *y)
Set the [i]th optimization solution&#39;s dual variable values, where i equals the given solution index...
Definition: OSResult.cpp:6291
bool setAnOtherVariableResultDense(int solIdx, int otherIdx, std::string name, std::string value, std::string description, std::string *s)
Set the [i]th optimization solution&#39;s other (non-standard/solver specific)variable-related results...
bool setObjectiveNumber(int objectiveNumber)
Set the objective number.
Definition: OSResult.cpp:4721
bool setInstanceName(std::string instanceName)
Set instance name.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
std::string writeOSrL(OSResult *theosresult)
create an osrl string from an OSResult object
Definition: OSrLWriter.cpp:45
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
bool setSolutionNumber(int number)
set the number of solutions.
Definition: OSResult.cpp:4740
Used to read an OSiL string.
Definition: OSiLReader.h:37
bool setNumberOfOtherVariableResults(int solIdx, int numberOfOtherVariableResults)
Set the [i]th optimization solution&#39;s other (non-standard/solver specific) variable-related results...
Definition: OSResult.cpp:5236
OSResult * osresult
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
bool setGeneralStatusType(std::string type)
Set the general status type, which can be: success, error, warning.
std::string getInstanceName()
Get instance name.
int getConstraintNumber()
Get number of constraints.
bool setConstraintNumber(int constraintNumber)
Set the constraint number.
Definition: OSResult.cpp:4731
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
Definition: OSFileUtil.cpp:35
bool setObjectiveValuesDense(int solIdx, double *objectiveValues)
Set the [i]th optimization solution&#39;s objective values, where i equals the given solution index...
Definition: OSResult.cpp:5824
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
class used to make it easy to read and write files.
Definition: OSFileUtil.h:37
used for throwing exceptions.
Definition: OSErrorClass.h:31
void fint fint fint real fint real * x