BlockIPPlatform
 All Classes Files Functions Variables
BlockIPInterface.h
Go to the documentation of this file.
1 
6 #ifndef BLOCKIP_INTERFACE_H
7 #define BLOCKIP_INTERFACE_H
8 
9 #ifndef EXPDLL
10 #ifdef WIN32
11 #ifdef BLOCKIP_INTERFACE_EXPORTS
12 #define EXPDLL __declspec(dllexport)
13 #else
14 #define EXPDLL __declspec(dllimport)
15 #endif //BLOCKIP_INTERFACE_EXPORTS
16 #else
17 #define EXPDLL
18 #endif //WIN32
19 #endif //EXPDLL
20 
22 #include "BlockIP.h"
23 #include "SMLBlockIP.h"
24 #ifdef OSI_INTERFACE
25 #include "OsiInterface.h"
26 #endif // OSI_INTERFACE
27 #include "StdForm.h"
28 #include <vector>
29 
30 using namespace std;
31 
33 class EXPDLL BlockIPInterface {
34  public:
35  enum SOLVER{CPLEX, XPRESS, GLPK, CLP, BLOCKIP, BLOCKIP_MATLAB, NONE};
36  enum OPT_STATUS{OPTIMAL_SOLUTION, TIME_LIMIT_INFEAS, TIME_LIMIT_FEAS, FEASIBLE, INFEASIBLE, OTHERWISE, UNBOUNDED};
37  enum TYPE_INPUT {NO_INPUT, MPS, AMPL, BLOCKIP_FORMAT};
38 
39  private:
42 #ifdef OSI_INTERFACE
44 #endif // OSI_INTERFACE
45  SOLVER solver;
46  double maxTime;
47  int numThreads;
48  bool solved;
49  char *logFilename;
50  char *solFilename;
52  string *varNames;
53  int numVars;
54  int *varsOrder;
55  bool amplOrMps;
56  TYPE_INPUT type_input;
57 
60 
61  // only for BlockIP_Matlab
62  int numBlocks;
63  int numCons;
64  int numLinCons;
65  string *consNames;
66  string *blockNames;
67  int *varsPerBlock;
68  int *consPerBlock;
69  vector<int> *indexUbInf;
70 
71  double fobj;
72  double dobj;
73  double *x;
74  double *y;
75  double *z;
76  double *w;
77  double *rc;
78  int iter;
79  int iterPCG;
80  double CPUsec;
81 
82  public:
83  double infMatlab;
84  int iterMatlab;
86  const char *pathMatlab;
87 
88  public:
89  // Constructor
91 
92  // Destructor
94 
95  // Set the solver to be used
96  void setSolver(SOLVER solver);
97 
98  // Get the solver to be used
99  SOLVER getSolver();
100 
101  // Set maximum time allowed to solve the problem
102  void setMaxTime(double time);
103 
104  // Get maximum time allowed to solve the problem
105  double getMaxTime();
106 
107  // Set the maximum number of threads to be used
108  void setMaxThreads(int numThreads);
109 
110  // Get the maximum number of threads to be used
111  int getMaxThreads();
112 
113  // Set the name of the log file
114  void setLogFilename(char *filename);
115 
116  // Get the name of the log file
117  const char* getLogFilename();
118 
119  // Set the name of the solution file
120  void setSolFilename(char *filename);
121 
122  // Get the name of the solution file
123  const char* getSolFilename();
124 
125  // Get BlockIP interface
126  BlockIP* getBlockIPInterface();
127 
128 #ifdef OSI_INTERFACE
129  // Get solver interface
130  OsiInterface* getSolverInterface();
131 #endif // OSI_INTERFACE
132 
133  // Load a problem from a mps file
134  void readMps(const char *filename);
135 
136  // Load a problem from ampl files
137  void readAmpl(const char *modelFilename, const char *dataFilename);
138 
139  // Load a problem from BlockIP format file
140  void readBlockIPFormat(const char *filename);
141 
142  // Converts a problem in ampl format to mps format
143  void amplToMps(const char *modelFilename, const char *dataFilename, const char *mpsFilename, bool convertToStd=false);
144 
145  // Converts a problem in ampl format to BlockIP format
146  void amplToBlockIPFormat(const char *modelFilename, const char *dataFilename, const char *BlockIPFormatFilename, bool convertToStd=false);
147 
148  // Converts a problem in mps format to BlockIP format
149  void mpsToBlockIPFormat(const char *mpsFilename, const char *BlockIPFormatFilename, bool convertToStd=false);
150 
151  // Converts a problem in BlockIP format to mps format
152  void BlockIPFormatToMps(const char *BlockIPFormatFilename, const char *mpsFilename, bool convertToStd=false);
153 
154  // Solve the problem loaded
155  OPT_STATUS solve();
156 
157  // Get the objective function value
158  double getObjValue();
159 
160  // Get the solution
161  const double* getSolution();
162 
163  // Get the number of variables
164  int getNumVars();
165 
166  // Only for BlockIP
167  // Get dual objective function
168  double getDualValue();
169  // Get the number of IP iterations performed
170  int getIter();
171  // Get the overall number of PCG iterations performed
172  int getPCGIter();
173  // Get the CPU seconds of the optimization process
174  double getCPUsec();
175 
176  // Display the primal variables value of blocks and slacks
177  void printX();
178  void printX(int globalVarIndex);
179  void printX(string varName);
180  void printX(int blockIndex, int varIndex);
181  void printX(string blockName, int varIndex);
182  void printXBlock(int blockIndex);
183  void printXBlock(string blockName);
184  void printXSlack();
185  void printXSlack(int slackIndex);
186 
187  // Display the reduced cost of primal variables of blocks and slacks
188  void printRC();
189  void printRC(int globalVarIndex);
190  void printRC(string varName);
191  void printRC(int blockIndex, int varIndex);
192  void printRC(string blockName, int varIndex);
193  void printRCBlock(int blockIndex);
194  void printRCBlock(string blockName);
195  void printRCSlack();
196  void printRCSlack(int slackIndex);
197 
198  // Display the dual variables value of blocks and linking constraints
199  void printCons();
200  void printCons(int globalConsIndex);
201  void printCons(string consName);
202  void printCons(int blockIndex, int consIndex);
203  void printCons(string blockName, int consIndex);
204  void printConsBlock(int blockIndex);
205  void printConsBlock(string blockName);
206  void printConsLinking();
207  void printConsLinking(int linkingIndex);
208 
209 
210  protected:
211  // Free all allocated memory
212  void freeMemory();
213 
214  // Create the files to load and solve a problem in BlockIP Matlab
215  void createMatlabProblem();
216 };
217 
218 inline void BlockIPInterface::setSolver(SOLVER solver)
220 
223 {
224 #ifndef OSI_INTERFACE
225  if (solver == CPLEX || solver == XPRESS || solver == GLPK || solver == CLP)
226  throw ExceptionBlockIPInterface(ExceptionBlockIPInterface::OSI_INTERFACE_NOT_AVAILABLE);
227 #endif
228  this->solver = solver;
229 }
230 
231 inline BlockIPInterface::SOLVER BlockIPInterface::getSolver()
233 {
234  return solver;
235 }
236 
237 inline void BlockIPInterface::setMaxTime(double time)
239 {
240  this->maxTime = time;
241 }
242 
245 
248 {
249  return maxTime;
250 }
251 
252 inline void BlockIPInterface::setMaxThreads(int numThreads)
254 {
255  this->numThreads = numThreads;
256 }
257 
260 {
261  return numThreads;
262 }
263 
266 
269 {
270  return logFilename;
271 }
272 
275 
278 {
279  return solFilename;
280 }
281 
284 
287 {
288  return bip;
289 }
290 
291 #ifdef OSI_INTERFACE
294 
297 {
298  return oi;
299 }
300 #endif // OSI_INTERFACE
301 
304 {
305  return fobj;
306 }
307 
308 inline double BlockIPInterface::getDualValue()
309 // Get dual objective function
310 {
311  return dobj;
312 }
313 
314 inline int BlockIPInterface::getIter()
315 // Get the number of IP iterations performed
316 {
317  return iter;
318 }
319 
320 inline int BlockIPInterface::getPCGIter()
321 // Get the overall number of PCG iterations performed
322 {
323  return iterPCG;
324 }
325 
326 inline double BlockIPInterface::getCPUsec()
327 // Get the CPU seconds of the optimization process
328 {
329  return CPUsec;
330 }
331 
332 #endif //BLOCKIP_INTERFACE_H