OSRunSolver.cpp
Go to the documentation of this file.
1 /* $Id: OSRunSolver.cpp 4263 2011-09-09 20:19:26Z Martin $ */
15 #include "OSRunSolver.h"
16 #include "OSCoinSolver.h"
17 #include "OSResult.h"
18 #include "OSrLWriter.h"
19 #include "OSInstance.h"
20 #include "OSOption.h"
21 #include "OSConfig.h"
22 #include "OSDefaultSolver.h"
23 #include "OSErrorClass.h"
24 
25 #ifdef COIN_HAS_KNITRO
26 #include "OSKnitroSolver.h"
27 #endif
28 
29 #ifdef COIN_HAS_LINDO
30 #include "OSLindoSolver.h"
31 #endif
32 
33 #ifdef COIN_HAS_IPOPT
34 # ifndef COIN_HAS_ASL
35 # include "OSIpoptSolver.h"
36 # undef COIN_HAS_ASL
37 # else
38 # include "OSIpoptSolver.h"
39 # endif
40 #endif
41 
42 #ifdef COIN_HAS_BONMIN
43 #include "OSBonminSolver.h"
44 #endif
45 
46 #ifdef COIN_HAS_COUENNE
47 #include "OSCouenneSolver.h"
48 #endif
49 
50 #ifdef COIN_HAS_CSDP
51 #include "OSCsdpSolver.h"
52 #endif
53 
54 #include <stdio.h>
55 #include <map>
56 
57 
58 using std::endl;
59 using std::ostringstream;
60 using std::string;
61 using std::map;
62 
63 /* Four implementations of this method, with different combinations of inputs */
64 
65 std::string runSolver(std::string solverName, std::string osol,
67 {
68  DefaultSolver *solverType = NULL;
69  try
70  {
71  solverType = selectSolver(solverName, osinstance);
72  if (solverType == NULL)
73  throw ErrorClass("No appropriate solver found");
74 
75  solverType->osinstance = osinstance;
76  solverType->osol = osol;
77  solverType->buildSolverInstance();
78  solverType->setSolverOptions();
79  solverType->solve();
80  std::string resultString = solverType->osrl;
81 
82  if (solverType != NULL)
83  delete solverType;
84  solverType = NULL;
85  return resultString;
86  }
87  catch (const ErrorClass& eclass)
88  {
89  if (solverType != NULL)
90  delete solverType;
91  solverType = NULL;
92  throw eclass;
93  return "";
94  }
95 } //runSolver (osinstance, osol)
96 
97 
98 std::string runSolver(std::string solverName, OSOption* osoption,
100 {
101  DefaultSolver *solverType = NULL;
102  try
103  {
104  solverType = selectSolver(solverName, osinstance);
105  if (solverType == NULL)
106  throw ErrorClass("No appropriate solver found");
107 
108  solverType->osinstance = osinstance;
109  solverType->osoption = osoption;
110  solverType->buildSolverInstance();
111  solverType->setSolverOptions();
112  solverType->solve();
113  std::string resultString = solverType->osrl;
114  if (solverType != NULL)
115  delete solverType;
116  solverType = NULL;
117  return resultString;
118  }
119  catch (const ErrorClass& eclass)
120  {
121  if (solverType != NULL)
122  delete solverType;
123  solverType = NULL;
124  throw eclass;
125  return "";
126  }
127 } //runSolver (osinstance, osoption)
128 
129 
130 std::string runSolver(std::string solverName, std::string osol,
131  std::string osil)
132 {
133  OSiLReader* osilreader = new OSiLReader();
135  DefaultSolver *solverType = NULL;
136 
137  try
138  {
139  osinstance = osilreader->readOSiL(osil);
140 
141  solverType = selectSolver(solverName, osinstance);
142  if (solverType == NULL)
143  throw ErrorClass("No appropriate solver found");
144 
145  solverType->osinstance = osinstance;
146  solverType->osol = osol;
147  solverType->buildSolverInstance();
148  solverType->setSolverOptions();
149  solverType->solve();
150  std::string resultString = solverType->osrl;
151  if (solverType != NULL)
152  delete solverType;
153  solverType = NULL;
154  delete osilreader;
155  osilreader = NULL;
156  return resultString;
157  }
158  catch (const ErrorClass& eclass)
159  {
160  if (solverType != NULL)
161  delete solverType;
162  solverType = NULL;
163  delete osilreader;
164  osilreader = NULL;
165  throw eclass;
166  return "";
167  }
168 } //runSolver (osil, osol)
169 
170 
171 std::string runSolver(std::string solverName, OSOption* osoption,
172  std::string osil)
173 {
174  OSiLReader* osilreader = new OSiLReader();
176  DefaultSolver *solverType = NULL;
177 
178  try
179  {
180  osinstance = osilreader->readOSiL(osil);
181 
182  solverType = selectSolver(solverName, osinstance);
183  if (solverType == NULL)
184  throw ErrorClass("No appropriate solver found");
185 
186  solverType->osinstance = osinstance;
187  solverType->osoption = osoption;
188  solverType->buildSolverInstance();
189  solverType->setSolverOptions();
190  solverType->solve();
191  std::string resultString = solverType->osrl;
192  if (solverType != NULL)
193  delete solverType;
194  solverType = NULL;
195  delete osilreader;
196  osilreader = NULL;
197  return resultString;
198  }
199  catch (const ErrorClass& eclass)
200  {
201  if (solverType != NULL)
202  delete solverType;
203  solverType = NULL;
204  delete osilreader;
205  osilreader = NULL;
206  throw eclass;
207  return "";
208  }
209 } //runSolver (osil, osoption)
210 
211 
213 {
214  DefaultSolver *solverType = NULL;
215  try
216  {
217  if (solverName == "") // must determine the default solver
218  {
219  if (osinstance == NULL)
220  throw ErrorClass(
221  "there was a NULL instance sent to buildSolver");
222 
223  // make sure there are no special items here, matrixProgramming, stochastic, etc.
224  // for which there is no default solver
225  // HIG: To do...
226 
227  // see if we have an integer program
228  if (osinstance->getNumberOfIntegerVariables()
229  + osinstance->getNumberOfBinaryVariables() > 0) //we have an integer program
230  {
231  if ((osinstance->getNumberOfNonlinearExpressions() > 0)
232  || (osinstance->getNumberOfQuadraticTerms() > 0)) // we are nonlinear and integer
233  {
234  solverName = "bonmin";
235  }
236  else //we are linear integer
237  {
238  solverName = "cbc";
239  }
240  }
241  else // we have a continuous problem
242  {
243  if ((osinstance->getNumberOfNonlinearExpressions() > 0)
244  || (osinstance->getNumberOfQuadraticTerms() > 0)) // we are nonlinear and continuous
245  {
246  solverName = "ipopt";
247  }
248  else //we have linear program
249  {
250  solverName = "clp";
251  }
252  }
253  }//end of if solverName == ""
254 
255  //now build the solver through its constructor
256 
257  if (solverName.find("ipopt") != std::string::npos)
258  {
259  // we are requesting the Ipopt solver
260 #ifdef COIN_HAS_IPOPT
261  solverType = new IpoptSolver();
262  solverType->sSolverName = "ipopt";
263 #else
264  throw ErrorClass("the Ipopt solver requested is not present");
265 #endif
266  }
267 
268  else if (solverName.find("lindo") != std::string::npos)
269  {
270  // we are requesting the Lindo solver
271 #ifdef COIN_HAS_LINDO
272  solverType = new LindoSolver();
273  solverType->sSolverName = "lindo";
274 #else
275  throw ErrorClass( "the Lindo solver requested is not present");
276 #endif
277  }
278 
279  else if (solverName.find("clp") != std::string::npos)
280  {
281  solverType = new CoinSolver();
282  solverType->sSolverName = "clp";
283  }
284 
285  else if (solverName.find("cplex") != std::string::npos)
286  {
287 #ifdef COIN_HAS_CPX
288  solverType = new CoinSolver();
289  solverType->sSolverName = "cplex";
290 #else
291  throw ErrorClass( "the Cplex solver requested is not present");
292 #endif
293  }
294 
295  else if (solverName.find("glpk") != std::string::npos)
296  {
297 #ifdef COIN_HAS_GLPK
298  solverType = new CoinSolver();
299  solverType->sSolverName = "glpk";
300 #else
301  throw ErrorClass( "the GLPK solver requested is not present");
302 #endif
303  }
304 
305  else if (solverName.find("dylp") != std::string::npos)
306  {
307 #ifdef COIN_HAS_DYLP
308  solverType = new CoinSolver();
309  solverType->sSolverName = "dylp";
310 #else
311  throw ErrorClass( "the DyLP solver requested is not present");
312 #endif
313  }
314 
315  else if (solverName.find("symphony") != std::string::npos)
316  {
317 #ifdef COIN_HAS_SYMPHONY
318  solverType = new CoinSolver();
319  solverType->sSolverName = "symphony";
320 #else
321  throw ErrorClass( "the SYMPHONY solver requested is not present");
322 #endif
323  }
324 
325  else if (solverName.find("knitro") != std::string::npos)
326  {
327 #ifdef COIN_HAS_KNITRO
328  solverType = new KnitroSolver();
329  solverType->sSolverName = "knitro";
330 #else
331  throw ErrorClass( "the Knitro solver requested is not present");
332 #endif
333  }
334 
335  else if (solverName.find("vol") != std::string::npos)
336  {
337 #ifdef COIN_HAS_VOL
338  solverType = new CoinSolver();
339  solverType->sSolverName = "vol";
340 #else
341  throw ErrorClass( "the Vol solver requested is not present");
342 #endif
343  }
344 
345  else if (solverName.find("bonmin") != std::string::npos)
346  {
347  // we are requesting the Bonmin solver
348 #ifdef COIN_HAS_BONMIN
349  solverType = new BonminSolver();
350  solverType->sSolverName = "bonmin";
351 #else
352  throw ErrorClass( "the Bonmin solver requested is not present");
353 #endif
354  }
355 
356  else if (solverName.find("couenne") != std::string::npos)
357  {
358  // we are requesting the Couenne solver
359 #ifdef COIN_HAS_COUENNE
360  solverType = new CouenneSolver();
361  solverType->sSolverName = "couenne";
362 #else
363  throw ErrorClass( "the Couenne solver requested is not present");
364 #endif
365  }
366 
367  else if (solverName.find("cbc") != std::string::npos)
368  {
369  solverType = new CoinSolver();
370  solverType->sSolverName = "cbc";
371  }
372 
373  else if (solverName.find("gurobi") != std::string::npos)
374  {
375 #ifdef COIN_HAS_GRB
376  solverType = new CoinSolver();
377  solverType->sSolverName = "gurobi";
378 #else
379  throw ErrorClass( "the Gurobi solver requested is not present");
380 #endif
381  }
382 
383  else if (solverName.find("mosek") != std::string::npos)
384  {
385 #ifdef COIN_HAS_MSK
386  solverType = new CoinSolver();
387  solverType->sSolverName = "mosek";
388 #else
389  throw ErrorClass( "the Mosek solver requested is not present");
390 #endif
391  }
392 
393  else if (solverName.find("soplex") != std::string::npos)
394  {
395 #ifdef COIN_HAS_SOPLEX
396  solverType = new CoinSolver();
397  solverType->sSolverName = "soplex";
398 #else
399  throw ErrorClass( "the Soplex solver requested is not present");
400 #endif
401  }
402 
403  else if (solverName.find("xpress") != std::string::npos)
404  {
405 #ifdef COIN_HAS_XPR
406  solverType = new CoinSolver();
407  solverType->sSolverName = "xpress";
408 #else
409  throw ErrorClass( "the Xpress solver requested is not present");
410 #endif
411  }
412 
413  else if (solverName.find("csdp") != std::string::npos)
414  {
415 #ifdef COIN_HAS_CSDP
416  solverType = new CsdpSolver();
417  solverType->sSolverName = "csdp";
418 // throw ErrorClass( "the CSDP solver requested is not yet supported");
419 #else
420  throw ErrorClass( "the CSDP solver requested is not present");
421 #endif
422  }
423 
424  else
425  {
426  std::string errorMessage;
427  errorMessage = "solver " + solverName + " is not supported";
428  throw ErrorClass( errorMessage );
429  }
430 
431  return solverType;
432  }
433 
434  catch (const ErrorClass& eclass)
435  {
436  if (solverType != NULL)
437  delete solverType;
438  solverType = NULL;
439  throw eclass;
440  return NULL;
441  }
442 
443 }//selectSolver
444 
445 
int getNumberOfIntegerVariables()
getNumberOfIntegerVariables
the LindoSolver class solves problems using Lindo.
Definition: OSLindoSolver.h:49
The CsdpSolver class solves problems using Csdp.
Definition: OSCsdpSolver.h:72
The CouenneSolver class solves problems using Ipopt.
std::string osrl
osrl holds the solution or result of the model
OSOption * osoption
the KnitroSolver class solves problems using Knitro.
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
The IpoptSolver class solves problems using Ipopt.
int getNumberOfNonlinearExpressions()
Get number of nonlinear expressions.
The Option Class.
Definition: OSOption.h:3564
std::string osol
osol holds the options for the solver
virtual void solve()=0
solve is a virtual function – the actual solvers will implement their own solve method ...
Used to read an OSiL string.
Definition: OSiLReader.h:37
DefaultSolver * selectSolver(std::string solverName, OSInstance *osinstance)
A method to select the solver.
OSOption * osoption
osoption holds the solver options in-memory as an OSOption object
The Default Solver Class.
std::string runSolver(std::string solverName, std::string osol, OSInstance *osinstance)
This class is used to invoke a solver locally.
Definition: OSRunSolver.cpp:65
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
virtual void setSolverOptions()=0
setSolverOptions is a virtual function – the actual solvers will implement their own setSolverOptions...
int getNumberOfQuadraticTerms()
Get the number of specified (usually nonzero) qTerms in the quadratic coefficients.
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
The BonminSolver class solves problems using Ipopt.
virtual void buildSolverInstance()=0
buildSolverInstance is a virtual function – the actual solvers will implement their own buildSolverIn...
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
used for throwing exceptions.
Definition: OSErrorClass.h:31
int getNumberOfBinaryVariables()
getNumberOfBinaryVariables