Scippy

SoPlex

Sequential object-oriented simPlex

solbase.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 Roland Wunderling */
7 /* 1996-2019 Konrad-Zuse-Zentrum */
8 /* fuer Informationstechnik Berlin */
9 /* */
10 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
11 /* */
12 /* You should have received a copy of the ZIB Academic License */
13 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
14 /* */
15 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16 
17 /**@file solbase.h
18  * @brief Class for storing a primal-dual solution with basis information
19  */
20 #ifndef _SOLBASE_H_
21 #define _SOLBASE_H_
22 
23 /* undefine SOPLEX_DEBUG flag from including files; if SOPLEX_DEBUG should be defined in this file, do so below */
24 #ifdef SOPLEX_DEBUG
25 #define SOPLEX_DEBUG_SOLBASE
26 #undef SOPLEX_DEBUG
27 #endif
28 
29 #include <assert.h>
30 #include <string.h>
31 #include <math.h>
32 #include <iostream>
33 
34 #include "soplex/basevectors.h"
35 #include "soplex/spxsolver.h" // needed for basis information
36 
37 namespace soplex
38 {
39 /**@class SolBase
40  * @brief Class for storing a primal-dual solution with basis information
41  * @ingroup Algo
42  */
43 template< class R >
44 class SolBase
45 {
46  friend class SoPlex;
47  template < class S > friend class SolBase;
48 
49 public:
50  /// is the stored solution primal feasible?
51  bool isPrimalFeasible() const
52  {
53  return _isPrimalFeasible;
54  }
55 
56  /// gets the primal solution vector; returns true on success
57  bool getPrimal(VectorBase<R>& vector) const
58  {
59  vector = _primal;
60 
61  return _isPrimalFeasible;
62  }
63 
64  /// gets the vector of slack values; returns true on success
65  bool getSlacks(VectorBase<R>& vector) const
66  {
67  vector = _slacks;
68 
69  return _isPrimalFeasible;
70  }
71 
72  /// is a primal unbounded ray available?
73  bool hasPrimalRay() const
74  {
75  return _hasPrimalRay;
76  }
77 
78  /// gets the primal unbounded ray if available; returns true on success
79  bool getPrimalRay(VectorBase<R>& vector) const
80  {
81  if(_hasPrimalRay)
82  vector = _primalRay;
83 
84  return _hasPrimalRay;
85  }
86 
87  /// is a dual solution available?
88  bool isDualFeasible() const
89  {
90  return _isDualFeasible;
91  }
92 
93  /// gets the dual solution vector; returns true on success
94  bool getDual(VectorBase<R>& vector) const
95  {
96  vector = _dual;
97 
98  return _isDualFeasible;
99  }
100 
101  /// gets the vector of reduced cost values if available; returns true on success
102  bool getRedCost(VectorBase<R>& vector) const
103  {
104  vector = _redCost;
105 
106  return _isDualFeasible;
107  }
108 
109  /// is a dual farkas ray available?
110  bool hasDualFarkas() const
111  {
112  return _hasDualFarkas;
113  }
114 
115  /// gets the Farkas proof if available; returns true on success
116  bool getDualFarkas(VectorBase<R>& vector) const
117  {
118  if(_hasDualFarkas)
119  vector = _dualFarkas;
120 
121  return _hasDualFarkas;
122  }
123 
124  /// returns total size of primal solution
125  int totalSizePrimal(const int base = 2) const
126  {
127  int size = 0;
128 
130  size += totalSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
131 
132  if(_hasPrimalRay)
133  size += totalSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
134 
135  return size;
136  }
137 
138  /// returns total size of dual solution
139  int totalSizeDual(const int base = 2) const
140  {
141  int size = 0;
142 
143  if(_isDualFeasible)
144  size += totalSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
145 
146  if(_hasDualFarkas)
147  size += totalSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
148 
149  return size;
150  }
151 
152  /// returns size of least common multiple of denominators in primal solution
153  int dlcmSizePrimal(const int base = 2) const
154  {
155  int size = 0;
156 
158  size += dlcmSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
159 
160  if(_hasPrimalRay)
161  size += dlcmSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
162 
163  return size;
164  }
165 
166  /// returns size of least common multiple of denominators in dual solution
167  int dlcmSizeDual(const int base = 2) const
168  {
169  int size = 0;
170 
171  if(_isDualFeasible)
172  size += dlcmSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
173 
174  if(_hasDualFarkas)
175  size += dlcmSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
176 
177  return size;
178  }
179 
180  /// returns size of largest denominator in primal solution
181  int dmaxSizePrimal(const int base = 2) const
182  {
183  int size = 0;
184 
186  size += dmaxSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
187 
188  if(_hasPrimalRay)
189  size += dmaxSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
190 
191  return size;
192  }
193 
194  /// returns size of largest denominator in dual solution
195  int dmaxSizeDual(const int base = 2) const
196  {
197  int size = 0;
198 
199  if(_isDualFeasible)
200  size += dmaxSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
201 
202  if(_hasDualFarkas)
203  size += dmaxSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
204 
205  return size;
206  }
207 
208  /// invalidate solution
209  void invalidate()
210  {
211  _isPrimalFeasible = false;
212  _hasPrimalRay = false;
213  _isDualFeasible = false;
214  _hasDualFarkas = false;
215  }
216 
217 private:
224 
226 
227  unsigned int _isPrimalFeasible: 1;
228  unsigned int _hasPrimalRay: 1;
229  unsigned int _isDualFeasible: 1;
230  unsigned int _hasDualFarkas: 1;
231 
232  /// default constructor only for friends
234  : _objVal(0)
235  {
236  invalidate();
237  }
238 
239  /// assignment operator only for friends
241  {
242  if(this != &sol)
243  {
244 
246  _primal = sol._primal;
247  _slacks = sol._slacks;
248  _objVal = sol._objVal;
249 
251 
252  if(_hasPrimalRay)
253  _primalRay = sol._primalRay;
254 
256  _dual = sol._dual;
257  _redCost = sol._redCost;
258 
260 
261  if(_hasDualFarkas)
262  _dualFarkas = sol._dualFarkas;
263  }
264 
265  return *this;
266  }
267 
268  /// assignment operator only for friends
269  template < class S >
271  {
272  if((SolBase<S>*)this != &sol)
273  {
274 
276  _primal = sol._primal;
277  _slacks = sol._slacks;
278  _objVal = R(sol._objVal);
279 
281 
282  if(_hasPrimalRay)
283  _primalRay = sol._primalRay;
284 
286  _dual = sol._dual;
287  _redCost = sol._redCost;
288 
290 
291  if(_hasDualFarkas)
292  _dualFarkas = sol._dualFarkas;
293  }
294 
295  return *this;
296  }
297 };
298 } // namespace soplex
299 
300 /* reset the SOPLEX_DEBUG flag to its original value */
301 #undef SOPLEX_DEBUG
302 #ifdef SOPLEX_DEBUG_SOLBASE
303 #define SOPLEX_DEBUG
304 #undef SOPLEX_DEBUG_SOLBASE
305 #endif
306 
307 #endif // _SOLBASE_H_
DVectorBase< R > _slacks
Definition: solbase.h:219
bool getDual(VectorBase< R > &vector) const
gets the dual solution vector; returns true on success
Definition: solbase.h:94
int totalSizeDual(const int base=2) const
returns total size of dual solution
Definition: solbase.h:139
DVectorBase< R > _dualFarkas
Definition: solbase.h:223
Dynamic dense vectors.Class DVectorBase is a derived class of VectorBase adding automatic memory mana...
Definition: dvectorbase.h:48
Dense vector.Class VectorBase provides dense linear algebra vectors. It does not provide memory manag...
Definition: dsvectorbase.h:28
int totalSizePrimal(const int base=2) const
returns total size of primal solution
Definition: solbase.h:125
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.cpp:4150
bool getSlacks(VectorBase< R > &vector) const
gets the vector of slack values; returns true on success
Definition: solbase.h:65
DVectorBase< R > _primalRay
Definition: solbase.h:220
DVectorBase< R > _redCost
Definition: solbase.h:222
SolBase< R > & operator=(const SolBase< R > &sol)
assignment operator only for friends
Definition: solbase.h:240
bool hasDualFarkas() const
is a dual farkas ray available?
Definition: solbase.h:110
unsigned int _hasPrimalRay
Definition: solbase.h:228
bool getRedCost(VectorBase< R > &vector) const
gets the vector of reduced cost values if available; returns true on success
Definition: solbase.h:102
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.cpp:4133
int dlcmSizeDual(const int base=2) const
returns size of least common multiple of denominators in dual solution
Definition: solbase.h:167
bool getPrimal(VectorBase< R > &vector) const
gets the primal solution vector; returns true on success
Definition: solbase.h:57
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.cpp:4162
DVectorBase< R > _primal
Definition: solbase.h:218
unsigned int _hasDualFarkas
Definition: solbase.h:230
SolBase< R > & operator=(const SolBase< S > &sol)
assignment operator only for friends
Definition: solbase.h:270
main LP solver class
bool isPrimalFeasible() const
is the stored solution primal feasible?
Definition: solbase.h:51
Class for storing a primal-dual solution with basis information.
Definition: solbase.h:44
bool getPrimalRay(VectorBase< R > &vector) const
gets the primal unbounded ray if available; returns true on success
Definition: solbase.h:79
int dmaxSizeDual(const int base=2) const
returns size of largest denominator in dual solution
Definition: solbase.h:195
bool isDualFeasible() const
is a dual solution available?
Definition: solbase.h:88
int dlcmSizePrimal(const int base=2) const
returns size of least common multiple of denominators in primal solution
Definition: solbase.h:153
Collection of dense, sparse, and semi-sparse vectors.
Everything should be within this namespace.
Preconfigured SoPlex LP-solver.
Definition: soplex.h:89
int dmaxSizePrimal(const int base=2) const
returns size of largest denominator in primal solution
Definition: solbase.h:181
unsigned int _isPrimalFeasible
Definition: solbase.h:227
bool getDualFarkas(VectorBase< R > &vector) const
gets the Farkas proof if available; returns true on success
Definition: solbase.h:116
void invalidate()
invalidate solution
Definition: solbase.h:209
bool hasPrimalRay() const
is a primal unbounded ray available?
Definition: solbase.h:73
unsigned int _isDualFeasible
Definition: solbase.h:229
DVectorBase< R > _dual
Definition: solbase.h:221