Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
LpSolver.h
Go to the documentation of this file.
1#ifndef STORM_SOLVER_LPSOLVER
2#define STORM_SOLVER_LPSOLVER
3
4#include <cstdint>
5#include <memory>
6#include <optional>
7#include <string>
8#include <vector>
10
11namespace storm {
12namespace expressions {
13class ExpressionManager;
14class Variable;
15class Expression;
16enum class RelationType;
17} // namespace expressions
18
19namespace solver {
20
21template<typename ValueType>
23 using VariableIndexType = uint64_t;
24
31 RawLpConstraint(storm::expressions::RelationType relationType, ValueType const& rhs = {}, uint64_t reservedSize = 0);
32
38 void addToLhs(VariableIndexType const& variable, ValueType const& coefficient);
39
40 std::vector<VariableIndexType> lhsVariableIndices;
41 std::vector<ValueType> lhsCoefficients;
43 ValueType rhs;
44};
45
50template<typename ValueType, bool RawMode = false>
51class LpSolver {
52 public:
53 using Variable = std::conditional_t<RawMode, typename RawLpConstraint<ValueType>::VariableIndexType, storm::expressions::Variable>;
54 using Constant = std::conditional_t<RawMode, ValueType, storm::expressions::Expression>;
55 using Constraint = std::conditional_t<RawMode, RawLpConstraint<ValueType>, storm::expressions::Expression>;
56
61
65 LpSolver();
72 LpSolver(OptimizationDirection const& optDir);
73
74 virtual ~LpSolver() = default;
75
88 Variable addBoundedContinuousVariable(std::string const& name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient = 0);
89
101 Variable addLowerBoundedContinuousVariable(std::string const& name, ValueType lowerBound, ValueType objectiveFunctionCoefficient = 0);
102
114 Variable addUpperBoundedContinuousVariable(std::string const& name, ValueType upperBound, ValueType objectiveFunctionCoefficient = 0);
115
125 Variable addUnboundedContinuousVariable(std::string const& name, ValueType objectiveFunctionCoefficient = 0);
126
138 Variable addContinuousVariable(std::string const& name, std::optional<ValueType> const& lowerBound = std::nullopt,
139 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0);
140
153 Variable addBoundedIntegerVariable(std::string const& name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient = 0);
154
166 Variable addLowerBoundedIntegerVariable(std::string const& name, ValueType lowerBound, ValueType objectiveFunctionCoefficient = 0);
167
179 Variable addUpperBoundedIntegerVariable(std::string const& name, ValueType upperBound, ValueType objectiveFunctionCoefficient = 0);
180
190 Variable addUnboundedIntegerVariable(std::string const& name, ValueType objectiveFunctionCoefficient = 0);
191
203 Variable addIntegerVariable(std::string const& name, std::optional<ValueType> const& lowerBound = std::nullopt,
204 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0);
205
215 Variable addBinaryVariable(std::string const& name, ValueType objectiveFunctionCoefficient = 0);
216
229 virtual Variable addVariable(std::string const& name, VariableType const& type, std::optional<ValueType> const& lowerBound = std::nullopt,
230 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0) = 0;
231
239 Constant getConstant(ValueType value) const;
240
245 virtual void update() const = 0;
246
254 virtual void addConstraint(std::string const& name, Constraint const& constraint) = 0;
255
266 virtual void addIndicatorConstraint(std::string const& name, Variable indicatorVariable, bool indicatorValue, Constraint const& constraint) = 0;
267
272 virtual void optimize() const = 0;
273
280 virtual bool isInfeasible() const = 0;
281
288 virtual bool isUnbounded() const = 0;
289
297 virtual bool isOptimal() const = 0;
298
306 virtual int_fast64_t getIntegerValue(Variable const& variable) const = 0;
307
315 virtual bool getBinaryValue(Variable const& variable) const = 0;
316
324 virtual ValueType getContinuousValue(Variable const& variable) const = 0;
325
332 virtual ValueType getObjectiveValue() const = 0;
333
339 virtual void writeModelToFile(std::string const& filename) const = 0;
340
346 void setOptimizationDirection(OptimizationDirection const& optimizationDirection);
347
354
361
366 virtual void push() = 0;
367
372 virtual void pop() = 0;
373
380 virtual void setMaximalMILPGap(ValueType const& gap, bool relative) = 0;
381
385 virtual ValueType getMILPGap(bool relative) const = 0;
386
387 protected:
389
390 // The manager responsible for the variables.
391 std::shared_ptr<storm::expressions::ExpressionManager> manager;
392
393 // A flag indicating whether the current model has been optimized and not changed afterwards.
395
396 private:
397 // A flag that indicates the model sense.
398 OptimizationDirection optimizationDirection;
399};
400} // namespace solver
401} // namespace storm
402
403#endif /* STORM_SOLVER_LPSOLVER */
This class is responsible for managing a set of typed variables and all expressions using these varia...
An interface that captures the functionality of an LP solver.
Definition LpSolver.h:51
Variable addUnboundedIntegerVariable(std::string const &name, ValueType objectiveFunctionCoefficient=0)
Registers an unbounded integer variable, i.e.
Definition LpSolver.cpp:90
virtual void setMaximalMILPGap(ValueType const &gap, bool relative)=0
Specifies the maximum difference between lower- and upper objective bounds that triggers termination.
OptimizationDirection getOptimizationDirection() const
Retrieves whether the objective function of this model is to be minimized or maximized.
Definition LpSolver.cpp:126
virtual bool getBinaryValue(Variable const &variable) const =0
Retrieves the value of the binary variable with the given name.
Variable addIntegerVariable(std::string const &name, std::optional< ValueType > const &lowerBound=std::nullopt, std::optional< ValueType > const &upperBound=std::nullopt, ValueType objectiveFunctionCoefficient=0)
Registers an integer variable, i.e.
Definition LpSolver.cpp:96
Variable addContinuousVariable(std::string const &name, std::optional< ValueType > const &lowerBound=std::nullopt, std::optional< ValueType > const &upperBound=std::nullopt, ValueType objectiveFunctionCoefficient=0)
Registers a continuous variable, i.e.
Definition LpSolver.cpp:63
virtual void update() const =0
Updates the model to make the variables that have been declared since the last call to update usable.
virtual void addConstraint(std::string const &name, Constraint const &constraint)=0
Adds a the given constraint to the LP problem.
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)=0
Registers a variable of the given type.
virtual void writeModelToFile(std::string const &filename) const =0
Writes the current LP problem to the given file.
virtual void pop()=0
Pops a backtracking point from the solver's stack.
Variable addUpperBoundedIntegerVariable(std::string const &name, ValueType upperBound, ValueType objectiveFunctionCoefficient=0)
Registers an upper-bounded integer variable, i.e.
Definition LpSolver.cpp:84
virtual ValueType getMILPGap(bool relative) const =0
Returns the obtained gap after a call to optimize()
storm::expressions::Variable declareOrGetExpressionVariable(std::string const &name, VariableType const &type)
Definition LpSolver.cpp:137
Variable addBoundedIntegerVariable(std::string const &name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient=0)
Registers an upper- and lower-bounded integer variable, i.e.
Definition LpSolver.cpp:71
storm::expressions::ExpressionManager const & getManager() const
Retrieves the manager for the variables created for this solver.
Definition LpSolver.cpp:131
virtual void push()=0
Pushes a backtracking point on the solver's stack.
void setOptimizationDirection(OptimizationDirection const &optimizationDirection)
Sets whether the objective function of this model is to be minimized or maximized.
Definition LpSolver.cpp:118
LpSolver()
Creates an empty LP solver.
Definition LpSolver.cpp:26
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
Variable addLowerBoundedContinuousVariable(std::string const &name, ValueType lowerBound, ValueType objectiveFunctionCoefficient=0)
Registers a lower-bounded continuous variable, i.e.
Definition LpSolver.cpp:45
virtual int_fast64_t getIntegerValue(Variable const &variable) const =0
Retrieves the value of the integer variable with the given name.
std::conditional_t< RawMode, typename RawLpConstraint< ValueType >::VariableIndexType, storm::expressions::Variable > Variable
Definition LpSolver.h:53
Constant getConstant(ValueType value) const
Retrieves an expression that characterizes the given constant value.
Definition LpSolver.cpp:109
std::shared_ptr< storm::expressions::ExpressionManager > manager
Definition LpSolver.h:391
virtual bool isInfeasible() const =0
Retrieves whether the model was found to be infeasible.
virtual void addIndicatorConstraint(std::string const &name, Variable indicatorVariable, bool indicatorValue, Constraint const &constraint)=0
Adds the given indicator constraint to the LP problem: "If indicatorVariable == indicatorValue,...
Variable addLowerBoundedIntegerVariable(std::string const &name, ValueType lowerBound, ValueType objectiveFunctionCoefficient=0)
Registers a lower-bounded integer variable, i.e.
Definition LpSolver.cpp:78
virtual bool isUnbounded() const =0
Retrieves whether the model was found to be infeasible.
Variable addBoundedContinuousVariable(std::string const &name, ValueType lowerBound, ValueType upperBound, ValueType objectiveFunctionCoefficient=0)
Registers an upper- and lower-bounded continuous variable, i.e.
Definition LpSolver.cpp:38
virtual ValueType getContinuousValue(Variable const &variable) const =0
Retrieves the value of the continuous variable with the given name.
virtual ~LpSolver()=default
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:60
virtual void optimize() const =0
Optimizes the LP problem previously constructed.
Variable addUnboundedContinuousVariable(std::string const &name, ValueType objectiveFunctionCoefficient=0)
Registers a unbounded continuous variable, i.e.
Definition LpSolver.cpp:57
virtual ValueType getObjectiveValue() const =0
Retrieves the value of the objective function.
Variable addUpperBoundedContinuousVariable(std::string const &name, ValueType upperBound, ValueType objectiveFunctionCoefficient=0)
Registers an upper-bounded continuous variable, i.e.
Definition LpSolver.cpp:51
Variable addBinaryVariable(std::string const &name, ValueType objectiveFunctionCoefficient=0)
Registers a boolean variable, i.e.
Definition LpSolver.cpp:103
virtual bool isOptimal() const =0
Retrieves whether the model was found to be optimal, i.e.
RelationType
An enum type specifying the different relations applicable.
LabParser.cpp.
Definition cli.cpp:18
std::vector< VariableIndexType > lhsVariableIndices
Definition LpSolver.h:40
void addToLhs(VariableIndexType const &variable, ValueType const &coefficient)
Adds the summand 'coefficient * variable' to the left hand side.
Definition LpSolver.cpp:20
storm::expressions::RelationType relationType
Definition LpSolver.h:42
std::vector< ValueType > lhsCoefficients
Definition LpSolver.h:41