Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Z3SmtSolver.cpp
Go to the documentation of this file.
2
3#include <memory>
4
9
10namespace storm {
11namespace solver {
12#ifdef STORM_HAVE_Z3
13Z3SmtSolver::Z3ModelReference::Z3ModelReference(storm::expressions::ExpressionManager const& manager, z3::model const& model,
14 storm::adapters::Z3ExpressionAdapter& expressionAdapter)
15 : ModelReference(manager), model(model), expressionAdapter(expressionAdapter) {
16 // Intentionally left empty.
17}
18#endif
19
21#ifdef STORM_HAVE_Z3
22 STORM_LOG_ASSERT(variable.getManager() == this->getManager(), "Requested variable is managed by a different manager.");
23 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
24 z3::expr z3ExprValuation = model.eval(z3Expr, true);
25 return this->expressionAdapter.translateExpression(z3ExprValuation).isTrue();
26#else
27 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
28#endif
29}
30
32#ifdef STORM_HAVE_Z3
33 STORM_LOG_ASSERT(variable.getManager() == this->getManager(), "Requested variable is managed by a different manager.");
34 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
35 z3::expr z3ExprValuation = model.eval(z3Expr, true);
36 return this->expressionAdapter.translateExpression(z3ExprValuation).evaluateAsInt();
37#else
38 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
39#endif
40}
41
43#ifdef STORM_HAVE_Z3
44 STORM_LOG_ASSERT(variable.getManager() == this->getManager(), "Requested variable is managed by a different manager.");
45 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
46 z3::expr z3ExprValuation = model.eval(z3Expr, true);
47 return this->expressionAdapter.translateExpression(z3ExprValuation).evaluateAsDouble();
48#else
49 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
50#endif
51}
52
54#ifdef STORM_HAVE_Z3
55 std::stringstream sstr;
56 sstr << model;
57 return sstr.str();
58#else
59 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
60#endif
61}
62
64 : SmtSolver(manager)
65#ifdef STORM_HAVE_Z3
66 ,
67 context(nullptr),
68 solver(nullptr),
69 expressionAdapter(nullptr),
70 lastCheckAssumptions(false),
71 lastResult(CheckResult::Unknown)
72#endif
73{
74#ifdef STORM_HAVE_Z3
75 z3::config config;
76 config.set("model", true);
77 context = std::make_unique<z3::context>(config);
78 solver = std::make_unique<z3::solver>(*context);
79 // Do not let z3 install its own SIGINT handler (the "ctrl_c" parameter): if we did, z3 would swallow interrupts
80 // during check() and just return "unknown" instead of letting Storm's signal handler abort the computation.
81 z3::params params(*context);
82 params.set("ctrl_c", false);
83 solver->set(params);
84 expressionAdapter = std::make_unique<storm::adapters::Z3ExpressionAdapter>(this->getManager(), *context);
85#endif
86}
87
89 // Intentionally left empty.
90}
91
93#ifdef STORM_HAVE_Z3
94 this->solver->push();
95#else
96 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
97#endif
98}
99
101#ifdef STORM_HAVE_Z3
102 this->solver->pop();
103#else
104 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
105#endif
106}
107
108void Z3SmtSolver::pop(uint_fast64_t n) {
109#ifdef STORM_HAVE_Z3
110 this->solver->pop(static_cast<unsigned int>(n));
111#else
112 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
113#endif
114}
115
117#ifdef STORM_HAVE_Z3
118 this->solver->reset();
119#else
120 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
121#endif
122}
123
125#ifdef STORM_HAVE_Z3
126 this->solver->add(expressionAdapter->translateExpression(assertion));
127#else
128 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
129#endif
130}
131
132void Z3SmtSolver::addNotCurrentModel(bool performSolverReset) {
133#ifdef STORM_HAVE_Z3
134 STORM_LOG_THROW(this->lastResult == SmtSolver::CheckResult::Sat, storm::exceptions::InvalidStateException,
135 "Unable to create model for formula that was not determined to be satisfiable.");
136
137 auto currentModel = this->solver->get_model();
138 z3::expr notThisModel = currentModel.ctx().bool_val(true);
139 for (auto const& variable : this->getManager().getVariables()) {
140 z3::expr var = this->expressionAdapter->translateExpression(variable);
141 auto value = currentModel.eval(var);
142 if (notThisModel.is_const()) {
143 notThisModel = var == value;
144 } else {
145 notThisModel = notThisModel && (var == value);
146 }
147 }
148 // https://stackoverflow.com/questions/78261966/z3-non-incremental-search-for-multiple-models-works-incremental-search-does-no
149 if (performSolverReset) {
150 auto const allAssertions = this->solver->assertions();
151 solver->reset();
152 for (auto const& assertion : allAssertions) {
153 solver->add(assertion);
154 }
155 }
156 this->solver->add(!notThisModel);
157#else
158 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
159#endif
160}
161
163#ifdef STORM_HAVE_Z3
164 lastCheckAssumptions = false;
165 switch (this->solver->check()) {
166 case z3::sat:
167 this->lastResult = SmtSolver::CheckResult::Sat;
168 break;
169 case z3::unsat:
170 this->lastResult = SmtSolver::CheckResult::Unsat;
171 break;
172 default:
173 this->lastResult = SmtSolver::CheckResult::Unknown;
174 break;
175 }
176 return this->lastResult;
177#else
178 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
179#endif
180}
181
183#ifdef STORM_HAVE_Z3
184 lastCheckAssumptions = true;
185 z3::expr_vector z3Assumptions(*this->context);
186
187 for (storm::expressions::Expression assumption : assumptions) {
188 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
189 }
190
191 switch (this->solver->check(z3Assumptions)) {
192 case z3::sat:
193 this->lastResult = SmtSolver::CheckResult::Sat;
194 break;
195 case z3::unsat:
196 this->lastResult = SmtSolver::CheckResult::Unsat;
197 break;
198 default:
199 this->lastResult = SmtSolver::CheckResult::Unknown;
200 break;
201 }
202 return this->lastResult;
203#else
204 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
205#endif
206}
207
208SmtSolver::CheckResult Z3SmtSolver::checkWithAssumptions(std::initializer_list<storm::expressions::Expression> const& assumptions) {
209#ifdef STORM_HAVE_Z3
210 lastCheckAssumptions = true;
211 z3::expr_vector z3Assumptions(*this->context);
212
213 for (storm::expressions::Expression assumption : assumptions) {
214 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
215 }
216
217 switch (this->solver->check(z3Assumptions)) {
218 case z3::sat:
219 this->lastResult = SmtSolver::CheckResult::Sat;
220 break;
221 case z3::unsat:
222 this->lastResult = SmtSolver::CheckResult::Unsat;
223 break;
224 default:
225 this->lastResult = SmtSolver::CheckResult::Unknown;
226 break;
227 }
228 return this->lastResult;
229#else
230 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
231#endif
232}
233
235#ifdef STORM_HAVE_Z3
236 STORM_LOG_THROW(this->lastResult == SmtSolver::CheckResult::Sat, storm::exceptions::InvalidStateException,
237 "Unable to create model for formula that was not determined to be satisfiable.");
238 return this->convertZ3ModelToValuation(this->solver->get_model());
239#else
240 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
241#endif
242}
243
244std::shared_ptr<SmtSolver::ModelReference> Z3SmtSolver::getModel() {
245#ifdef STORM_HAVE_Z3
246 STORM_LOG_THROW(this->lastResult == SmtSolver::CheckResult::Sat, storm::exceptions::InvalidStateException,
247 "Unable to create model for formula that was not determined to be satisfiable.");
248 return std::shared_ptr<SmtSolver::ModelReference>(new Z3ModelReference(this->getManager(), this->solver->get_model(), *this->expressionAdapter));
249#else
250 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
251#endif
252}
253
254#ifdef STORM_HAVE_Z3
255storm::expressions::SimpleValuation Z3SmtSolver::convertZ3ModelToValuation(z3::model const& model) {
256 storm::expressions::SimpleValuation stormModel(this->getManager().getSharedPointer());
257
258 for (unsigned i = 0; i < model.num_consts(); ++i) {
259 z3::func_decl variableI = model.get_const_decl(i);
260 storm::expressions::Variable stormVariable = this->expressionAdapter->getVariable(variableI);
261 storm::expressions::Expression variableInterpretation = this->expressionAdapter->translateExpression(model.get_const_interp(variableI));
262
263 if (variableInterpretation.getType().isBooleanType()) {
264 stormModel.setBooleanValue(this->getManager().getVariable(variableI.name().str()), variableInterpretation.isTrue());
265 } else if (variableInterpretation.getType().isIntegerType()) {
266 stormModel.setIntegerValue(this->getManager().getVariable(variableI.name().str()), variableInterpretation.evaluateAsInt());
267 } else if (variableInterpretation.getType().isRationalType()) {
268 stormModel.setRationalValue(this->getManager().getVariable(variableI.name().str()), variableInterpretation.evaluateAsDouble());
269 } else {
270 STORM_LOG_ASSERT(false, "Variable interpretation in model is not of type bool, int or rational.");
271 }
272 }
273
274 return stormModel;
275}
276#endif
277
278std::vector<storm::expressions::SimpleValuation> Z3SmtSolver::allSat(std::vector<storm::expressions::Variable> const& important) {
279#ifdef STORM_HAVE_Z3
280 std::vector<storm::expressions::SimpleValuation> valuations;
281 this->allSat(important, static_cast<std::function<bool(storm::expressions::SimpleValuation&)>>(
282 [&valuations](storm::expressions::SimpleValuation const& valuation) -> bool {
283 valuations.push_back(valuation);
284 return true;
285 }));
286 return valuations;
287#else
288 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
289#endif
290}
291
292uint_fast64_t Z3SmtSolver::allSat(std::vector<storm::expressions::Variable> const& important,
293 std::function<bool(storm::expressions::SimpleValuation&)> const& callback) {
294#ifdef STORM_HAVE_Z3
295 for (storm::expressions::Variable const& variable : important) {
296 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException, "The important atoms for AllSat must be boolean variables.");
297 }
298
299 uint_fast64_t numberOfModels = 0;
300 bool proceed = true;
301
302 // Save the current assertion stack, to be able to backtrack after the procedure.
303 this->push();
304
305 // Enumerate models as long as the conjunction is satisfiable and the callback has not aborted the enumeration.
306 while (proceed && this->check() == CheckResult::Sat) {
307 ++numberOfModels;
308 z3::model model = this->solver->get_model();
309
310 z3::expr modelExpr = this->context->bool_val(true);
311 storm::expressions::SimpleValuation valuation(this->getManager().getSharedPointer());
312
313 for (storm::expressions::Variable const& importantAtom : important) {
314 z3::expr z3ImportantAtom = this->expressionAdapter->translateExpression(importantAtom.getExpression());
315 z3::expr z3ImportantAtomValuation = model.eval(z3ImportantAtom, true);
316 modelExpr = modelExpr && (z3ImportantAtom == z3ImportantAtomValuation);
317 valuation.setBooleanValue(importantAtom, this->expressionAdapter->translateExpression(z3ImportantAtomValuation).isTrue());
318 }
319
320 // Check if we are required to proceed, and if so rule out the current model.
321 proceed = callback(valuation);
322 if (proceed) {
323 this->solver->add(!modelExpr);
324 }
325 }
326
327 // Restore the old assertion stack and return.
328 this->pop();
329 return numberOfModels;
330#else
331 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
332#endif
333}
334
335uint_fast64_t Z3SmtSolver::allSat(std::vector<storm::expressions::Variable> const& important, std::function<bool(SmtSolver::ModelReference&)> const& callback) {
336#ifdef STORM_HAVE_Z3
337 for (storm::expressions::Variable const& variable : important) {
338 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException, "The important atoms for AllSat must be boolean variables.");
339 }
340
341 uint_fast64_t numberOfModels = 0;
342 bool proceed = true;
343
344 // Save the current assertion stack, to be able to backtrack after the procedure.
345 this->push();
346
347 // Enumerate models as long as the conjunction is satisfiable and the callback has not aborted the enumeration.
348 while (proceed && this->check() == CheckResult::Sat) {
349 ++numberOfModels;
350 z3::model model = this->solver->get_model();
351
352 z3::expr modelExpr = this->context->bool_val(true);
353 storm::expressions::SimpleValuation valuation(this->getManager().getSharedPointer());
354
355 for (storm::expressions::Variable const& importantAtom : important) {
356 z3::expr z3ImportantAtom = this->expressionAdapter->translateExpression(importantAtom.getExpression());
357 z3::expr z3ImportantAtomValuation = model.eval(z3ImportantAtom, true);
358 modelExpr = modelExpr && (z3ImportantAtom == z3ImportantAtomValuation);
359 }
360 Z3ModelReference modelRef(this->getManager(), model, *expressionAdapter);
361
362 // Check if we are required to proceed, and if so rule out the current model.
363 proceed = callback(modelRef);
364 if (proceed) {
365 this->solver->add(!modelExpr);
366 }
367 }
368
369 this->pop();
370 return numberOfModels;
371#else
372 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
373#endif
374}
375
376std::vector<storm::expressions::Expression> Z3SmtSolver::getUnsatAssumptions() {
377#ifdef STORM_HAVE_Z3
378 STORM_LOG_THROW(lastResult == SmtSolver::CheckResult::Unsat, storm::exceptions::InvalidStateException,
379 "Unable to generate unsatisfiable core of assumptions, because the last check did not determine the formulas to be unsatisfiable.");
380 STORM_LOG_THROW(lastCheckAssumptions, storm::exceptions::InvalidStateException,
381 "Unable to generate unsatisfiable core of assumptions, because the last check did not involve assumptions.");
382
383 z3::expr_vector z3UnsatAssumptions = this->solver->unsat_core();
384 std::vector<storm::expressions::Expression> unsatAssumptions;
385
386 for (unsigned int i = 0; i < z3UnsatAssumptions.size(); ++i) {
387 unsatAssumptions.push_back(this->expressionAdapter->translateExpression(z3UnsatAssumptions[i]));
388 }
389
390 return unsatAssumptions;
391#else
392 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
393#endif
394}
395
396void Z3SmtSolver::setTimeout(uint_fast64_t milliseconds) {
397#ifdef STORM_HAVE_Z3
398 z3::params paramObject(*context);
399 paramObject.set(":timeout", static_cast<unsigned>(milliseconds));
400 solver->set(paramObject);
401#else
402 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
403#endif
404}
405
407#ifdef STORM_HAVE_Z3
408 z3::params paramObject(*context);
409 paramObject.set(":timeout", 0u);
410 solver->set(paramObject);
411#else
412 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
413#endif
414}
415
416std::string Z3SmtSolver::getSmtLibString() const {
417#ifdef STORM_HAVE_Z3
418 return solver->to_smt2();
419#else
420 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Storm is compiled without Z3 support.");
421#endif
422}
423
424} // namespace solver
425} // namespace storm
int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
double evaluateAsDouble(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
Type const & getType() const
Retrieves the type of the expression.
bool isTrue() const
Checks if the expression is equal to the boolean literal true.
This class is responsible for managing a set of typed variables and all expressions using these varia...
A simple implementation of the valuation interface.
virtual void setBooleanValue(Variable const &booleanVariable, bool value) override
Sets the value of the given boolean variable to the provided value.
bool isBooleanType() const
Checks whether this type is a boolean type.
Definition Type.cpp:194
bool isIntegerType() const
Checks whether this type is an integral type.
Definition Type.cpp:198
bool isRationalType() const
Checks whether this type is a rational type.
Definition Type.cpp:234
ExpressionManager const & getManager() const
Retrieves the manager responsible for this variable.
Definition Variable.cpp:54
The base class for all model references.
Definition SmtSolver.h:30
storm::expressions::ExpressionManager const & getManager() const
Retrieves the expression manager associated with the solver.
Definition SmtSolver.cpp:79
SmtSolver(storm::expressions::ExpressionManager &manager)
Constructs a new Smt solver with the given options.
Definition SmtSolver.cpp:17
CheckResult
possible check results
Definition SmtSolver.h:24
virtual bool getBooleanValue(storm::expressions::Variable const &variable) const override
virtual std::string toString() const override
virtual int_fast64_t getIntegerValue(storm::expressions::Variable const &variable) const override
virtual double getRationalValue(storm::expressions::Variable const &variable) const override
virtual void add(storm::expressions::Expression const &assertion) override
Adds an assertion to the solver's stack.
virtual std::shared_ptr< SmtSolver::ModelReference > getModel() override
If the last call to check() or checkWithAssumptions() returned Sat, this method retrieves a model tha...
virtual storm::expressions::SimpleValuation getModelAsValuation() override
If the last call to check() or checkWithAssumptions() returned Sat, this method retrieves a model tha...
virtual std::string getSmtLibString() const override
If supported by the solver, this function returns the current assertions in the SMT-LIB format.
Z3SmtSolver(storm::expressions::ExpressionManager &manager)
virtual std::vector< storm::expressions::SimpleValuation > allSat(std::vector< storm::expressions::Variable > const &important) override
Performs AllSat over the (provided) important atoms.
virtual CheckResult checkWithAssumptions(storm::expressions::ExpressionSet const &assumptions) override
Checks whether the conjunction of assertions that are currently on the solver's stack together with t...
virtual void reset() override
Removes all assertions from the solver's stack.
virtual void push() override
Pushes a backtracking point on the solver's stack.
virtual std::vector< storm::expressions::Expression > getUnsatAssumptions() override
If the last call to checkWithAssumptions() returned Unsat, this function can be used to retrieve a su...
virtual void setTimeout(uint_fast64_t milliseconds) override
Limits each subsequent satisfiability query to the given number of milliseconds.
virtual void addNotCurrentModel(bool performSolverReset=true) override
If supported by the solver, this function tells the SMT solver to produce a model different from the ...
virtual void unsetTimeout() override
Unsets a previous timeout such that subsequent queries are no longer limited in time.
virtual void pop() override
Pops a backtracking point from the solver's stack.
virtual CheckResult check() override
Checks whether the conjunction of assertions that are currently on the solver's stack is satisfiable.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
std::set< Expression, ExpressionComparator > ExpressionSet
Definition Expression.h:515
SettingsManager const & manager()
Retrieves the settings manager.
The contract of a DD backend.