SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
CoCoAGBModule.h
Go to the documentation of this file.
1 /**
2  * @file CoCoAGBModule.h
3  * @author YOUR NAME <YOUR EMAIL ADDRESS>
4  *
5  * @version 2017-11-29
6  * Created on 2017-11-29.
7  */
8 
9 #pragma once
10 
11 #include <smtrat-solver/Module.h>
12 #include "CoCoAGBSettings.h"
13 
14 #include <optional>
15 #include <algorithm>
16 
17 #include <carl-common/config.h>
18 
19 namespace smtrat
20 {
21  template<typename Settings>
22  class CoCoAGBModule : public Module
23  {
24 #ifdef USE_COCOA
25  struct VariableOrdering {
26  private:
27  std::vector<carl::Variable> mOrdering;
28 
29  void addPoly(std::map<carl::Variable, std::size_t>& ordermap, const Poly& p) {
30  for (auto var: carl::variables(p)) {
31  auto it = ordermap.find(var);
32  if (it == ordermap.end()) {
33  ordermap.emplace(var, 1);
34  } else {
35  it->second++;
36  }
37  }
38  }
39  void makeOrdering(const std::map<carl::Variable, std::size_t>& ordermap) {
40  std::vector<std::pair<std::size_t, carl::Variable>> flatorder;
41  for (const auto& v: ordermap) {
42  flatorder.emplace_back(v.second, v.first);
43  }
44  std::sort(flatorder.begin(), flatorder.end());
45  for (const auto& t: flatorder) {
46  mOrdering.emplace_back(t.second);
47  }
48  }
49  public:
50  VariableOrdering(const std::vector<Poly>& polys) {
51  std::map<carl::Variable, std::size_t> ordermap;
52  for (const auto& p: polys) addPoly(ordermap, p);
53  makeOrdering(ordermap);
54  }
55  const std::vector<carl::Variable>& getOrdering() const {
56  return mOrdering;
57  }
58  };
59  private:
60  // Auxiliary variables are needed when transforming inequalities to equalities
61  std::map<ConstraintT, carl::Variable> mAuxVariables;
62  // Polys constructed from constraints
63  std::map<ConstraintT, Poly> mGBPolys;
64 
65  std::vector<Poly> mLastBasis;
66 
67  // Create a unique auxiliary variable for every constraint
68  carl::Variable getAuxVar(const ConstraintT& c) {
69  auto it = mAuxVariables.find(c);
70  if (it != mAuxVariables.end()) {
71  return it->second;
72  }
73  return mAuxVariables.emplace(c, carl::fresh_real_variable()).first->second;
74  }
75  // Return the polynomial to be put in the GB, does conversion for inequalities (if enabled)
76  std::optional<Poly> getPoly(const ConstraintT& c) {
77  if (c.relation() == carl::Relation::EQ) {
78  return c.lhs();
79  }
80  if (!Settings::convert_inequalities) {
81  return std::nullopt;
82  }
83  carl::Variable aux = getAuxVar(c);
84  switch (c.relation()) {
85  case carl::Relation::GEQ:
86  return c.lhs() + TermT(-1, aux, 2);
87  case carl::Relation::LEQ:
88  return c.lhs() + TermT(1, aux, 2);
89  case carl::Relation::GREATER:
90  return c.lhs() * aux * aux - Rational(1);
91  case carl::Relation::LESS:
92  return c.lhs() * aux * aux + Rational(1);
93  case carl::Relation::NEQ:
94  return c.lhs() * aux + Rational(1);
95  default:
96  assert(false);
97  return std::nullopt;
98  }
99  }
100 
101  public:
102  typedef Settings SettingsType;
103  std::string moduleName() const {
104  return SettingsType::moduleName;
105  }
106  CoCoAGBModule(const ModuleInput* _formula, Conditionals& _conditionals, Manager* _manager = nullptr);
107 
108  ~CoCoAGBModule();
109 
110  // Main interfaces.
111  /**
112  * Informs the module about the given constraint. It should be tried to inform this
113  * module about any constraint it could receive eventually before assertSubformula
114  * is called (preferably for the first time, but at least before adding a formula
115  * containing that constraint).
116  * @param _constraint The constraint to inform about.
117  * @return false, if it can be easily decided whether the given constraint is inconsistent;
118  * true, otherwise.
119  */
120  bool informCore( const FormulaT& _constraint );
121 
122  /**
123  * Informs all backends about the so far encountered constraints, which have not yet been communicated.
124  * This method must not and will not be called more than once and only before the first runBackends call.
125  */
126  void init();
127 
128  /**
129  * The module has to take the given sub-formula of the received formula into account.
130  *
131  * @param _subformula The sub-formula to take additionally into account.
132  * @return false, if it can be easily decided that this sub-formula causes a conflict with
133  * the already considered sub-formulas;
134  * true, otherwise.
135  */
136  bool addCore( ModuleInput::const_iterator _subformula );
137 
138  /**
139  * Removes the subformula of the received formula at the given position to the considered ones of this module.
140  * Note that this includes every stored calculation which depended on this subformula, but should keep the other
141  * stored calculation, if possible, untouched.
142  *
143  * @param _subformula The position of the subformula to remove.
144  */
145  void removeCore( ModuleInput::const_iterator _subformula );
146 
147  /**
148  * Updates the current assignment into the model.
149  * Note, that this is a unique but possibly symbolic assignment maybe containing newly introduced variables.
150  */
151  void updateModel() const;
152 
153  /**
154  * Checks the received formula for consistency.
155  * @return True, if the received formula is satisfiable;
156  * False, if the received formula is not satisfiable;
157  * Unknown, otherwise.
158  */
159  Answer checkCore();
160 #endif
161  };
162 }
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
A base class for all kind of theory solving methods.
Definition: Module.h:70
virtual void init()
Informs all backends about the so far encountered constraints, which have not yet been communicated.
Definition: Module.cpp:234
virtual bool addCore([[maybe_unused]] ModuleInput::const_iterator formula)
The module has to take the given sub-formula of the received formula into account.
Definition: Module.h:706
virtual std::string moduleName() const
Definition: Module.h:615
virtual void updateModel() const
Updates the model, if the solver has detected the consistency of the received formula,...
Definition: Module.cpp:250
virtual void removeCore([[maybe_unused]] ModuleInput::const_iterator formula)
Removes everything related to the given sub-formula of the received formula.
Definition: Module.h:729
virtual bool informCore([[maybe_unused]] const FormulaT &_constraint)
Informs the module about the given constraint.
Definition: Module.h:684
virtual Answer checkCore()
Checks the received formula for consistency.
Definition: Module.cpp:207
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
int var(Lit p)
Definition: SolverTypes.h:64
std::vector< carl::Variable > VariableOrdering
Definition: common.h:11
Class to create the formulas for axioms.
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
mpq_class Rational
Definition: types.h:19