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