SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
CADPreprocessor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <smtrat-common/model.h>
7 
8 
9 #include "../common.h"
11 
12 namespace smtrat::cad {
13 
16  bool disable_resultants = false;
17 
18  static void register_settings(SettingsParser& parser) {
19  namespace po = boost::program_options;
20  auto& settings = settings::Settings::getInstance();
21  auto& s = settings.get<CADPreprocessorSettings>("cad-pp");
22 
23  parser.add("CAD Preprocessor settings").add_options()
24  ("cad.pp.no-elimination", po::bool_switch(&s.disable_variable_elimination), "disable variable elimination")
25  ("cad.pp.no-resultants", po::bool_switch(&s.disable_resultants), "disable resultant rule")
26  ;
27  }
28  static bool register_hook() {
29  SettingsComponents::getInstance().add(&register_settings);
30  return true;
31  }
32  static const bool dummy;
33 };
34 
35 inline const auto& settings_cadpp() {
36  static const auto& s = settings::Settings::getInstance().get<CADPreprocessorSettings>("cad-pp");
37  return s;
38 }
39 
40 namespace preprocessor {
41 
42 struct Origins {
43  std::map<FormulaT, std::vector<std::vector<FormulaT>>> mOrigins;
44 
45  void cleanOrigins(const FormulaT& f);
46  void add(const FormulaT& f, const std::vector<FormulaT>& origin);
47  void remove(const FormulaT& f);
48  const std::vector<FormulaT>& get(const FormulaT& f) const;
49  bool resolve(std::set<FormulaT>& conflict) const;
50 };
51 
53 public:
54  /// Result of an assignment collection.
55  /// true: new assignments were found
56  /// false: no new assignments were found
57  /// constraint: found direct conflict
58  using CollectionResult = std::variant<bool,ConstraintT>;
59 private:
61  /// Reasons for the assignment of variables.
62  std::map<carl::Variable, ConstraintT> mReasons;
63  std::map<ConstraintT, carl::Variable> mConstraints;
64 
65  bool extractValueAssignments(std::map<ConstraintT, ConstraintT>& constraints);
66  bool extractParametricAssignments(std::map<ConstraintT, ConstraintT>& constraints);
67  bool extractAssignments(std::map<ConstraintT, ConstraintT>& constraints);
68  CollectionResult simplify(std::map<ConstraintT, ConstraintT>& constraints);
69 public:
70 
71  AssignmentCollector(Model& model): mModel(model) {}
72 
73  const auto& reasons() const {
74  return mReasons;
75  }
76  auto& reasons() {
77  return mReasons;
78  }
79  const auto& constraints() const {
80  return mConstraints;
81  }
82  auto& constraints() {
83  return mConstraints;
84  }
85 
86  CollectionResult collect(std::map<ConstraintT, ConstraintT>& constraints);
87 };
88 
90 private:
91  const std::vector<carl::Variable>& mVars;
92  std::vector<ConstraintT> mConstraints;
93  std::vector<std::vector<UPoly>> mData;
94  std::vector<ConstraintT> mNewECs;
96 
97  bool addPoly(const Poly& poly);
98  bool addPoly(const UPoly& poly, std::size_t level, const std::vector<FormulaT>& origin);
99  std::optional<std::vector<FormulaT>> computeResultants(std::size_t level);
100 
101 public:
102  ResultantRule(Origins& origins, const std::vector<carl::Variable>& vars):
103  mVars(vars),
104  mOrigins(origins)
105  {}
106 
107  std::optional<std::vector<FormulaT>> compute(const std::map<ConstraintT,ConstraintT>& constraints);
108 
109  const auto& getNewECs() const {
110  return mNewECs;
111  }
112 };
113 
115  std::vector<ConstraintT> toAdd;
116  std::vector<ConstraintT> toRemove;
117 };
118 
119 }
120 
122 public:
123  friend std::ostream& operator<<(std::ostream& os, const CADPreprocessor& cadpp);
124 private:
125  /// The model used for variable elimination.
127  /// Variable ordering.
128  const std::vector<carl::Variable>& mVars;
129  /// Origins of new formulas.
131  /// The assignment collector.
133  /// The resultant rule.
135 
136  /// Equalities from the input.
137  std::vector<ConstraintT> mEqualities;
138  /// Inequalities from the input.
139  std::map<ConstraintT, ConstraintT> mInequalities;
140  /// Derived set of equalities, essentially mEqualities - mModel.
141  std::map<ConstraintT, ConstraintT> mDerivedEqualities;
142 
143 
144  std::optional<std::set<FormulaT>> mConflict;
145 
146  void resetCached();
147  void removeEquality(const ConstraintT& c);
148  bool addEqualities(const std::vector<ConstraintT>& constraints);
149  std::vector<ConstraintT> collectDerivedEqualities() const;
150 
151  /**
152  * Replace constraints that have been modified by its origins in the given conflict.
153  */
154  bool collectOriginsOfConflict(std::set<FormulaT>& conflict, const std::map<ConstraintT, ConstraintT>& constraints) const;
155  bool addModelToConflict(std::set<FormulaT>& conflict, carl::Variables& added) const;
156 
157 public:
158  CADPreprocessor(const std::vector<carl::Variable>& vars):
159  mModel(),
160  mVars(vars),
163  {}
164 
165  const auto& equalities() const {
166  return mEqualities;
167  }
168  const auto& inequalities() const {
169  return mInequalities;
170  }
171 
172  void addConstraint(const ConstraintT& c);
173  void removeConstraint(const ConstraintT& c);
174 
175  /**
176  * Performs the preprocessing.
177  * Return false if a direct conflict was found, true otherwise.
178  */
179  bool preprocess();
180 
181  const Model& model() const {
182  return mModel;
183  }
184 
185  template<typename Map>
186  preprocessor::ConstraintUpdate result(const Map& oldC) const {
187  std::set<ConstraintT> newC;
188  for (const auto& c: mInequalities) {
189  if (c.second.is_consistent() == 1) continue;
190  newC.insert(c.second);
191  }
192  for (const auto& c: mDerivedEqualities) {
193  if (c.second.is_consistent() == 1) continue;
194  newC.insert(c.second);
195  }
196 
197  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "Old state:" << std::endl << oldC);
198  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "New state:" << std::endl << newC);
199 
200  std::vector<ConstraintT> toAdd;
201  std::vector<ConstraintT> toRemove;
202 
203  for (const auto& c: newC) {
204  if (oldC.find(c) == oldC.end()) toAdd.emplace_back(c);
205  }
206  for (const auto& c: oldC) {
207  if (newC.find(c.first) == newC.end()) toRemove.emplace_back(c.first);
208  }
209 
210  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "To add:" << std::endl << toAdd);
211  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "To remove:" << std::endl << toRemove);
212 
213  return {toAdd, toRemove};
214  }
215 
216  std::set<FormulaT> getConflict() const;
217  void postprocessConflict(std::set<FormulaT>& mis) const;
218 
219  FormulaT simplify(const FormulaT& f) const {
220  return carl::substitute(f, mModel);
221  }
222 };
223 
224 inline std::ostream& operator<<(std::ostream& os, const CADPreprocessor& cadpp) {
225  os << "Equalities: " << cadpp.mEqualities << std::endl;
226  os << "Inequalities: " << cadpp.mInequalities << std::endl;
227  os << "Derived: " << cadpp.mDerivedEqualities << std::endl;
228  os << "Model: " << cadpp.mModel << std::endl;
229  os << "Reasons: " << cadpp.mAssignments.reasons() << std::endl;
230  return os;
231 }
232 
233 
234 
235 }
CADPreprocessor(const std::vector< carl::Variable > &vars)
std::vector< ConstraintT > mEqualities
Equalities from the input.
std::map< ConstraintT, ConstraintT > mDerivedEqualities
Derived set of equalities, essentially mEqualities - mModel.
friend std::ostream & operator<<(std::ostream &os, const CADPreprocessor &cadpp)
bool collectOriginsOfConflict(std::set< FormulaT > &conflict, const std::map< ConstraintT, ConstraintT > &constraints) const
Replace constraints that have been modified by its origins in the given conflict.
bool addEqualities(const std::vector< ConstraintT > &constraints)
std::set< FormulaT > getConflict() const
void postprocessConflict(std::set< FormulaT > &mis) const
preprocessor::ResultantRule mResultants
The resultant rule.
void removeEquality(const ConstraintT &c)
std::optional< std::set< FormulaT > > mConflict
preprocessor::AssignmentCollector mAssignments
The assignment collector.
void removeConstraint(const ConstraintT &c)
bool addModelToConflict(std::set< FormulaT > &conflict, carl::Variables &added) const
const Model & model() const
bool preprocess()
Performs the preprocessing.
preprocessor::ConstraintUpdate result(const Map &oldC) const
Model mModel
The model used for variable elimination.
std::vector< ConstraintT > collectDerivedEqualities() const
const auto & equalities() const
void addConstraint(const ConstraintT &c)
std::map< ConstraintT, ConstraintT > mInequalities
Inequalities from the input.
preprocessor::Origins mOrigins
Origins of new formulas.
const auto & inequalities() const
const std::vector< carl::Variable > & mVars
Variable ordering.
FormulaT simplify(const FormulaT &f) const
bool extractAssignments(std::map< ConstraintT, ConstraintT > &constraints)
std::map< ConstraintT, carl::Variable > mConstraints
std::variant< bool, ConstraintT > CollectionResult
Result of an assignment collection.
CollectionResult simplify(std::map< ConstraintT, ConstraintT > &constraints)
CollectionResult collect(std::map< ConstraintT, ConstraintT > &constraints)
bool extractParametricAssignments(std::map< ConstraintT, ConstraintT > &constraints)
bool extractValueAssignments(std::map< ConstraintT, ConstraintT > &constraints)
std::map< carl::Variable, ConstraintT > mReasons
Reasons for the assignment of variables.
std::vector< std::vector< UPoly > > mData
std::vector< ConstraintT > mNewECs
std::vector< ConstraintT > mConstraints
std::optional< std::vector< FormulaT > > compute(const std::map< ConstraintT, ConstraintT > &constraints)
std::optional< std::vector< FormulaT > > computeResultants(std::size_t level)
const std::vector< carl::Variable > & mVars
ResultantRule(Origins &origins, const std::vector< carl::Variable > &vars)
std::ostream & operator<<(std::ostream &os, const SampleIteratorQueue< I, C > &siq)
carl::UnivariatePolynomial< Poly > UPoly
Definition: common.h:17
const auto & settings_cadpp()
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
Definition: VSHelper.h:138
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::Model< Rational, Poly > Model
Definition: model.h:13
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
carl::Constraint< Poly > ConstraintT
Definition: types.h:29
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
static void register_settings(SettingsParser &parser)
const std::vector< FormulaT > & get(const FormulaT &f) const
bool resolve(std::set< FormulaT > &conflict) const
void cleanOrigins(const FormulaT &f)
void add(const FormulaT &f, const std::vector< FormulaT > &origin)
std::map< FormulaT, std::vector< std::vector< FormulaT > > > mOrigins