Scippy

SoPlex

Sequential object-oriented simPlex

lprowbase.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-2016 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 lprowbase.h
17  * @brief (In)equality for LPs.
18  */
19 #ifndef _LPROWBASE_H_
20 #define _LPROWBASE_H_
21 
22 #include <assert.h>
23 
24 #include "spxdefines.h"
25 #include "basevectors.h"
26 
27 namespace soplex
28 {
29 /**@brief (In)equality for LPs.
30  * @ingroup Algo
31  *
32  * Class LPRowBase provides constraints for linear programs in the form \f[ l \le a^Tx \le r, \f] where \em a is a
33  * DSVector. \em l is referred to as %left hand side, \em r as %right hand side and \em a as \em row \em vector or the
34  * constraint vector. \em l and \em r may also take values \f$\pm\f$ #infinity. This static member is predefined, but
35  * may be overridden to meet the needs of the LP solver to be used.
36  *
37  * LPRowBases allow to specify regular inequalities of the form \f[ a^Tx \sim \alpha, \f] where \f$\sim\f$ can take any
38  * value of \f$\le, =, \ge\f$, by setting rhs and lhs to the same value or setting one of them to \f$\infty\f$.
39  *
40  * Since constraints in the regular form occur often, LPRowBases offers methods type() and value() for retreiving
41  * \f$\sim\f$ and \f$\alpha\f$ of an LPRowBase in this form, respectively. Also, a constructor for LPRowBases given in
42  * regular form is provided.
43  */
44 template < class R >
45 class LPRowBase
46 {
47  template < class S > friend class LPRowBase;
48 
49 private:
50 
51  // ------------------------------------------------------------------------------------------------------------------
52  /**@name Data */
53  //@{
54 
55  R left; ///< left-hand side of the constraint
56  R right; ///< right-hand side of the constraint
57  R object; ///< objective coefficient of corresponding slack variable s = vec times primal
58  DSVectorBase<R> vec; ///< the row vector
59 
60  //@}
61 
62 public:
63 
64  // ------------------------------------------------------------------------------------------------------------------
65  /**@name Types */
66  //@{
67 
68  /// (In)Equality type of an LP row.
69  /** #LPRowBase%s may be of one of the following Types. This datatype may be used for constructing new #LPRowBase%s in the
70  * regular form.
71  */
72  enum Type
73  {
74  LESS_EQUAL, ///< \f$a^Tx \le \alpha\f$.
75  EQUAL, ///< \f$a^Tx = \alpha\f$.
76  GREATER_EQUAL, ///< \f$a^Tx \ge \alpha\f$.
77  RANGE ///< \f$\lambda \le a^Tx \le \rho\f$.
78  };
79 
80  //@}
81 
82  // ------------------------------------------------------------------------------------------------------------------
83  /**@name Construction / destruction */
84  //@{
85 
86  /// Constructs LPRowBase with a vector ready to hold \p defDim nonzeros.
87  explicit LPRowBase<R>(int defDim = 0)
88  : left(0), right(infinity), object(0), vec(defDim)
89  {
90  assert(isConsistent());
91  }
92 
93  /// Copy constructor.
95  : left(row.left), right(row.right), object(row.object), vec(row.vec)
96  {
97  assert(isConsistent());
98  }
99 
100  /// Copy constructor.
101  template < class S >
103  : left(row.left), right(row.right), object(row.object), vec(row.vec)
104  {
105  assert(isConsistent());
106  }
107 
108  /// Constructs LPRowBase with the given left-hand side, right-hand side and rowVector.
109  LPRowBase<R>(const R& p_lhs, const SVectorBase<R>& p_rowVector, const R& p_rhs, const R& p_obj = 0)
110  : left(p_lhs), right(p_rhs), object(p_obj), vec(p_rowVector)
111  {
112  assert(isConsistent());
113  }
114 
115  /// Constructs LPRowBase from passed \p rowVector, \p type and \p value.
116  LPRowBase<R>(const SVectorBase<R>& p_rowVector, Type p_type, const R& p_value, const R& p_obj = 0)
117  : object(p_obj), vec(p_rowVector)
118  {
119  switch( p_type )
120  {
121  case LESS_EQUAL:
122  left = -infinity;
123  right = p_value;
124  break;
125  case EQUAL:
126  left = p_value;
127  right = p_value;
128  break;
129  case GREATER_EQUAL:
130  left = p_value;
131  right = infinity;
132  break;
133  default:
134  throw SPxInternalCodeException("XLPROW03 This should never happen.");
135  }
136 
137  assert(isConsistent());
138  }
139 
140  /// Destructor.
142  {}
143 
144  //@}
145 
146  // ------------------------------------------------------------------------------------------------------------------
147  /**@name Access / modification */
148  //@{
149 
150  /// Gets type of row.
151  Type type() const
152  {
153  if( rhs() >= infinity )
154  return GREATER_EQUAL;
155 
156  if( lhs() <= -infinity )
157  return LESS_EQUAL;
158 
159  if( lhs() == rhs() )
160  return EQUAL;
161 
162  return RANGE;
163  }
164 
165  /// Sets type of (in)equality.
166  void setType(Type p_type)
167  {
168  switch( p_type )
169  {
170  case LESS_EQUAL:
171  left = -infinity;
172  break;
173  case EQUAL:
174  if( lhs() > -infinity )
175  right = lhs();
176  else
177  left = rhs();
178  break;
179  case GREATER_EQUAL:
180  right = infinity;
181  break;
182  case RANGE:
183  MSG_ERROR( std::cerr << "ELPROW01 RANGE not supported in LPRow::setType()"
184  << std::endl; )
185  throw SPxInternalCodeException("XLPROW01 This should never happen.");
186  default:
187  throw SPxInternalCodeException("XLPROW02 This should never happen.");
188  }
189  }
190 
191  /// Right hand side value of (in)equality.
192  /** This method returns \f$\alpha\f$ for a LPRowBase in regular form. However, value() may only be called for
193  * LPRowBase%s with type() != \c RANGE.
194  */
195  R value() const
196  {
197  assert(type() != RANGE);
198 
199  return (rhs() < infinity) ? rhs() : lhs();
200  }
201 
202  /// Left-hand side value.
203  R lhs() const
204  {
205  return left;
206  }
207 
208  /// Sets left-hand side value.
209  void setLhs(const R& p_left)
210  {
211  left = p_left;
212  }
213 
214  /// Right-hand side value.
215  R rhs() const
216  {
217  return right;
218  }
219 
220  /// Sets right-hand side value.
221  void setRhs(const R& p_right)
222  {
223  right = p_right;
224  }
225 
226  /// Objective coefficient value.
227  R obj() const
228  {
229  return object;
230  }
231 
232  /// Sets objective coefficient value.
233  void setObj(const R& p_obj)
234  {
235  object = p_obj;
236  }
237 
238  /// Constraint row vector.
239  const SVectorBase<R>& rowVector() const
240  {
241  return vec;
242  }
243 
244  /// access constraint row vector.
245  void setRowVector(const DSVectorBase<R>& p_vec)
246  {
247  vec = p_vec;
248  }
249 
250  //@}
251 
252  // ------------------------------------------------------------------------------------------------------------------
253  /**@name Consistency check */
254  //@{
255 
256  /// Checks consistency.
257  bool isConsistent() const
258  {
259 #ifdef ENABLE_CONSISTENCY_CHECKS
260  return vec.isConsistent();
261 #else
262  return true;
263 #endif
264  }
265 
266  //@}
267 };
268 
269 } // namespace soplex
270 #endif // _LPROWBASE_H_
Exception class for things that should NEVER happen.This class is derived from the SoPlex exception b...
Definition: exceptions.h:109
bool isConsistent() const
Checks consistency.
Definition: lprowbase.h:257
Dynamic sparse vectors.Class DSVectorBase implements dynamic sparse vectors, i.e. SVectorBases with a...
Definition: dsvectorbase.h:42
~LPRowBase()
Destructor.
Definition: lprowbase.h:141
void setType(Type p_type)
Sets type of (in)equality.
Definition: lprowbase.h:166
DSVectorBase< R > vec
the row vector
Definition: lprowbase.h:58
bool isConsistent() const
Consistency check.
Definition: dsvectorbase.h:289
R value() const
Right hand side value of (in)equality.
Definition: lprowbase.h:195
const SVectorBase< R > & rowVector() const
Constraint row vector.
Definition: lprowbase.h:239
#define MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::ERROR.
Definition: spxdefines.h:109
void setObj(const R &p_obj)
Sets objective coefficient value.
Definition: lprowbase.h:233
(In)equality for LPs.Class LPRowBase provides constraints for linear programs in the form where a is...
Definition: lprowbase.h:45
Debugging, floating point type and parameter definitions.
void setLhs(const R &p_left)
Sets left-hand side value.
Definition: lprowbase.h:209
Collection of dense, sparse, and semi-sparse vectors.
Everything should be within this namespace.
R obj() const
Objective coefficient value.
Definition: lprowbase.h:227
Type
(In)Equality type of an LP row.
Definition: lprowbase.h:72
void setRowVector(const DSVectorBase< R > &p_vec)
access constraint row vector.
Definition: lprowbase.h:245
R right
right-hand side of the constraint
Definition: lprowbase.h:56
R lhs() const
Left-hand side value.
Definition: lprowbase.h:203
R object
objective coefficient of corresponding slack variable s = vec times primal
Definition: lprowbase.h:57
const Real infinity
Definition: spxdefines.cpp:26
R left
left-hand side of the constraint
Definition: lprowbase.h:55
Sparse vectors.Class SVectorBase provides packed sparse vectors. Such are a sparse vectors...
Definition: dvectorbase.h:31
void setRhs(const R &p_right)
Sets right-hand side value.
Definition: lprowbase.h:221
R rhs() const
Right-hand side value.
Definition: lprowbase.h:215
Type type() const
Gets type of row.
Definition: lprowbase.h:151