SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ExecutionState.h
Go to the documentation of this file.
2 #include <smtrat-common/model.h>
3 
4 namespace smtrat {
5 namespace execution {
6 
7 enum Mode {
8  START=0, ASSERT=1, SAT=2, UNSAT=3
9 };
10 
11 
12 struct Assertion {
14 };
15 struct SoftAssertion {
18  std::string id;
19 };
20 struct Objective {
21  Poly function;
22  bool minimize;
23 };
24 
27 
28  carl::Logic mLogic;
29 
30  std::vector<Assertion> mAssertions;
31  std::vector<SoftAssertion> mSoftAssertions;
32  std::vector<Objective> mObjectives;
33  std::vector<std::pair<FormulaT, std::string>> mAnnotatedNames;
34  std::vector<std::tuple<size_t,size_t,size_t,size_t>> mBacktrackPoints;
35 
38 
39 
40  std::function<void (const FormulaT&)> addAssertionHandler;
41  std::function<void (const FormulaT&, Rational, const std::string&)> addSoftAssertionHandler;
42  std::function<void (const Poly&, bool)> addObjectiveHandler;
43  std::function<void (const FormulaT&, const std::string&)> addAnnotatedNameHandler;
44  std::function<void (const FormulaT&)> removeAssertionHandler;
45  std::function<void (const FormulaT&)> removeSoftAssertionHandler;
46  std::function<void (const Poly&)> removeObjectiveHandler;
47  std::function<void (const FormulaT&)> removeAnnotatedNameHandler;
48 
49  void set_mode(Mode mode) {
50  mMode = mode;
51  }
52 
53 public:
55 
56  void set_add_assertion_handler(std::function<void (const FormulaT&)> f) { addAssertionHandler = f; }
57  void set_add_soft_assertion_handler(std::function<void (const FormulaT&, Rational, const std::string&)> f) { addSoftAssertionHandler = f; }
58  void set_add_objective_handler(std::function<void (const Poly&, bool)> f) { addObjectiveHandler = f; }
59  void set_add_annotated_name_handler(std::function<void (const FormulaT&, const std::string&)> f) { addAnnotatedNameHandler = f; }
60  void set_remove_assertion_handler(std::function<void (const FormulaT&)> f) { removeAssertionHandler = f; }
61  void set_remove_soft_assertion_handler(std::function<void (const FormulaT&)> f) { removeSoftAssertionHandler = f; }
62  void set_remove_objective_handler(std::function<void (const Poly&)> f) { removeObjectiveHandler = f; }
63  void set_remove_annotated_name_handler(std::function<void (const FormulaT&)> f) { removeAnnotatedNameHandler = f; }
64 
65 
66  bool is_mode(Mode mode) const {
67  return mMode == mode;
68  }
69 
70  void set_logic(carl::Logic logic) {
71  assert(is_mode(Mode::START));
72  mLogic = logic;
74  }
75  auto logic() const {
76  return mLogic;
77  }
78 
79  void add_assertion(const FormulaT& formula) {
80  reset_answer();
81  assert(is_mode(Mode::ASSERT));
82 
83  // we do not try to resolve any conflict between assertions and soft assertions as the
84  // semantic is ambiguous and there is no standard
85  assert(!has_assertion(formula));
86  assert(!has_soft_assertion(formula));
87  mAssertions.push_back({formula});
88  addAssertionHandler(formula);
89  }
90 
91  bool has_assertion(const FormulaT& formula) const {
92  return std::find_if(mAssertions.begin(), mAssertions.end(), [&formula](const Assertion& a) { return a.formula == formula; } ) != mAssertions.end();
93  }
94 
95  const auto& assertions() const {
96  return mAssertions;
97  }
98 
99  void add_soft_assertion(const FormulaT& formula, Rational weight, const std::string& id) {
100  reset_answer();
101  assert(is_mode(Mode::ASSERT));
102 
103  // we do not try to resolve any conflict between assertions and soft assertions as the
104  // semantic is ambiguous and there is no standard
105  assert(!has_assertion(formula));
106  assert(!has_soft_assertion(formula));
107  mSoftAssertions.push_back({formula, weight, id});
108  addSoftAssertionHandler(formula, weight, id);
109  }
110 
111  bool has_soft_assertion(const FormulaT& formula) const {
112  return std::find_if(mSoftAssertions.begin(), mSoftAssertions.end(), [&formula](const SoftAssertion& a) { return a.formula == formula; } ) != mSoftAssertions.end();
113  }
114 
115  void add_objective(const Poly& function, bool minimize) {
117  reset_answer();
118  }
119  assert(is_mode(Mode::ASSERT));
120 
121  assert(!has_objective(function));
122  mObjectives.push_back({function, minimize});
123  addObjectiveHandler(function, minimize);
124  }
125 
126  bool has_objective(const Poly& function) {
127  return std::find_if(mObjectives.begin(), mObjectives.end(), [&function](const Objective& a) { return a.function == function; }) != mObjectives.end();
128  }
129 
130  void push() {
131  reset_answer();
132  assert(is_mode(Mode::ASSERT));
133 
134  mBacktrackPoints.emplace_back(mAssertions.size(), mSoftAssertions.size(), mObjectives.size(), mAnnotatedNames.size());
135  }
136  void push(size_t n) {
137  for (size_t i = 0; i < n; i++) push();
138  }
139  void pop() {
140  reset_answer();
141  assert(is_mode(Mode::ASSERT));
142 
143  if (mBacktrackPoints.empty()) return;
144 
145  assert(std::get<0>(mBacktrackPoints.back()) <= mAssertions.size());
146  assert(std::get<1>(mBacktrackPoints.back()) <= mSoftAssertions.size());
147  assert(std::get<2>(mBacktrackPoints.back()) <= mObjectives.size());
148  assert(std::get<3>(mBacktrackPoints.back()) <= mAnnotatedNames.size());
149 
150  for (size_t i = mAssertions.size()-1; i >= std::get<0>(mBacktrackPoints.back()) ; i--) {
152  }
153  for (size_t i = mSoftAssertions.size()-1; i >= std::get<1>(mBacktrackPoints.back()) ; i--) {
155  }
156  for (size_t i = mObjectives.size()-1; i >= std::get<2>(mBacktrackPoints.back()) ; i--) {
157  removeObjectiveHandler(mObjectives[i].function);
158  }
159  for (size_t i = mAnnotatedNames.size()-1; i >= std::get<3>(mBacktrackPoints.back()) ; i--) {
161  }
162 
163  mAssertions.erase(mAssertions.begin() + static_cast<long>(std::get<0>(mBacktrackPoints.back())), mAssertions.end());
164  mSoftAssertions.erase(mSoftAssertions.begin() + static_cast<long>(std::get<1>(mBacktrackPoints.back())), mSoftAssertions.end());
165  mObjectives.erase(mObjectives.begin() + static_cast<long>(std::get<2>(mBacktrackPoints.back())), mObjectives.end());
166  mAnnotatedNames.erase(mAnnotatedNames.begin() + static_cast<long>(std::get<3>(mBacktrackPoints.back())), mAnnotatedNames.end());
167 
168  mBacktrackPoints.pop_back();
169  }
170  void pop(size_t n) {
171  for (size_t i = 0; i < n; i++) pop();
172  }
173 
174  bool has_annotated_name(const std::string& n) {
175  return std::find_if(mAnnotatedNames.begin(), mAnnotatedNames.end(), [&n](const auto& a) { return a.second == n; }) != mAnnotatedNames.end();
176  }
177 
179  return std::find_if(mAnnotatedNames.begin(), mAnnotatedNames.end(), [&f](const auto& a) { return a.first == f; }) != mAnnotatedNames.end();
180  }
181 
182  void annotate_name(const std::string& name, const smtrat::FormulaT& f) {
183  reset_answer();
184  assert(is_mode(Mode::ASSERT));
185 
186  assert(!has_annotated_name(name));
187  assert(!has_annotated_name_formula(f));
188  mAnnotatedNames.emplace_back(f, name);
189  addAnnotatedNameHandler(f, name);
190  }
191 
193  reset_answer();
194  assert(is_mode(Mode::ASSERT));
195  mModel = model;
198  }
199 
200  void enter_unsat() {
201  reset_answer();
202  assert(is_mode(Mode::ASSERT));
204  }
205 
206  const smtrat::Model& model() const {
207  assert(is_mode(Mode::SAT));
208  return mModel;
209  }
210 
212  assert(is_mode(Mode::SAT));
213  return mObjectiveValues;
214  }
215 
216  void reset() {
217  if (!is_mode(Mode::START)) {
218  mLogic = carl::Logic::UNDEFINED;
220  reset_answer();
221  mAssertions.clear();
222  mSoftAssertions.clear();
223  mObjectives.clear();
224  mBacktrackPoints.clear();
225  mAnnotatedNames.clear();
226  }
228  }
229  }
230 
232  if (!is_mode(Mode::START)) {
233  while (!mBacktrackPoints.empty()) pop();
234  mBacktrackPoints.emplace_back(0,0,0,0);
235  pop();
236  assert(is_mode(Mode::ASSERT));
237  }
238  }
239 
240  void reset_answer() {
242  mModel.clear();
243  mObjectiveValues.clear();
245  }
246  }
247 
248 
249 };
250 
251 }
252 }
smtrat::ObjectiveValues mObjectiveValues
void add_assertion(const FormulaT &formula)
void set_add_soft_assertion_handler(std::function< void(const FormulaT &, Rational, const std::string &)> f)
bool has_objective(const Poly &function)
std::vector< Assertion > mAssertions
std::function< void(const Poly &, bool)> addObjectiveHandler
const auto & assertions() const
std::function< void(const Poly &)> removeObjectiveHandler
void set_remove_objective_handler(std::function< void(const Poly &)> f)
void set_add_assertion_handler(std::function< void(const FormulaT &)> f)
std::vector< std::pair< FormulaT, std::string > > mAnnotatedNames
void set_remove_annotated_name_handler(std::function< void(const FormulaT &)> f)
bool has_soft_assertion(const FormulaT &formula) const
bool has_assertion(const FormulaT &formula) const
bool has_annotated_name_formula(const smtrat::FormulaT &f)
const smtrat::Model & model() const
void set_add_objective_handler(std::function< void(const Poly &, bool)> f)
bool has_annotated_name(const std::string &n)
std::function< void(const FormulaT &)> addAssertionHandler
std::function< void(const FormulaT &)> removeAssertionHandler
void add_soft_assertion(const FormulaT &formula, Rational weight, const std::string &id)
std::vector< Objective > mObjectives
void annotate_name(const std::string &name, const smtrat::FormulaT &f)
bool is_mode(Mode mode) const
std::function< void(const FormulaT &, const std::string &)> addAnnotatedNameHandler
std::function< void(const FormulaT &)> removeSoftAssertionHandler
std::vector< SoftAssertion > mSoftAssertions
void set_logic(carl::Logic logic)
std::function< void(const FormulaT &)> removeAnnotatedNameHandler
void enter_sat(const smtrat::Model &model, const ObjectiveValues &objectiveValues)
void set_add_annotated_name_handler(std::function< void(const FormulaT &, const std::string &)> f)
void add_objective(const Poly &function, bool minimize)
std::vector< std::tuple< size_t, size_t, size_t, size_t > > mBacktrackPoints
const smtrat::ObjectiveValues & objectiveValues() const
std::function< void(const FormulaT &, Rational, const std::string &)> addSoftAssertionHandler
void set_remove_soft_assertion_handler(std::function< void(const FormulaT &)> f)
void set_remove_assertion_handler(std::function< void(const FormulaT &)> f)
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
std::vector< std::pair< std::variant< Poly, std::string >, Model::mapped_type > > ObjectiveValues
Definition: model.h:32
carl::Model< Rational, Poly > Model
Definition: model.h:13
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
mpq_class Rational
Definition: types.h:19