Scippy

SoPlex

Sequential object-oriented simPlex

soplexlegacy.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2015 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file soplexlegacy.h
17  * @brief preconfigured \ref soplex::SoPlexLegacy "SoPlexLegacy" LP-solver.
18  */
19 #ifndef _SOPLEXLEGACY_H_
20 #define _SOPLEXLEGACY_H_
21 
22 #include <assert.h>
23 #include <string.h>
24 
25 #include "spxsolver.h"
26 #include "spxscaler.h"
27 #include "spxsimplifier.h"
28 #include "spxsteeppr.h"
29 #include "spxboundflippingrt.h"
30 #include "spxfileio.h"
31 #include "spxweightst.h"
32 #include "slufactor.h"
33 
34 namespace soplex
35 {
36 
37 /**@class SoPlexLegacy
38  @brief Preconfigured SoPlexLegacy LP-solver.
39  @ingroup Algo
40 */
41 class SoPlexLegacy : public SPxLP
42 {
43 protected:
44 
45  //-------------------------
46  //**@name Protected data */
47  //@{
48  // SPxWeightST st; ///< weight starter
49  SLUFactor m_slu; ///< LU Factorisation
50  SPxSolver m_solver; ///< solver
51  SPxScaler* m_preScaler; ///< pre-scaler
52  SPxScaler* m_postScaler; ///< post-scaler
53  SPxSimplifier* m_simplifier; ///< simplifier
54  bool m_vanished; ///< did the presolver solve the problem ?
55  bool m_freePreScaler; ///< true iff m_preScaler should be freed inside of this object
56  bool m_freePostScaler; ///< true iff m_postScaler should be freed inside of this object
57  bool m_freeSimplifier; ///< true iff m_simplifier should be freed inside of this object
60  //@}
61 
62 public:
63 
64  //---------------------------------------
65  //**@name Construction / destruction */
66  //@{
67  /// default construtor.
68  explicit SoPlexLegacy(
69  SPxOut& outstream,
72  virtual ~SoPlexLegacy();
73  /// assignment operator.
75  /// copy constructor.
76  SoPlexLegacy(const SoPlexLegacy&);
77  //@}
78 
79  /// message handler
80 // mutable SPxOut spxout;
81 
82  //---------------------------------------
83  //**@name Access / modification */
84  //@{
85 
86  /// set verbosity
87 // virtual void setVerbosity(const SPxOut::Verbosity v)
88 // {
89 // spxout.setVerbosity(v);
90 // setOutstream(spxout);
91 // }
92 
93  /// set update type for factorization.
95  {
96  m_slu.setUtype(tp);
97  }
98  /// return current Pricing.
99  inline SPxSolver::Pricing pricing() const
100  {
101  return m_solver.pricing();
102  }
103  /// set FULL or PARTIAL pricing.
105  {
106  m_solver.setPricing(pr);
107  }
108  /// return current Type.
109  inline SPxSolver::Type type() const
110  {
111  return m_solver.type();
112  }
113  /// return current basis representation.
115  {
116  return m_solver.rep();
117  }
118  /// set LEAVE or ENTER algorithm.
119  virtual void setType(SPxSolver::Type tp)
120  {
121  m_solver.setType(tp);
122  }
123  /// set ROW or COLUMN representation.
124  virtual void setRep (SPxSolver::Representation p_rep)
125  {
126  m_solver.setRep(p_rep);
127  }
128  /// setup prescaler to use. If \p destroy is true, \p scaler will be freed in destructor.
129  virtual void setPreScaler(SPxScaler* scaler, const bool destroy = false);
130  /// setup postscaler to use. If \p destroy is true, \p scaler will be freed in destructor.
131  virtual void setPostScaler(SPxScaler* scaler, const bool destroy = false);
132  /// setup simplifier to use. If \p destroy is true, \p simpli will be freed in destructor.
133  virtual void setSimplifier(SPxSimplifier* simpli, const bool destroy = false);
134  /// has a simplifier been set?
135  inline bool has_simplifier() const
136  {
137  return m_simplifier != 0;
138  }
139  /// has a prescaler been set?
140  inline bool has_prescaler() const
141  {
142  return m_preScaler != 0;
143  }
144  /// has a postscaler been set?
145  inline bool has_postscaler() const
146  {
147  return m_postScaler != 0;
148  }
149  /// setup pricer to use.
150  virtual void setPricer(SPxPricer* pricer, const bool destroy = false)
151  {
152  m_solver.setPricer(pricer, destroy);
153  }
154  /// setup ratio-tester to use.
155  virtual void setTester(SPxRatioTester* tester, const bool destroy = false)
156  {
157  m_solver.setTester(tester, destroy);
158  }
159  /// setup starting basis generator to use.
160  virtual void setStarter(SPxStarter* starter, const bool destroy = false)
161  {
162  m_solver.setStarter(starter, destroy);
163  }
164  /// @throw SPxStatusException if simplifier loaded, this is not yet implemented
165  /// set starting basis
167  {
168  if (has_simplifier())
169  {
170  MSG_ERROR( std::cerr << "ESOLVR04 setting starting basis with presolving not yet implemented" << std::endl; )
171  throw SPxStatusException("XSOLVR04 setting starting basis with presolving not yet implemented");
172  }
173 
174  m_colsbasisstatus.reSize(nCols());
175  for (int i = 0; i < nCols(); i++)
176  m_colsbasisstatus[i] = cols[i];
177 
178  m_rowsbasisstatus.reSize(nRows());
179  for (int i = 0; i < nRows(); i++)
180  m_rowsbasisstatus[i] = rows[i];
181  }
182  /// clear starting basis
183  virtual void clearBasis()
184  {
185  m_colsbasisstatus.clear();
186  m_rowsbasisstatus.clear();
187  m_solver.reLoad();
188  }
189  /// set time limit.
190  virtual void setTerminationTime(Real time = infinity)
191  {
193  }
194  /// return time limit.
195  inline Real terminationTime() const
196  {
197  return m_solver.terminationTime();
198  }
199  /// set iteration limit.
200  virtual void setTerminationIter(int iter = -1)
201  {
203  }
204  /// return iteration limit.
205  inline int terminationIter() const
206  {
207  return m_solver.terminationIter();
208  }
209  /// set objective limit.
210  virtual void setTerminationValue(Real val = infinity)
211  {
213  }
214  /// return objective limit.
215  inline Real terminationValue() const
216  {
217  return m_solver.terminationValue();
218  }
219  /// allowed primal feasibility tolerance.
220  virtual Real feastol() const
221  {
222  return m_solver.feastol();
223  }
224  /// allowed optimality, i.e., dual feasibility tolerance.
225  virtual Real opttol() const
226  {
227  return m_solver.opttol();
228  }
229  /// guaranteed primal and dual bound violation for optimal solution, returning the maximum of feastol() and opttol(), i.e., the less tight tolerance.
230  virtual Real delta() const
231  {
232  return m_solver.delta();
233  }
234  /// set parameter \p feastol.
235  virtual void setFeastol(Real d)
236  {
237  m_solver.setFeastol(d);
238  }
239  /// set parameter \p opttol.
240  virtual void setOpttol(Real d)
241  {
242  m_solver.setOpttol(d);
243  }
244  /// set parameter \p delta, i.e., set \p feastol and \p opttol to same value.
245  virtual void setDelta(Real d)
246  {
247  m_solver.setDelta(d);
248  }
249  //@}
250 
251  //---------------------------------------
252  //**@name Solving and solution query */
253  //@{
254  /// @throw SPxStatusException if no problem loaded
255  virtual SPxSolver::Status solve();
256  ///
257  virtual Real objValue() const;
258  ///
259  virtual SPxSolver::Status getPrimal(Vector& vector) const;
260  ///
261  virtual SPxSolver::Status getSlacks(Vector& vector) const;
262  ///
263  virtual SPxSolver::Status getDual(Vector& vector) const;
264  ///
265  virtual SPxSolver::Status getRedCost(Vector& vector) const;
266 
267  /// gets basis status for a single row.
269 
270  /// gets basis status for a single column.
272 
273  /// get current basis, and return solver status.
275 
276  const char* getColName(
277  int idx,
278  const NameSet* cnames,
279  char* buf)
280  {
281  assert(buf != 0);
282  assert(idx >= 0);
283  assert(idx < nCols());
284 
285  if (cnames != 0)
286  {
287  DataKey key = cId(idx);
288 
289  if (cnames->has(key))
290  return (*cnames)[key];
291  }
292  std::sprintf(buf, "x%d", idx);
293 
294  return buf;
295  }
296 
297  const char* getRowName(
298  int idx,
299  const NameSet* rnames,
300  char* buf)
301  {
302  assert(buf != 0);
303  assert(idx >= 0);
304  assert(idx < nRows());
305 
306  if (rnames != 0)
307  {
308  DataKey key = rId(idx);
309 
310  if (rnames->has(key))
311  return (*rnames)[key];
312  }
313  std::sprintf(buf, "C%d", idx);
314 
315  return buf;
316  }
317 
318  /// @throw SPxStatusException if simplifier loaded, this is not yet
319  /// implemented
320  virtual SPxSolver::Status getPrimalray(Vector& vector) const;
321 
322  /// @throw SPxStatusException if simplifier loaded, this is not yet
323  /// implemented
324  virtual SPxSolver::Status getDualfarkas(Vector& vector) const;
325 
326  /// get violation of constraints.
327  virtual void qualConstraintViolation(Real& maxviol, Real& sumviol) const;
328  /// get violations of bounds.
329  virtual void qualBoundViolation(Real& maxviol, Real& sumviol) const;
330 #if 0
331  /// get the residuum |Ax-b|.
332  virtual void qualSlackViolation(Real& maxviol, Real& sumviol) const;
333  /// get violation of optimality criterion.
334  virtual void qualRedCostViolation(Real& maxviol, Real& sumviol) const;
335 #endif
336  /// time spent in factorizations
337  virtual Real getFactorTime() const
338  {
339  return m_vanished ? REAL(0.0) : m_slu.getFactorTime();
340  }
341  /// number of factorizations performed
342  virtual int getFactorCount() const
343  {
344  return m_vanished ? 0 : m_slu.getFactorCount();
345  }
346  /// time spent in solves
347  virtual Real getSolveTime() const
348  {
349  return m_vanished ? REAL(0.0) : m_slu.getSolveTime();
350  }
351  /// number of solves performed
352  virtual int getSolveCount() const
353  {
354  return m_vanished ? 0 : m_slu.getSolveCount();
355  }
356  ///
357  virtual int iteration() const
358  {
359  return m_vanished ? 0 : m_solver.basis().iteration();
360  }
361  ///
362  virtual bool terminate()
363  {
364  return m_solver.terminate();
365  }
366  /// returns the current status
367  virtual SPxSolver::Status status() const
368  {
369  if (m_vanished)
370  return SPxSolver::OPTIMAL;
371 
372  return m_solver.status();
373  }
374  //@}
375 
376  //---------------------------------------
377  //**@name I/O */
378  //@{
379 
380  /** Load basis from \p filename in MPS format. If \p rowNames and \p
381  * colNames are \c NULL, default names are used for the constraints and
382  * variables.
383  */
384  virtual bool readBasisFile(const char* filename,
385  const NameSet* rowNames, const NameSet* colNames);
386 
387  /** Write basis to \p filename in MPS format. If \p rowNames and \p
388  * colNames are \c NULL, default names are used for the constraints and
389  * variables.
390  */
391  virtual bool writeBasisFile(const char* filename,
392  const NameSet* rowNames, const NameSet* colNames);
393 
394  /** Write LP, basis and parameter settings of the current SPxSolver object
395  * (i.e. after simplifying and scaling).
396  * LP is written in MPS format to "\p filename".mps, basis is written in
397  * "\p filename".bas, and parameters are written to "\p filename".set.
398  * If \p rowNames and \p colNames are \c NULL, default names are used for
399  * the constraints and variables.
400  */
401  virtual bool writeState(const char* filename,
402  const NameSet* rowNames = NULL, const NameSet* colNames = NULL) const;
403 
404  /// returns statistical information in form of a string.
405  std::string statistics() const
406  {
407  return m_solver.statistics();
408  }
409  //@}
410 
411 private:
412 
413  //------------------------------------
414  //**@name Private helpers */
415  //@{
416  /// undoes preprocessing such that the unsimplified solution values and basis is available
417  void unsimplify() const;
418  //@}
419 };
420 } // namespace soplex
421 #endif // _SOPLEX_H_