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-2022 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  template <class T> friend class SoPlexBase;
47  // Why do we need the following? This is at least used in the operator=
48  // When Rational solution needs to be copied into Real, the private member
49  // _objVal is accessed.
50  template <class S> friend class SolBase;
51 
52 public:
53  /// is the stored solution primal feasible?
54  bool isPrimalFeasible() const
55  {
56  return _isPrimalFeasible;
57  }
58 
59  /// gets the primal solution vector; returns true on success
60  bool getPrimalSol(VectorBase<R>& vector) const
61  {
62  vector = _primal;
63 
64  return _isPrimalFeasible;
65  }
66 
67  /// gets the vector of slack values; returns true on success
68  bool getSlacks(VectorBase<R>& vector) const
69  {
70  vector = _slacks;
71 
72  return _isPrimalFeasible;
73  }
74 
75  /// is a primal unbounded ray available?
76  bool hasPrimalRay() const
77  {
78  return _hasPrimalRay;
79  }
80 
81  /// gets the primal unbounded ray if available; returns true on success
82  bool getPrimalRaySol(VectorBase<R>& vector) const
83  {
84  if(_hasPrimalRay)
85  vector = _primalRay;
86 
87  return _hasPrimalRay;
88  }
89 
90  /// is a dual solution available?
91  bool isDualFeasible() const
92  {
93  return _isDualFeasible;
94  }
95 
96  /// gets the dual solution vector; returns true on success
97  bool getDualSol(VectorBase<R>& vector) const
98  {
99  vector = _dual;
100 
101  return _isDualFeasible;
102  }
103 
104  /// gets the vector of reduced cost values if available; returns true on success
105  bool getRedCostSol(VectorBase<R>& vector) const
106  {
107  vector = _redCost;
108 
109  return _isDualFeasible;
110  }
111 
112  /// is a dual farkas ray available?
113  bool hasDualFarkas() const
114  {
115  return _hasDualFarkas;
116  }
117 
118  /// gets the Farkas proof if available; returns true on success
119  bool getDualFarkasSol(VectorBase<R>& vector) const
120  {
121  if(_hasDualFarkas)
122  vector = _dualFarkas;
123 
124  return _hasDualFarkas;
125  }
126 
127  /// returns total size of primal solution
128  int totalSizePrimal(const int base = 2) const
129  {
130  int size = 0;
131 
133  size += totalSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
134 
135  if(_hasPrimalRay)
136  size += totalSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
137 
138  return size;
139  }
140 
141  /// returns total size of dual solution
142  int totalSizeDual(const int base = 2) const
143  {
144  int size = 0;
145 
146  if(_isDualFeasible)
147  size += totalSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
148 
149  if(_hasDualFarkas)
150  size += totalSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
151 
152  return size;
153  }
154 
155  /// returns size of least common multiple of denominators in primal solution
156  int dlcmSizePrimal(const int base = 2) const
157  {
158  int size = 0;
159 
161  size += dlcmSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
162 
163  if(_hasPrimalRay)
164  size += dlcmSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
165 
166  return size;
167  }
168 
169  /// returns size of least common multiple of denominators in dual solution
170  int dlcmSizeDual(const int base = 2) const
171  {
172  int size = 0;
173 
174  if(_isDualFeasible)
175  size += dlcmSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
176 
177  if(_hasDualFarkas)
178  size += dlcmSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
179 
180  return size;
181  }
182 
183  /// returns size of largest denominator in primal solution
184  int dmaxSizePrimal(const int base = 2) const
185  {
186  int size = 0;
187 
189  size += dmaxSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
190 
191  if(_hasPrimalRay)
192  size += dmaxSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
193 
194  return size;
195  }
196 
197  /// returns size of largest denominator in dual solution
198  int dmaxSizeDual(const int base = 2) const
199  {
200  int size = 0;
201 
202  if(_isDualFeasible)
203  size += dmaxSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
204 
205  if(_hasDualFarkas)
206  size += dmaxSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
207 
208  return size;
209  }
210 
211  /// invalidate solution
212  void invalidate()
213  {
214  _isPrimalFeasible = false;
215  _hasPrimalRay = false;
216  _isDualFeasible = false;
217  _hasDualFarkas = false;
218  }
219 
220 private:
227 
229 
230  unsigned int _isPrimalFeasible: 1;
231  unsigned int _hasPrimalRay: 1;
232  unsigned int _isDualFeasible: 1;
233  unsigned int _hasDualFarkas: 1;
234 
235  /// default constructor only for friends
237  : _objVal(0)
238  {
239  invalidate();
240  }
241 
242  /// assignment operator only for friends
244  {
245  if(this != &sol)
246  {
247 
249  _primal = sol._primal;
250  _slacks = sol._slacks;
251  _objVal = sol._objVal;
252 
254 
255  if(_hasPrimalRay)
256  _primalRay = sol._primalRay;
257 
259  _dual = sol._dual;
260  _redCost = sol._redCost;
261 
263 
264  if(_hasDualFarkas)
265  _dualFarkas = sol._dualFarkas;
266  }
267 
268  return *this;
269  }
270 
271  /// assignment operator only for friends
272  template <class S>
274  {
275  if((SolBase<S>*)this != &sol)
276  {
277 
279  _primal = sol._primal;
280  _slacks = sol._slacks;
281 
282  _objVal = R(sol._objVal);
283 
285 
286  if(_hasPrimalRay)
287  _primalRay = sol._primalRay;
288 
290  _dual = sol._dual;
291  _redCost = sol._redCost;
292 
294 
295  if(_hasDualFarkas)
296  _dualFarkas = sol._dualFarkas;
297  }
298 
299  return *this;
300  }
301 
302 };
303 } // namespace soplex
304 
305 /* reset the SOPLEX_DEBUG flag to its original value */
306 #undef SOPLEX_DEBUG
307 #ifdef SOPLEX_DEBUG_SOLBASE
308 #define SOPLEX_DEBUG
309 #undef SOPLEX_DEBUG_SOLBASE
310 #endif
311 
312 #endif // _SOLBASE_H_
int totalSizeDual(const int base=2) const
returns total size of dual solution
Definition: solbase.h:142
bool getPrimalSol(VectorBase< R > &vector) const
gets the primal solution vector; returns true on success
Definition: solbase.h:60
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:28
VectorBase< R > _redCost
Definition: solbase.h:225
VectorBase< R > _slacks
Definition: solbase.h:222
int totalSizePrimal(const int base=2) const
returns total size of primal solution
Definition: solbase.h:128
bool getSlacks(VectorBase< R > &vector) const
gets the vector of slack values; returns true on success
Definition: solbase.h:68
VectorBase< R > _dualFarkas
Definition: solbase.h:226
bool getRedCostSol(VectorBase< R > &vector) const
gets the vector of reduced cost values if available; returns true on success
Definition: solbase.h:105
SolBase< R > & operator=(const SolBase< R > &sol)
assignment operator only for friends
Definition: solbase.h:243
bool hasDualFarkas() const
is a dual farkas ray available?
Definition: solbase.h:113
unsigned int _hasPrimalRay
Definition: solbase.h:231
bool getDualSol(VectorBase< R > &vector) const
gets the dual solution vector; returns true on success
Definition: solbase.h:97
VectorBase< R > _primalRay
Definition: solbase.h:223
int dlcmSizeDual(const int base=2) const
returns size of least common multiple of denominators in dual solution
Definition: solbase.h:170
unsigned int _hasDualFarkas
Definition: solbase.h:233
SolBase< R > & operator=(const SolBase< S > &sol)
assignment operator only for friends
Definition: solbase.h:273
bool getPrimalRaySol(VectorBase< R > &vector) const
gets the primal unbounded ray if available; returns true on success
Definition: solbase.h:82
main LP solver class
bool isPrimalFeasible() const
is the stored solution primal feasible?
Definition: solbase.h:54
VectorBase< R > _dual
Definition: solbase.h:224
Class for storing a primal-dual solution with basis information.
Definition: solbase.h:44
int dmaxSizeDual(const int base=2) const
returns size of largest denominator in dual solution
Definition: solbase.h:198
bool isDualFeasible() const
is a dual solution available?
Definition: solbase.h:91
int dlcmSizePrimal(const int base=2) const
returns size of least common multiple of denominators in primal solution
Definition: solbase.h:156
Collection of dense, sparse, and semi-sparse vectors.
Everything should be within this namespace.
bool getDualFarkasSol(VectorBase< R > &vector) const
gets the Farkas proof if available; returns true on success
Definition: solbase.h:119
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.h:978
int dmaxSizePrimal(const int base=2) const
returns size of largest denominator in primal solution
Definition: solbase.h:184
unsigned int _isPrimalFeasible
Definition: solbase.h:230
void invalidate()
invalidate solution
Definition: solbase.h:212
bool hasPrimalRay() const
is a primal unbounded ray available?
Definition: solbase.h:76
unsigned int _isDualFeasible
Definition: solbase.h:232
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.h:1016
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.h:993
VectorBase< R > _primal
Definition: solbase.h:221