Scippy

SoPlex

Sequential object-oriented simPlex

spxscaler.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 spxscaler.h
17  * @brief LP scaling base class.
18  */
19 #ifndef _SPXSCALER_H_
20 #define _SPXSCALER_H_
21 
22 #include <assert.h>
23 
24 #include "soplex/spxdefines.h"
25 #include "soplex/dataarray.h"
26 #include "soplex/vector.h"
27 #include "soplex/svector.h"
28 #include "soplex/svset.h"
29 #include "soplex/dsvector.h"
30 #include "soplex/dvector.h"
31 #include <vector>
32 
33 namespace soplex
34 {
35 
36 template < class R >
37 class SPxLPBase;
38 /**@brief LP scaler abstract base class.
39  @ingroup Algo
40 
41  Instances of classes derived from SPxScaler may be loaded to SoPlex in
42  order to scale LPs before solving them. SoPlex will load() itself to
43  the SPxScaler and then call #scale(). Generally any SPxLP can be
44  loaded to a SPxScaler for #scale()%ing it. The scaling can
45  be undone by calling unscale().
46 
47  Mathematically, the scaling of a constraint matrix A can be written
48  as \f$ A' = R A C \f$, with \f$ R \f$ and \f$ C \f$, being diagonal matrices
49  corresponding to the row and column scale factors, respectively. Besides the
50  constraints matrix, also the upper and lower bounds of both columns and rows
51  need to be scaled.
52 
53  Note that by default scaling is performed both before and after presolving and
54  the former scaling factors are retained during branch-and-bound (persistent scaling).
55  However, while within SoPlex the scaled problem is used, data accessed through
56  the soplex.cpp interface is provided w.r.t. the original problem (i.e., in unscaled form).
57  For instance, consider a scaled constraints matrix A' that is extended by artificial slack
58  variables to the matrix (A',I).
59  A basis \f$ B' = [(A',I)P]_{[1:m][1:m] }\f$ (with P being a permutation matrix)
60  for the scaled problem corresponds to the basis
61  \f$ B = R^{-1} [(A',I)P]_{[1:m][1:m]} [P^{T} \tilde{C}^{-1} P]_{[1:m][1:m] } \f$. In
62  this equation, \f$ \tilde{C} \f$ is of the form
63 
64  \f[
65  \begin{array}{cc}
66  C & 0 \\
67  O & R^{-1}
68  \end{array}
69  \f]
70 
71  Note that in SoPlex only scaling factors \f$ 2^k, k \in \mathbb{Z} \f$ are used.
72 
73 
74 */
75 
76 template <class R>
77 class SPxScaler
78 {
79 protected:
80 
81  //-------------------------------------
82  /**@name Data */
83  ///@{
84  const char* m_name; ///< Name of the scaler
85  DataArray < int >* m_activeColscaleExp; ///< pointer to currently active column scaling factors
86  DataArray < int >* m_activeRowscaleExp; ///< pointer to currently active row scaling factors
87  bool m_colFirst; ///< do column scaling first
88  bool m_doBoth; ///< do columns and rows
89  SPxOut* spxout; ///< message handler
90  ///@}
91 
92  //-------------------------------------
93  /**@name Protected helpers */
94  ///@{
95 
96  /// clear and setup scaling arrays in the LP
97  virtual void setup(SPxLPBase<R>& lp);
98  ///@}
99 
100 public:
101 
102  /// compute a single scaling vector , e.g. of a newly added row
103  virtual int computeScaleExp(const SVectorBase<R>& vec, const DataArray<int>& oldScaleExp) const;
104 
105  // The following is now redundant because of the above function.
106  // virtual int computeScaleExp(const SVectorBase<Rational>& vec, const DataArray<int>& oldScaleExp) const;
107 
108  /// applies m_colscale and m_rowscale to the \p lp.
109  virtual void applyScaling(SPxLPBase<R>& lp);
110 
111 
112  template <class T>
113  friend std::ostream& operator<<(std::ostream& s, const SPxScaler<T>& sc);
114 
115  //-------------------------------------
116  /**@name Construction / destruction */
117  ///@{
118  /// constructor
119  explicit SPxScaler(const char* name, bool colFirst = false, bool doBoth = true,
120  SPxOut* spxout = NULL);
121  /// copy constructor
122  SPxScaler(const SPxScaler&);
123  /// assignment operator
124  SPxScaler& operator=(const SPxScaler&);
125  /// destructor.
126  virtual ~SPxScaler();
127  /// clone function for polymorphism
128  virtual SPxScaler* clone() const = 0;
129  ///@}
130 
131  //-------------------------------------
132  /**@name Access / modification */
133  ///@{
134  /// get name of scaler
135  virtual const char* getName() const;
136  /// set scaling order
137  virtual void setOrder(bool colFirst);
138  /// set wether column and row scaling should be performed
139  virtual void setBoth(bool both);
140  /// set message handler
141  virtual void setOutstream(SPxOut& newOutstream)
142  {
143  spxout = &newOutstream;
144  }
145  /// set R parameter
146  virtual void setRealParam(R param, const char* name = "realparam");
147  /// set int parameter
148  virtual void setIntParam(int param, const char* name = "intparam");
149  ///@}
150 
151  //-------------------------------------
152  /**@name Scaling */
153  ///@{
154  /// scale SPxLP.
155  virtual void scale(SPxLPBase<R>& lp, bool persistent = true) = 0;
156  /// unscale SPxLP
157  virtual void unscale(SPxLPBase<R>& lp);
158  /// returns scaling factor for column \p i
159  virtual int getColScaleExp(int i) const;
160  /// returns scaling factor for row \p i
161  virtual int getRowScaleExp(int i) const;
162  /// gets unscaled column \p i
163  virtual void getColUnscaled(const SPxLPBase<R>& lp, int i, DSVectorBase<R>& vec) const;
164  /// returns maximum absolute value of unscaled column \p i
165  virtual R getColMaxAbsUnscaled(const SPxLPBase<R>& lp, int i) const;
166  /// returns minumum absolute value of unscaled column \p i
167  virtual R getColMinAbsUnscaled(const SPxLPBase<R>& lp, int i) const;
168  /// returns unscaled upper bound \p i
169  virtual R upperUnscaled(const SPxLPBase<R>& lp, int i) const;
170  /// returns unscaled upper bound vector of \p lp
171  virtual void getUpperUnscaled(const SPxLPBase<R>& lp, VectorBase<R>& vec) const;
172  /// returns unscaled lower bound \p i
173  virtual R lowerUnscaled(const SPxLPBase<R>& lp, int i) const;
174  /// gets unscaled lower bound vector
175  virtual void getLowerUnscaled(const SPxLPBase<R>& lp, VectorBase<R>& vec) const;
176  /// returns unscaled objective function coefficient of \p i
177  virtual R maxObjUnscaled(const SPxLPBase<R>& lp, int i) const;
178  /// gets unscaled objective function
179  virtual void getMaxObjUnscaled(const SPxLPBase<R>& lp, VectorBase<R>& vec) const;
180  /// returns unscaled row \p i
181  virtual void getRowUnscaled(const SPxLPBase<R>& lp, int i, DSVectorBase<R>& vec) const;
182  /// returns maximum absolute value of unscaled row \p i
183  virtual R getRowMaxAbsUnscaled(const SPxLPBase<R>& lp, int i) const;
184  /// returns minimum absolute value of unscaled row \p i
185  virtual R getRowMinAbsUnscaled(const SPxLPBase<R>& lp, int i) const;
186  /// returns unscaled right hand side \p i
187  virtual R rhsUnscaled(const SPxLPBase<R>& lp, int i) const;
188  /// gets unscaled right hand side vector
189  virtual void getRhsUnscaled(const SPxLPBase<R>& lp, VectorBase<R>& vec) const;
190  /// returns unscaled left hand side \p i of \p lp
191  virtual R lhsUnscaled(const SPxLPBase<R>& lp, int i) const;
192  /// returns unscaled left hand side vector of \p lp
193  virtual void getLhsUnscaled(const SPxLPBase<R>& lp, VectorBase<R>& vec) const;
194  /// returns unscaled coefficient of \p lp
195  virtual R getCoefUnscaled(const SPxLPBase<R>& lp, int row, int col) const;
196  /// unscale dense primal solution vector given in \p x.
197  virtual void unscalePrimal(const SPxLPBase<R>& lp, VectorBase<R>& x) const;
198  /// unscale dense slack vector given in \p s.
199  virtual void unscaleSlacks(const SPxLPBase<R>& lp, VectorBase<R>& s) const;
200  /// unscale dense dual solution vector given in \p pi.
201  virtual void unscaleDual(const SPxLPBase<R>& lp, VectorBase<R>& pi) const;
202  /// unscale dense reduced cost vector given in \p r.
203  virtual void unscaleRedCost(const SPxLPBase<R>& lp, VectorBase<R>& r) const;
204  /// unscale primal ray given in \p ray.
205  virtual void unscalePrimalray(const SPxLPBase<R>& lp, VectorBase<R>& ray) const;
206  /// unscale dual ray given in \p ray.
207  virtual void unscaleDualray(const SPxLPBase<R>& lp, VectorBase<R>& ray) const;
208  /// apply scaling to objective function vector \p origObj.
209  virtual void scaleObj(const SPxLPBase<R>& lp, VectorBase<R>& origObj) const;
210  /// returns scaled objective function coefficient \p origObj.
211  virtual R scaleObj(const SPxLPBase<R>& lp, int i, R origObj) const;
212  /// returns scaled LP element in \p row and \p col.
213  virtual R scaleElement(const SPxLPBase<R>& lp, int row, int col, R val) const;
214  /// returns scaled lower bound of column \p col.
215  virtual R scaleLower(const SPxLPBase<R>& lp, int col, R lower) const;
216  /// returns scaled upper bound of column \p col.
217  virtual R scaleUpper(const SPxLPBase<R>& lp, int col, R upper) const;
218  /// returns scaled left hand side of row \p row.
219  virtual R scaleLhs(const SPxLPBase<R>& lp, int row, R lhs) const;
220  /// returns scaled right hand side of row \p row.
221  virtual R scaleRhs(const SPxLPBase<R>& lp, int row, R rhs) const;
222  /// absolute smallest column scaling factor
223  virtual R minAbsColscale() const;
224  /// absolute biggest column scaling factor
225  virtual R maxAbsColscale() const;
226  /// absolute smallest row scaling factor
227  virtual R minAbsRowscale() const;
228  /// absolute biggest row scaling factor
229  virtual R maxAbsRowscale() const;
230  /// maximum ratio between absolute biggest and smallest element in any column.
231  virtual R maxColRatio(const SPxLPBase<R>& lp) const;
232  /// maximum ratio between absolute biggest and smallest element in any row.
233  virtual R maxRowRatio(const SPxLPBase<R>& lp) const;
234  /// round vector entries to power of 2
235  void computeExpVec(const std::vector<R>& vec, DataArray<int>& vecExp);
236  ///@}
237 
238  //-------------------------------------
239  /**@name Debugging */
240  ///@{
241  /// consistency check
242  virtual bool isConsistent() const;
243  ///@}
244 };
245 } // namespace soplex
246 
247 // General templated definitions
248 #include "spxscaler.hpp"
249 
250 #endif // _SPXSCALER_H_
virtual const char * getName() const
get name of scaler
virtual void scale(SPxLPBase< R > &lp, bool persistent=true)=0
scale SPxLP.
virtual void getUpperUnscaled(const SPxLPBase< R > &lp, VectorBase< R > &vec) const
returns unscaled upper bound vector of lp
virtual R lhsUnscaled(const SPxLPBase< R > &lp, int i) const
returns unscaled left hand side i of lp
virtual void scaleObj(const SPxLPBase< R > &lp, VectorBase< R > &origObj) const
apply scaling to objective function vector origObj.
virtual void setOrder(bool colFirst)
set scaling order
Dense vector.Class VectorBase provides dense linear algebra vectors. Internally, VectorBase wraps std...
Definition: dsvectorbase.h:28
virtual R maxRowRatio(const SPxLPBase< R > &lp) const
maximum ratio between absolute biggest and smallest element in any row.
virtual R scaleUpper(const SPxLPBase< R > &lp, int col, R upper) const
returns scaled upper bound of column col.
virtual void unscaleDual(const SPxLPBase< R > &lp, VectorBase< R > &pi) const
unscale dense dual solution vector given in pi.
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:43
virtual R maxObjUnscaled(const SPxLPBase< R > &lp, int i) const
returns unscaled objective function coefficient of i
virtual void setBoth(bool both)
set wether column and row scaling should be performed
virtual void getLhsUnscaled(const SPxLPBase< R > &lp, VectorBase< R > &vec) const
returns unscaled left hand side vector of lp
virtual void applyScaling(SPxLPBase< R > &lp)
applies m_colscale and m_rowscale to the lp.
virtual void getRowUnscaled(const SPxLPBase< R > &lp, int i, DSVectorBase< R > &vec) const
returns unscaled row i
Dense vector for linear algebra.
virtual void unscaleDualray(const SPxLPBase< R > &lp, VectorBase< R > &ray) const
unscale dual ray given in ray.
DataArray< int > * m_activeColscaleExp
pointer to currently active column scaling factors
Definition: spxscaler.h:85
virtual void setOutstream(SPxOut &newOutstream)
set message handler
Definition: spxscaler.h:141
virtual SPxScaler * clone() const =0
clone function for polymorphism
SPxScaler & operator=(const SPxScaler &)
assignment operator
virtual R scaleElement(const SPxLPBase< R > &lp, int row, int col, R val) const
returns scaled LP element in row and col.
virtual void setup(SPxLPBase< R > &lp)
clear and setup scaling arrays in the LP
SPxOut * spxout
message handler
Definition: spxscaler.h:89
virtual R maxAbsRowscale() const
absolute biggest row scaling factor
virtual void unscalePrimalray(const SPxLPBase< R > &lp, VectorBase< R > &ray) const
unscale primal ray given in ray.
virtual void setIntParam(int param, const char *name="intparam")
set int parameter
Dynamic vectors.
Wrapper for several output streams. A verbosity level is used to decide which stream to use and wheth...
Definition: spxout.h:63
virtual void getColUnscaled(const SPxLPBase< R > &lp, int i, DSVectorBase< R > &vec) const
gets unscaled column i
virtual R upperUnscaled(const SPxLPBase< R > &lp, int i) const
returns unscaled upper bound i
Sparse vectors.
virtual R getRowMaxAbsUnscaled(const SPxLPBase< R > &lp, int i) const
returns maximum absolute value of unscaled row i
virtual R getCoefUnscaled(const SPxLPBase< R > &lp, int row, int col) const
returns unscaled coefficient of lp
virtual R rhsUnscaled(const SPxLPBase< R > &lp, int i) const
returns unscaled right hand side i
virtual ~SPxScaler()
destructor.
virtual R maxAbsColscale() const
absolute biggest column scaling factor
DataArray< int > * m_activeRowscaleExp
pointer to currently active row scaling factors
Definition: spxscaler.h:86
Debugging, floating point type and parameter definitions.
virtual int getColScaleExp(int i) const
returns scaling factor for column i
virtual R scaleLhs(const SPxLPBase< R > &lp, int row, R lhs) const
returns scaled left hand side of row row.
void computeExpVec(const std::vector< R > &vec, DataArray< int > &vecExp)
round vector entries to power of 2
Everything should be within this namespace.
virtual void unscaleSlacks(const SPxLPBase< R > &lp, VectorBase< R > &s) const
unscale dense slack vector given in s.
Saving LPs in a form suitable for SoPlex.Class SPxLPBase provides the data structures required for sa...
Definition: spxlpbase.h:56
virtual R maxColRatio(const SPxLPBase< R > &lp) const
maximum ratio between absolute biggest and smallest element in any column.
bool m_colFirst
do column scaling first
Definition: spxscaler.h:87
SPxScaler(const char *name, bool colFirst=false, bool doBoth=true, SPxOut *spxout=NULL)
constructor
virtual R getColMinAbsUnscaled(const SPxLPBase< R > &lp, int i) const
returns minumum absolute value of unscaled column i
virtual R minAbsColscale() const
absolute smallest column scaling factor
virtual void getRhsUnscaled(const SPxLPBase< R > &lp, VectorBase< R > &vec) const
gets unscaled right hand side vector
virtual void unscaleRedCost(const SPxLPBase< R > &lp, VectorBase< R > &r) const
unscale dense reduced cost vector given in r.
Dynamic sparse vectors.
virtual R minAbsRowscale() const
absolute smallest row scaling factor
virtual void getLowerUnscaled(const SPxLPBase< R > &lp, VectorBase< R > &vec) const
gets unscaled lower bound vector
LP scaler abstract base class.Instances of classes derived from SPxScaler may be loaded to SoPlex in ...
Definition: spxscaler.h:77
virtual int computeScaleExp(const SVectorBase< R > &vec, const DataArray< int > &oldScaleExp) const
compute a single scaling vector , e.g. of a newly added row
virtual bool isConsistent() const
consistency check
virtual void unscalePrimal(const SPxLPBase< R > &lp, VectorBase< R > &x) const
unscale dense primal solution vector given in x.
virtual R scaleLower(const SPxLPBase< R > &lp, int col, R lower) const
returns scaled lower bound of column col.
Set of sparse vectors.
Sparse vectors.Class SVectorBase provides packed sparse vectors. Such are a sparse vectors...
Definition: ssvectorbase.h:34
virtual R lowerUnscaled(const SPxLPBase< R > &lp, int i) const
returns unscaled lower bound i
bool m_doBoth
do columns and rows
Definition: spxscaler.h:88
virtual void getMaxObjUnscaled(const SPxLPBase< R > &lp, VectorBase< R > &vec) const
gets unscaled objective function
virtual R scaleRhs(const SPxLPBase< R > &lp, int row, R rhs) const
returns scaled right hand side of row row.
Save arrays of data objects.
virtual R getColMaxAbsUnscaled(const SPxLPBase< R > &lp, int i) const
returns maximum absolute value of unscaled column i
virtual void unscale(SPxLPBase< R > &lp)
unscale SPxLP
const char * m_name
Name of the scaler.
Definition: spxscaler.h:84
virtual R getRowMinAbsUnscaled(const SPxLPBase< R > &lp, int i) const
returns minimum absolute value of unscaled row i
virtual void setRealParam(R param, const char *name="realparam")
set R parameter
virtual int getRowScaleExp(int i) const
returns scaling factor for row i