SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
VariableBounds.h
Go to the documentation of this file.
1 /**
2  * @file VariableBounds.h
3  * @author Florian Corzilius
4  * @since 2012-10-04
5  * @version 2013-02-05
6  */
7 
8 #pragma once
9 
10 #include <smtrat-common/model.h>
11 #include <iomanip>
12 
13 namespace smtrat
14 {
15  namespace vb
16  {
17  // Forward declaration
18  template <typename T> class Variable;
19 
20  /**
21  * Class for the bound of a variable.
22  */
23  template <typename T> class Bound
24  {
25  public:
26  /// The type of a bounds.
28 
29  private:
30  /// The type of this bound.
32  /// A pointer to bound value, which is plus or minus infinity if the pointer is NULL.
34  /// The variable for which the bound is declared.
36  /// A set of origins of the bound, e.g., x-3<0 is the origin of the bound <3.
37  std::set<T,carl::less<T,false>>* const mpOrigins; // Here, we cannot use the carl::PointerSet, which falls back on the comparison operator
38  // of T, as we must ensure to store for every pointer to T one origin.
39 
40  public:
41 
42  /**
43  * Constructs this bound.
44  * @param _limit A pointer to the limit (rational) of the bound. It is NULL, if the limit is not finite.
45  * @param _variable The variable to which this bound belongs.
46  * @param _type The type of the bound (either it is an equal bound or it is weak resp. strict and upper resp. lower)
47  */
48  Bound( Rational* const _limit, Variable<T>* const _variable, Type _type );
49 
50  /**
51  * Destructs this bound.
52  */
53  ~Bound();
54 
55  /**
56  * Checks whether the this bound is smaller than the given one.
57  * @param _bound The bound to compare with.
58  * @return true, if for this bound (A) and the given bound (B) it holds that:
59  * A is not inf and B is inf,
60  * A smaller than B, where A and B are rationals,
61  * A equals B, where A and B are rationals, but A is an equal bound but B is not;
62  * false, otherwise.
63  */
64  bool operator<( const Bound<T>& _bound ) const;
65 
66  /**
67  * Prints the bound on the given output stream.
68  * @param _out The output stream to print on.
69  * @param _bound The bound to print.
70  * @return The output stream after printing the bound on it.
71  */
72  template <typename T1> friend std::ostream& operator<<( std::ostream& _out, const Bound<T1>& _bound );
73 
74  /**
75  * Prints this bound on the given stream.
76  * @param _out The stream to print on.
77  * @param _withRelation A flag indicating whether to print also a relation symbol in front of the bound value.
78  */
79  void print( std::ostream& _out, bool _withRelation = false ) const;
80 
81 
82  /**
83  * @return A constant reference to the value of the limit. Note, that it must be ensured that
84  * the bound is finite before calling this method.
85  */
86  const Rational& limit() const
87  {
88  return *mpLimit;
89  }
90 
91  /**
92  * @return A pointer to the limit of this bound.
93  */
94  const Rational* pLimit() const
95  {
96  return mpLimit;
97  }
98 
99  /**
100  * @return true, if the bound infinite;
101  * false, otherwise.
102  */
103  bool isInfinite() const
104  {
105  return mpLimit == NULL;
106  }
107 
108  /**
109  * @return The type of this bound.
110  */
111  Type type() const
112  {
113  return mType;
114  }
115 
116  /**
117  * @return true, if the bound is an upper bound;
118  * false, otherwise.
119  */
120  bool isUpperBound() const
121  {
122  return mType > WEAK_LOWER_BOUND;
123  }
124 
125  /**
126  * @return true, if the bound is a lower bound;
127  * false, otherwise.
128  */
129  bool isLowerBound() const
130  {
131  return mType < WEAK_UPPER_BOUND;
132  }
133 
134  /**
135  * @return A pointer to the variable wrapper considered by this bound.
136  */
138  {
139  return mpVariable;
140  }
141 
142  /**
143  * @return A constant reference to the variable wrapper considered by this bound.
144  */
145  const Variable<T>& variable() const
146  {
147  return *mpVariable;
148  }
149 
150  /**
151  * @return true, if this bound is active, which means that there is at least one origin for it left.
152  * Note, that origins can be removed belatedly.
153  * false, otherwise.
154  */
155  bool isActive() const
156  {
157  return !mpOrigins->empty();
158  }
159 
160  /**
161  * Adds an origin to this bound.
162  * @param _origin The origin to add.
163  * @return true, if this has activated this bound;
164  * false, if the bound was already active before.
165  */
166  bool activate( const T& _origin ) const
167  {
168  assert(mpOrigins->find(_origin) == mpOrigins->end());
169  mpOrigins->insert( _origin );
170  return mpOrigins->size() == 1;
171  }
172 
173  /**
174  * Removes an origin from this bound.
175  * @param _origin The origin to add.
176  * @return true, if this has deactivated this bound;
177  * false, if the bound was already inactive before.
178  */
179  bool deactivate( const T& _origin ) const
180  {
181  assert( mpOrigins->find( _origin ) != mpOrigins->end() );
182  mpOrigins->erase( _origin );
183  return mpOrigins->empty();
184  }
185 
186  /**
187  * @return A constant reference to the set of origins of this bound.
188  */
189  const std::set<T,carl::less<T,false>>& origins() const
190  {
191  return *mpOrigins;
192  }
193  };
194 
195  /**
196  * Class for a variable.
197  */
198  template <typename T> class Variable
199  {
200  /**
201  * Compares two pointers showing to bounds.
202  */
204  {
205  /**
206  * @param pBoundA A pointer to a bound.
207  * @param pBoundB A pointer to a bound.
208  * @return true, if the bound, the first pointer shows to is less than the bound the second pointer shows to.
209  */
210  bool operator ()( const Bound<T>* const pBoundA, const Bound<T>* const pBoundB ) const
211  {
212  return (*pBoundA) < (*pBoundB);
213  }
214  };
215 
216  /// A set of bounds.
217  typedef std::set<const Bound<T>*, boundPointerComp> BoundSet;
218 
219  private:
220  /// A flag that indicates that the stored exact interval of this variable is up to date.
221  mutable bool mUpdatedExactInterval;
222  /// A flag that indicates that the stored double interval of this variable is up to date.
224  /// The least upper bound of this variable.
226  /// The greatest lower bound of this variable.
228  /// The set of all upper bounds of this variable.
230  /// The set of all lower bounds of this variable.
232 
233  public:
234  /**
235  * Constructs this variable.
236  */
238 
239  /*
240  * Destructs this variable.
241  */
243 
244  /**
245  * @return true, if there is a conflict;
246  * false, otherwise.
247  */
248  bool conflicting() const;
249 
250  /**
251  * Adds the bound corresponding to the constraint to the given variable. The constraint
252  * is expected to contain only one variable and this one only linearly.
253  * @param _constraint A pointer to a constraint of the form ax~b.
254  * @param _var Hence, the variable x.
255  * @param _origin The origin of the constraint. This is could be the constraint itself or anything else
256  * in the data structure which uses this object.
257  * @param _limit
258  * @return The added bound.
259  */
260  const Bound<T>* addBound( const ConstraintT& _constraint, const carl::Variable& _var, const T& _origin );
261 
262  /**
263  * Updates the infimum and supremum of this variable.
264  * @param _changedBound The bound, for which we certainly know that it got deactivated before.
265  * @return true, if there is a conflict;
266  * false, otherwise.
267  */
268  bool updateBounds( const Bound<T>& _changedBound );
269 
270  /**
271  * @return true, if the stored exact interval representing the variable's bounds is up to date.
272  * false, otherwise
273  */
274  bool updatedExactInterval() const
275  {
276  return mUpdatedExactInterval;
277  }
278 
279  /**
280  * Sets the flag indicating that the stored exact interval representing the variable's bounds is up to date to false.
281  */
283  {
284  mUpdatedExactInterval = false;
285  }
286 
287  /**
288  * @return true, if the stored double interval representing the variable's bounds is up to date.
289  * false, otherwise
290  */
292  {
293  return mUpdatedDoubleInterval;
294  }
295 
296  /**
297  * Sets the flag indicating that the stored double interval representing the variable's bounds is up to date to false.
298  */
300  {
301  mUpdatedDoubleInterval = false;
302  }
303 
304  /**
305  * @return A pointer to the supremum of this variable.
306  */
307  const Bound<T>* pSupremum() const
308  {
309  return mpSupremum;
310  }
311 
312  /**
313  * @return A constant reference to the supremum of this variable.
314  */
315  const Bound<T>& supremum() const
316  {
317  return *mpSupremum;
318  }
319 
320  /**
321  * @return A pointer to the infimum of this variable.
322  */
323  const Bound<T>* pInfimum() const
324  {
325  return mpInfimum;
326  }
327 
328  /**
329  * @return A constant reference to the infimum of this variable.
330  */
331  const Bound<T>& infimum() const
332  {
333  return *mpInfimum;
334  }
335 
336  /**
337  * @return A constant reference to the set of upper bounds of this variable.
338  */
339  const BoundSet& upperbounds() const
340  {
341  return mUpperbounds;
342  }
343 
344  /**
345  * @return A constant reference to the set of lower bounds of this variable.
346  */
347  const BoundSet& lowerbounds() const
348  {
349  return mLowerbounds;
350  }
351  };
352 
353  /**
354  * Class to manage the bounds of a variable.
355  */
356  template <typename T> class VariableBounds
357  {
358  public:
359  /// A map from Constraint pointers to Bound pointers.
360  typedef std::map<ConstraintT,const Bound<T>*> ConstraintBoundMap;
361  /// A hash-map from arithmetic variables to variables managing the bounds.
362  typedef carl::FastMap<carl::Variable, Variable<T>*> VariableMap;
363  private:
364  /// A pointer to one of the conflicting variables (its supremum is smaller than its infimum)
365  /// or NULL if there is no conflict.
367  /// A mapping from arithmetic variables to the variable objects storing the bounds.
369  /// A mapping from constraint pointer to the corresponding bounds.
371  /// The stored exact interval map representing the currently tightest bounds.
372  /// Note, that it is updated on demand.
374  /// The stored double interval map representing the currently tightest bounds.
375  /// Note, that it is updated on demand.
377  /// Stores the constraints which cannot be used to infer a bound for a variable.
379  /// Stores deductions which this variable bounds manager has detected.
380  mutable std::unordered_set<std::vector<ConstraintT>> mBoundDeductions;
381  public:
382  /**
383  * Constructs a variable bounds manager.
384  */
386 
387  /**
388  * Destructs a variable bounds manager.
389  */
391  void clear();
392 
393  bool empty() const
394  {
395  return mpConstraintBoundMap->empty();
396  }
397 
398  /**
399  * Updates the variable bounds by the given constraint.
400  * @param _constraint The constraint to consider.
401  * @param _origin The origin of the given constraint.
402  * @return true, if the variable bounds have been changed;
403  * false, otherwise.
404  */
405  bool addBound( const ConstraintT& _constraint, const T& _origin );
406 
407  bool addBound( const FormulaT& _formula, const T& _origin );
408 
409  /**
410  * Removes all effects the given constraint has on the variable bounds.
411  * @param _constraint The constraint, which effects shall be undone for the variable bounds.
412  * @param _origin The origin of the given constraint.
413  * @return 2, if the variables supremum or infimum have been changed;
414  * 1, if the constraint was a (not the strictest) bound;
415  * 0, otherwise.
416  */
417  unsigned removeBound( const ConstraintT& _constraint, const T& _origin );
418 
419  unsigned removeBound( const FormulaT& _formula, const T& _origin );
420 
421  /**
422  * Removes all effects the given constraint has on the variable bounds.
423  * @param _constraint The constraint, which effects shall be undone for the variable bounds.
424  * @param _origin The origin of the given constraint.
425  * @param _changedVariable The variable whose interval domain has been changed, if the return value is 2.
426  * @return 2, if the variables supremum or infimum have been changed;
427  * 1, if the constraint was a (not the strictest) bound;
428  * 0, otherwise.
429  */
430  unsigned removeBound( const ConstraintT& _constraint, const T& _origin, carl::Variable*& _changedVariable );
431 
432  /**
433  * Creates an evalintervalmap corresponding to the variable bounds.
434  * @return The variable bounds as an evalintervalmap.
435  */
437 
438  /**
439  * Creates an interval corresponding to the variable bounds of the given variable.
440  * @param _var The variable to compute the variable bounds as interval for.
441  * @return The variable bounds as an interval.
442  */
443  const RationalInterval& getInterval( const carl::Variable& _var ) const;
444 
445  /**
446  * Creates an interval corresponding to the bounds of the given monomial.
447  * @param _mon The monomial to compute the bounds as interval for.
448  * @return The monomial bounds as an interval.
449  */
450  RationalInterval getInterval( carl::Monomial::Arg _mon ) const;
451 
452  /**
453  * Creates an interval corresponding to the bounds of the given term.
454  * @param _term The term to compute the bounds as interval for.
455  * @return The term bounds as an interval.
456  */
457  RationalInterval getInterval( const TermT& _term ) const;
458 
459  /**
460  * Creates an interval map corresponding to the variable bounds.
461  * @return The variable bounds as an interval map.
462  */
464 
465  /**
466  * Creates a double interval corresponding to the variable bounds of the given variable.
467  * @param _var The variable to compute the variable bounds as double interval for.
468  * @return The variable bounds as a double interval.
469  */
470  const carl::Interval<double>& getDoubleInterval( const carl::Variable& _var ) const;
471 
472  /**
473  * @param _var The variable to get origins of the bounds for.
474  * @return The origin constraints of the supremum and infimum of the given variable.
475  */
476  std::vector<T> getOriginsOfBounds( const carl::Variable& _var ) const;
477  std::set<T> getOriginSetOfBounds( const carl::Variable& _var ) const;
478 
479  /**
480  * @param _variables The variables to get origins of the bounds for.
481  * @return The origin constraints of the supremum and infimum of the given variables.
482  */
483  std::vector<T> getOriginsOfBounds( const carl::Variables& _variables ) const;
484  std::set<T> getOriginSetOfBounds( const carl::Variables& _variables ) const;
485 
486  /**
487  * Collect the origins to the supremums and infimums of all variables.
488  * @return A set of origins corresponding to the supremums and infimums of all variables.
489  */
490  std::vector<T> getOriginsOfBounds() const;
491  std::set<T> getOriginSetOfBounds() const;
492 
493  /**
494  * @return The deductions which this variable bounds manager has detected.
495  */
496  std::vector<std::pair<std::vector<ConstraintT>, ConstraintT>> getBoundDeductions() const;
497 
498  /**
499  * Prints the variable bounds.
500  * @param _out The output stream to print on.
501  * @param _init A string which is displayed at the beginning of every row.
502  * @param _printAllBounds A flag that indicates whether to print also the bounds of each variable (=true).
503  */
504  void print( std::ostream& _out = std::cout, const std::string _init = "", bool _printAllBounds = false ) const;
505 
506  /**
507  * @return true, if there is a conflicting variable;
508  * false, otherwise.
509  */
510  bool isConflicting() const
511  {
512  return mpConflictingVariable != NULL;
513  }
514 
515  /**
516  * @return The origins which cause the conflict. This method can only be called, if there is a conflict.
517  */
518  std::set<T> getConflict() const
519  {
520  assert( isConflicting() );
521  assert( !mpConflictingVariable->infimum().isInfinite() && !mpConflictingVariable->supremum().isInfinite() );
522  std::set<T> conflict;
523  conflict.insert( *mpConflictingVariable->infimum().origins().begin() );
524  conflict.insert( *mpConflictingVariable->supremum().origins().begin() );
525  return conflict;
526  }
527  };
528 
529  /**
530  * Prints the contents of the given variable bounds manager to the given stream.
531  * @param _os The stream to print on.
532  * @param _vs The variable bounds manager to print.
533  * @return The stream after printing on it.
534  */
535  template<typename Type>
536  inline std::ostream& operator<<( std::ostream& _os, const VariableBounds<Type>& _vs )
537  {
538  _vs.print(_os);
539  return _os;
540  }
541  } // namespace vb
542 } // namespace smtrat
543 
544 #include "VariableBounds.tpp"
Class for the bound of a variable.
Type
The type of a bounds.
bool isInfinite() const
bool isLowerBound() const
void print(std::ostream &_out, bool _withRelation=false) const
Prints this bound on the given stream.
Rational * mpLimit
A pointer to bound value, which is plus or minus infinity if the pointer is NULL.
const Rational * pLimit() const
bool isActive() const
Variable< T > * pVariable() const
Variable< T > *const mpVariable
The variable for which the bound is declared.
bool isUpperBound() const
bool activate(const T &_origin) const
Adds an origin to this bound.
const Variable< T > & variable() const
~Bound()
Destructs this bound.
bool deactivate(const T &_origin) const
Removes an origin from this bound.
Type mType
The type of this bound.
friend std::ostream & operator<<(std::ostream &_out, const Bound< T1 > &_bound)
Prints the bound on the given output stream.
Bound(Rational *const _limit, Variable< T > *const _variable, Type _type)
Constructs this bound.
std::set< T, carl::less< T, false > > *const mpOrigins
A set of origins of the bound, e.g., x-3<0 is the origin of the bound <3.
Type type() const
bool operator<(const Bound< T > &_bound) const
Checks whether the this bound is smaller than the given one.
const Rational & limit() const
const std::set< T, carl::less< T, false > > & origins() const
Class to manage the bounds of a variable.
std::set< T > getOriginSetOfBounds(const carl::Variable &_var) const
EvalRationalIntervalMap mEvalIntervalMap
The stored exact interval map representing the currently tightest bounds.
std::set< T > getOriginSetOfBounds() const
std::vector< T > getOriginsOfBounds(const carl::Variables &_variables) const
bool addBound(const FormulaT &_formula, const T &_origin)
VariableMap * mpVariableMap
A mapping from arithmetic variables to the variable objects storing the bounds.
const EvalRationalIntervalMap & getEvalIntervalMap() const
Creates an evalintervalmap corresponding to the variable bounds.
std::unordered_set< std::vector< ConstraintT > > mBoundDeductions
Stores deductions which this variable bounds manager has detected.
unsigned removeBound(const FormulaT &_formula, const T &_origin)
VariableBounds()
Constructs a variable bounds manager.
Variable< T > * mpConflictingVariable
A pointer to one of the conflicting variables (its supremum is smaller than its infimum) or NULL if t...
ConstraintsT mNonBoundConstraints
Stores the constraints which cannot be used to infer a bound for a variable.
void print(std::ostream &_out=std::cout, const std::string _init="", bool _printAllBounds=false) const
Prints the variable bounds.
std::set< T > getOriginSetOfBounds(const carl::Variables &_variables) const
std::map< ConstraintT, const Bound< T > * > ConstraintBoundMap
A map from Constraint pointers to Bound pointers.
const smtrat::EvalDoubleIntervalMap & getIntervalMap() const
Creates an interval map corresponding to the variable bounds.
unsigned removeBound(const ConstraintT &_constraint, const T &_origin)
Removes all effects the given constraint has on the variable bounds.
const carl::Interval< double > & getDoubleInterval(const carl::Variable &_var) const
Creates a double interval corresponding to the variable bounds of the given variable.
std::vector< std::pair< std::vector< ConstraintT >, ConstraintT > > getBoundDeductions() const
RationalInterval getInterval(const TermT &_term) const
Creates an interval corresponding to the bounds of the given term.
std::vector< T > getOriginsOfBounds() const
Collect the origins to the supremums and infimums of all variables.
unsigned removeBound(const ConstraintT &_constraint, const T &_origin, carl::Variable *&_changedVariable)
Removes all effects the given constraint has on the variable bounds.
ConstraintBoundMap * mpConstraintBoundMap
A mapping from constraint pointer to the corresponding bounds.
carl::FastMap< carl::Variable, Variable< T > * > VariableMap
A hash-map from arithmetic variables to variables managing the bounds.
EvalDoubleIntervalMap mDoubleIntervalMap
The stored double interval map representing the currently tightest bounds.
std::set< T > getConflict() const
~VariableBounds()
Destructs a variable bounds manager.
RationalInterval getInterval(carl::Monomial::Arg _mon) const
Creates an interval corresponding to the bounds of the given monomial.
bool addBound(const ConstraintT &_constraint, const T &_origin)
Updates the variable bounds by the given constraint.
const RationalInterval & getInterval(const carl::Variable &_var) const
Creates an interval corresponding to the variable bounds of the given variable.
std::vector< T > getOriginsOfBounds(const carl::Variable &_var) const
Class for a variable.
std::set< const Bound< T > *, boundPointerComp > BoundSet
A set of bounds.
bool updatedExactInterval() const
const Bound< T > * pInfimum() const
const Bound< T > & infimum() const
bool updateBounds(const Bound< T > &_changedBound)
Updates the infimum and supremum of this variable.
BoundSet mLowerbounds
The set of all lower bounds of this variable.
BoundSet mUpperbounds
The set of all upper bounds of this variable.
bool conflicting() const
const Bound< T > * mpInfimum
The greatest lower bound of this variable.
const Bound< T > * addBound(const ConstraintT &_constraint, const carl::Variable &_var, const T &_origin)
Adds the bound corresponding to the constraint to the given variable.
bool mUpdatedDoubleInterval
A flag that indicates that the stored double interval of this variable is up to date.
void doubleIntervalHasBeenUpdated() const
Sets the flag indicating that the stored double interval representing the variable's bounds is up to ...
const Bound< T > & supremum() const
Variable()
Constructs this variable.
const Bound< T > * mpSupremum
The least upper bound of this variable.
bool mUpdatedExactInterval
A flag that indicates that the stored exact interval of this variable is up to date.
const BoundSet & upperbounds() const
const Bound< T > * pSupremum() const
void exactIntervalHasBeenUpdated() const
Sets the flag indicating that the stored exact interval representing the variable's bounds is up to d...
bool updatedDoubleInterval() const
const BoundSet & lowerbounds() const
std::ostream & operator<<(std::ostream &_os, const VariableBounds< Type > &_vs)
Prints the contents of the given variable bounds manager to the given stream.
Class to create the formulas for axioms.
carl::Constraints< Poly > ConstraintsT
Definition: types.h:31
carl::Term< Rational > TermT
Definition: types.h:23
carl::Formula< Poly > FormulaT
Definition: types.h:37
std::map< carl::Variable, RationalInterval > EvalRationalIntervalMap
Definition: model.h:30
carl::Interval< Rational > RationalInterval
Definition: model.h:27
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
mpq_class Rational
Definition: types.h:19
std::map< carl::Variable, DoubleInterval > EvalDoubleIntervalMap
Definition: model.h:29
Compares two pointers showing to bounds.
bool operator()(const Bound< T > *const pBoundA, const Bound< T > *const pBoundB) const