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);
79 expressionAdapter = std::make_unique<storm::adapters::Z3ExpressionAdapter>(this->
getManager(), *context);
91 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
99 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
105 this->
solver->pop(
static_cast<unsigned int>(n));
107 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
115 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
121 this->
solver->add(expressionAdapter->translateExpression(assertion));
123 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
130 "Unable to create model for formula that was not determined to be satisfiable.");
132 auto currentModel = this->
solver->get_model();
133 z3::expr notThisModel = currentModel.ctx().bool_val(
true);
134 for (
auto const& variable : this->
getManager().getVariables()) {
135 z3::expr var = this->expressionAdapter->translateExpression(variable);
136 auto value = currentModel.eval(var);
137 if (notThisModel.is_const()) {
138 notThisModel = var == value;
140 notThisModel = notThisModel && (var == value);
144 if (performSolverReset) {
145 auto const allAssertions = this->
solver->assertions();
147 for (
auto const& assertion : allAssertions) {
151 this->
solver->add(!notThisModel);
153 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
159 lastCheckAssumptions =
false;
160 switch (this->
solver->check()) {
171 return this->lastResult;
173 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
179 lastCheckAssumptions =
true;
180 z3::expr_vector z3Assumptions(*this->context);
183 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
186 switch (this->
solver->check(z3Assumptions)) {
197 return this->lastResult;
199 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
205 lastCheckAssumptions =
true;
206 z3::expr_vector z3Assumptions(*this->context);
209 z3Assumptions.push_back(this->expressionAdapter->translateExpression(assumption));
212 switch (this->
solver->check(z3Assumptions)) {
223 return this->lastResult;
225 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
232 "Unable to create model for formula that was not determined to be satisfiable.");
233 return this->convertZ3ModelToValuation(this->
solver->get_model());
235 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
242 "Unable to create model for formula that was not determined to be satisfiable.");
245 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
253 for (
unsigned i = 0; i < model.num_consts(); ++i) {
254 z3::func_decl variableI = model.get_const_decl(i);
259 stormModel.setBooleanValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
isTrue());
261 stormModel.setIntegerValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
evaluateAsInt());
263 stormModel.setRationalValue(this->
getManager().getVariable(variableI.name().str()), variableInterpretation.
evaluateAsDouble());
265 STORM_LOG_ASSERT(
false,
"Variable interpretation in model is not of type bool, int or rational.");
273std::vector<storm::expressions::SimpleValuation>
Z3SmtSolver::allSat(std::vector<storm::expressions::Variable>
const& important) {
275 std::vector<storm::expressions::SimpleValuation> valuations;
278 valuations.push_back(valuation);
283 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
291 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException,
"The important atoms for AllSat must be boolean variables.");
294 uint_fast64_t numberOfModels = 0;
303 z3::model model = this->
solver->get_model();
305 z3::expr modelExpr = this->context->bool_val(
true);
309 z3::expr z3ImportantAtom = this->expressionAdapter->translateExpression(importantAtom.getExpression());
310 z3::expr z3ImportantAtomValuation = model.eval(z3ImportantAtom,
true);
311 modelExpr = modelExpr && (z3ImportantAtom == z3ImportantAtomValuation);
312 valuation.
setBooleanValue(importantAtom, this->expressionAdapter->translateExpression(z3ImportantAtomValuation).isTrue());
316 proceed = callback(valuation);
318 this->
solver->add(!modelExpr);
324 return numberOfModels;
326 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
333 STORM_LOG_THROW(variable.hasBooleanType(), storm::exceptions::InvalidArgumentException,
"The important atoms for AllSat must be boolean variables.");
336 uint_fast64_t numberOfModels = 0;
345 z3::model model = this->
solver->get_model();
347 z3::expr modelExpr = this->context->bool_val(
true);
351 z3::expr z3ImportantAtom = this->expressionAdapter->translateExpression(importantAtom.getExpression());
352 z3::expr z3ImportantAtomValuation = model.eval(z3ImportantAtom,
true);
353 modelExpr = modelExpr && (z3ImportantAtom == z3ImportantAtomValuation);
355 Z3ModelReference modelRef(this->
getManager(), model, *expressionAdapter);
358 proceed = callback(modelRef);
360 this->solver->add(!modelExpr);
365 return numberOfModels;
367 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
374 "Unable to generate unsatisfiable core of assumptions, because the last check did not determine the formulas to be unsatisfiable.");
375 STORM_LOG_THROW(lastCheckAssumptions, storm::exceptions::InvalidStateException,
376 "Unable to generate unsatisfiable core of assumptions, because the last check did not involve assumptions.");
378 z3::expr_vector z3UnsatAssumptions = this->
solver->unsat_core();
379 std::vector<storm::expressions::Expression> unsatAssumptions;
381 for (
unsigned int i = 0; i < z3UnsatAssumptions.size(); ++i) {
382 unsatAssumptions.push_back(this->expressionAdapter->translateExpression(z3UnsatAssumptions[i]));
385 return unsatAssumptions;
387 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
393 z3::params paramObject(*context);
394 paramObject.set(
":timeout",
static_cast<unsigned>(milliseconds));
398 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
404 z3::params paramObject(*context);
405 paramObject.set(
":timeout", 0u);
409 STORM_LOG_THROW(
false, storm::exceptions::MissingLibraryException,
"Storm is compiled without Z3 support.");
417 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 bool unsetTimeout() override
If supported by the solver, this unsets a previous timeout.
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 bool setTimeout(uint_fast64_t milliseconds) override
If supported by the solver, this will limit all subsequent satisfiability queries to the given number...
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 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.