SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Preprocessor.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <smtrat-common/model.h>
7 
8 namespace smtrat::cad {
9 
12  bool disable_resultants = false;
13 
14  static void register_settings(SettingsParser& parser) {
15  namespace po = boost::program_options;
16  auto& settings = settings::Settings::getInstance();
17  auto& s = settings.get<PreprocessorSettings>("cad-pp2");
18 
19  parser.add("CAD Preprocessor settings").add_options()
20  ("cad.pp.no-elimination", po::bool_switch(&s.disable_variable_elimination), "disable variable elimination")
21  ("cad.pp.no-resultants", po::bool_switch(&s.disable_resultants), "disable resultant rule")
22  ;
23  }
24  static bool register_hook() {
25  SettingsComponents::getInstance().add(&register_settings);
26  return true;
27  }
28  static const bool dummy;
29 };
30 
31 inline const auto& settings_cadpp() {
32  static const auto& s = settings::Settings::getInstance().get<PreprocessorSettings>("cad-pp2");
33  return s;
34 }
35 
36 namespace preprocessor {
37  struct ConstraintUpdate {
38  std::vector<ConstraintT> toAdd;
39  std::vector<ConstraintT> toRemove;
40  };
41 }
42 
43 class Preprocessor {
44 public:
45  friend std::ostream& operator<<(std::ostream& os, const Preprocessor& cadpp);
46 private:
47  using Origins = std::vector<ConstraintT>;
48  using Trail = std::vector<std::pair<ConstraintT,Origins>>;
49 
50  /// Variable ordering.
51  const std::vector<carl::Variable>& mVars;
52  /// Origins for the assignments.
53  std::map<ConstraintT,carl::Variable> mAssignments;
54  /// The model used for variable elimination.
56  /// The trail.
58  /// Next element to be processed.
59  std::size_t mTrailID = 0;
60  /// Current set of equalities.
61  std::set<ConstraintT> mCurrent;
62  /// A possibly found conflict.
63  std::optional<std::set<FormulaT>> mConflict;
64 
65  void apply_assignments(const ConstraintT& c);
66  void resolve_conflict();
67  carl::Variable main_variable_of(const ConstraintT& c) const;
68 
69  bool try_variable_elimination(const ConstraintT& cur);
70  void compute_resultants(const ConstraintT& cur);
71 public:
72  Preprocessor(const std::vector<carl::Variable>& vars):
73  mVars(vars)
74  {}
75  void clear() {
76  mAssignments.clear();
77  mModel.clear();
78  mTrail.clear();
79  mTrailID = 0;
80  mCurrent.clear();
81  mConflict = std::nullopt;
82  }
83  void addConstraint(const ConstraintT& c);
84  void removeConstraint(const ConstraintT& c);
85  bool preprocess();
86  void postprocessConflict(std::set<FormulaT>& mis) const;
87 
88  const Model& model() const {
89  return mModel;
90  }
91  const auto& getConflict() const {
92  assert(mConflict);
93  return *mConflict;
94  }
95  FormulaT simplify(const FormulaT& f) const {
96  return carl::substitute(f, mModel);
97  }
98 
99  template<typename Map>
100  preprocessor::ConstraintUpdate result(const Map& oldC) const {
101  std::set<ConstraintT> newC = mCurrent;
102 
103  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "Old state:" << std::endl << oldC);
104  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "New state:" << std::endl << newC);
105 
106  std::vector<ConstraintT> toAdd;
107  std::vector<ConstraintT> toRemove;
108 
109  for (const auto& c: newC) {
110  if (oldC.find(c) == oldC.end()) toAdd.emplace_back(c);
111  }
112  for (const auto& c: oldC) {
113  if (newC.find(c.first) == newC.end()) toRemove.emplace_back(c.first);
114  }
115 
116  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "To add:" << std::endl << toAdd);
117  SMTRAT_LOG_DEBUG("smtrat.cad.pp", "To remove:" << std::endl << toRemove);
118 
119  return {toAdd, toRemove};
120  }
121 };
122 
123 inline std::ostream& operator<<(std::ostream& os, const Preprocessor& cadpp) {
124  os << "Current Trail:" << std::endl;
125  for (const auto& t: cadpp.mTrail) {
126  if (cadpp.mTrailID < cadpp.mTrail.size() && cadpp.mTrail[cadpp.mTrailID] == t) {
127  os << "-->";
128  }
129  os << "\t" << t.first << "\t" << t.second << std::endl;
130  }
131  os << "Model: " << cadpp.mModel << std::endl;
132  os << "from " << cadpp.mAssignments << std::endl;
133  os << "Current: " << cadpp.mCurrent << std::endl;
134  return os;
135 }
136 
137 }
void addConstraint(const ConstraintT &c)
void apply_assignments(const ConstraintT &c)
carl::Variable main_variable_of(const ConstraintT &c) const
const Model & model() const
Definition: Preprocessor.h:88
FormulaT simplify(const FormulaT &f) const
Definition: Preprocessor.h:95
const auto & getConflict() const
Definition: Preprocessor.h:91
preprocessor::ConstraintUpdate result(const Map &oldC) const
Definition: Preprocessor.h:100
friend std::ostream & operator<<(std::ostream &os, const Preprocessor &cadpp)
Definition: Preprocessor.h:123
std::optional< std::set< FormulaT > > mConflict
A possibly found conflict.
Definition: Preprocessor.h:63
void removeConstraint(const ConstraintT &c)
void compute_resultants(const ConstraintT &cur)
std::size_t mTrailID
Next element to be processed.
Definition: Preprocessor.h:59
std::map< ConstraintT, carl::Variable > mAssignments
Origins for the assignments.
Definition: Preprocessor.h:53
const std::vector< carl::Variable > & mVars
Variable ordering.
Definition: Preprocessor.h:51
Trail mTrail
The trail.
Definition: Preprocessor.h:57
Model mModel
The model used for variable elimination.
Definition: Preprocessor.h:55
std::vector< ConstraintT > Origins
Definition: Preprocessor.h:47
std::vector< std::pair< ConstraintT, Origins > > Trail
Definition: Preprocessor.h:48
std::set< ConstraintT > mCurrent
Current set of equalities.
Definition: Preprocessor.h:61
bool try_variable_elimination(const ConstraintT &cur)
Preprocessor(const std::vector< carl::Variable > &vars)
Definition: Preprocessor.h:72
void postprocessConflict(std::set< FormulaT > &mis) const
std::ostream & operator<<(std::ostream &os, const SampleIteratorQueue< I, C > &siq)
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::Constraint< Poly > ConstraintT
Definition: types.h:29
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
static void register_settings(SettingsParser &parser)
Definition: Preprocessor.h:14