3 * @author Florian Corzilius <corzilius@cs.rwth-aachen.de>
16 template<typename T1, typename T2>
17 Variable<T1, T2>::Variable( size_t _position, const typename Poly::PolyType* _expression, ModuleInput::iterator _defaultBoundPosition, bool _isInteger, size_t _id ):
20 mInteger( _isInteger ),
21 mStartEntry( LAST_ENTRY_ID ),
23 mConflictActivity( 0 ),
24 mPosition( _position ),
28 mExpression( _expression),
29 mAssignment( T1( 0 ) ),
30 mLastConsistentAssignment( mAssignment )
31 #ifdef LRA_NO_DIVISION
36 mpSupremum = addUpperBound( NULL, _defaultBoundPosition, FormulaT( carl::FormulaType::TRUE ) ).first;
37 mpInfimum = addLowerBound( NULL, _defaultBoundPosition, FormulaT( carl::FormulaType::TRUE ) ).first;
40 template<typename T1, typename T2>
41 Variable<T1, T2>::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 ):
44 mInteger( _isInteger ),
45 mStartEntry( LAST_ENTRY_ID ),
47 mConflictActivity( 0 ),
48 mPositionInNonActives( _positionInNonActives ),
52 mExpression( _expression),
53 mAssignment( T1( 0 ) ),
54 mLastConsistentAssignment( mAssignment )
55 #ifdef LRA_NO_DIVISION
60 mpSupremum = addUpperBound( NULL, _defaultBoundPosition, FormulaT( carl::FormulaType::TRUE ) ).first;
61 mpInfimum = addLowerBound( NULL, _defaultBoundPosition, FormulaT( carl::FormulaType::TRUE ) ).first;
64 template<typename T1, typename T2>
65 Variable<T1, T2>::~Variable()
67 while( !mLowerbounds.empty() )
69 const Bound<T1, T2>* b = *mLowerbounds.begin();
70 mLowerbounds.erase( mLowerbounds.begin() );
71 if( b->type() != Bound<T1, T2>::EQUAL ) delete b;
73 while( !mUpperbounds.empty() )
75 const Bound<T1, T2>* b = *mUpperbounds.begin();
76 mUpperbounds.erase( mUpperbounds.begin() );
82 template<typename T1, typename T2>
83 std::pair<const Bound<T1, T2>*, bool> Variable<T1, T2>::addUpperBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint, bool _deduced )
85 struct Bound<T1, T2>::Info* boundInfo = new struct Bound<T1, T2>::Info( _position );
86 Bound<T1, T2>* newBound = new Bound<T1, T2>( _val, this, Bound<T1, T2>::UPPER, _constraint, boundInfo, _deduced );
87 std::pair<typename Bound<T1, T2>::BoundSet::iterator, bool> result = mUpperbounds.insert( newBound );
92 return std::pair<const Bound<T1, T2>*, bool>( *result.first, result.second );
95 template<typename T1, typename T2>
96 std::pair<const Bound<T1, T2>*, bool> Variable<T1, T2>::addLowerBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint, bool _deduced )
98 struct Bound<T1, T2>::Info* boundInfo = new struct Bound<T1, T2>::Info( _position );
99 Bound<T1, T2>* newBound = new Bound<T1, T2>( _val, this, Bound<T1, T2>::LOWER, _constraint, boundInfo, _deduced );
100 std::pair<typename Bound<T1, T2>::BoundSet::iterator, bool> result = mLowerbounds.insert( newBound );
105 return std::pair<const Bound<T1, T2>*, bool>( *result.first, result.second );
108 template<typename T1, typename T2>
109 std::pair<const Bound<T1, T2>*, bool> Variable<T1, T2>::addEqualBound( Value<T1>* const _val, ModuleInput::iterator _position, const FormulaT& _constraint )
111 struct Bound<T1, T2>::Info* boundInfo = new struct Bound<T1, T2>::Info( _position );
112 Bound<T1, T2>* newBound = new Bound<T1, T2>( _val, this, Bound<T1, T2>::EQUAL, _constraint, boundInfo );
113 std::pair<typename Bound<T1, T2>::BoundSet::iterator, bool> result = mLowerbounds.insert( newBound );
117 return std::pair<const Bound<T1, T2>*, bool>( *result.first, false );
121 std::pair<typename Bound<T1, T2>::BoundSet::iterator, bool> resultB = mUpperbounds.insert( newBound );
122 assert( resultB.second );
123 return std::pair<const Bound<T1, T2>*, bool>( *result.first, true );
127 template<typename T1, typename T2>
128 void Variable<T1, T2>::removeBound( const Bound<T1, T2>* _bound )
130 assert( _bound != mpInfimum );
131 assert( _bound != mpSupremum );
132 switch( _bound->type() )
134 case Bound<T1, T2>::LOWER:
135 mLowerbounds.erase( _bound );
137 case Bound<T1, T2>::UPPER:
138 mUpperbounds.erase( _bound );
141 mLowerbounds.erase( _bound );
142 mUpperbounds.erase( _bound );
147 template<typename T1, typename T2>
148 bool Variable<T1, T2>::deactivateBound( const Bound<T1, T2>* bound, ModuleInput::iterator _position )
150 assert( !bound->isInfinite() );
151 assert( !bound->isActive() );
152 bound->pInfo()->updated = 0;
153 bound->pInfo()->position = _position;
154 bool variableBoundsChanged = false;
155 if( bound->isUpperBound() )
157 //check if it is the supremum
158 if( mpSupremum == bound )
161 typename Bound<T1, T2>::BoundSet::iterator newBound = mUpperbounds.begin();
162 while( newBound != --mUpperbounds.end() )
164 if( (*newBound)->isActive() )
166 ++(*newBound)->pInfo()->updated;
171 mpSupremum = *newBound;
172 variableBoundsChanged = true;
175 if( bound->isLowerBound() )
177 //check if it is the infimum
178 if( mpInfimum == bound )
181 typename Bound<T1, T2>::BoundSet::reverse_iterator newBound = mLowerbounds.rbegin();
182 while( newBound != --mLowerbounds.rend() )
184 if( (*newBound)->isActive() )
186 ++(*newBound)->pInfo()->updated;
191 mpInfimum = *newBound;
192 variableBoundsChanged = true;
195 return variableBoundsChanged;
198 template<typename T1, typename T2>
199 RationalInterval Variable<T1, T2>::getVariableBounds() const
201 carl::BoundType lowerBoundType;
202 Rational lowerBoundValue;
203 carl::BoundType upperBoundType;
204 Rational upperBoundValue;
205 if( infimum().isInfinite() )
207 lowerBoundType = carl::BoundType::INFTY;
208 lowerBoundValue = Rational(0);
212 lowerBoundType = infimum().isWeak() ? carl::BoundType::WEAK : carl::BoundType::STRICT;
213 lowerBoundValue = Rational(infimum().limit().mainPart());
215 if( supremum().isInfinite() )
217 upperBoundType = carl::BoundType::INFTY;
218 upperBoundValue = Rational(0);
222 upperBoundType = supremum().isWeak() ? carl::BoundType::WEAK : carl::BoundType::STRICT;
223 upperBoundValue = Rational(supremum().limit().mainPart());
225 RationalInterval result = RationalInterval( lowerBoundValue, lowerBoundType, upperBoundValue, upperBoundType );
229 template<typename T1, typename T2>
230 FormulasT Variable<T1, T2>::getDefiningOrigins() const
233 if( !infimum().isInfinite() )
235 result.push_back( infimum().origins().front() );
237 if( !supremum().isInfinite() )
239 result.push_back( supremum().origins().front() );
244 template<typename T1, typename T2>
245 void Variable<T1, T2>::print( std::ostream& _out ) const
247 std::stringstream out;
249 _out << std::setw( 15 ) << out.str();
250 _out << std::setw( 6 ) << " -> ";
251 _out << std::setw( 35 ) << mAssignment;
252 _out << std::setw( 6 ) << " in [";
253 _out << std::setw( 12 ) << mpInfimum;
254 _out << std::setw( 2 ) << ", ";
255 _out << std::setw( 12 ) << mpSupremum;
256 _out << std::setw( 1 ) << "]";
259 template<typename T1, typename T2>
260 void Variable<T1, T2>::printAllBounds( std::ostream& _out, const std::string _init ) const
262 _out << _init << " Upper bounds: " << std::endl;
263 for( typename Bound<T1, T2>::BoundSet::const_iterator bIter = mUpperbounds.begin(); bIter != mUpperbounds.end(); ++bIter )
265 _out << _init << " ";
266 (*bIter)->print( true, _out, true );
267 _out << " [" << (*bIter)->pInfo()->updated << "]" << std::endl;
269 _out << _init << " Lower bounds: " << std::endl;
270 for( typename Bound<T1, T2>::BoundSet::const_reverse_iterator bIter = mLowerbounds.rbegin(); bIter != mLowerbounds.rend(); ++bIter )
272 _out << _init << " ";
273 (*bIter)->print( true, _out, true );
274 _out << " [" << (*bIter)->pInfo()->updated << "]" << std::endl;
277 } // end namspace lra
278 } // end namespace smtrat