Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
solver.cpp
Go to the documentation of this file.
2
3#include <type_traits>
4
19
20namespace storm {
21namespace utility {
22namespace solver {
23
24template<typename ValueType>
25std::unique_ptr<storm::solver::LpSolver<ValueType>> GlpkLpSolverFactory<ValueType>::create(storm::Environment const& env, std::string const& name) const {
26 return std::unique_ptr<storm::solver::LpSolver<ValueType>>(
28}
29
30template<typename ValueType>
31std::unique_ptr<storm::solver::LpSolver<ValueType, true>> GlpkLpSolverFactory<ValueType>::createRaw(storm::Environment const& env,
32 std::string const& name) const {
33 return std::unique_ptr<storm::solver::LpSolver<ValueType, true>>(
35}
36
37template<typename ValueType>
38std::unique_ptr<LpSolverFactory<ValueType>> GlpkLpSolverFactory<ValueType>::clone() const {
39 return std::make_unique<GlpkLpSolverFactory<ValueType>>(*this);
40}
41
42template<typename ValueType>
43std::unique_ptr<storm::solver::LpSolver<ValueType>> SoplexLpSolverFactory<ValueType>::create(storm::Environment const&, std::string const& name) const {
44 return std::unique_ptr<storm::solver::LpSolver<ValueType>>(new storm::solver::SoplexLpSolver<ValueType>(name));
45}
46
47template<typename ValueType>
48std::unique_ptr<storm::solver::LpSolver<ValueType, true>> SoplexLpSolverFactory<ValueType>::createRaw(storm::Environment const&,
49 std::string const& name) const {
50 return std::unique_ptr<storm::solver::LpSolver<ValueType, true>>(new storm::solver::SoplexLpSolver<ValueType, true>(name));
51}
52
53template<typename ValueType>
54std::unique_ptr<LpSolverFactory<ValueType>> SoplexLpSolverFactory<ValueType>::clone() const {
55 return std::make_unique<SoplexLpSolverFactory<ValueType>>(*this);
56}
57
58template<typename ValueType>
59std::unique_ptr<storm::solver::LpSolver<ValueType>> HighsLpSolverFactory<ValueType>::create(storm::Environment const&, std::string const& name) const {
60 return std::unique_ptr<storm::solver::LpSolver<ValueType>>(new storm::solver::HighsLpSolver<ValueType>(name));
61}
62
63template<typename ValueType>
64std::unique_ptr<storm::solver::LpSolver<ValueType, true>> HighsLpSolverFactory<ValueType>::createRaw(storm::Environment const&, std::string const& name) const {
65 return std::unique_ptr<storm::solver::LpSolver<ValueType, true>>(new storm::solver::HighsLpSolver<ValueType, true>(name));
66}
67
68template<typename ValueType>
69std::unique_ptr<LpSolverFactory<ValueType>> HighsLpSolverFactory<ValueType>::clone() const {
70 return std::make_unique<HighsLpSolverFactory<ValueType>>(*this);
71}
72
73template<typename ValueType>
74std::shared_ptr<storm::solver::GurobiEnvironment> const& GurobiLpSolverFactory<ValueType>::getOrCreateGurobiEnvironment(storm::Environment const& env) const {
75 if (!environment) {
76 environment = std::make_shared<storm::solver::GurobiEnvironment>();
77 environment->initialize(env.solver().gurobi(), env.solver().isDebugSet());
78 }
79 return environment;
80}
81
82template<typename ValueType>
83std::unique_ptr<storm::solver::LpSolver<ValueType>> GurobiLpSolverFactory<ValueType>::create(storm::Environment const& env, std::string const& name) const {
84 return std::unique_ptr<storm::solver::LpSolver<ValueType>>(new storm::solver::GurobiLpSolver<ValueType>(getOrCreateGurobiEnvironment(env), name));
85}
86
87template<typename ValueType>
88std::unique_ptr<storm::solver::LpSolver<ValueType, true>> GurobiLpSolverFactory<ValueType>::createRaw(storm::Environment const& env,
89 std::string const& name) const {
90 return std::unique_ptr<storm::solver::LpSolver<ValueType, true>>(
91 new storm::solver::GurobiLpSolver<ValueType, true>(getOrCreateGurobiEnvironment(env), name));
92}
93
94template<typename ValueType>
95std::unique_ptr<LpSolverFactory<ValueType>> GurobiLpSolverFactory<ValueType>::clone() const {
96 return std::make_unique<GurobiLpSolverFactory<ValueType>>(*this);
97}
98
99template<typename ValueType>
100std::unique_ptr<storm::solver::LpSolver<ValueType>> Z3LpSolverFactory<ValueType>::create(storm::Environment const&, std::string const& name) const {
101 return std::unique_ptr<storm::solver::LpSolver<ValueType>>(new storm::solver::Z3LpSolver<ValueType>(name));
102}
103
104template<typename ValueType>
105std::unique_ptr<storm::solver::LpSolver<ValueType, true>> Z3LpSolverFactory<ValueType>::createRaw(storm::Environment const&, std::string const& name) const {
106 return std::unique_ptr<storm::solver::LpSolver<ValueType, true>>(new storm::solver::Z3LpSolver<ValueType, true>(name));
107}
108
109template<typename ValueType>
110std::unique_ptr<LpSolverFactory<ValueType>> Z3LpSolverFactory<ValueType>::clone() const {
111 return std::make_unique<Z3LpSolverFactory<ValueType>>(*this);
112}
113
114template<typename ValueType>
115std::unique_ptr<LpSolverFactory<ValueType>> getLpSolverFactory(storm::Environment const& env, storm::solver::LpSolverTypeSelection solvType) {
116 storm::solver::LpSolverType t;
117 if (solvType == storm::solver::LpSolverTypeSelection::FROMSETTINGS) {
118 t = env.solver().getLpSolverType();
120 if (useExact && t != storm::solver::LpSolverType::Z3 && env.solver().isLpSolverTypeSetFromDefaultValue()) {
121 t = storm::solver::LpSolverType::Z3;
122 }
123 } else {
124 t = convert(solvType);
125 }
126 switch (t) {
127 case storm::solver::LpSolverType::Glpk:
128 return std::unique_ptr<LpSolverFactory<ValueType>>(new GlpkLpSolverFactory<ValueType>());
129 case storm::solver::LpSolverType::Gurobi:
130 return std::unique_ptr<LpSolverFactory<ValueType>>(new GurobiLpSolverFactory<ValueType>());
131 case storm::solver::LpSolverType::Highs:
132 if constexpr (std::is_same_v<ValueType, double>) {
133 return std::unique_ptr<LpSolverFactory<ValueType>>(new HighsLpSolverFactory<ValueType>());
134 } else {
135 STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "The HiGHS LP solver only supports double precision.");
136 }
137 case storm::solver::LpSolverType::Soplex:
138 return std::unique_ptr<LpSolverFactory<ValueType>>(new SoplexLpSolverFactory<ValueType>());
139 case storm::solver::LpSolverType::Z3:
140 return std::unique_ptr<LpSolverFactory<ValueType>>(new Z3LpSolverFactory<ValueType>());
141 }
142 return nullptr;
143}
144
145template<typename ValueType>
146std::unique_ptr<storm::solver::LpSolver<ValueType>> getLpSolver(storm::Environment const& env, std::string const& name,
147 storm::solver::LpSolverTypeSelection solvType) {
148 std::unique_ptr<storm::utility::solver::LpSolverFactory<ValueType>> factory = getLpSolverFactory<ValueType>(env, solvType);
149 return factory->create(env, name);
150}
151
152template<typename ValueType>
153std::unique_ptr<storm::solver::LpSolver<ValueType, true>> getRawLpSolver(storm::Environment const& env, std::string const& name,
154 storm::solver::LpSolverTypeSelection solvType) {
155 std::unique_ptr<storm::utility::solver::LpSolverFactory<ValueType>> factory = getLpSolverFactory<ValueType>(env, solvType);
156 return factory->createRaw(env, name);
157}
158
159std::unique_ptr<storm::solver::SmtSolver> SmtSolverFactory::create(storm::expressions::ExpressionManager& manager) const {
160 storm::solver::SmtSolverType smtSolverType;
163 } else {
164#ifdef STORM_HAVE_Z3
165 smtSolverType = storm::solver::SmtSolverType::Z3;
166#elif defined STORM_HAVE_MATHSAT
167 smtSolverType = storm::solver::SmtSolverType::Mathsat;
168#else
169 STORM_LOG_THROW(false, storm::exceptions::MissingLibraryException, "Requested an SMT solver but none was installed.");
170#endif
171 }
172 switch (smtSolverType) {
173 case storm::solver::SmtSolverType::Z3:
174 return std::unique_ptr<storm::solver::SmtSolver>(new storm::solver::Z3SmtSolver(manager));
175 case storm::solver::SmtSolverType::Mathsat:
176 return std::unique_ptr<storm::solver::SmtSolver>(new storm::solver::MathsatSmtSolver(manager));
177 }
178 return nullptr;
179}
180
181std::unique_ptr<storm::solver::SmtSolver> Z3SmtSolverFactory::create(storm::expressions::ExpressionManager& manager) const {
182 return std::unique_ptr<storm::solver::SmtSolver>(new storm::solver::Z3SmtSolver(manager));
183}
184
185std::unique_ptr<storm::solver::SmtSolver> MathsatSmtSolverFactory::create(storm::expressions::ExpressionManager& manager) const {
186 return std::unique_ptr<storm::solver::SmtSolver>(new storm::solver::MathsatSmtSolver(manager));
187}
188
189std::unique_ptr<storm::solver::SmtSolver> getSmtSolver(storm::expressions::ExpressionManager& manager) {
190 std::unique_ptr<storm::utility::solver::SmtSolverFactory> factory = std::make_unique<SmtSolverFactory>();
191 return factory->create(manager);
192}
193
194template class LpSolverFactory<double>;
195template class LpSolverFactory<storm::RationalNumber>;
196template class GlpkLpSolverFactory<double>;
197template class GlpkLpSolverFactory<storm::RationalNumber>;
198template class GurobiLpSolverFactory<double>;
199template class GurobiLpSolverFactory<storm::RationalNumber>;
200template class Z3LpSolverFactory<double>;
201template class Z3LpSolverFactory<storm::RationalNumber>;
202template class SoplexLpSolverFactory<double>;
203template class SoplexLpSolverFactory<storm::RationalNumber>;
204template class HighsLpSolverFactory<double>;
205
206template std::unique_ptr<LpSolverFactory<double>> getLpSolverFactory(storm::Environment const& env, storm::solver::LpSolverTypeSelection solvType);
207template std::unique_ptr<LpSolverFactory<storm::RationalNumber>> getLpSolverFactory(storm::Environment const& env,
208 storm::solver::LpSolverTypeSelection solvType);
209template std::unique_ptr<storm::solver::LpSolver<double>> getLpSolver(storm::Environment const& env, std::string const& name,
210 storm::solver::LpSolverTypeSelection solvType);
211template std::unique_ptr<storm::solver::LpSolver<storm::RationalNumber>> getLpSolver(storm::Environment const& env, std::string const& name,
212 storm::solver::LpSolverTypeSelection solvType);
213template std::unique_ptr<storm::solver::LpSolver<double, true>> getRawLpSolver(storm::Environment const& env, std::string const& name,
214 storm::solver::LpSolverTypeSelection solvType);
215template std::unique_ptr<storm::solver::LpSolver<storm::RationalNumber, true>> getRawLpSolver(storm::Environment const& env, std::string const& name,
216 storm::solver::LpSolverTypeSelection solvType);
217} // namespace solver
218} // namespace utility
219} // namespace storm
SolverEnvironment & solver()
GlpkSolverEnvironment & glpk()
bool isLpSolverTypeSetFromDefaultValue() const
storm::solver::LpSolverType const & getLpSolverType() const
GurobiSolverEnvironment & gurobi()
This class is responsible for managing a set of typed variables and all expressions using these varia...
A class that implements the LpSolver interface using glpk as the background solver.
A class that implements the LpSolver interface using Gurobi.
A class that implements the LpSolver interface using HiGHS.
A class that implements the LpSolver interface using Z3.
Definition Z3LpSolver.h:23
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, false > > create(storm::Environment const &env, std::string const &name) const override
Creates a new linear equation solver instance with the given name.
Definition solver.cpp:25
virtual std::unique_ptr< LpSolverFactory< ValueType > > clone() const override
Definition solver.cpp:38
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, true > > createRaw(storm::Environment const &env, std::string const &name) const override
Definition solver.cpp:31
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, true > > createRaw(storm::Environment const &env, std::string const &name) const override
Definition solver.cpp:88
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, false > > create(storm::Environment const &env, std::string const &name) const override
Creates a new linear equation solver instance with the given name.
Definition solver.cpp:83
virtual std::unique_ptr< LpSolverFactory< ValueType > > clone() const override
Definition solver.cpp:95
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, true > > createRaw(storm::Environment const &env, std::string const &name) const override
Definition solver.cpp:64
virtual std::unique_ptr< LpSolverFactory< ValueType > > clone() const override
Definition solver.cpp:69
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, false > > create(storm::Environment const &env, std::string const &name) const override
Creates a new linear equation solver instance with the given name.
Definition solver.cpp:59
virtual std::unique_ptr< storm::solver::SmtSolver > create(storm::expressions::ExpressionManager &manager) const
Creates a new SMT solver instance.
Definition solver.cpp:185
virtual std::unique_ptr< storm::solver::SmtSolver > create(storm::expressions::ExpressionManager &manager) const
Creates a new SMT solver instance.
Definition solver.cpp:159
virtual std::unique_ptr< LpSolverFactory< ValueType > > clone() const override
Definition solver.cpp:54
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, false > > create(storm::Environment const &env, std::string const &name) const override
Creates a new linear equation solver instance with the given name.
Definition solver.cpp:43
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, true > > createRaw(storm::Environment const &env, std::string const &name) const override
Definition solver.cpp:48
virtual std::unique_ptr< LpSolverFactory< ValueType > > clone() const override
Definition solver.cpp:110
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, false > > create(storm::Environment const &env, std::string const &name) const override
Creates a new linear equation solver instance with the given name.
Definition solver.cpp:100
virtual std::unique_ptr< storm::solver::LpSolver< ValueType, true > > createRaw(storm::Environment const &env, std::string const &name) const override
Definition solver.cpp:105
virtual std::unique_ptr< storm::solver::SmtSolver > create(storm::expressions::ExpressionManager &manager) const
Creates a new SMT solver instance.
Definition solver.cpp:181
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
bool hasModule()
Returns true if the given module is registered.
SettingsType const & getModule()
Get module.
std::unique_ptr< LpSolverFactory< ValueType > > getLpSolverFactory(storm::Environment const &env, storm::solver::LpSolverTypeSelection solvType)
Definition solver.cpp:115
std::unique_ptr< storm::solver::LpSolver< ValueType, true > > getRawLpSolver(storm::Environment const &env, std::string const &name, storm::solver::LpSolverTypeSelection solvType)
Definition solver.cpp:153
std::unique_ptr< storm::solver::SmtSolver > getSmtSolver(storm::expressions::ExpressionManager &manager)
Definition solver.cpp:189
std::unique_ptr< storm::solver::LpSolver< ValueType > > getLpSolver(storm::Environment const &env, std::string const &name, storm::solver::LpSolverTypeSelection solvType)
Definition solver.cpp:146
static const bool IsExact