Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Z3LpSolver.h
Go to the documentation of this file.
1#ifndef STORM_SOLVER_Z3LPSOLVER
2#define STORM_SOLVER_Z3LPSOLVER
3
4#include <map>
6// To detect whether the usage of Z3 is possible, this include is neccessary.
7#include "storm-config.h"
10
11#ifdef STORM_HAVE_Z3_OPTIMIZE
12#include "z3++.h"
13#include "z3.h"
14#endif
15
16namespace storm {
17namespace solver {
18
23template<typename ValueType, bool RawMode = false>
24class Z3LpSolver : public LpSolver<ValueType, RawMode> {
25 public:
37 Z3LpSolver(std::string const& name, OptimizationDirection const& optDir);
38
45 Z3LpSolver(std::string const& name);
46
53 Z3LpSolver(OptimizationDirection const& optDir);
54
59 Z3LpSolver();
60
64 virtual ~Z3LpSolver();
65
66 // Methods to add variables.
67 virtual Variable addVariable(std::string const& name, VariableType const& type, std::optional<ValueType> const& lowerBound = std::nullopt,
68 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0) override;
69
70 // Methods to incorporate recent changes.
71 virtual void update() const override;
72
73 // Methods to add constraints
74 virtual void addConstraint(std::string const& name, Constraint const& constraint) override;
75 virtual void addIndicatorConstraint(std::string const& name, Variable indicatorVariable, bool indicatorValue, Constraint const& constraint) override;
76
77 // Methods to optimize and retrieve optimality status.
78 virtual void optimize() const override;
79 virtual bool isInfeasible() const override;
80 virtual bool isUnbounded() const override;
81 virtual bool isOptimal() const override;
82
83 // Methods to retrieve values of variables and the objective function in the optimal solutions.
84 virtual ValueType getContinuousValue(Variable const& variable) const override;
85 virtual int_fast64_t getIntegerValue(Variable const& variable) const override;
86 virtual bool getBinaryValue(Variable const& variable) const override;
87 virtual ValueType getObjectiveValue() const override;
88
89 // Methods to print the LP problem to a file.
90 virtual void writeModelToFile(std::string const& filename) const override;
91
92 virtual void push() override;
93 virtual void pop() override;
94
95 virtual void setMaximalMILPGap(ValueType const& gap, bool relative) override;
96 virtual ValueType getMILPGap(bool relative) const override;
97
98 private:
99 virtual storm::expressions::Expression getValue(Variable const& variable) const;
100
101#ifdef STORM_HAVE_Z3_OPTIMIZE
102
103 // The context used by the solver.
104 std::unique_ptr<z3::context> context;
105
106 // The actual solver object.
107 std::unique_ptr<z3::optimize> solver;
108
109 // The results of the most recent check
110 mutable bool lastCheckInfeasible;
111 mutable bool lastCheckUnbounded;
112 mutable std::unique_ptr<z3::expr> lastCheckObjectiveValue;
113 mutable std::unique_ptr<z3::model> lastCheckModel;
114
115 // An expression adapter that is used for translating the expression into Z3's format.
116 std::unique_ptr<storm::adapters::Z3ExpressionAdapter> expressionAdapter;
117
118 // The function that is to be optimized (we interpret this as a sum)
119 std::vector<storm::expressions::Expression> optimizationSummands;
120
121 // Stores whether this solver is used in an incremental way (with push() and pop())
122 bool isIncremental;
123
124 // Stores the number of optimization summands at each call of push().
125 std::vector<uint64_t> incrementaOptimizationSummandIndicators;
126
127 // allows to map from raw indices to expression variables. Only used in RawMode
128 std::vector<storm::expressions::Variable> rawIndexToVariableMap;
129
130#endif
131};
132
133} // namespace solver
134} // namespace storm
135
136#endif /* STORM_SOLVER_Z3LPSOLVER */
An interface that captures the functionality of an LP solver.
Definition LpSolver.h:51
std::conditional_t< RawMode, RawLpConstraint< ValueType >, storm::expressions::Expression > Constraint
Definition LpSolver.h:55
std::conditional_t< RawMode, ValueType, storm::expressions::Expression > Constant
Definition LpSolver.h:54
std::conditional_t< RawMode, typename RawLpConstraint< ValueType >::VariableIndexType, storm::expressions::Variable > Variable
Definition LpSolver.h:53
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:60
A class that implements the LpSolver interface using Z3.
Definition Z3LpSolver.h:24
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
typename LpSolver< ValueType, RawMode >::Variable Variable
Definition Z3LpSolver.h:27
virtual void update() const override
Updates the model to make the variables that have been declared since the last call to update usable.
virtual int_fast64_t getIntegerValue(Variable const &variable) const override
Retrieves the value of the integer variable with the given name.
virtual Variable addVariable(std::string const &name, VariableType const &type, std::optional< ValueType > const &lowerBound=std::nullopt, std::optional< ValueType > const &upperBound=std::nullopt, ValueType objectiveFunctionCoefficient=0) override
Registers a variable of the given type.
typename LpSolver< ValueType, RawMode >::Constraint Constraint
Definition Z3LpSolver.h:29
virtual void writeModelToFile(std::string const &filename) const override
Writes the current LP problem to the given file.
typename LpSolver< ValueType, RawMode >::Constant Constant
Definition Z3LpSolver.h:28
virtual bool isUnbounded() const override
Retrieves whether the model was found to be infeasible.
virtual ValueType getContinuousValue(Variable const &variable) const override
Retrieves the value of the continuous variable with the given name.
Z3LpSolver()
Constructs a solver without a name.
virtual void addIndicatorConstraint(std::string const &name, Variable indicatorVariable, bool indicatorValue, Constraint const &constraint) override
Adds the given indicator constraint to the LP problem: "If indicatorVariable == indicatorValue,...
virtual ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize()
virtual void setMaximalMILPGap(ValueType const &gap, bool relative) override
Specifies the maximum difference between lower- and upper objective bounds that triggers termination.
virtual void push() override
Pushes a backtracking point on the solver's stack.
virtual void addConstraint(std::string const &name, Constraint const &constraint) override
Adds a the given constraint to the LP problem.
virtual bool isOptimal() const override
Retrieves whether the model was found to be optimal, i.e.
typename LpSolver< ValueType, RawMode >::VariableType VariableType
Definition Z3LpSolver.h:26
virtual ~Z3LpSolver()
Destructs a solver by freeing the pointers to Z3's structures.
virtual bool isInfeasible() const override
Retrieves whether the model was found to be infeasible.
virtual void pop() override
Pops a backtracking point from the solver's stack.
virtual bool getBinaryValue(Variable const &variable) const override
Retrieves the value of the binary variable with the given name.
virtual void optimize() const override
Optimizes the LP problem previously constructed.
LabParser.cpp.
Definition cli.cpp:18