OSDipBlockCoinSolver.cpp
Go to the documentation of this file.
1 /* $Id: OSBlockSolver.cpp 3038 2009-11-07 11:43:44Z Gassmann $ */
15 #include "OSDipBlockCoinSolver.h"
16 #include "OSErrorClass.h"
17 
18 
19 
21  m_osinstance(NULL) {
22  std::cout << "INSIDE OSDipBlockCoinSolver CONSTRUCTOR" << std::endl;
23 }//end OSDipBlockCoinSolver
24 
25 
27 
28  try{
30  //initialize the solver
31  m_solver = NULL;
32  m_solver = new CoinSolver();
33  //lets just start with Cbc
34  m_solver->sSolverName ="cbc";
37  //m_osrlreader = NULL;
38  //m_osrlreader = new OSrLReader();
40 
41  } catch (const ErrorClass& eclass) {
42 
43  throw ErrorClass(eclass.errormsg);
44 
45  }
46 
47 }//end OSDipBlockCoinSolver
48 
50 
51  if(m_solver != NULL) delete m_solver;
52  std::cout << "INSIDED ~OSDipBlockCoinSolver()" << std::endl;
53  //if(m_osrlreader != NULL) delete m_osrlreader;
54 }//end ~OSDipBlockCoinSolver
55 
56 void OSDipBlockCoinSolver::solve(double *cost, std::vector<IndexValuePair*> *solIndexValPair, double *optVal){
57 
58  try{
59  //set the objective function
60  //here we are using the Osi Interface
61  //we have already built the model
62  m_solver->osiSolver->setObjective( cost);
63 
64  //an echo check -- kipp put this inside a statement using a print log
65  for(int i = 0 ; i < m_numberOfVar; i++){
66 
68  = cost[ i];
69 
70  }
72  std::cout << m_osinstance->printModel( ) << std::endl;
73  //
74 
75 
76  //solve the model
77  m_solver->solve();
79  std::string solStatus;
80  // the argument is the solution index
81 
82 
83  solStatus = m_osresult->getSolutionStatusType( 0 );
84 
85  std::cout << "SOLUTION STATUS " << solStatus << std::endl;
86  // if solStatus is optimal get the optimal solution value
87  if( solStatus.find("ptimal") != std::string::npos ){
88  //first index is objIdx, second is solution index
89  *optVal = m_osresult->getOptimalObjValue( -1, 0);
90  *solIndexValPair = m_osresult->getOptimalPrimalVariableValues( 0);
91  }else{
92  throw ErrorClass("problem -- did not optimize a subproblem");
93  }
94 
95  } catch (const ErrorClass& eclass) {
96 
97  throw ErrorClass(eclass.errormsg);
98 
99  }
100 
101 }//end solve
102 
103 
104 void OSDipBlockCoinSolver::solve(double *cost, std::string *osrl){
105 
106 
107  try{
108  //set the objective function
109  //here we are using the Osi Interface
110  //we have already built the model
111  m_solver->osiSolver->setObjective( cost);
112 
113  //std::cout << m_osinstance->printModel( ) << std::endl;
114  //solve the model
115  m_solver->solve();
116  //std::cout << "MODEL BEING SOLVED " << std::endl;
117  //get the solution
118  //m_osresult = m_osrlreader->readOSrL( m_solver->osrl );
120  std::string solStatus;
121  // the argument is the solution index
122 
123 
124  solStatus = m_osresult->getSolutionStatusType( 0 );
125 
126  //std::cout << "SOLUTION STATUS " << solStatus << std::endl;
127  // if solStatus is optimal get the optimal solution value
128  if( solStatus.find("ptimal") != std::string::npos ){
129  //first index is objIdx, second is solution index
130  *osrl = m_solver->osrl;
131  }else{
132  throw ErrorClass("problem -- did not optimize a subproblem");
133  }
134 
135  } catch (const ErrorClass& eclass) {
136 
137  throw ErrorClass(eclass.errormsg);
138 
139  }
140 
141 }//end solve
142 
143 
std::string getSolutionStatusType(int solIdx)
Get the [i]th optimization solution status type, where i equals the given solution index...
Definition: OSResult.cpp:2051
int getVariableNumber()
Get number of variables.
std::string printModel()
Print the infix representation of the problem.
std::string osrl
osrl holds the solution or result of the model
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
OsiSolverInterface * osiSolver
osiSolver is the osi solver object – in this case clp, glpk, cbc, cplex, symphony or dylp ...
Definition: OSCoinSolver.h:93
std::vector< IndexValuePair * > getOptimalPrimalVariableValues(int solIdx)
Get one solution of optimal primal variable values.
Definition: OSResult.cpp:2215
ObjCoef ** coef
coef is pointer to an array of ObjCoef object pointers
Definition: OSInstance.h:176
OSResult * osresult
osresult holds the solution or result of the model in-memory as an OSResult object ...
double getOptimalObjValue(int objIdx, int solIdx)
Get one solution&#39;s optimal objective value.
Definition: OSResult.cpp:3065
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
OSDipBlockCoinSolver()
Default Constructor.
bool bObjectivesModified
bObjectivesModified is true if the objective function data has been modified.
Definition: OSInstance.h:2293
double value
value is the value of the objective function coefficient corresponding to the variable with index idx...
Definition: OSInstance.h:128
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
InstanceData * instanceData
A pointer to an InstanceData object.
Definition: OSInstance.h:2278
Objective ** obj
coef is pointer to an array of ObjCoef object pointers
Definition: OSInstance.h:205
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
Objectives * objectives
objectives is a pointer to a Objectives object
Definition: OSInstance.h:2188
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
~OSDipBlockCoinSolver()
Default destructor.
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
virtual void solve()
The implementation of the corresponding virtual function.
used for throwing exceptions.
Definition: OSErrorClass.h:31
virtual void solve(double *cost, std::vector< IndexValuePair * > *solIndexValPair, double *optVal)