SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ICEModule.h
Go to the documentation of this file.
1 /**
2  * @file ICEModule.h
3  * @author YOUR NAME <YOUR EMAIL ADDRESS>
4  *
5  * Supports optimization.
6  *
7  * @version 2015-11-24
8  * Created on 2015-11-24.
9  */
10 
11 #pragma once
12 
13 #include <smtrat-solver/PModule.h>
15 
16 #include "ICESettings.h"
17 #include "ForwardHyperGraph.h"
18 
19 namespace smtrat
20 {
21  template<typename Settings>
22  class ICEModule : public PModule
23  {
24  struct Coefficient {
26  bool strict;
27  Coefficient(): r(0), strict(false) {}
29  Coefficient(const Coefficient& c): r(c.r), strict(c.strict) {}
31  r = c.r;
32  strict = c.strict;
33  return *this;
34  }
36  r += c.r;
37  strict |= c.strict;
38  return *this;
39  }
40  friend std::ostream& operator<<(std::ostream& os, const Coefficient& c) {
41  return os << "(" << c.r << "," << std::boolalpha << c.strict << ")";
42  }
43  };
44 
46  struct EdgeProperty {
49  EdgeProperty(const Coefficient& c, const FormulaT& con): coeff(c), constraint(con) {}
50  friend std::ostream& operator<<(std::ostream& os, const EdgeProperty& ep) {
51  return os << ep.coeff;
52  }
53  };
55 
56  struct CycleCollector {
57  using Vertex = typename FHG::Vertex;
58  using Edge = typename FHG::Edge;
59 
61  std::vector<std::pair<FormulaT,FormulaT>> mLemmas;
62 
63  FormulaT buildFormula(const std::vector<Edge>& edges) const {
64  FormulasT res;
65  std::transform(edges.begin(), edges.end(), std::back_inserter(res), [](const Edge& edge){ return edge->constraint; });
66  return FormulaT(carl::FormulaType::AND, std::move(res));
67  }
68  std::vector<Vertex> collectAdjacent(const std::vector<Vertex>& vertices, const std::vector<Edge>& edges) {
69  std::vector<Vertex> res;
70  auto curVertex = vertices.begin();
71  for (const auto& edge: edges) {
72  curVertex++;
73  if (curVertex == vertices.end()) curVertex = vertices.begin();
74  for (const auto& v: edge.out()) {
75  if (v == *curVertex) continue;
76  res.push_back(v);
77  }
78  }
79  return res;
80  }
81 
82  /**
83  * Is called whenever a cycle is found.
84  * If true is returned, the search for further cycles is aborted.
85  */
86  bool operator()(const FHG& graph, const std::vector<Vertex>& vertices, const std::vector<Edge>& edges) {
87  Coefficient sum;
88  for (const auto& edge: edges) sum += edge->coeff;
89 
90  if (carl::is_zero(sum.r)) {
91  FormulaT origin = buildFormula(edges);
92  if (sum.strict) {
93  // Sum is zero but there is a strict inequality -> UNSAT
94  mInfeasibleSubset = FormulaSetT(origin.subformulas().begin(), origin.subformulas().end());
95  return true;
96  } else {
97  // Sum is zero and all inequalities are weak
98  // -> Variables on the cycle
99  for (const auto& v: collectAdjacent(vertices, edges)) {
100  FormulaT lemma(Poly(graph[v]), carl::Relation::EQ);
101  SMTRAT_LOG_DEBUG("smtrat.ice", "Deducted " << lemma);
102  mLemmas.emplace_back(lemma, origin);
103  }
104  for (std::size_t i = 1; i < vertices.size(); i++) {
105  const auto& a = graph[vertices[i-1]];
106  const auto& b = graph[vertices[i]];
107  FormulaT lemma(a - b + edges[i-1]->coeff.r, carl::Relation::EQ);
108  SMTRAT_LOG_DEBUG("smtrat.ice", "Deducted " << lemma);
109  mLemmas.emplace_back(lemma, origin);
110  }
111  }
112  } else if (sum.r > 0) {
113  SMTRAT_LOG_DEBUG("smtrat.ice", "What to do? sum > 0");
114  } else if (sum.r < 0) {
115  FormulaT origin = buildFormula(edges);
116  mInfeasibleSubset = FormulaSetT(origin.subformulas().begin(), origin.subformulas().end());
117  return true;
118  }
119  return false;
120  }
121  };
122 
123  private:
125  std::map<FormulaT,ConstraintT> mConstraints;
126  std::set<FormulaT> mOtherFormulas;
127 
128  void addFormula(const FormulaT& f);
129  void removeFormula(const FormulaT& f);
131 
132  bool is_zero(const TermT& t) const {
133  return mBounds.getInterval(t).is_zero();
134  }
135  bool isSemiPositive(const TermT& t) const {
136  return mBounds.getInterval(t).is_semi_positive();
137  }
138  bool isSemiNegative(const TermT& t) const {
139  return mBounds.getInterval(t).is_semi_negative();
140  }
141  bool isSuitable(const ConstraintT& c, TermT& src, std::vector<TermT>& dest, Coefficient& coeff);
142 
143  public:
145  ICEModule(const ModuleInput* _formula, Conditionals& _conditionals, Manager* _manager = nullptr);
146 
148 
149  /**
150  * The module has to take the given sub-formula of the received formula into account.
151  *
152  * @param _subformula The sub-formula to take additionally into account.
153  * @return false, if it can be easily decided that this sub-formula causes a conflict with
154  * the already considered sub-formulas;
155  * true, otherwise.
156  */
158 
159  /**
160  * Removes the subformula of the received formula at the given position to the considered ones of this module.
161  * Note that this includes every stored calculation which depended on this subformula, but should keep the other
162  * stored calculation, if possible, untouched.
163  *
164  * @param _subformula The position of the subformula to remove.
165  */
167 
168  /**
169  * Checks the received formula for consistency.
170  * @return SAT, if the received formula is satisfiable;
171  * UNSAT, if the received formula is not satisfiable;
172  * Unknown, otherwise.
173  */
175  };
176 }
All preprocessing modules shall derive from this module, that is modules, which simplify their receiv...
This class implements a forward hypergraph.
std::size_t Vertex
Internal type of a vertex.
bool isSuitable(const ConstraintT &c, TermT &src, std::vector< TermT > &dest, Coefficient &coeff)
ICEModule(const ModuleInput *_formula, Conditionals &_conditionals, Manager *_manager=nullptr)
void removeFormula(const FormulaT &f)
TermT VertexProperty
Definition: ICEModule.h:45
bool is_zero(const TermT &t) const
Definition: ICEModule.h:132
bool isSemiPositive(const TermT &t) const
Definition: ICEModule.h:135
vb::VariableBounds< FormulaT > mBounds
Definition: ICEModule.h:124
bool addCore(ModuleInput::const_iterator _subformula)
The module has to take the given sub-formula of the received formula into account.
void addFormula(const FormulaT &f)
Answer checkCore()
Checks the received formula for consistency.
Settings SettingsType
Definition: ICEModule.h:144
void removeCore(ModuleInput::const_iterator _subformula)
Removes the subformula of the received formula at the given position to the considered ones of this m...
Answer processConstraints()
std::set< FormulaT > mOtherFormulas
Definition: ICEModule.h:126
std::map< FormulaT, ConstraintT > mConstraints
Definition: ICEModule.h:125
bool isSemiNegative(const TermT &t) const
Definition: ICEModule.h:138
Base class for solvers.
Definition: Manager.h:34
The input formula a module has to consider for it's satisfiability check.
Definition: ModuleInput.h:131
super::const_iterator const_iterator
Passing through the list::const_iterator.
Definition: ModuleInput.h:146
const RationalInterval & getInterval(const carl::Variable &_var) const
Creates an interval corresponding to the variable bounds of the given variable.
Class to create the formulas for axioms.
carl::FormulaSet< Poly > FormulaSetT
Definition: types.h:41
carl::Term< Rational > TermT
Definition: types.h:23
carl::Formula< Poly > FormulaT
Definition: types.h:37
const settings::Settings & Settings()
Definition: Settings.h:96
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
Answer
An enum with the possible answers a Module can give.
Definition: types.h:57
std::vector< std::atomic_bool * > Conditionals
A vector of atomic bool pointers.
Definition: types.h:17
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
carl::Formulas< Poly > FormulasT
Definition: types.h:39
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
Internal type of an edge.
friend std::ostream & operator<<(std::ostream &os, const Coefficient &c)
Definition: ICEModule.h:40
Coefficient & operator+=(const Coefficient &c)
Definition: ICEModule.h:35
Coefficient & operator=(const Coefficient &c)
Definition: ICEModule.h:30
Coefficient(Coefficient &&c)
Definition: ICEModule.h:28
Coefficient(const Coefficient &c)
Definition: ICEModule.h:29
FormulaT buildFormula(const std::vector< Edge > &edges) const
Definition: ICEModule.h:63
bool operator()(const FHG &graph, const std::vector< Vertex > &vertices, const std::vector< Edge > &edges)
Is called whenever a cycle is found.
Definition: ICEModule.h:86
std::vector< std::pair< FormulaT, FormulaT > > mLemmas
Definition: ICEModule.h:61
std::vector< Vertex > collectAdjacent(const std::vector< Vertex > &vertices, const std::vector< Edge > &edges)
Definition: ICEModule.h:68
typename FHG::Vertex Vertex
Definition: ICEModule.h:57
friend std::ostream & operator<<(std::ostream &os, const EdgeProperty &ep)
Definition: ICEModule.h:50
EdgeProperty(const Coefficient &c, const FormulaT &con)
Definition: ICEModule.h:49