SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ContractionCandidate.h
Go to the documentation of this file.
1 /*
2  * File: ContractionCandidate.h
3  * Author: stefan
4  *
5  * Created on October 29, 2012, 2:14 PM
6  */
7 
8 #pragma once
9 
10 //#define CCPRINTORIGINS
11 
12 //#include "ContractionCandidateManager.h"
13 #include <carl-arith/intervalcontraction/Contraction.h>
14 #include <smtrat-common/model.h>
15 
16 namespace smtrat
17 {
18  namespace icp{
19 
20  class IcpVariable;
21  class ContractionCandidateManager;
22 
23  template<template<typename> class Operator>
24  using Contractor = carl::Contraction<Operator, Poly>;
25 
27  {
29 
30  private:
31 
32  /**
33  * Members:
34  */
35  const Poly mRhs;
36  ConstraintT mConstraint; // Todo: Do not save constraints but formulae instead
38 
39  carl::Variable mLhs;
40  carl::Variable mDerivationVar;
43  unsigned mId;
44  bool mIsLinear;
45  bool mDerived;
47  std::set<IcpVariable*> mIcpVariables;
49 
50 
51  // RWA
52  static const size_t mK = 10;
53 #ifdef __VS
54  static const double mAlpha;
55 #else
56  static constexpr double mAlpha = 0.9;
57 #endif
58  double mRWA;
59  double mLastRWA;
60  double mLastPayoff;
61 
62  public:
63 
64  /**
65  * Constructors:
66  */
67 
68  ContractionCandidate( const ContractionCandidate& _original ) = delete;
69  ContractionCandidate( ) = delete;
70 
71  ContractionCandidate( carl::Variable _lhs, const Poly _rhs, const ConstraintT& _constraint, carl::Variable _derivationVar, Contractor<carl::SimpleNewton>& _contractor, const FormulaT& _origin, unsigned _id, bool _usePropagation ):
72  mRhs(_rhs),
73  mConstraint(_constraint),
74  mContractor(_contractor),
75  mLhs(_lhs),
76  mDerivationVar(_derivationVar),
77  mDerivative(),
78  mOrigin(),
79  mId(_id),
80  mIsLinear(true),
81  mDerived(false),
82  mUsePropagation(_usePropagation),
83  mIcpVariables(),
85  mRWA(1),
86  mLastPayoff(0)
87  {
88  mOrigin.insert(_origin);
89  }
90 
91  /**
92  * Constructor only for nonlinear candidates
93  * @param _constraint
94  * @param _derivationVar
95  */
96  ContractionCandidate( carl::Variable _lhs, const Poly _rhs, const ConstraintT& _constraint, carl::Variable _derivationVar, Contractor<carl::SimpleNewton>& _contractor, unsigned _id, bool _usePropagation ):
97  mRhs(_rhs),
98  mConstraint(_constraint),
99  mContractor(_contractor),
100  mLhs(_lhs),
101  mDerivationVar(_derivationVar),
102  mDerivative(),
103  mOrigin(),
104  mId(_id),
105  mIsLinear(false),
106  mDerived(false),
107  mUsePropagation(_usePropagation),
108  mIcpVariables(),
110  mRWA(1),
111  mLastRWA(1),
112  mLastPayoff(0)
113  {}
114 
115  /**
116  * Destructor:
117  */
119  {}
120 
121  /**
122  * Functions:
123  */
124 
125  const Poly& rhs() const
126  {
127  return mRhs;
128  }
129 
130  const ConstraintT& constraint() const
131  {
132  return mConstraint;
133  }
134 
136  {
137  return mContractor;
138  }
139 
141  {
142  return mContractor(_intervals, mDerivationVar, _resA, _resB, true, mUsePropagation);
143  }
144 
145  carl::Variable::Arg derivationVar() const
146  {
147  return mDerivationVar;
148  }
149 
150  const Poly& derivative() const
151  {
152  return mDerivative;
153  }
154 
155  carl::Variable::Arg lhs() const
156  {
157  return mLhs;
158  }
159 
160  const FormulaSetT& origin() const
161  {
162  return mOrigin;
163  }
164 
166  {
167  return mOrigin;
168  }
169 
170  void addOrigin( const FormulaT& _origin );
171 
172  void removeOrigin( const FormulaT& _origin );
173 
174  bool hasOrigin( const FormulaT& _origin ) const
175  {
176  return ( mOrigin.find(_origin) != mOrigin.end() );
177  }
178 
179  void setLinear()
180  {
181  mIsLinear = true;
182  }
183 
185  {
186  mIsLinear = false;
187  }
188 
189  bool is_linear() const
190  {
191  return mIsLinear;
192  }
193 
195  {
197  }
198 
200  {
202  }
203 
205  {
207  }
208 
209  void addICPVariable( IcpVariable* _icpVar )
210  {
211  mIcpVariables.insert( _icpVar );
212  }
213 
214  double calcRWA()
215  {
216  mRWA = mRWA + mAlpha * (mLastPayoff - mRWA);
217  return mRWA;
218  }
219 
220  double RWA() const
221  {
222  return mRWA;
223  }
224 
225  double lastRWA() const
226  {
227  return mRWA;
228  }
229 
231  {
232  mLastRWA = mRWA;
233  }
234 
235  double lastPayoff() const
236  {
237  return mLastPayoff;
238  }
239 
240  unsigned id() const
241  {
242  return mId;
243  }
244 
245  void setDerivationVar( carl::Variable _var )
246  {
247  mDerivationVar = _var;
248  }
249 
250  void setLhs( carl::Variable _lhs )
251  {
252  mLhs = _lhs;
253  }
254 
255  void setPayoff( double _weight )
256  {
257  mLastPayoff = _weight;
258  }
259 
260  void calcDerivative() throw ()
261  {
262  if( !mDerived )
263  {
264  mDerivative = carl::derivative(mRhs, mDerivationVar);
265  mDerived = true;
266  }
267  }
268 
269  size_t activity()
270  {
271  return mOrigin.size();
272  }
273 
274  bool isActive() const
275  {
276  return mOrigin.size() > 0;
277  }
278 
279  bool isDerived() const
280  {
281  return mDerived;
282  }
283 
285  {
286  mLastPayoff = 0;
287  mRWA = 0;
288  }
289 
290  void print( std::ostream& _out = std::cout ) const
291  {
292  _out << mId << ": \t" << mContractor.polynomial() << ", LHS = " << mLhs << ", VAR = " << mDerivationVar << ", DERIVATIVE = " << mDerivative;
293 // _out << mId << ": \t" << ", LHS = " << mLhs << ", VAR = " << mDerivationVar << ", DERIVATIVE = " << mDerivative;
294 #ifdef CCPRINTORIGINS
295  std::cout << std::endl << "Origins(" << mOrigin.size()<< "): " << std::endl;
296  if ( !mOrigin.empty())
297  {
298  for ( auto originIt = mOrigin.begin(); originIt != mOrigin.end(); ++originIt )
299  {
300  std::cout << "\t ";
301  (*originIt)->print();
302  std::cout << "\t [" << (*originIt) << "]" << std::endl;
303  }
304  }
305 #else
306  _out << ", #Origins: " << mOrigin.size() << std::endl;
307 #endif
308  }
309 
311  {
312  return mRWA < rhs.RWA() || (mRWA == rhs.RWA() && mId < rhs.id() );
313  }
314 
316  {
317  return mId == rhs.id();
318  }
319 
320  private:
321 
322  /**
323  * Methods:
324  */
325  };
326 
328  {
329  bool operator() (const ContractionCandidate* const lhs, const ContractionCandidate* const rhs) const
330  {
331  return lhs->id() < rhs->id();
332  }
333  };
334 
335  }// namespace icp
336 }// namespace smtrat
void removeOrigin(const FormulaT &_origin)
carl::Variable::Arg lhs() const
ContractionCandidate(carl::Variable _lhs, const Poly _rhs, const ConstraintT &_constraint, carl::Variable _derivationVar, Contractor< carl::SimpleNewton > &_contractor, const FormulaT &_origin, unsigned _id, bool _usePropagation)
bool operator<(ContractionCandidate const &rhs) const
std::set< IcpVariable * > mIcpVariables
ContractionCandidate(const ContractionCandidate &_original)=delete
Constructors:
carl::Variable::Arg derivationVar() const
Contractor< carl::SimpleNewton > & mContractor
const Poly & rhs() const
Functions:
Contractor< carl::SimpleNewton > & contractor() const
bool hasOrigin(const FormulaT &_origin) const
const ConstraintT & constraint() const
void print(std::ostream &_out=std::cout) const
ContractionCandidate(carl::Variable _lhs, const Poly _rhs, const ConstraintT &_constraint, carl::Variable _derivationVar, Contractor< carl::SimpleNewton > &_contractor, unsigned _id, bool _usePropagation)
Constructor only for nonlinear candidates.
void addOrigin(const FormulaT &_origin)
bool contract(EvalDoubleIntervalMap &_intervals, DoubleInterval &_resA, DoubleInterval &_resB)
bool operator==(ContractionCandidate const &rhs) const
const FormulaSetT & origin() const
void addICPVariable(IcpVariable *_icpVar)
void setDerivationVar(carl::Variable _var)
carl::Contraction< Operator, Poly > Contractor
Class to create the formulas for axioms.
carl::FormulaSet< Poly > FormulaSetT
Definition: types.h:41
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Interval< double > DoubleInterval
Definition: model.h:26
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
std::map< carl::Variable, DoubleInterval > EvalDoubleIntervalMap
Definition: model.h:29
bool operator()(const ContractionCandidate *const lhs, const ContractionCandidate *const rhs) const