SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
HistoryNode.h
Go to the documentation of this file.
1 /*
2  * File: HistoryNode.h
3  * Author: Stefan Schupp
4  *
5  * Created on November 9, 2012, 2:02 PM
6  */
7 
8 #pragma once
9 
10 //#define HISTORY_DEBUG
11 
12 #include "IcpVariable.h"
13 #include "ContractionCandidate.h"
15 
16 namespace smtrat
17 {
18  namespace icp
19  {
21  {
22  public:
23 
24  private:
25 
26  /**
27  * Members
28  */
29 
30  EvalDoubleIntervalMap mIntervals; // intervals AFTER contraction with Candidates of the incoming edge has been applied
31  std::map<carl::Variable, std::set<ConstraintT> > mReasons;
32  std::map<carl::Variable, set_icpVariable> mVariableReasons;
33  std::set<const ContractionCandidate*> mAppliedContractions;
34  std::set<ConstraintT> mStateInfeasibleConstraints;
36 
37 
38  public:
39 
40  /**
41  * Methods
42  */
43 
45  mIntervals(),
46  mReasons(),
51  {}
52 
53  HistoryNode( const EvalDoubleIntervalMap& _intervals):
54  mIntervals( _intervals ),
55  mReasons(),
60  {}
61 
63  {}
64 
65  /**
66  * Getters and Setters
67  */
68 
70  {
71  return mIntervals;
72  }
73 
75  {
76  return mIntervals;
77  }
78 
79  DoubleInterval& getInterval( carl::Variable::Arg _variable )
80  {
81  assert( mIntervals.find( _variable ) != mIntervals.end() );
82  return mIntervals.at( _variable );
83  }
84 
85  void setIntervals( const EvalDoubleIntervalMap& _map )
86  {
87  EvalDoubleIntervalMap::const_iterator intervalIt;
88  for( intervalIt = _map.begin(); intervalIt != _map.end(); ++intervalIt )
89  {
90  if( mIntervals.find( (*intervalIt).first ) != mIntervals.end() )
91  {
92  if( mIntervals.at((*intervalIt).first) != (*intervalIt).second )
93  mIntervals[(*intervalIt).first] = (*intervalIt).second;
94  }
95  else
96  mIntervals[(*intervalIt).first] = (*intervalIt).second;
97  }
98  }
99 
100  /**
101  * updates or inserts an interval into the actual map
102  * @param _var
103  * @param _interval
104  * @return true if only an update
105  */
106  bool addInterval( carl::Variable::Arg _var, const DoubleInterval& _interval )
107  {
108  if( mIntervals.find( _var ) != mIntervals.end() )
109  {
110  mIntervals[_var] = _interval;
111  return true;
112  }
113  else
114  {
115  mIntervals[_var] = _interval;
116  return false;
117  }
118  }
119 
120  bool hasEmptyInterval() const
121  {
122  for( EvalDoubleIntervalMap::const_iterator intervalIt = mIntervals.begin(); intervalIt != mIntervals.end();
123  ++intervalIt )
124  {
125  if( (*intervalIt).second.is_empty() )
126  return true;
127  }
128  return false;
129  }
130 
131  std::set<const ContractionCandidate*> appliedContractions()
132  {
133  return mAppliedContractions;
134  }
135 
137  {
139  for( std::set<const ContractionCandidate*>::iterator candidateIt = mAppliedContractions.begin(); candidateIt != mAppliedContractions.end(); ++candidateIt )
140  {
141  for( auto originIt = (*candidateIt)->origin().begin(); originIt != (*candidateIt)->origin().end(); ++originIt )
142  {
143  appliedConstraints.insert(*originIt);
144  }
145  }
146  return appliedConstraints;
147  }
148 
149  void appliedConstraints( std::vector<FormulaT>& _result )
150  {
151  for( std::set<const ContractionCandidate*>::iterator candidateIt = mAppliedContractions.begin(); candidateIt != mAppliedContractions.end(); ++candidateIt )
152  {
153  for( auto originIt = (*candidateIt)->origin().begin(); originIt != (*candidateIt)->origin().end(); ++originIt )
154  {
155  _result.push_back(*originIt);
156  }
157  }
158  }
159 
160  std::set<ConstraintT>& rStateInfeasibleConstraints()
161  {
163  }
164 
165  const std::set<ConstraintT>& stateInfeasibleConstraints() const
166  {
168  }
169 
171  {
173  }
174 
176  {
178  }
179 
180  bool addInfeasibleConstraint(const ConstraintT& _constraint, bool _addOnlyConstraint = false)
181  {
182  if(!_addOnlyConstraint)
183  {
184  // also add all variables contained in the constraint to stateInfeasibleVariables
185  for( auto variable: _constraint.variables() )
186  {
187  if(mVariableReasons.find(variable) != mVariableReasons.end())
188  {
189  for( set_icpVariable::const_iterator icpVarIt = mVariableReasons.at(variable).begin(); icpVarIt != mVariableReasons.at(variable).end(); ++icpVarIt )
190  addInfeasibleVariable(*icpVarIt);
191  }
192  }
193  }
194  return mStateInfeasibleConstraints.insert(_constraint).second;
195  }
196 
197  bool addInfeasibleVariable( const IcpVariable* _variable, bool _addOnlyVariable = false )
198  {
199  if(!_addOnlyVariable)
200  {
201  //also add the reasons for the variables
202  if (mVariableReasons.find(_variable->var()) != mVariableReasons.end())
203  {
204  for( set_icpVariable::iterator variableIt = mVariableReasons.at(_variable->var()).begin(); variableIt != mVariableReasons.at(_variable->var()).end(); ++variableIt )
205  mStateInfeasibleVariables.insert(*variableIt);
206  }
207  }
208  return mStateInfeasibleVariables.insert(_variable).second;
209  }
210 
211  void addContraction( ContractionCandidate* _candidate, const set_icpVariable& _variables )
212  {
213  mAppliedContractions.insert( _candidate );
214 
215  for( set_icpVariable::iterator variableIt = _variables.begin(); variableIt != _variables.end(); ++variableIt )
216  addVariableReason( _candidate->derivationVar(), *variableIt );
217 
218  // update reasons
219  assert(!_candidate->origin().empty());
220  // TEMPORARY!!! -> Very coarse overapprox!
221  for( auto originIt = _candidate->rOrigin().begin(); originIt != _candidate->rOrigin().end(); ++originIt )
222  addReason( _candidate->derivationVar(), originIt->constraint() );
223  }
224 
225  const std::set<const ContractionCandidate*>& getCandidates() const
226  {
227  return mAppliedContractions;
228  }
229 
230  std::map<carl::Variable, std::set<ConstraintT>>& rReasons()
231  {
232  return mReasons;
233  }
234 
235  const std::map<carl::Variable, std::set<ConstraintT>>& reasons() const
236  {
237  return mReasons;
238  }
239 
240 
241  std::set<ConstraintT>& reasons( carl::Variable::Arg _variable )
242  {
243  assert( mReasons.find( _variable ) != mReasons.end() );
244  return mReasons.at( _variable );
245  }
246 
247  void addReason( carl::Variable::Arg _variable, const ConstraintT& _reason )
248  {
249  if( mReasons.find( _variable ) == mReasons.end() )
250  mReasons[_variable] = std::set<ConstraintT>();
251 
252  bool inserted = mReasons.at( _variable ).insert( _reason ).second;
253  if( inserted )
254  {
255  for( auto var: _reason.variables() )
256  {
257  if( mReasons.find(var) == mReasons.end() )
258  mReasons[var] = std::set<ConstraintT>();
259  addReasons( _variable, mReasons.at( var ) );
260  }
261  }
262  assert( mReasons.find( _variable ) != mReasons.end() );
263  }
264 
265  void addReasons( carl::Variable::Arg _variable, const std::set<ConstraintT>& _reasons )
266  {
267  for( std::set<ConstraintT>::iterator reasonsIt = _reasons.begin(); reasonsIt != _reasons.end(); ++reasonsIt )
268  addReason( _variable, (*reasonsIt) );
269  }
270 
271  void addReasons( carl::Variable::Arg _variable, const FormulasT& _origins )
272  {
273  assert( mReasons.find( _variable ) != mReasons.end() );
274  bool contained = false;
275  auto minimal = _origins.begin();
276  for( auto originIt = _origins.begin(); originIt != _origins.end(); ++originIt )
277  {
278  if( mReasons.at( _variable ).find( originIt->constraint() ) != mReasons.at( _variable ).end() )
279  {
280  contained = true;
281  break;
282  }
283  if( originIt->constraint().variables().size() < minimal->constraint().variables().size() )
284  minimal = originIt;
285  }
286  if( !contained )
287  addReason( _variable, minimal->constraint() );
288  }
289 
290  void addVariableReason( carl::Variable::Arg _variable, const IcpVariable* _reason )
291  {
292  mVariableReasons[_variable].insert(_reason);
293  }
294 
295  void addVariableReasons( carl::Variable::Arg _variable, set_icpVariable _variables )
296  {
297  for( set_icpVariable::iterator variableIt = _variables.begin(); variableIt != _variables.end(); ++variableIt )
298  mVariableReasons[_variable].insert(*variableIt);
299  }
300 
301  const std::map<carl::Variable, set_icpVariable>& variableReasons()
302  {
303  return mVariableReasons;
304  }
305 
306  std::map<carl::Variable, set_icpVariable>& rVariableReasons()
307  {
308  return mVariableReasons;
309  }
310 
311  set_icpVariable variableReasons( carl::Variable::Arg _variable )
312  {
313  assert(mVariableReasons.find(_variable) != mVariableReasons.end());
314  return mVariableReasons.at(_variable);
315  }
316 
317 
318  void variableHull( carl::Variable::Arg _variable, set_icpVariable& _result ) const
319  {
320  gatherVariables(_variable, _result);
321  }
322 
323 
325  {
326  for( std::set<ConstraintT>::iterator constraintIt = mStateInfeasibleConstraints.begin(); constraintIt != mStateInfeasibleConstraints.end(); ++constraintIt )
327  _target->addInfeasibleConstraint(*constraintIt);
328  }
329 
331  {
333  for( set_icpVariable::iterator variableIt = mStateInfeasibleVariables.begin(); variableIt != mStateInfeasibleVariables.end(); ++variableIt )
334  {
335  gatherVariables((*variableIt)->var(), result);
336  for( set_icpVariable::iterator collectedVarIt = result.begin(); collectedVarIt != result.end(); ++collectedVarIt )
337  {
338  _target->addInfeasibleVariable(*collectedVarIt);
339  }
340  }
341  }
342 
343  void print( std::ostream& _out = std::cout ) const
344  {
345  _out << "Intervals: " << std::endl;
346  for( EvalDoubleIntervalMap::const_iterator intervalIt = mIntervals.begin(); intervalIt != mIntervals.end();
347  ++intervalIt )
348  {
349  _out << (*intervalIt).first << "\t : " << (*intervalIt).second << std::endl;
350  }
351  _out << "Applied Contractions: ";
352  if( mAppliedContractions.size() > 0 )
353  {
354  _out << std::endl;
355  for( std::set<const ContractionCandidate*>::iterator candidateIt = mAppliedContractions.begin();
356  candidateIt != mAppliedContractions.end(); ++candidateIt )
357  {
358  (*candidateIt)->print();
359  }
360  }
361  else
362  _out << "None" << std::endl;
363  _out << "Infeasible Variables: ";
364  if( !mStateInfeasibleVariables.empty())
365  {
366  _out << std::endl;
367  for( set_icpVariable::iterator varIt = mStateInfeasibleVariables.begin(); varIt != mStateInfeasibleVariables.end(); ++varIt )
368  (*varIt)->print();
369  }
370  else
371  {
372  _out << "None" << std::endl;
373  }
374 
375  _out << "Infeasible Constraints: ";
376  if( !mStateInfeasibleConstraints.empty() )
377  {
378  _out << std::endl;
379  for( std::set<ConstraintT>::iterator constraintIt = mStateInfeasibleConstraints.begin(); constraintIt != mStateInfeasibleConstraints.end(); ++constraintIt )
380  _out << *constraintIt << std::endl;
381  }
382  else
383  {
384  _out << "None" << std::endl;
385  }
386 // printReasons();
387  }
388 
389  void printReasons( std::ostream& _out = std::cout ) const
390  {
391  _out << "Reasons(" << mReasons.size() << ")" << std::endl;
392  for( std::map<carl::Variable, std::set<ConstraintT> >::const_iterator variablesIt = mReasons.begin();
393  variablesIt != mReasons.end(); ++variablesIt )
394  {
395  _out << (*variablesIt).first << ":\t";
396  for( std::set<ConstraintT>::const_iterator reasonIt = (*variablesIt).second.begin(); reasonIt != (*variablesIt).second.end(); ++reasonIt )
397  {
398  _out << *reasonIt << ", ";
399  }
400  _out << std::endl;
401  }
402  }
403 
404  void printVariableReasons( std::ostream& _out = std::cout ) const
405  {
406  _out << "VariableReasons(" << mVariableReasons.size() << ")" << std::endl;
407  for( std::map<carl::Variable, set_icpVariable>::const_iterator variablesIt = mVariableReasons.begin();
408  variablesIt != mVariableReasons.end(); ++variablesIt )
409  {
410  _out << (*variablesIt).first << ":\t";
411  for( set_icpVariable::const_iterator reasonIt = (*variablesIt).second.begin();
412  reasonIt != (*variablesIt).second.end(); ++reasonIt )
413  {
414  _out << **reasonIt << ", ";
415  }
416  std::cout << std::endl;
417  }
418  }
419 
420  void reset()
421  {
424  mAppliedContractions.clear();
425  mReasons.clear();
426  mVariableReasons.clear();
427  }
428 
429  private:
430 
431  /**
432  * Functions
433  */
434 
435  void gatherVariables(carl::Variable::Arg _var, set_icpVariable& _result) const
436  {
437  if( mVariableReasons.find(_var) != mVariableReasons.end() )
438  {
439 // std::cout << "search." << std::endl;
440  set_icpVariable variables = mVariableReasons.at(_var);
441  for( set_icpVariable::iterator varIt = variables.begin(); varIt != variables.end(); ++varIt )
442  {
443  if( _result.insert( *varIt ).second )
444  {
445 // std::cout << "Inserted " << (*varIt)->var() << std::endl;
446  gatherVariables((*varIt)->var(), _result);
447  }
448  else
449  {
450 // std::cout << "Already contained: " << (*varIt)->var() << std::endl;
451  }
452  }
453  }
454  }
455  }; // class HistoryNode
456  } // namespace icp
457 } // namespace smtrat
carl::Variable::Arg derivationVar() const
const FormulaSetT & origin() const
void addVariableReasons(carl::Variable::Arg _variable, set_icpVariable _variables)
Definition: HistoryNode.h:295
bool addInfeasibleConstraint(const ConstraintT &_constraint, bool _addOnlyConstraint=false)
Definition: HistoryNode.h:180
std::set< const ContractionCandidate * > appliedContractions()
Definition: HistoryNode.h:131
FormulaSetT appliedConstraints()
Definition: HistoryNode.h:136
set_icpVariable variableReasons(carl::Variable::Arg _variable)
Definition: HistoryNode.h:311
std::set< ConstraintT > mStateInfeasibleConstraints
Definition: HistoryNode.h:34
std::map< carl::Variable, std::set< ConstraintT > > mReasons
Definition: HistoryNode.h:31
std::map< carl::Variable, set_icpVariable > & rVariableReasons()
Definition: HistoryNode.h:306
void propagateStateInfeasibleConstraints(HistoryNode *_target) const
Definition: HistoryNode.h:324
const std::set< ConstraintT > & stateInfeasibleConstraints() const
Definition: HistoryNode.h:165
EvalDoubleIntervalMap mIntervals
Members.
Definition: HistoryNode.h:30
void appliedConstraints(std::vector< FormulaT > &_result)
Definition: HistoryNode.h:149
void addVariableReason(carl::Variable::Arg _variable, const IcpVariable *_reason)
Definition: HistoryNode.h:290
void addReasons(carl::Variable::Arg _variable, const std::set< ConstraintT > &_reasons)
Definition: HistoryNode.h:265
void addReason(carl::Variable::Arg _variable, const ConstraintT &_reason)
Definition: HistoryNode.h:247
HistoryNode(const EvalDoubleIntervalMap &_intervals)
Definition: HistoryNode.h:53
bool hasEmptyInterval() const
Definition: HistoryNode.h:120
void propagateStateInfeasibleVariables(HistoryNode *_target) const
Definition: HistoryNode.h:330
std::set< ConstraintT > & reasons(carl::Variable::Arg _variable)
Definition: HistoryNode.h:241
void variableHull(carl::Variable::Arg _variable, set_icpVariable &_result) const
Definition: HistoryNode.h:318
std::set< ConstraintT > & rStateInfeasibleConstraints()
Definition: HistoryNode.h:160
void addReasons(carl::Variable::Arg _variable, const FormulasT &_origins)
Definition: HistoryNode.h:271
std::map< carl::Variable, std::set< ConstraintT > > & rReasons()
Definition: HistoryNode.h:230
bool addInfeasibleVariable(const IcpVariable *_variable, bool _addOnlyVariable=false)
Definition: HistoryNode.h:197
const std::map< carl::Variable, set_icpVariable > & variableReasons()
Definition: HistoryNode.h:301
void printReasons(std::ostream &_out=std::cout) const
Definition: HistoryNode.h:389
set_icpVariable mStateInfeasibleVariables
Definition: HistoryNode.h:35
DoubleInterval & getInterval(carl::Variable::Arg _variable)
Definition: HistoryNode.h:79
set_icpVariable stateInfeasibleVariables() const
Definition: HistoryNode.h:175
void setIntervals(const EvalDoubleIntervalMap &_map)
Definition: HistoryNode.h:85
const std::map< carl::Variable, std::set< ConstraintT > > & reasons() const
Definition: HistoryNode.h:235
void addContraction(ContractionCandidate *_candidate, const set_icpVariable &_variables)
Definition: HistoryNode.h:211
void gatherVariables(carl::Variable::Arg _var, set_icpVariable &_result) const
Functions.
Definition: HistoryNode.h:435
EvalDoubleIntervalMap & rIntervals()
Definition: HistoryNode.h:74
bool addInterval(carl::Variable::Arg _var, const DoubleInterval &_interval)
updates or inserts an interval into the actual map
Definition: HistoryNode.h:106
set_icpVariable & rStateInfeasibleVariables()
Definition: HistoryNode.h:170
const EvalDoubleIntervalMap & intervals() const
Getters and Setters.
Definition: HistoryNode.h:69
std::map< carl::Variable, set_icpVariable > mVariableReasons
Definition: HistoryNode.h:32
void printVariableReasons(std::ostream &_out=std::cout) const
Definition: HistoryNode.h:404
std::set< const ContractionCandidate * > mAppliedContractions
Definition: HistoryNode.h:33
void print(std::ostream &_out=std::cout) const
Definition: HistoryNode.h:343
const std::set< const ContractionCandidate * > & getCandidates() const
Definition: HistoryNode.h:225
carl::Variable::Arg var() const
Definition: IcpVariable.h:96
int var(Lit p)
Definition: SolverTypes.h:64
std::set< const IcpVariable *, icpVariableComp > set_icpVariable
Definition: IcpVariable.h:336
Class to create the formulas for axioms.
carl::FormulaSet< Poly > FormulaSetT
Definition: types.h:41
carl::Interval< double > DoubleInterval
Definition: model.h:26
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
carl::Formulas< Poly > FormulasT
Definition: types.h:39
std::map< carl::Variable, DoubleInterval > EvalDoubleIntervalMap
Definition: model.h:29