Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GurobiLpSolver.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <optional>
6// To detect whether the usage of Gurobi is possible, this include is neccessary.
7#include "storm-config.h"
8
9#ifdef STORM_HAVE_GUROBI
10extern "C" {
11#include <gurobi_c.h>
12
13int __stdcall GRBislp(GRBenv**, const char*, const char*, const char*, const char*);
14}
15#endif
16
17namespace storm {
18
20
21namespace solver {
22
24 public:
25 GurobiEnvironment() = default;
28 virtual ~GurobiEnvironment();
32 void initialize(storm::GurobiSolverEnvironment const& gurobiSettings, bool debug);
33 void setOutput(bool set = false);
34 double getIntegerTolerance() const;
35#ifdef STORM_HAVE_GUROBI
36 GRBenv* operator*();
37#endif
38 private:
39 bool initialized = false;
40 double integerTolerance = 0.0;
41#ifdef STORM_HAVE_GUROBI
42 GRBenv* env = nullptr;
43#endif
44};
45
49template<typename ValueType, bool RawMode = false>
50class GurobiLpSolver : public LpSolver<ValueType, RawMode> {
51 public:
56
64 GurobiLpSolver(std::shared_ptr<GurobiEnvironment> const& environment, std::string const& name, OptimizationDirection const& optDir);
65
72 GurobiLpSolver(std::shared_ptr<GurobiEnvironment> const& environment, std::string const& name);
73
80 GurobiLpSolver(std::shared_ptr<GurobiEnvironment> const& environment, OptimizationDirection const& optDir);
81
86 GurobiLpSolver(std::shared_ptr<GurobiEnvironment> const& environment);
87
91 virtual ~GurobiLpSolver();
92
93 // Methods to add variables.
94 virtual Variable addVariable(std::string const& name, VariableType const& type, std::optional<ValueType> const& lowerBound = std::nullopt,
95 std::optional<ValueType> const& upperBound = std::nullopt, ValueType objectiveFunctionCoefficient = 0) override;
96
97 // Methods to incorporate recent changes.
98 virtual void update() const override;
99
100 // Methods to add constraints
101 virtual void addConstraint(std::string const& name, Constraint const& constraint) override;
102 virtual void addIndicatorConstraint(std::string const& name, Variable indicatorVariable, bool indicatorValue, Constraint const& constraint) override;
103
104 // Methods to optimize and retrieve optimality status.
105 virtual void optimize() const override;
106 virtual bool isInfeasible() const override;
107 virtual bool isUnbounded() const override;
108 virtual bool isOptimal() const override;
109
110 // Methods to retrieve values of variables and the objective function in the optimal solutions.
111 virtual ValueType getContinuousValue(Variable const& name) const override;
112 virtual int_fast64_t getIntegerValue(Variable const& name) const override;
113 virtual bool getBinaryValue(Variable const& name) const override;
114 virtual ValueType getObjectiveValue() const override;
115 // Methods to print the LP problem to a file.
116 virtual void writeModelToFile(std::string const& filename) const override;
117
118 virtual void push() override;
119 virtual void pop() override;
120
121 virtual void setMaximalMILPGap(ValueType const& gap, bool relative) override;
122 virtual ValueType getMILPGap(bool relative) const override;
123
124 // Methods to retrieve values of sub-optimal solutions found along the way.
125 void setMaximalSolutionCount(uint64_t value); // How many solutions will be stored (at max)
126 uint64_t getSolutionCount() const; // How many solutions have been found
127 ValueType getContinuousValue(Variable const& name, uint64_t const& solutionIndex) const;
128 int_fast64_t getIntegerValue(Variable const& name, uint64_t const& solutionIndex) const;
129 bool getBinaryValue(Variable const& name, uint64_t const& solutionIndex) const;
130 ValueType getObjectiveValue(uint64_t solutionIndex) const;
131
132 // Method for specifying a time limit
133 void setTimeLimit(uint64_t seconds);
134 // Retrieve the time limit in seconds. Requires that a time limit has been set before.
135 uint64_t getTimeLimit();
136 // Method for checking whether the model has a time limit
137 bool hasTimeLimit();
138 // Method for checking whether the optimization has timed out
139 bool hasTimedOut();
140
141 private:
142#ifdef STORM_HAVE_GUROBI
143 // The Gurobi model.
144 GRBmodel* model;
145#endif
146
147 std::shared_ptr<GurobiEnvironment> environment;
148
149 // The index of the next variable.
150 int nextVariableIndex;
151
152 // A mapping from variables to their indices.
153 std::map<storm::expressions::Variable, int> variableToIndexMap;
154
155 struct IncrementalLevel {
156 std::vector<storm::expressions::Variable> variables;
157 // Gurobi considers a different set of indices for linear constraints and general constraints.
158 // The term "general constraints" is used by Gurobi to refer to "non-standard" constraints, e.g., indicator constraint.
159 int firstConstraintIndex;
160 int firstGenConstraintIndex;
161 };
162 std::vector<IncrementalLevel> incrementalData;
163
164 std::optional<uint64_t> timeLimit;
165};
166
168
174std::string toString(GurobiSolverMethod const& method);
175std::optional<GurobiSolverMethod> gurobiSolverMethodFromString(std::string const&);
176std::vector<GurobiSolverMethod> getGurobiSolverMethods();
177
178} // namespace solver
179} // namespace storm
GurobiEnvironment(GurobiEnvironment const &)=delete
GurobiEnvironment & operator=(GurobiEnvironment const &)=delete
void initialize(storm::GurobiSolverEnvironment const &gurobiSettings, bool debug)
Sets some properties of the Gurobi environment according to parameters given by the options.
virtual ~GurobiLpSolver()
Destructs a solver by freeing the pointers to Gurobi's structures.
virtual bool isInfeasible() const override
Retrieves whether the model was found to be infeasible.
virtual int_fast64_t getIntegerValue(Variable const &name) const override
Retrieves the value of the integer variable with the given name.
virtual bool isUnbounded() const override
Retrieves whether the model was found to be infeasible.
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.
void setMaximalSolutionCount(uint64_t value)
typename LpSolver< ValueType, RawMode >::VariableType VariableType
GurobiLpSolver(std::shared_ptr< GurobiEnvironment > const &environment, std::string const &name, OptimizationDirection const &optDir)
Constructs a solver with the given name and model sense.
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 void addConstraint(std::string const &name, Constraint const &constraint) override
Adds a the given constraint to the LP problem.
virtual ValueType getContinuousValue(Variable const &name) const override
Retrieves the value of the continuous variable with the given name.
virtual bool getBinaryValue(Variable const &name) const override
Retrieves the value of the binary variable with the given name.
virtual ValueType getObjectiveValue() const override
Retrieves the value of the objective function.
void setTimeLimit(uint64_t seconds)
typename LpSolver< ValueType, RawMode >::Constant Constant
virtual void pop() override
Pops a backtracking point from the solver's stack.
virtual void writeModelToFile(std::string const &filename) const override
Writes the current LP problem to the given file.
typename LpSolver< ValueType, RawMode >::Variable Variable
virtual void optimize() const override
Optimizes the LP problem previously constructed.
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
virtual ValueType getMILPGap(bool relative) const override
Returns the obtained gap after a call to optimize().
virtual bool isOptimal() const override
Retrieves whether the model was found to be optimal, i.e.
virtual void update() const override
Updates the model to make the variables that have been declared since the last call to update usable.
std::conditional_t< RawMode, RawLpConstraint< ValueType >, storm::expressions::Expression > Constraint
Definition LpSolver.h:54
std::conditional_t< RawMode, ValueType, storm::expressions::Expression > Constant
Definition LpSolver.h:53
std::conditional_t< RawMode, typename RawLpConstraint< ValueType >::VariableIndexType, storm::expressions::Variable > Variable
Definition LpSolver.h:52
VariableType
Enumerates the different types of variables.
Definition LpSolver.h:59
std::string toString(GurobiSolverMethod const &method)
Yields a string representation of the GurobiSolverMethod.
std::optional< GurobiSolverMethod > gurobiSolverMethodFromString(std::string const &method)
std::vector< GurobiSolverMethod > getGurobiSolverMethods()