Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Z3LpSolver.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4
5#include "storm-config.h" // To detect whether the usage of Z3 is possible, this include is neccessary.
9
10#ifdef STORM_HAVE_Z3
11#include "z3++.h"
12#include "z3.h"
13#endif
14
15namespace storm {
16namespace solver {
17
22template<typename ValueType, bool RawMode = false>
23class Z3LpSolver : public LpSolver<ValueType, RawMode> {
24 public:
36 Z3LpSolver(std::string const& name, OptimizationDirection const& optDir);
37
44 Z3LpSolver(std::string const& name);
45
52 Z3LpSolver(OptimizationDirection const& optDir);
53
58 Z3LpSolver();
59
63 virtual ~Z3LpSolver();
64
65 // Methods to add variables.
66 virtual Variable addVariable(std::string const& name, VariableType const& type, std::optional<ValueType> const& lowerBound = std::nullopt,
67 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0) override;
68
69 // Methods to incorporate recent changes.
70 virtual void update() const override;
71
72 // Methods to add constraints
73 virtual void addConstraint(std::string const& name, Constraint const& constraint) override;
74 virtual void addIndicatorConstraint(std::string const& name, Variable indicatorVariable, bool indicatorValue, Constraint const& constraint) override;
75
76 // Methods to optimize and retrieve optimality status.
77 virtual void optimize() const override;
78 virtual bool isInfeasible() const override;
79 virtual bool isUnbounded() const override;
80 virtual bool isOptimal() const override;
81
82 // Methods to retrieve values of variables and the objective function in the optimal solutions.
83 virtual ValueType getContinuousValue(Variable const& variable) const override;
84 virtual int_fast64_t getIntegerValue(Variable const& variable) const override;
85 virtual bool getBinaryValue(Variable const& variable) const override;
86 virtual ValueType getObjectiveValue() const override;
87
88 // Methods to print the LP problem to a file.
89 virtual void writeModelToFile(std::string const& filename) const override;
90
91 virtual void push() override;
92 virtual void pop() override;
93
94 virtual void setMaximalMILPGap(ValueType const& gap, bool relative) override;
95 virtual ValueType getMILPGap(bool relative) const override;
96
97 private:
98 virtual storm::expressions::Expression getValue(Variable const& variable) const;
99
100#ifdef STORM_HAVE_Z3
101
102 // The context used by the solver.
103 std::unique_ptr<z3::context> context;
104
105 // The actual solver object.
106 std::unique_ptr<z3::optimize> solver;
107
108 // The results of the most recent check
109 mutable bool lastCheckInfeasible;
110 mutable bool lastCheckUnbounded;
111 mutable std::unique_ptr<z3::expr> lastCheckObjectiveValue;
112 mutable std::unique_ptr<z3::model> lastCheckModel;
113
114 // An expression adapter that is used for translating the expression into Z3's format.
115 std::unique_ptr<storm::adapters::Z3ExpressionAdapter> expressionAdapter;
116
117 // The function that is to be optimized (we interpret this as a sum)
118 std::vector<storm::expressions::Expression> optimizationSummands;
119
120 // Stores whether this solver is used in an incremental way (with push() and pop())
121 bool isIncremental;
122
123 // Stores the number of optimization summands at each call of push().
124 std::vector<uint64_t> incrementaOptimizationSummandIndicators;
125
126 // allows to map from raw indices to expression variables. Only used in RawMode
127 std::vector<storm::expressions::Variable> rawIndexToVariableMap;
128
129#endif
130};
131
132} // namespace solver
133} // namespace storm
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
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
typename LpSolver< ValueType, RawMode >::Variable Variable
Definition Z3LpSolver.h:26
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
typename LpSolver< ValueType, RawMode >::Constraint Constraint
Definition Z3LpSolver.h:28
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:27
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.
Z3LpSolver(std::string const &name, OptimizationDirection const &optDir)
Constructs a solver with the given name and optimization direction.
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:25
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.