Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
LpSolver.cpp
Go to the documentation of this file.
1
2#include "LpSolver.h"
3
8
9namespace storm {
10namespace solver {
11
12template<typename ValueType>
13RawLpConstraint<ValueType>::RawLpConstraint(storm::expressions::RelationType relationType, ValueType const& rhs, uint64_t reservedSize)
14 : relationType(relationType), rhs(rhs) {
15 lhsCoefficients.reserve(reservedSize);
16 lhsVariableIndices.reserve(reservedSize);
17}
18
19template<typename ValueType>
20void RawLpConstraint<ValueType>::addToLhs(VariableIndexType const& variable, ValueType const& coefficient) {
21 lhsCoefficients.push_back(coefficient);
22 lhsVariableIndices.push_back(variable);
23}
24
25template<typename ValueType, bool RawMode>
27 : manager(new storm::expressions::ExpressionManager()), currentModelHasBeenOptimized(false), optimizationDirection(OptimizationDirection::Minimize) {
28 // Intentionally left empty.
29}
30
31template<typename ValueType, bool RawMode>
33 : manager(new storm::expressions::ExpressionManager()), currentModelHasBeenOptimized(false), optimizationDirection(optimizationDir) {
34 // Intentionally left empty.
35}
36
37template<typename ValueType, bool RawMode>
39 ValueType upperBound,
40 ValueType objectiveFunctionCoefficient) {
41 return addVariable(name, VariableType::Continuous, lowerBound, upperBound, objectiveFunctionCoefficient);
42}
43
44template<typename ValueType, bool RawMode>
46 ValueType objectiveFunctionCoefficient) {
47 return addVariable(name, VariableType::Continuous, lowerBound, std::nullopt, objectiveFunctionCoefficient);
48}
49
50template<typename ValueType, bool RawMode>
52 ValueType objectiveFunctionCoefficient) {
53 return addVariable(name, VariableType::Continuous, std::nullopt, upperBound, objectiveFunctionCoefficient);
54}
55
56template<typename ValueType, bool RawMode>
58 ValueType objectiveFunctionCoefficient) {
59 return addVariable(name, VariableType::Continuous, std::nullopt, std::nullopt, objectiveFunctionCoefficient);
60}
61
62template<typename ValueType, bool RawMode>
64 std::optional<ValueType> const& lowerBound,
65 std::optional<ValueType> const& upperBound,
66 ValueType objectiveFunctionCoefficient) {
67 return addVariable(name, VariableType::Continuous, lowerBound, upperBound, objectiveFunctionCoefficient);
68}
69
70template<typename ValueType, bool RawMode>
72 ValueType upperBound,
73 ValueType objectiveFunctionCoefficient) {
74 return addVariable(name, VariableType::Integer, lowerBound, upperBound, objectiveFunctionCoefficient);
75}
76
77template<typename ValueType, bool RawMode>
79 ValueType objectiveFunctionCoefficient) {
80 return addVariable(name, VariableType::Integer, lowerBound, std::nullopt, objectiveFunctionCoefficient);
81}
82
83template<typename ValueType, bool RawMode>
85 ValueType objectiveFunctionCoefficient) {
86 return addVariable(name, VariableType::Integer, std::nullopt, upperBound, objectiveFunctionCoefficient);
87}
89template<typename ValueType, bool RawMode>
91 ValueType objectiveFunctionCoefficient) {
92 return addVariable(name, VariableType::Integer, std::nullopt, std::nullopt, objectiveFunctionCoefficient);
93}
94
95template<typename ValueType, bool RawMode>
97 std::optional<ValueType> const& lowerBound,
98 std::optional<ValueType> const& upperBound,
99 ValueType objectiveFunctionCoefficient) {
100 return addVariable(name, VariableType::Integer, lowerBound, upperBound, objectiveFunctionCoefficient);
102template<typename ValueType, bool RawMode>
104 ValueType objectiveFunctionCoefficient) {
105 return addVariable(name, VariableType::Binary, std::nullopt, std::nullopt, objectiveFunctionCoefficient);
106}
107
108template<typename ValueType, bool RawMode>
110 if constexpr (RawMode) {
111 return value;
112 } else {
113 return manager->rational(value);
115}
116
117template<typename ValueType, bool RawMode>
119 if (optimizationDirection != this->optimizationDirection) {
120 currentModelHasBeenOptimized = false;
121 }
122 this->optimizationDirection = optimizationDirection;
123}
124
125template<typename ValueType, bool RawMode>
129
130template<typename ValueType, bool RawMode>
132 STORM_LOG_ASSERT(!RawMode, "Asking for a manager in raw mode which does not make sense.");
133 return *manager;
134}
135
136template<typename ValueType, bool RawMode>
138 switch (type) {
139 case VariableType::Continuous:
140 return this->manager->declareOrGetVariable(name, this->manager->getRationalType());
141 case VariableType::Integer:
142 case VariableType::Binary:
143 return this->manager->declareOrGetVariable(name, this->manager->getIntegerType());
144 }
145 STORM_LOG_ASSERT(false, "Unable to declare or get expression variable: Unknown type");
146 return {};
147}
148
149template struct RawLpConstraint<double>;
150template struct RawLpConstraint<storm::RationalNumber>;
151template class LpSolver<double, true>;
152template class LpSolver<double, false>;
155
156} // namespace solver
157} // namespace storm
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
OptimizationDirection getOptimizationDirection() const
Retrieves whether the objective function of this model is to be minimized or maximized.
Definition LpSolver.cpp:126
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
Variable addUpperBoundedIntegerVariable(std::string const &name, ValueType upperBound, ValueType objectiveFunctionCoefficient=0)
Registers an upper-bounded integer variable, i.e.
Definition LpSolver.cpp:84
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
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, 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
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
Variable addLowerBoundedIntegerVariable(std::string const &name, ValueType lowerBound, ValueType objectiveFunctionCoefficient=0)
Registers a lower-bounded integer variable, i.e.
Definition LpSolver.cpp:78
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
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:60
Variable addUnboundedContinuousVariable(std::string const &name, ValueType objectiveFunctionCoefficient=0)
Registers a unbounded continuous variable, i.e.
Definition LpSolver.cpp:57
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
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
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
RawLpConstraint(storm::expressions::RelationType relationType, ValueType const &rhs={}, uint64_t reservedSize=0)
Creates a RawLpConstraint which represents a linear (in)equality of the form a_1*x_1 + ....
Definition LpSolver.cpp:13
std::vector< ValueType > lhsCoefficients
Definition LpSolver.h:41