SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Variable.h
Go to the documentation of this file.
1 /**
2  * @file Variable.h
3  * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
4  * @since 2012-04-05
5  * @version 2014-10-01
6  */
7 
8 #pragma once
9 
10 #define LRA_NO_DIVISION
11 
12 #include "Bound.h"
14 #include <sstream>
15 #include <iomanip>
16 #include <list>
17 
18 namespace smtrat
19 {
20  namespace lra
21  {
22  ///
23  typedef size_t EntryID;
24  ///
25  static EntryID LAST_ENTRY_ID = 0;
26 
27  template<typename T1, typename T2>
28  class Variable
29  {
30  private:
31  ///
32  bool mBasic;
33  ///
34  bool mOriginal;
35  ///
36  bool mInteger;
37  ///
39  ///
40  size_t mSize;
41  ///
43  ///
44  size_t mPosition;
45  ///
46  typename std::list<std::list<std::pair<Variable<T1,T2>*,T2>>>::iterator mPositionInNonActives;
47  ///
48  size_t mId;
49  ///
51  ///
53  ///
55  ///
57  ///
58  const typename Poly::PolyType* mExpression;
59  ///
61  ///
63  #ifdef LRA_NO_DIVISION
64  ///
65  T2 mFactor;
66  #endif
67 
68  public:
69  /**
70  *
71  * @param _position
72  * @param _expression
73  * @param _defaultBoundPosition
74  * @param _isInteger
75  * @param _id
76  */
77  Variable( size_t _position, const typename Poly::PolyType* _expression, ModuleInput::iterator _defaultBoundPosition, bool _isInteger, size_t _id );
78 
79  /**
80  *
81  * @param _positionInNonActives
82  * @param _expression
83  * @param _defaultBoundPosition
84  * @param _isInteger
85  * @param _id
86  */
87  Variable( typename std::list<std::list<std::pair<Variable<T1,T2>*,T2>>>::iterator _positionInNonActives, const typename Poly::PolyType* _expression, ModuleInput::iterator _defaultBoundPosition, bool _isInteger, size_t _id );
88 
89  /**
90  *
91  */
92  virtual ~Variable();
93 
94  /**
95  * @return
96  */
97  const Value<T1>& assignment() const
98  {
99  return mAssignment;
100  }
101 
102  /**
103  * @return
104  */
106  {
107  return mAssignment;
108  }
109 
110  /**
111  *
112  */
114  {
116  }
117 
118  /**
119  * @return
120  */
122  {
124  }
125 
126  /**
127  *
128  * @param _basic
129  */
130  void setBasic( bool _basic )
131  {
132  mBasic = _basic;
133  }
134 
135  /**
136  * @return
137  */
138  bool isBasic() const
139  {
140  return mBasic;
141  }
142 
143  /**
144  * @return
145  */
146  bool isOriginal() const
147  {
148  return mOriginal;
149  }
150 
151  /**
152  * @return
153  */
154  bool is_integer() const
155  {
156  return mInteger;
157  }
158 
159  /**
160  * @return
161  */
162  bool hasBound() const
163  {
164  return !(mpInfimum->isInfinite() && mpSupremum->isInfinite());
165  }
166 
167  /**
168  * @return
169  */
170  bool involvesEquation() const
171  {
172  return !mpInfimum->isInfinite() && mpInfimum->type() == Bound<T1,T2>::EQUAL;
173  }
174 
175  /**
176  * @return
177  */
179  {
180  return mStartEntry;
181  }
182 
183  /**
184  * @return
185  */
187  {
188  return mStartEntry;
189  }
190 
191  /**
192  * @return
193  */
194  size_t size() const
195  {
196  return mSize;
197  }
198 
199  /**
200  * @return
201  */
202  size_t& rSize()
203  {
204  return mSize;
205  }
206 
207  /**
208  * @return
209  */
210  double conflictActivity() const
211  {
212  return mConflictActivity;
213  }
214 
215  /**
216  *
217  * @param _supremum
218  */
219  void setSupremum( const Bound<T1, T2>* _supremum )
220  {
221  assert( _supremum->isActive() );
222  assert( mpSupremum->isActive() );
223  if( !mpSupremum->isInfinite() )
224  --mpSupremum->pInfo()->updated;
225  ++_supremum->pInfo()->updated;
226  mpSupremum = _supremum;
227  }
228 
229  /**
230  * @return
231  */
232  const Bound<T1, T2>* pSupremum() const
233  {
234 // assert( !mpSupremum->origins().empty() );
235  return mpSupremum;
236  }
237 
238  /**
239  * @return
240  */
241  const Bound<T1, T2>& supremum() const
242  {
243  assert( !mpSupremum->origins().empty() );
244  return *mpSupremum;
245  }
246 
247  /**
248  *
249  * @param _infimum
250  */
251  void setInfimum( const Bound<T1, T2>* _infimum )
252  {
253  assert( _infimum->isActive() );
254  assert( mpInfimum->isActive() );
255  if( !mpInfimum->isInfinite() )
256  --mpInfimum->pInfo()->updated;
257  ++_infimum->pInfo()->updated;
258  mpInfimum = _infimum;
260  }
261 
262  /**
263  * @return
264  */
265  const Bound<T1, T2>* pInfimum() const
266  {
267  assert( !mpInfimum->origins().empty() );
268  return mpInfimum;
269  }
270 
271  /**
272  * @return
273  */
274  const Bound<T1, T2>& infimum() const
275  {
276  assert( !mpInfimum->origins().empty() );
277  return *mpInfimum;
278  }
279 
280  /**
281  * @return
282  */
283  size_t position() const
284  {
285  return mPosition;
286  }
287 
288  size_t id() const
289  {
290  return mId;
291  }
292 
293  /**
294  * @param _position
295  */
296  void setPosition( size_t _position )
297  {
298  mPosition = _position;
299  }
300 
301  bool isConflicting() const
302  {
303  return *mpInfimum > *mpSupremum;
304  }
305 
306  /**
307  * @return
308  */
309  typename std::list<std::list<std::pair<Variable<T1,T2>*,T2>>>::iterator positionInNonActives() const
310  {
311  return mPositionInNonActives;
312  }
313 
314  /**
315  *
316  * @param _positionInNonActives
317  */
318  void setPositionInNonActives( typename std::list<std::list<std::pair<Variable<T1,T2>*,T2>>>::iterator _positionInNonActives )
319  {
320  assert( !isOriginal() );
321  mPositionInNonActives = _positionInNonActives;
322  }
323 
324  /**
325  * @return
326  */
328  {
329  return mLowerbounds.size();
330  }
331 
332  /**
333  * @return
334  */
336  {
337  return mUpperbounds.size();
338  }
339 
340  /**
341  * @return
342  */
343  const typename Bound<T1, T2>::BoundSet& upperbounds() const
344  {
345  return mUpperbounds;
346  }
347 
348  /**
349  * @return
350  */
351  const typename Bound<T1, T2>::BoundSet& lowerbounds() const
352  {
353  return mLowerbounds;
354  }
355 
356  /**
357  * @return
358  */
360  {
361  return mUpperbounds;
362  }
363 
364  /**
365  * @return
366  */
368  {
369  return mLowerbounds;
370  }
371 
372  /**
373  * @return
374  */
375  size_t& rPosition()
376  {
377  return mPosition;
378  }
379 
380  /**
381  * @return
382  */
383  const typename Poly::PolyType* pExpression() const
384  {
385  return mExpression;
386  }
387 
388  /**
389  * @return
390  */
391  const typename Poly::PolyType& expression() const
392  {
393  return *mExpression;
394  }
395 
396  #ifdef LRA_NO_DIVISION
397  /**
398  * @return
399  */
400  const T2& factor() const
401  {
402  return mFactor;
403  }
404 
405  /**
406  * @return
407  */
408  T2& rFactor()
409  {
410  return mFactor;
411  }
412  #endif
413 
414  /**
415  *
416  * @param _ass
417  * @return
418  */
419  unsigned isSatisfiedBy( const RationalAssignment& _ass ) const
420  {
421  typename Poly::PolyType polyTmp = carl::substitute(*mExpression, _ass);
422  if( polyTmp.is_constant() )
423  return (*mpInfimum) <= polyTmp.constant_part() && (*mpSupremum) >= polyTmp.constant_part();
424  for( auto& lb : mLowerbounds )
425  {
426  unsigned neqSatisfied = lb->neqRepresentation().satisfiedBy( _ass );
427  assert( neqSatisfied != 2 );
428  if( neqSatisfied == 0 )
429  return 0;
430  }
431  for( auto& ub : mUpperbounds )
432  {
433  unsigned neqSatisfied = ub->neqRepresentation().satisfiedBy( _ass );
434  assert( neqSatisfied != 2 );
435  if( neqSatisfied == 0 )
436  return 0;
437  }
438  return 2;
439  }
440 
441  /**
442  *
443  * @param _ass
444  * @return
445  */
447  {
448  typename Poly::PolyType polyTmp = carl::substitute(*mExpression, _ass);
449  assert( polyTmp.is_constant() );
450 
451  if( (*mpInfimum) > polyTmp.constant_part() )
452  return mpInfimum->asConstraint();
453  else if ( (*mpSupremum) < polyTmp.constant_part() )
454  return mpSupremum->asConstraint();
455  else
456  {
457  for( auto& lb : mLowerbounds )
458  {
459  unsigned neqSatisfied = lb->neqRepresentation().satisfiedBy( _ass );
460  assert( neqSatisfied != 2 );
461  if( neqSatisfied == 0 )
462  return lb->neqRepresentation();
463  }
464  for( auto& ub : mUpperbounds )
465  {
466  unsigned neqSatisfied = ub->neqRepresentation().satisfiedBy( _ass );
467  assert( neqSatisfied != 2 );
468  if( neqSatisfied == 0 )
469  return ub->neqRepresentation();
470  }
471  return FormulaT( carl::FormulaType::TRUE );
472  }
473  }
474 
475  /**
476  *
477  */
479  {
480  mConflictActivity = 0;
481  int counter = 0;
482  if( !mpInfimum->isInfinite() )
483  {
484  if( mpInfimum->pOrigins()->front().type() == carl::FormulaType::AND )
485  {
486  for( const FormulaT& form : mpInfimum->pOrigins()->front().subformulas() )
487  {
488  mConflictActivity += form.activity();
489  ++counter;
490  }
491  }
492  else
493  {
494  assert( mpInfimum->pOrigins()->front().type() == carl::FormulaType::CONSTRAINT );
495  mConflictActivity += mpInfimum->pOrigins()->front().activity();
496  ++counter;
497  }
498  }
499  if( !mpSupremum->isInfinite() )
500  {
501  if( mpSupremum->pOrigins()->front().type() == carl::FormulaType::AND )
502  {
503  for( const FormulaT& form : mpSupremum->pOrigins()->front().subformulas() )
504  {
505  mConflictActivity += form.activity();
506  ++counter;
507  }
508  }
509  else
510  {
511  assert( mpSupremum->pOrigins()->front().type() == carl::FormulaType::CONSTRAINT );
512  mConflictActivity += mpSupremum->pOrigins()->front().activity();
513  ++counter;
514  }
515  }
516  if( counter != 0 ) mConflictActivity /= counter;
517  }
518 
519  /**
520  *
521  * @param _val
522  * @param _position
523  * @param _constraint
524  * @param _deduced
525  * @return
526  */
527  std::pair<const Bound<T1, T2>*, bool> addUpperBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint, bool _deduced = false );
528 
529  /**
530  *
531  * @param _val
532  * @param _position
533  * @param _constraint
534  * @param _deduced
535  * @return
536  */
537  std::pair<const Bound<T1, T2>*, bool> addLowerBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint, bool _deduced = false );
538 
539  /**
540  *
541  * @param _val
542  * @param _position
543  * @param _constraint
544  * @return
545  */
546  std::pair<const Bound<T1, T2>*, bool> addEqualBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint );
547 
548  /**
549  *
550  * @param _bound
551  */
552  void removeBound( const Bound<T1, T2>* _bound );
553 
554  /**
555  *
556  * @param bound
557  * @param _position
558  * @return
559  */
560  bool deactivateBound( const Bound<T1, T2>* bound, ModuleInput::iterator _position );
561 
562  /**
563  *
564  * @return
565  */
567 
568  /**
569  *
570  * @return
571  */
573 
574  /**
575  *
576  * @param _bound
577  * @return
578  */
579  bool operator<( const Variable& _variable ) const
580  {
581  if( this == &_variable )
582  return false;
583  return this->id() < _variable.id();
584  }
585 
586  bool operator>( const Variable& _variable ) const
587  {
588  return _variable < *this;
589  }
590 
591  bool operator==( const Variable& _variable ) const
592  {
593  return _variable.id() == this->id();
594  }
595 
596  bool operator!=( const Variable& _variable ) const
597  {
598  return !(_variable == *this);
599  }
600 
601  /**
602  *
603  * @param _out
604  */
605  void print( std::ostream& _out = std::cout ) const;
606 
607  /**
608  *
609  * @param _out
610  * @param _init
611  */
612  void printAllBounds( std::ostream& _out = std::cout, const std::string _init = "" ) const;
613  };
614  } // end namspace lra
615 } // end namespace smtrat
616 
617 namespace std
618 {
619  /**
620  * Implements std::hash for sort value.
621  */
622  template<typename T1, typename T2>
623  struct hash<smtrat::lra::Variable<T1,T2>>
624  {
625  public:
626  /**
627  * @param _lraVar The LRA-variable to get the hash for.
628  * @return The hash of the given LRA-variable.
629  */
630  size_t operator()( const smtrat::lra::Variable<T1,T2>& _lraVar ) const
631  {
632  return _lraVar.id();
633  }
634  };
635 }
636 
637 #include "Variable.tpp"
super::iterator iterator
Passing through the list::iterator.
Definition: ModuleInput.h:144
Stores a bound, which could be an upper "<= b" or a lower bound ">= b" for a bound value b.
Definition: Bound.h:33
Info * pInfo() const
Definition: Bound.h:422
carl::PointerSet< Bound< T1, T2 > > BoundSet
A set of pointer to bounds.
Definition: Bound.h:39
bool isActive() const
Definition: Bound.h:401
size_t size() const
Definition: Variable.h:194
bool involvesEquation() const
Definition: Variable.h:170
const Bound< T1, T2 > & infimum() const
Definition: Variable.h:274
EntryID & rStartEntry()
Definition: Variable.h:186
size_t & rPosition()
Definition: Variable.h:375
const Poly::PolyType & expression() const
Definition: Variable.h:391
Value< T1 > & rAssignment()
Definition: Variable.h:105
Bound< T1, T2 >::BoundSet & rUpperbounds()
Definition: Variable.h:359
std::list< std::list< std::pair< Variable< T1, T2 > *, T2 > > >::iterator positionInNonActives() const
Definition: Variable.h:309
const Poly::PolyType * pExpression() const
Definition: Variable.h:383
bool operator!=(const Variable &_variable) const
Definition: Variable.h:596
const Bound< T1, T2 > * mpSupremum
Definition: Variable.h:54
const Bound< T1, T2 > * pInfimum() const
Definition: Variable.h:265
void setSupremum(const Bound< T1, T2 > *_supremum)
Definition: Variable.h:219
std::pair< const Bound< T1, T2 > *, bool > addUpperBound(Value< T1 > *const _val, ModuleInput::iterator _position, const FormulaT &_constraint, bool _deduced=false)
std::pair< const Bound< T1, T2 > *, bool > addEqualBound(Value< T1 > *const _val, ModuleInput::iterator _position, const FormulaT &_constraint)
const Bound< T1, T2 > * mpInfimum
Definition: Variable.h:56
EntryID startEntry() const
Definition: Variable.h:178
bool operator<(const Variable &_variable) const
Definition: Variable.h:579
const Value< T1 > & assignment() const
Definition: Variable.h:97
const T2 & factor() const
Definition: Variable.h:400
void print(std::ostream &_out=std::cout) const
bool isOriginal() const
Definition: Variable.h:146
const Bound< T1, T2 > & supremum() const
Definition: Variable.h:241
RationalInterval getVariableBounds() const
size_t & rSize()
Definition: Variable.h:202
bool hasBound() const
Definition: Variable.h:162
const Bound< T1, T2 >::BoundSet & upperbounds() const
Definition: Variable.h:343
double conflictActivity() const
Definition: Variable.h:210
bool operator==(const Variable &_variable) const
Definition: Variable.h:591
double mConflictActivity
Definition: Variable.h:42
void printAllBounds(std::ostream &_out=std::cout, const std::string _init="") const
const Bound< T1, T2 >::BoundSet & lowerbounds() const
Definition: Variable.h:351
Value< T1 > mLastConsistentAssignment
Definition: Variable.h:62
size_t rLowerBoundsSize()
Definition: Variable.h:327
size_t id() const
Definition: Variable.h:288
void setPosition(size_t _position)
Definition: Variable.h:296
bool isConflicting() const
Definition: Variable.h:301
Value< T1 > mAssignment
Definition: Variable.h:60
std::list< std::list< std::pair< Variable< T1, T2 > *, T2 > > >::iterator mPositionInNonActives
Definition: Variable.h:46
void setInfimum(const Bound< T1, T2 > *_infimum)
Definition: Variable.h:251
Variable(typename std::list< std::list< std::pair< Variable< T1, T2 > *, T2 >>>::iterator _positionInNonActives, const typename Poly::PolyType *_expression, ModuleInput::iterator _defaultBoundPosition, bool _isInteger, size_t _id)
const Poly::PolyType * mExpression
Definition: Variable.h:58
void setBasic(bool _basic)
Definition: Variable.h:130
bool deactivateBound(const Bound< T1, T2 > *bound, ModuleInput::iterator _position)
void removeBound(const Bound< T1, T2 > *_bound)
void setPositionInNonActives(typename std::list< std::list< std::pair< Variable< T1, T2 > *, T2 >>>::iterator _positionInNonActives)
Definition: Variable.h:318
FormulasT getDefiningOrigins() const
bool operator>(const Variable &_variable) const
Definition: Variable.h:586
FormulaT inConflictWith(const RationalAssignment &_ass) const
Definition: Variable.h:446
Bound< T1, T2 >::BoundSet mUpperbounds
Definition: Variable.h:50
unsigned isSatisfiedBy(const RationalAssignment &_ass) const
Definition: Variable.h:419
Variable(size_t _position, const typename Poly::PolyType *_expression, ModuleInput::iterator _defaultBoundPosition, bool _isInteger, size_t _id)
Bound< T1, T2 >::BoundSet mLowerbounds
Definition: Variable.h:52
const Value< T1 > & lastConsistentAssignment() const
Definition: Variable.h:121
size_t position() const
Definition: Variable.h:283
Bound< T1, T2 >::BoundSet & rLowerbounds()
Definition: Variable.h:367
bool isBasic() const
Definition: Variable.h:138
void updateConflictActivity()
Definition: Variable.h:478
size_t rUpperBoundsSize()
Definition: Variable.h:335
bool is_integer() const
Definition: Variable.h:154
std::pair< const Bound< T1, T2 > *, bool > addLowerBound(Value< T1 > *const _val, ModuleInput::iterator _position, const FormulaT &_constraint, bool _deduced=false)
const Bound< T1, T2 > * pSupremum() const
Definition: Variable.h:232
static uint32_t hash(uint32_t x)
Definition: Map.h:32
size_t EntryID
Definition: Variable.h:23
static EntryID LAST_ENTRY_ID
Definition: Variable.h:25
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
Definition: VSHelper.h:138
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Assignment< Rational > RationalAssignment
Definition: types.h:45
carl::Interval< Rational > RationalInterval
Definition: model.h:27
carl::Formulas< Poly > FormulasT
Definition: types.h:39
int updated
A value to store the information whether this bounds constraint has already been processed (=0),...
Definition: Bound.h:50
size_t operator()(const smtrat::lra::Variable< T1, T2 > &_lraVar) const
Definition: Variable.h:630