Scippy

SoPlex

Sequential object-oriented simPlex

slufactor_rational.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-2024 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 slufactor_rational.h
26 * @brief Implementation of Sparse Linear Solver with Rational precision.
27 */
28#ifndef _SLUFACTOR_RATIONAL_H_
29#define _SLUFACTOR_RATIONAL_H_
30
31#include <assert.h>
32
33#include "soplex/spxdefines.h"
34#include "soplex/timerfactory.h"
37#include "soplex/rational.h"
38
39namespace soplex
40{
41/// maximum nr. of factorization updates allowed before refactorization.
42#define SOPLEX_MAXUPDATES 1000
43
44/**@brief Implementation of Sparse Linear Solver with Rational precision.
45 * @ingroup Algo
46 *
47 * This class implements a SLinSolverRational interface by using the sparse LU
48 * factorization implemented in CLUFactorRational.
49 */
51{
52public:
53
54 //--------------------------------
55 /**@name Types */
56 ///@{
57 /// Specifies how to perform \ref soplex::SLUFactorRational::change "change" method.
59 {
60 ETA = 0, ///<
61 FOREST_TOMLIN ///<
62 };
63 /// for convenience
65 ///@}
66
67private:
68
69 //--------------------------------
70 /**@name Private data */
71 ///@{
72 VectorRational vec; ///< Temporary vector
73 SSVectorRational ssvec; ///< Temporary semi-sparse vector
74 ///@}
75
76protected:
77
78 //--------------------------------
79 /**@name Protected data */
80 ///@{
81 bool usetup; ///< TRUE iff update vector has been setup
82 UpdateType uptype; ///< the current \ref soplex::SLUFactor<R>::UpdateType "UpdateType".
85 forest; ///< ? Update vector set up by solveRight4update() and solve2right4update()
86 Rational lastThreshold; ///< pivoting threshold of last factorization
87 ///@}
88
89 //--------------------------------
90 /**@name Control Parameters */
91 ///@{
92 /// minimum threshold to use.
94 /// minimum stability to achieve by setting threshold.
96 /// Time spent in solves
99 /// Number of solves
101 ///@}
102
103protected:
104
105 //--------------------------------
106 /**@name Protected helpers */
107 ///@{
108 ///
109 void freeAll();
110 ///
112 ///
113 void init();
114 ///@}
115
116
117public:
118
119 //--------------------------------
120 /**@name Update type */
121 ///@{
122 /// returns the current update type uptype.
124 {
125 return uptype;
126 }
127
128 /// sets update type.
129 /** The new UpdateType becomes valid only after the next call to
130 method load().
131 */
133 {
134 uptype = tp;
135 }
136
137 /// sets minimum Markowitz threshold.
138 void setMarkowitz(const Rational& m)
139 {
140 if(m < 0.01)
141 {
142 minThreshold = 0.01;
143 lastThreshold = 0.01;
144 }
145 else if(m > 0.99)
146 {
147 minThreshold = 0.99;
148 lastThreshold = 0.99;
149 }
150 else
151 {
152 minThreshold = m;
153 lastThreshold = m;
154 }
155 }
156
157 /// returns Markowitz threshold.
159 {
160 return lastThreshold;
161 }
162 ///@}
163
164 //--------------------------------
165 /**@name Derived from SLinSolverRational
166 See documentation of \ref soplex::SLinSolverRational "SLinSolverRational" for a
167 documentation of these methods.
168 */
169 ///@{
170 ///
171 void clear();
172 ///
173 int dim() const
174 {
175 return thedim;
176 }
177 ///
178 int memory() const
179 {
180 return nzCnt + l.start[l.firstUnused];
181 }
182 ///
183 const char* getName() const
184 {
185 return (uptype == SLUFactorRational::ETA) ? "SLU-Eta" : "SLU-Forest-Tomlin";
186 }
187 ///
189 {
190 return Status(stat);
191 }
192 ///
194 ///
195 std::string statistics() const;
196 ///
198 ///@}
199
200public:
201
202 //--------------------------------
203 /**@name Solve */
204 ///@{
205 /// Solves \f$Ax=b\f$.
207 /// Solves \f$Ax=b\f$.
209 /// Solves \f$Ax=b\f$.
211 /// Solves \f$Ax=b\f$ and \f$Ay=d\f$.
214 /// Solves \f$Ax=b\f$, \f$Ay=d\f$ and \f$Az=e\f$.
217 /// Solves \f$Ax=b\f$.
219 /// Solves \f$Ax=b\f$.
221 /// Solves \f$Ax=b\f$ and \f$Ay=d\f$.
224 /// Solves \f$Ax=b\f$, \f$Ay=d\f$ and \f$Az=e\f$.
227 ///
228 Status change(int idx, const SVectorRational& subst, const SSVectorRational* eta = nullptr);
229 ///@}
230
231 //--------------------------------
232 /**@name Miscellaneous */
233 ///@{
234 /// time spent in factorizations
236 {
237 return factorTime->time();
238 }
239 /// set time limit on factorization
240 void setTimeLimit(const Real limit)
241 {
242 timeLimit = limit;
243 }
244 /// reset FactorTime
246 {
247 factorTime->reset();
248 }
249 /// number of factorizations performed
250 int getFactorCount() const
251 {
252 return factorCount;
253 }
254 /// time spent in solves
256 {
257 return solveTime->time();
258 }
259 /// reset SolveTime
261 {
262 solveTime->reset();
263 }
264 /// number of solves performed
265 int getSolveCount() const
266 {
267 return solveCount;
268 }
269 /// reset timers and counters
271 {
272 factorTime->reset();
273 solveTime->reset();
274 factorCount = 0;
275 solveCount = 0;
276 }
277 /// prints the LU factorization to stdout.
278 void dump() const;
279
280 /// consistency check.
281 bool isConsistent() const;
282 ///@}
283
284 //------------------------------------
285 /**@name Constructors / Destructors */
286 ///@{
287 /// default constructor.
290 , vec(1)
291 , ssvec(1)
292 , usetup(false)
294 , eta(1)
295 , forest(1)
296 , minThreshold(0.01)
297 , timerType(Timer::USER_TIME)
298 {
299 row.perm = nullptr;
300 row.orig = nullptr;
301 col.perm = nullptr;
302 col.orig = nullptr;
303 u.row.elem = nullptr;
304 u.row.idx = nullptr;
305 u.row.start = nullptr;
306 u.row.len = nullptr;
307 u.row.max = nullptr;
308 u.col.elem = nullptr;
309 u.col.idx = nullptr;
310 u.col.start = nullptr;
311 u.col.len = nullptr;
312 u.col.max = nullptr;
313 l.idx = nullptr;
314 l.start = nullptr;
315 l.row = nullptr;
316 l.ridx = nullptr;
317 l.rbeg = nullptr;
318 l.rorig = nullptr;
319 l.rperm = nullptr;
320
321 nzCnt = 0;
322 thedim = 0;
323
324 try
325 {
333
334 work = vec.get_ptr();
335
336 u.row.used = 0;
338 u.row.val.reDim(1);
340 spx_alloc(u.row.start, thedim + 1);
341 spx_alloc(u.row.len, thedim + 1);
342 spx_alloc(u.row.max, thedim + 1);
343
344 u.row.list.idx = thedim;
345 u.row.start[thedim] = 0;
346 u.row.max [thedim] = 0;
347 u.row.len [thedim] = 0;
348
349 u.col.size = 1;
350 u.col.used = 0;
353 spx_alloc(u.col.start, thedim + 1);
354 spx_alloc(u.col.len, thedim + 1);
355 spx_alloc(u.col.max, thedim + 1);
356 u.col.val.reDim(0);
357
358 u.col.list.idx = thedim;
359 u.col.start[thedim] = 0;
360 u.col.max[thedim] = 0;
361 u.col.len[thedim] = 0;
362
363 l.val.reDim(1);
364 spx_alloc(l.idx, l.val.dim());
365
366 l.startSize = 1;
367 l.firstUpdate = 0;
368 l.firstUnused = 0;
369
372 }
373 catch(const SPxMemoryException& x)
374 {
375 freeAll();
376 throw x;
377 }
378
379 l.rval.reDim(0);
380 l.ridx = nullptr;
381 l.rbeg = nullptr;
382 l.rorig = nullptr;
383 l.rperm = nullptr;
384
386
387 factorCount = 0;
388 timeLimit = -1.0;
389 solveCount = 0;
390
391 assert(row.perm != nullptr);
392 assert(row.orig != nullptr);
393 assert(col.perm != nullptr);
394 assert(col.orig != nullptr);
395
396 assert(u.row.elem != nullptr);
397 assert(u.row.idx != nullptr);
398 assert(u.row.start != nullptr);
399 assert(u.row.len != nullptr);
400 assert(u.row.max != nullptr);
401
402 assert(u.col.elem != nullptr);
403 assert(u.col.idx != nullptr);
404 assert(u.col.start != nullptr);
405 assert(u.col.len != nullptr);
406 assert(u.col.max != nullptr);
407
408 assert(l.idx != nullptr);
409 assert(l.start != nullptr);
410 assert(l.row != nullptr);
411
413 }
414 /// assignment operator.
416 {
417
418 if(this != &old)
419 {
420 // we don't need to copy them, because they are temporary vectors
421 vec.clear();
422 ssvec.clear();
423
424 eta = old.eta;
425 forest = old.forest;
426
427 freeAll();
428
429 try
430 {
431 assign(old);
432 }
433 catch(const SPxMemoryException& x)
434 {
435 freeAll();
436 throw x;
437 }
438
439 assert(isConsistent());
440 }
441
442 return *this;
443 }
444
445 /// copy constructor.
447 : SLinSolverRational(old)
449 , vec(1) // we don't need to copy it, because they are temporary vectors
450 , ssvec(1) // we don't need to copy it, because they are temporary vectors
451 , usetup(old.usetup)
452 , eta(old.eta)
453 , forest(old.forest)
454 , timerType(old.timerType)
455 {
456 row.perm = nullptr;
457 row.orig = nullptr;
458 col.perm = nullptr;
459 col.orig = nullptr;
460 u.row.elem = nullptr;
461 u.row.idx = nullptr;
462 u.row.start = nullptr;
463 u.row.len = nullptr;
464 u.row.max = nullptr;
465 u.col.elem = nullptr;
466 u.col.idx = nullptr;
467 u.col.start = nullptr;
468 u.col.len = nullptr;
469 u.col.max = nullptr;
470 l.idx = nullptr;
471 l.start = nullptr;
472 l.row = nullptr;
473 l.ridx = nullptr;
474 l.rbeg = nullptr;
475 l.rorig = nullptr;
476 l.rperm = nullptr;
477
478 solveCount = 0;
481
482 try
483 {
484 assign(old);
485 }
486 catch(const SPxMemoryException& x)
487 {
488 freeAll();
489 throw x;
490 }
491
493 }
494
495 /// destructor.
497 /// clone function for polymorphism
498 inline virtual SLinSolverRational* clone() const
499 {
500 return new SLUFactorRational(*this);
501 }
502 ///@}
503
504private:
505
506 //------------------------------------
507 /**@name Private helpers */
508 ///@{
509 /// used to implement the assignment operator
510 void assign(const SLUFactorRational& old);
511 ///@}
512};
513
514} // namespace soplex
515#include "slufactor_rational.hpp"
516#endif // _SLUFACTOR_RATIONAL_H_
Implementation of sparse LU factorization with Rational precision.
int factorCount
Number of factorizations.
Timer * factorTime
Time spent in factorizations.
Rational * work
Working array: must always be left as 0!
Real timeLimit
Time limit on factorization or solves.
Perm col
column permutation matrices
Perm row
row permutation matrices
SLinSolverRational::Status stat
Status indicator.
VectorRational diag
Array of pivot elements.
int thedim
dimension of factorized matrix
int nzCnt
number of nonzeros in U
Implementation of Sparse Linear Solver with Rational precision.
UpdateType
Specifies how to perform change method.
SSVectorRational forest
? Update vector set up by solveRight4update() and solve2right4update()
Rational stability() const
Timer * solveTime
Time spent in solves.
virtual SLinSolverRational * clone() const
clone function for polymorphism
SLUFactorRational()
default constructor.
void assign(const SLUFactorRational &old)
used to implement the assignment operator
Real getFactorTime() const
time spent in factorizations
bool isConsistent() const
consistency check.
void changeEta(int idx, SSVectorRational &eta)
void solve3right4update(SSVectorRational &x, VectorRational &y, VectorRational &z, const SVectorRational &b, SSVectorRational &d, SSVectorRational &e)
Solves , and .
SLinSolverRational::Status Status
for convenience
void resetSolveTime()
reset SolveTime
void resetFactorTime()
reset FactorTime
UpdateType utype() const
returns the current update type uptype.
void setUtype(UpdateType tp)
sets update type.
void resetCounters()
reset timers and counters
Status change(int idx, const SVectorRational &subst, const SSVectorRational *eta=nullptr)
UpdateType uptype
the current UpdateType.
int solveCount
Number of solves.
const char * getName() const
bool usetup
TRUE iff update vector has been setup.
void solveRight4update(SSVectorRational &x, const SVectorRational &b)
Solves .
Rational markowitz()
returns Markowitz threshold.
void solveLeft(SSVectorRational &x, const SVectorRational &b)
Solves .
Rational minStability
minimum stability to achieve by setting threshold.
void setMarkowitz(const Rational &m)
sets minimum Markowitz threshold.
void solve2right4update(SSVectorRational &x, VectorRational &y, const SVectorRational &b, SSVectorRational &d)
Solves and .
virtual ~SLUFactorRational()
destructor.
void solveLeft(SSVectorRational &x, VectorRational &y, VectorRational &z, const SVectorRational &b, SSVectorRational &d, SSVectorRational &e)
Solves , and .
SLUFactorRational(const SLUFactorRational &old)
copy constructor.
void setTimeLimit(const Real limit)
set time limit on factorization
int getSolveCount() const
number of solves performed
Rational minThreshold
minimum threshold to use.
void solveRight(SSVectorRational &x, const SVectorRational &b)
Solves .
int getFactorCount() const
number of factorizations performed
VectorRational vec
Temporary vector.
Rational lastThreshold
pivoting threshold of last factorization
void solveLeft(SSVectorRational &x, VectorRational &y, const SVectorRational &b, SSVectorRational &d)
Solves and .
std::string statistics() const
SSVectorRational ssvec
Temporary semi-sparse vector.
void solveRight(VectorRational &x, const VectorRational &b)
Solves .
SLUFactorRational & operator=(const SLUFactorRational &old)
assignment operator.
Real getSolveTime() const
time spent in solves
void solveLeft(VectorRational &x, const VectorRational &b)
Solves .
void dump() const
prints the LU factorization to stdout.
Status load(const SVectorRational *vec[], int dim)
Sparse Linear Solver virtual base class with Rational precision.
Status
status flags of the SLinSolverRational class.
Exception class for out of memory exceptions.
Definition: exceptions.h:80
void clear()
Clears vector.
Definition: ssvectorbase.h:616
static Timer * createTimer(Timer::TYPE ttype)
create timers and allocate memory for them
Definition: timerfactory.h:53
Wrapper for the system time query methods.
Definition: timer.h:86
TYPE
types of timers
Definition: timer.h:109
virtual void reset()=0
initialize timer, set timing accounts to zero.
virtual Real time() const =0
R * get_ptr()
Conversion to C-style pointer.
Definition: vectorbase.h:494
void reDim(int newdim, const bool setZero=true)
Resets VectorBase's dimension to newdim.
Definition: vectorbase.h:541
int dim() const
Dimension of vector.
Definition: vectorbase.h:270
void clear()
Set vector to contain all-zeros (keeping the same length)
Definition: vectorbase.h:308
Implementation of sparse LU factorization with Rational precision.
Everything should be within this namespace.
double Real
Definition: spxdefines.h:269
number< gmp_rational, et_off > Rational
Definition: rational.h:29
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:58
Sparse Linear Solver virtual base class with Rational precision.
Debugging, floating point type and parameter definitions.
VectorRational rval
values of rows of L
int * rorig
original row permutation
int * row
column indices of L vectors
int startSize
size of array start
int * ridx
indices of rows of L
int * rbeg
start of rows in rval and ridx
int * rperm
original row permutation
int * idx
array of size val.dim() storing indices of L vectors
VectorRational val
values of L vectors
int firstUpdate
number of first update L vector
int * start
starting positions in val and idx
int firstUnused
number of first unused L vector
int * orig
orig[p] original index from p
int * perm
perm[i] permuted index from i
int * max
maximum available nonzeros per colunn: start[i] + max[i] == start[elem[i].next->idx] len[i] <= max[i]...
int * len
used nonzeros per column vector
int used
used entries of array idx
Dring * elem
Array of ring elements.
Dring list
Double linked ringlist of vector indices in the order they appear in the column file
int * idx
hold row indices of nonzeros
VectorRational val
hold nonzero values: this is only initialized in the end of the factorization with DEFAULT updates.
int * start
starting positions in val and idx
int * max
maximum available nonzeros per row: start[i] + max[i] == start[elem[i].next->idx] len[i] <= max[i].
int * len
used nonzeros per row vectors
int used
used entries of arrays idx and val
Dring * elem
Array of ring elements.
Dring list
Double linked ringlist of vector indices in the order they appear in the row file
int * idx
array of length val.dim() to hold column indices of nonzeros in val
VectorRational val
hold nonzero values
int * start
starting positions in val and idx
struct soplex::CLUFactorRational::U::Col col
struct soplex::CLUFactorRational::U::Row row
TimerFactory class.