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-2025 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file solbase.h
26 * @brief Class for storing a primal-dual solution with basis information
27 */
28#ifndef _SOLBASE_H_
29#define _SOLBASE_H_
30
31/* undefine SOPLEX_DEBUG flag from including files; if SOPLEX_DEBUG should be defined in this file, do so below */
32#ifdef SOPLEX_DEBUG
33#define SOPLEX_DEBUG_SOLBASE
34#undef SOPLEX_DEBUG
35#endif
36
37#include <assert.h>
38#include <string.h>
39#include <math.h>
40#include <iostream>
41
42#include "soplex/basevectors.h"
43#include "soplex/spxsolver.h" // needed for basis information
44
45namespace soplex
46{
47/**@class SolBase
48 * @brief Class for storing a primal-dual solution with basis information
49 * @ingroup Algo
50 */
51template <class R>
53{
54 template <class T> friend class SoPlexBase;
55 // Why do we need the following? This is at least used in the operator=
56 // When Rational solution needs to be copied into Real, the private member
57 // _objVal is accessed.
58 template <class S> friend class SolBase;
59
60public:
61
62
63 SolBase(VectorBase<R>& primal, VectorBase<R>& dual, VectorBase<R>& reducedCosts)
64 : _primal(primal), _dual(dual), _redCost(reducedCosts), _hasDualFarkas(false), _hasPrimalRay(false)
65 {
66 }
67
68
69 /// is the stored solution primal feasible?
70 bool isPrimalFeasible() const
71 {
72 return _isPrimalFeasible;
73 }
74
75 /// gets the primal solution vector; returns true on success
76 bool getPrimalSol(VectorBase<R>& vector) const
77 {
78 vector = _primal;
79
80 return _isPrimalFeasible;
81 }
82
83 /// gets the vector of slack values; returns true on success
84 bool getSlacks(VectorBase<R>& vector) const
85 {
86 vector = _slacks;
87
88 return _isPrimalFeasible;
89 }
90
91 /// is a primal unbounded ray available?
92 bool hasPrimalRay() const
93 {
94 return _hasPrimalRay;
95 }
96
97 /// gets the primal unbounded ray if available; returns true on success
98 bool getPrimalRaySol(VectorBase<R>& vector) const
99 {
100 if(_hasPrimalRay)
101 vector = _primalRay;
102
103 return _hasPrimalRay;
104 }
105
106 /// is a dual solution available?
107 bool isDualFeasible() const
108 {
109 return _isDualFeasible;
110 }
111
112 /// gets the dual solution vector; returns true on success
113 bool getDualSol(VectorBase<R>& vector) const
114 {
115 vector = _dual;
116
117 return _isDualFeasible;
118 }
119
120 /// gets the vector of reduced cost values if available; returns true on success
121 bool getRedCostSol(VectorBase<R>& vector) const
122 {
123 vector = _redCost;
124
125 return _isDualFeasible;
126 }
127
128 /// is a dual farkas ray available?
129 bool hasDualFarkas() const
130 {
131 return _hasDualFarkas;
132 }
133
134 /// gets the Farkas proof if available; returns true on success
135 bool getDualFarkasSol(VectorBase<R>& vector) const
136 {
138 vector = _dualFarkas;
139
140 return _hasDualFarkas;
141 }
142
143 /// returns total size of primal solution
144 int totalSizePrimal(const int base = 2) const
145 {
146 int size = 0;
147
149 size += totalSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
150
151 if(_hasPrimalRay)
152 size += totalSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
153
154 return size;
155 }
156
157 /// returns total size of dual solution
158 int totalSizeDual(const int base = 2) const
159 {
160 int size = 0;
161
163 size += totalSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
164
166 size += totalSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
167
168 return size;
169 }
170
171 /// returns size of least common multiple of denominators in primal solution
172 int dlcmSizePrimal(const int base = 2) const
173 {
174 int size = 0;
175
177 size += dlcmSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
178
179 if(_hasPrimalRay)
180 size += dlcmSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
181
182 return size;
183 }
184
185 /// returns size of least common multiple of denominators in dual solution
186 int dlcmSizeDual(const int base = 2) const
187 {
188 int size = 0;
189
191 size += dlcmSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
192
194 size += dlcmSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
195
196 return size;
197 }
198
199 /// returns size of largest denominator in primal solution
200 int dmaxSizePrimal(const int base = 2) const
201 {
202 int size = 0;
203
205 size += dmaxSizeRational(_primal.get_const_ptr(), _primal.dim(), base);
206
207 if(_hasPrimalRay)
208 size += dmaxSizeRational(_primalRay.get_const_ptr(), _primalRay.dim(), base);
209
210 return size;
211 }
212
213 /// returns size of largest denominator in dual solution
214 int dmaxSizeDual(const int base = 2) const
215 {
216 int size = 0;
217
219 size += dmaxSizeRational(_dual.get_const_ptr(), _dual.dim(), base);
220
222 size += dmaxSizeRational(_dualFarkas.get_const_ptr(), _dualFarkas.dim(), base);
223
224 return size;
225 }
226
227 /// invalidate solution
229 {
230 _isPrimalFeasible = false;
231 _hasPrimalRay = false;
232 _isDualFeasible = false;
233 _hasDualFarkas = false;
234 }
235
236private:
243
245
246 unsigned int _isPrimalFeasible: 1;
247 unsigned int _hasPrimalRay: 1;
248 unsigned int _isDualFeasible: 1;
249 unsigned int _hasDualFarkas: 1;
250
251 /// default constructor only for friends
253 : _objVal(0)
254 {
255 invalidate();
256 }
257
258 /// assignment operator only for friends
260 {
261 if(this != &sol)
262 {
263
265 _primal = sol._primal;
266 _slacks = sol._slacks;
267 _objVal = sol._objVal;
268
270
271 if(_hasPrimalRay)
273
275 _dual = sol._dual;
276 _redCost = sol._redCost;
277
279
282 }
283
284 return *this;
285 }
286
287 /// assignment operator only for friends
288 template <class S>
290 {
291 if((SolBase<S>*)this != &sol)
292 {
293
295 _primal = sol._primal;
296 _slacks = sol._slacks;
297
298 _objVal = R(sol._objVal);
299
301
302 if(_hasPrimalRay)
304
306 _dual = sol._dual;
307 _redCost = sol._redCost;
308
310
313 }
314
315 return *this;
316 }
317
318};
319} // namespace soplex
320
321/* reset the SOPLEX_DEBUG flag to its original value */
322#undef SOPLEX_DEBUG
323#ifdef SOPLEX_DEBUG_SOLBASE
324#define SOPLEX_DEBUG
325#undef SOPLEX_DEBUG_SOLBASE
326#endif
327
328#endif // _SOLBASE_H_
Collection of dense, sparse, and semi-sparse vectors.
Class for storing a primal-dual solution with basis information.
Definition: solbase.h:53
VectorBase< R > _dualFarkas
Definition: solbase.h:242
VectorBase< R > _slacks
Definition: solbase.h:238
bool getSlacks(VectorBase< R > &vector) const
gets the vector of slack values; returns true on success
Definition: solbase.h:84
unsigned int _hasPrimalRay
Definition: solbase.h:247
bool getRedCostSol(VectorBase< R > &vector) const
gets the vector of reduced cost values if available; returns true on success
Definition: solbase.h:121
int dmaxSizeDual(const int base=2) const
returns size of largest denominator in dual solution
Definition: solbase.h:214
bool isDualFeasible() const
is a dual solution available?
Definition: solbase.h:107
bool getDualFarkasSol(VectorBase< R > &vector) const
gets the Farkas proof if available; returns true on success
Definition: solbase.h:135
unsigned int _isDualFeasible
Definition: solbase.h:248
SolBase< R > & operator=(const SolBase< S > &sol)
assignment operator only for friends
Definition: solbase.h:289
unsigned int _isPrimalFeasible
Definition: solbase.h:246
VectorBase< R > _redCost
Definition: solbase.h:241
bool getDualSol(VectorBase< R > &vector) const
gets the dual solution vector; returns true on success
Definition: solbase.h:113
VectorBase< R > _primalRay
Definition: solbase.h:239
VectorBase< R > _primal
Definition: solbase.h:237
bool getPrimalRaySol(VectorBase< R > &vector) const
gets the primal unbounded ray if available; returns true on success
Definition: solbase.h:98
int totalSizeDual(const int base=2) const
returns total size of dual solution
Definition: solbase.h:158
void invalidate()
invalidate solution
Definition: solbase.h:228
int dlcmSizeDual(const int base=2) const
returns size of least common multiple of denominators in dual solution
Definition: solbase.h:186
bool hasPrimalRay() const
is a primal unbounded ray available?
Definition: solbase.h:92
bool hasDualFarkas() const
is a dual farkas ray available?
Definition: solbase.h:129
int dmaxSizePrimal(const int base=2) const
returns size of largest denominator in primal solution
Definition: solbase.h:200
int totalSizePrimal(const int base=2) const
returns total size of primal solution
Definition: solbase.h:144
bool isPrimalFeasible() const
is the stored solution primal feasible?
Definition: solbase.h:70
SolBase()
default constructor only for friends
Definition: solbase.h:252
VectorBase< R > _dual
Definition: solbase.h:240
int dlcmSizePrimal(const int base=2) const
returns size of least common multiple of denominators in primal solution
Definition: solbase.h:172
SolBase< R > & operator=(const SolBase< R > &sol)
assignment operator only for friends
Definition: solbase.h:259
SolBase(VectorBase< R > &primal, VectorBase< R > &dual, VectorBase< R > &reducedCosts)
Definition: solbase.h:63
unsigned int _hasDualFarkas
Definition: solbase.h:249
bool getPrimalSol(VectorBase< R > &vector) const
gets the primal solution vector; returns true on success
Definition: solbase.h:76
Dense vector.
Definition: vectorbase.h:86
Everything should be within this namespace.
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.h:1050
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.h:1035
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.h:1072
main LP solver class