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