13Z3SmtSolver::Z3ModelReference::Z3ModelReference(storm::expressions::ExpressionManager
const& manager, z3::model
const& model,
14 storm::adapters::Z3ExpressionAdapter& expressionAdapter)
15 : ModelReference(
manager), model(model), expressionAdapter(expressionAdapter) {
23 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
24 z3::expr z3ExprValuation = model.eval(z3Expr,
true);
25 return this->expressionAdapter.translateExpression(z3ExprValuation).isTrue();
27 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
34 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
35 z3::expr z3ExprValuation = model.eval(z3Expr,
true);
36 return this->expressionAdapter.translateExpression(z3ExprValuation).evaluateAsInt();
38 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
45 z3::expr z3Expr = this->expressionAdapter.translateExpression(variable);
46 z3::expr z3ExprValuation = model.eval(z3Expr,
true);
47 return this->expressionAdapter.translateExpression(z3ExprValuation).evaluateAsDouble();
49 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
55 std::stringstream sstr;
59 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
69 expressionAdapter(nullptr),
70 lastCheckAssumptions(false),
76 config.set(
"model",
true);
77 context = std::make_unique<z3::context>(config);
78 solver = std::make_unique<z3::solver>(*context);
81 z3::params params(*context);
82 params.set(
"ctrl_c",
false);
84 expressionAdapter = std::make_unique<storm::adapters::Z3ExpressionAdapter>(this->
getManager(), *context);
96 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
104 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
110 this->
solver->pop(
static_cast<unsigned int>(n));
112 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
120 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
126 this->
solver->add(expressionAdapter->translateExpression(assertion));
128 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
135 "Unable to create model for formula that was not determined to be satisfiable.");
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;
145 notThisModel = notThisModel && (var == value);
149 if (performSolverReset) {
150 auto const allAssertions = this->
solver->assertions();
152 for (
auto const& assertion : allAssertions) {
156 this->
solver->add(!notThisModel);
158 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
164 lastCheckAssumptions =
false;
165 switch (this->
solver->check()) {
176 return this->lastResult;
178 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
184 lastCheckAssumptions =
true;
185 z3::expr_vector z3Assumptions(*this->context);
188 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
191 switch (this->
solver->check(z3Assumptions)) {
202 return this->lastResult;
204 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
210 lastCheckAssumptions =
true;
211 z3::expr_vector z3Assumptions(*this->context);
214 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
217 switch (this->
solver->check(z3Assumptions)) {
228 return this->lastResult;
230 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
237 "Unable to create model for formula that was not determined to be satisfiable.");
238 return this->convertZ3ModelToValuation(this->
solver->get_model());
240 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
247 "Unable to create model for formula that was not determined to be satisfiable.");
250 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
258 for (
unsigned i = 0; i < model.num_consts(); ++i) {
259 z3::func_decl variableI = model.get_const_decl(i);
264 stormModel.setBooleanValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
isTrue());
266 stormModel.setIntegerValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
evaluateAsInt());
268 stormModel.setRationalValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
evaluateAsDouble());
270 STORM_LOG_ASSERT(
false,
"Variable interpretation in model is not of type bool, int or rational.");
278std::vector<storm::expressions::SimpleValuation>
Z3SmtSolver::allSat(std::vector<storm::expressions::Variable>
const& important) {
280 std::vector<storm::expressions::SimpleValuation> valuations;
283 valuations.push_back(valuation);
288 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
296 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException,
"The important atoms for AllSat must be boolean variables.");
299 uint_fast64_t numberOfModels = 0;
308 z3::model model = this->
solver->get_model();
310 z3::expr modelExpr = this->context->bool_val(
true);
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());
321 proceed = callback(valuation);
323 this->
solver->add(!modelExpr);
329 return numberOfModels;
331 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
338 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException,
"The important atoms for AllSat must be boolean variables.");
341 uint_fast64_t numberOfModels = 0;
350 z3::model model = this->
solver->get_model();
352 z3::expr modelExpr = this->context->bool_val(
true);
356 z3::expr z3ImportantAtom = this->expressionAdapter->translateExpression(importantAtom.getExpression());
357 z3::expr z3ImportantAtomValuation = model.eval(z3ImportantAtom,
true);
358 modelExpr = modelExpr && (z3ImportantAtom == z3ImportantAtomValuation);
360 Z3ModelReference modelRef(this->
getManager(), model, *expressionAdapter);
363 proceed = callback(modelRef);
365 this->solver->add(!modelExpr);
370 return numberOfModels;
372 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
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.");
383 z3::expr_vector z3UnsatAssumptions = this->
solver->unsat_core();
384 std::vector<storm::expressions::Expression> unsatAssumptions;
386 for (
unsigned int i = 0; i < z3UnsatAssumptions.size(); ++i) {
387 unsatAssumptions.push_back(this->expressionAdapter->translateExpression(z3UnsatAssumptions[i]));
390 return unsatAssumptions;
392 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
398 z3::params paramObject(*context);
399 paramObject.set(
":timeout",
static_cast<unsigned>(milliseconds));
402 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
408 z3::params paramObject(*context);
409 paramObject.set(
":timeout", 0u);
412 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
420 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
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.
bool isIntegerType() const
Checks whether this type is an integral type.
bool isRationalType() const
Checks whether this type is a rational type.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this variable.
The base class for all model references.
storm::expressions::ExpressionManager const & getManager() const
Retrieves the expression manager associated with the solver.
SmtSolver(storm::expressions::ExpressionManager &manager)
Constructs a new Smt solver with the given options.
CheckResult
possible check results
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)
#define STORM_LOG_THROW(cond, exception, message)
std::set< Expression, ExpressionComparator > ExpressionSet
SettingsManager const & manager()
Retrieves the settings manager.
The contract of a DD backend.