SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Substitution.h
Go to the documentation of this file.
1 /**
2  * Class to create a substitution object.
3  * @author Florian Corzilius
4  * @since 2010-05-11
5  * @version 2013-10-23
6  */
7 
8 #pragma once
9 
10 #include "Condition.h"
11 
13 #include <smtrat-common/model.h>
14 
15 namespace smtrat {
16 namespace vs
17 {
18 
20  {
21  public:
22  /// The type of a substitution.
24 
25  private:
26  // Members.
27 
28  /// The variable to substitute.
29  carl::Variable mVariable;
30  /// The pointer (if != NULL) to the square root term to substitute the variable for.
32  /// The type.
34  /// The variables occurring in the term to substitute for.
35  mutable carl::Variables* mpTermVariables;
36  /// The conditions from which this substitution has been originated. (e.g. [x -> 2] could have had the origins {x-2<=0, x^2-4=0})
37  carl::PointerSet<Condition> mOriginalConditions;
38  /// The side conditions, which have to hold to make this substitution valid. (e.g. [x -> 1/a] has the side condition {a!=0})
40 
41  public:
42 
43  /**
44  * Constructs a substitution with no square root term to map to.
45  * @param _variable The variable to substitute of the substitution to construct.
46  * @param _type The type of the substitution of the substitution to construct.
47  * @param _oConditions The original conditions of the substitution to construct.
48  * @param _sideCondition The side conditions of the substitution to construct.
49  */
50  Substitution( const carl::Variable& _variable, const Type& _type, carl::PointerSet<Condition>&& _oConditions, smtrat::ConstraintsT&& _sideCondition = smtrat::ConstraintsT() );
51 
52  /**
53  * Constructs a substitution with a square root term to map to.
54  * @param _variable The variable to substitute of the substitution to construct.
55  * @param _term The square root term to which the variable maps to.
56  * @param _type The type of the substitution of the substitution to construct.
57  * @param _oConditions The original conditions of the substitution to construct.
58  * @param _sideCondition The side conditions of the substitution to construct.
59  */
60  Substitution( const carl::Variable&, const smtrat::SqrtEx& _term, const Type& _type, carl::PointerSet<Condition>&& _oConditions, smtrat::ConstraintsT&& _sideCondition = smtrat::ConstraintsT() );
61 
62  /**
63  * Copy constructor.
64  */
65  Substitution( const Substitution& _substitution );
66 
67  /**
68  * The destructor.
69  */
70  ~Substitution();
71 
72  /**
73  * @return A constant reference to the variable this substitution substitutes.
74  */
75  const carl::Variable& variable() const
76  {
77  return mVariable;
78  }
79 
80  /**
81  * @return A constant reference to the term this substitution maps its variable to.
82  */
83  const smtrat::SqrtEx& term() const
84  {
85  return *mpTerm;
86  }
87 
88  /**
89  * Sets the substitution term to the given rational.
90  * @param _value The value to set the substitution term to.
91  */
92  void setTerm( const smtrat::Rational& _value )
93  {
94  assert( mType == Type::MINUS_INFINITY );
95  *mpTerm = smtrat::SqrtEx( smtrat::Poly( _value ) );
97  }
98 
99  /**
100  * @return A reference to the type of this substitution.
101  */
103  {
104  return mType;
105  }
106 
107  /**
108  * @return A constant reference to the type of this substitution.
109  */
110  const Type& type() const
111  {
112  return mType;
113  }
114 
115  /**
116  * @return A reference to the original conditions of this substitution.
117  */
118  carl::PointerSet<Condition>& rOriginalConditions()
119  {
120  return mOriginalConditions;
121  }
122 
123  /**
124  * @return A constant reference to the original conditions of this substitution.
125  */
126  const carl::PointerSet<Condition>& originalConditions() const
127  {
128  return mOriginalConditions;
129  }
130 
131  /**
132  * @return A constant reference to the side condition of this substitution.
133  */
135  {
136  return mSideCondition;
137  }
138 
139  /**
140  * @return A constant reference to the variables occurring in the substitution term.
141  */
142  const carl::Variables& termVariables() const
143  {
144  if( mpTermVariables == NULL )
145  {
146  mpTermVariables = new carl::Variables();
147  *mpTermVariables = carl::variables(*mpTerm).as_set();
148  }
149  return *mpTermVariables;
150  }
151 
152  /**
153  * @param _preferMinusInf A flag indicating whether to valuate the substitution type best or otherwise
154  * worst.
155  * @return The valuation of this substitution according to a heuristic.
156  */
157  unsigned valuate( bool _preferMinusInf = true ) const;
158 
159  /**
160  * @param The substitution to compare with.
161  * @return true, if this substitution is equal to the given substitution;
162  * false, otherwise.
163  */
164  bool operator==( const Substitution& ) const;
165 
166  /**
167  * @param The substitution to compare with.
168  * @return true, if this substitution is less than the given substitution;
169  * false, otherwise.
170  */
171 // bool operator<( const Substitution& ) const;
172 
173  /**
174  * Prints this substitution on the given stream, with some additional information.
175  * @param _withOrigins A flag indicating whether to print also the origins of this substitution.
176  * @param _withSideConditions A flag indication whether to also the side conditions of this substitution.
177  * @param _out The stream to print on.
178  * @param _init The string to print at the beginning of every row.
179  */
180  void print( bool _withOrigins = false, bool _withSideConditions = false, std::ostream& _out = std::cout, const std::string& _init = "" ) const;
181  };
182  /**
183  * Prints the given substitution on the given output stream.
184  * @param _out The output stream, on which to write.
185  * @param _substitution The substitution to print.
186  * @return The output stream after printing the substitution on it.
187  */
188  std::ostream& operator<<(std::ostream& os, const Substitution& s);
189 
190 } // end namspace vs
191 }
192 
193 namespace std
194 {
195  template<>
196  struct hash<smtrat::vs::Substitution>
197  {
198  public:
199  size_t operator()( const smtrat::vs::Substitution& _substitution ) const
200  {
201  return ((hash<smtrat::SqrtEx>()(_substitution.term()) << 7) ^ hash<carl::Variable>()( _substitution.variable() ) << 2) ^ _substitution.type();
202  }
203  };
204 } // namespace std
205 
206 namespace smtrat {
207 namespace vs
208 {
210  {
211  bool operator ()( const Substitution* _substitutionA, const Substitution* _substitutionB ) const
212  {
213  return (*_substitutionA)==(*_substitutionB);
214  }
215  };
216 
218  {
219  size_t operator ()( const Substitution* _substitution ) const
220  {
221  if( _substitution == NULL )
222  return 0;
223  return std::hash<Substitution>()( *_substitution );
224  }
225  };
226 
227  template<typename T>
228  using SubstitutionFastPointerMap = std::unordered_map<const smtrat::Poly*, T, substitutionPointerHash, substitutionPointerEqual>;
229 }
230 }
const carl::Variables & termVariables() const
Definition: Substitution.h:142
smtrat::ConstraintsT mSideCondition
The side conditions, which have to hold to make this substitution valid. (e.g. [x -> 1/a] has the sid...
Definition: Substitution.h:39
Type
The type of a substitution.
Definition: Substitution.h:23
carl::PointerSet< Condition > & rOriginalConditions()
Definition: Substitution.h:118
void print(bool _withOrigins=false, bool _withSideConditions=false, std::ostream &_out=std::cout, const std::string &_init="") const
Prints this substitution on the given stream, with some additional information.
~Substitution()
The destructor.
carl::Variable mVariable
The variable to substitute.
Definition: Substitution.h:29
carl::Variables * mpTermVariables
The variables occurring in the term to substitute for.
Definition: Substitution.h:35
bool operator==(const Substitution &) const
const smtrat::SqrtEx & term() const
Definition: Substitution.h:83
const smtrat::ConstraintsT & sideCondition() const
Definition: Substitution.h:134
const carl::Variable & variable() const
Definition: Substitution.h:75
smtrat::SqrtEx * mpTerm
The pointer (if != NULL) to the square root term to substitute the variable for.
Definition: Substitution.h:31
const carl::PointerSet< Condition > & originalConditions() const
Definition: Substitution.h:126
void setTerm(const smtrat::Rational &_value)
Sets the substitution term to the given rational.
Definition: Substitution.h:92
Substitution(const carl::Variable &_variable, const Type &_type, carl::PointerSet< Condition > &&_oConditions, smtrat::ConstraintsT &&_sideCondition=smtrat::ConstraintsT())
Constructs a substitution with no square root term to map to.
unsigned valuate(bool _preferMinusInf=true) const
carl::PointerSet< Condition > mOriginalConditions
The conditions from which this substitution has been originated. (e.g. [x -> 2] could have had the or...
Definition: Substitution.h:37
const Type & type() const
Definition: Substitution.h:110
static uint32_t hash(uint32_t x)
Definition: Map.h:32
std::unordered_map< const smtrat::Poly *, T, substitutionPointerHash, substitutionPointerEqual > SubstitutionFastPointerMap
Definition: Substitution.h:228
std::ostream & operator<<(std::ostream &_out, const Condition &_condition)
Definition: Condition.cpp:246
Class to create the formulas for axioms.
@ NORMAL
Definition: types.h:53
carl::Constraints< Poly > ConstraintsT
Definition: types.h:31
carl::SqrtEx< Poly > SqrtEx
Definition: model.h:22
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19
bool operator()(const Substitution *_substitutionA, const Substitution *_substitutionB) const
Definition: Substitution.h:211
size_t operator()(const Substitution *_substitution) const
Definition: Substitution.h:219
size_t operator()(const smtrat::vs::Substitution &_substitution) const
Definition: Substitution.h:199