BlockIPPlatform
 All Classes Files Functions Variables
OsiSolver.h
Go to the documentation of this file.
1 
6 #ifndef OSI_SOLVER_H
7 #define OSI_SOLVER_H
8 
9 #ifndef EXPDLL
10 #ifdef WIN32
11 #ifdef OSI_INTERFACE_EXPORTS
12 #define EXPDLL __declspec(dllexport)
13 #else
14 #define EXPDLL __declspec(dllimport)
15 #endif //OSI_INTERFACE_EXPORTS
16 #else
17 #define EXPDLL
18 #endif //WIN32
19 #endif //EXPDLL
20 
21 #include "CoinPackedMatrix.hpp"
22 #include "OsiSolverInterface.hpp"
23 #include "CoinMessageHandler.hpp"
24 #include "ExceptionOsiSolver.h"
25 
26 using namespace std;
27 
29 
32 class EXPDLL OsiSolver {
33  public:
34  enum STATUS{OPTIMAL_SOLUTION, TIME_LIMIT_INFEAS, TIME_LIMIT_FEAS, FEASIBLE, FIRST_FEASIBLE, INFEASIBLE, OTHERWISE};
35 
36  protected:
37  double maxTime;
39  bool solved;
40  int numThreads;
41  OsiSolverInterface *si;
42  CoinMessageHandler *cmh;
43  FILE *f;
44  char *filename;
45  double fobj;
46 
47  public:
48  // Constructor
49  OsiSolver();
50 
51  // Destructor
52  ~OsiSolver();
53 
54  // Set maximum time allowed to solve the problem
55  inline void setMaxTime(double time);
56 
57  // Get maximum time allowed to solve the problem
58  inline double getMaxTime();
59 
60  // Set the maximum number of threads that the solver can use to solve the problem
61  inline void setNumThreads(int numThreads);
62 
63  // Get the maximum number of threads that the solver can use to solve the problem
64  inline int getNumThreads();
65 
66  // Set if the execution must be stopped at first feasible solution
67  inline void setFirstFeasible(bool stopAtFirstFeasible);
68 
69  // Get if the execution must be stopped at first feasible solution
70  inline bool getFirstFeasible();
71 
72  // Set the name of the log file
73  void setLogFileName(char *filename);
74 
75  // Get the name of the log file
76  inline const char* getLogFileName();
77 
78  // Get solver interface
79  inline OsiSolverInterface* getSolverInterface();
80 
81  // Load a problem from a mps file
82  virtual void readMps(const char *filename);
83 
84  // Load a problem
85  void loadProblem(int numRows, int numColumns, int nz, double obj[], double qobj[], double lb[], double ub[],
86  double lhs[], double rhs[], int begRows[], int indCols[], double values[], bool copy_vectors=true);
87 
88  // Solve the problem loaded
89  virtual STATUS solve();
90 
91  // Get the objective function value
92  inline double getObjValue();
93 
94  // Get the solution
95  virtual const double* getSolution();
96 
97  // Get the number of variables
98  inline int getNumVars();
99 
100  // Get the name of variables
101  virtual const string* getVarNames();
102 
103  protected:
104  // Free all allocated memory
105  void freeMemory();
106 
107  // Load the quadratic objective function cost
108  virtual void loadQuadraticObj(double qobj[]);
109 };
110 
111 void OsiSolver::setMaxTime(double time)
113 {
114  this->maxTime = time;
115 }
116 
119 
122 {
123  return maxTime;
124 }
125 
126 void OsiSolver::setNumThreads(int numThreads)
128 {
129  if (numThreads > 0)
130  this->numThreads = numThreads;
131  else
132  this->numThreads = -1;
133 }
134 
137 
141 {
142  return numThreads;
143 }
144 
145 void OsiSolver::setFirstFeasible(bool stopAtFirstFeasible)
147 
151 {
152  this->stopAtFirstFeasible = stopAtFirstFeasible;
153 }
154 
157 
162 {
163  return stopAtFirstFeasible;
164 }
165 
168 {
169  return filename;
170 }
171 
172 OsiSolverInterface* OsiSolver::getSolverInterface()
174 
177 {
178  return si;
179 }
180 
183 {
184  return fobj;
185 }
186 
189 
192 {
193  if (!si)
194  return 0;
195  return si->getNumCols();
196 }
197 
198 #endif //OSI_SOLVER_H