Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MinMaxLinearEquationSolver.cpp
Go to the documentation of this file.
2
3#include <cstdint>
4
10
12
14
19
20namespace storm::solver {
21
22template<typename ValueType, typename SolutionType>
24 : direction(direction), trackScheduler(false), uniqueSolution(false), noEndComponents(false), cachingEnabled(false), requirementsChecked(false) {
25 // Intentionally left empty.
26}
27
28template<typename ValueType, typename SolutionType>
32
33template<typename ValueType, typename SolutionType>
35 std::vector<ValueType> const& b) const {
36 STORM_LOG_WARN_COND_DEBUG(this->isRequirementsCheckedSet(),
37 "The requirements of the solver have not been marked as checked. Please provide the appropriate check or mark the requirements "
38 "as checked (if applicable).");
39 return internalSolveEquations(env, d, x, b);
41
42template<typename ValueType, typename SolutionType>
44 std::vector<ValueType> const& b) const {
45 STORM_LOG_THROW(isSet(this->direction), storm::exceptions::IllegalFunctionCallException, "Optimization direction not set.");
46 solveEquations(env, convert(this->direction), x, b);
47}
48
49template<typename ValueType, typename SolutionType>
53
54template<typename ValueType, typename SolutionType>
57}
58
59template<typename ValueType, typename SolutionType>
63
64template<typename ValueType, typename SolutionType>
66 return uniqueSolution || noEndComponents;
68
69template<typename ValueType, typename SolutionType>
73
74template<typename ValueType, typename SolutionType>
78
79template<typename ValueType, typename SolutionType>
81 this->trackScheduler = trackScheduler;
82 if (!this->trackScheduler) {
83 schedulerChoices = boost::none;
84 }
85}
86
87template<typename ValueType, typename SolutionType>
89 return this->trackScheduler;
90}
91
92template<typename ValueType, typename SolutionType>
94 return static_cast<bool>(schedulerChoices);
95}
96
97template<typename ValueType, typename SolutionType>
99 STORM_LOG_THROW(hasScheduler(), storm::exceptions::IllegalFunctionCallException, "Cannot retrieve scheduler, because none was generated.");
100 storm::storage::Scheduler<ValueType> result(schedulerChoices->size());
101 uint_fast64_t state = 0;
102 for (auto const& schedulerChoice : schedulerChoices.get()) {
103 result.setChoice(schedulerChoice, state);
104 ++state;
105 }
106 return result;
108
109template<typename ValueType, typename SolutionType>
111 STORM_LOG_THROW(hasScheduler(), storm::exceptions::IllegalFunctionCallException, "Cannot retrieve scheduler choices, because they were not generated.");
112 return schedulerChoices.get();
113}
114
115template<typename ValueType, typename SolutionType>
117 if (cachingEnabled && !value) {
118 // caching will be turned off. Hence we clear the cache at this point
119 clearCache();
120 }
121 cachingEnabled = value;
122}
124template<typename ValueType, typename SolutionType>
129template<typename ValueType, typename SolutionType>
131 // Intentionally left empty.
132}
134template<typename ValueType, typename SolutionType>
136 initialScheduler = std::move(choices);
137}
139template<typename ValueType, typename SolutionType>
141 return static_cast<bool>(initialScheduler);
142}
143
144template<typename ValueType, typename SolutionType>
146 return initialScheduler.get();
147}
148
149template<typename ValueType, typename SolutionType>
151 Environment const&, boost::optional<storm::solver::OptimizationDirection> const& direction, bool const& hasInitialScheduler) const {
153}
155template<typename ValueType, typename SolutionType>
157 this->requirementsChecked = value;
158}
160template<typename ValueType, typename SolutionType>
165template<typename ValueType, typename SolutionType>
167 STORM_LOG_ASSERT(this->hasInitialScheduler(), "Expecting an initial scheduler to be set before setting the states for which the choices are fixed");
168 this->choiceFixedForRowGroup = std::move(schedulerFixedForRowGroup);
170
171template<typename ValueType, typename SolutionType>
173 // Intentionally left empty
174}
176template<typename ValueType, typename SolutionType>
178 this->requirementsChecked = value;
179}
180
181template<typename ValueType, typename SolutionType>
183 return this->requirementsChecked;
184}
185
186template<typename ValueType, typename SolutionType>
188 this->robustUncertainty = robust;
189}
190
191template<typename ValueType, typename SolutionType>
193 return this->robustUncertainty;
194}
195
196template<typename ValueType, typename SolutionType>
198 Environment const& env, bool hasUniqueSolution, bool hasNoEndComponents, boost::optional<storm::solver::OptimizationDirection> const& direction,
199 bool hasInitialScheduler, bool trackScheduler) const {
200 // Create dummy solver and ask it for requirements.
201 std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> solver = this->create(env);
202 solver->setTrackScheduler(trackScheduler);
203 solver->setHasUniqueSolution(hasUniqueSolution);
204 solver->setHasNoEndComponents(hasNoEndComponents);
205 return solver->getRequirements(env, direction, hasInitialScheduler);
206}
207
208template<typename ValueType, typename SolutionType>
209std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> MinMaxLinearEquationSolverFactory<ValueType, SolutionType>::create(
210 Environment const& env, storm::storage::SparseMatrix<ValueType> const& matrix) const {
211 std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> solver = this->create(env);
212 solver->setMatrix(matrix);
213 return solver;
214}
215
216template<typename ValueType, typename SolutionType>
217std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> MinMaxLinearEquationSolverFactory<ValueType, SolutionType>::create(
218 Environment const& env, storm::storage::SparseMatrix<ValueType>&& matrix) const {
219 std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> solver = this->create(env);
220 solver->setMatrix(std::move(matrix));
221 return solver;
222}
223
224template<typename ValueType, typename SolutionType>
230template<typename ValueType, typename SolutionType>
231std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> GeneralMinMaxLinearEquationSolverFactory<ValueType, SolutionType>::create(
232 Environment const& env) const {
233 std::unique_ptr<MinMaxLinearEquationSolver<ValueType, SolutionType>> result;
234 if constexpr (std::is_same_v<ValueType, storm::Interval>) {
235 // TODO: consider robust minMax solver methods and corresponding entries in the environment.
236 return std::make_unique<IterativeMinMaxLinearEquationSolver<ValueType, SolutionType>>();
237 } else {
238 // TODO some minmax linear equation solvers only support SolutionType == ValueType.
239 auto method = env.solver().minMax().getMethod();
240 if (method == MinMaxMethod::ValueIteration || method == MinMaxMethod::PolicyIteration || method == MinMaxMethod::RationalSearch ||
241 method == MinMaxMethod::IntervalIteration || method == MinMaxMethod::SoundValueIteration || method == MinMaxMethod::OptimisticValueIteration ||
242 method == MinMaxMethod::ViToPi) {
243 result = std::make_unique<IterativeMinMaxLinearEquationSolver<ValueType, SolutionType>>(
244 std::make_unique<GeneralLinearEquationSolverFactory<ValueType>>());
245 } else if (method == MinMaxMethod::Topological) {
246 result = std::make_unique<TopologicalMinMaxLinearEquationSolver<ValueType>>();
247 } else if (method == MinMaxMethod::LinearProgramming || method == MinMaxMethod::ViToLp) {
248 result = std::make_unique<LpMinMaxLinearEquationSolver<ValueType>>(storm::utility::solver::getLpSolverFactory<ValueType>());
249 } else if (method == MinMaxMethod::Acyclic) {
250 result = std::make_unique<AcyclicMinMaxLinearEquationSolver<ValueType>>();
251 } else {
252 STORM_LOG_THROW(false, storm::exceptions::InvalidSettingsException, "Unsupported technique.");
253 }
254 result->setRequirementsChecked(this->isRequirementsCheckedSet());
255 return result;
256 }
257}
258
259template<>
260std::unique_ptr<MinMaxLinearEquationSolver<storm::RationalNumber>> GeneralMinMaxLinearEquationSolverFactory<storm::RationalNumber>::create(
261 Environment const& env) const {
262 std::unique_ptr<MinMaxLinearEquationSolver<storm::RationalNumber>> result;
263 auto method = env.solver().minMax().getMethod();
264 if (method == MinMaxMethod::ValueIteration || method == MinMaxMethod::PolicyIteration || method == MinMaxMethod::RationalSearch ||
265 method == MinMaxMethod::IntervalIteration || method == MinMaxMethod::SoundValueIteration || method == MinMaxMethod::OptimisticValueIteration ||
266 method == MinMaxMethod::ViToPi) {
267 result = std::make_unique<IterativeMinMaxLinearEquationSolver<storm::RationalNumber>>(
268 std::make_unique<GeneralLinearEquationSolverFactory<storm::RationalNumber>>());
269 } else if (method == MinMaxMethod::LinearProgramming || method == MinMaxMethod::ViToLp) {
270 result = std::make_unique<LpMinMaxLinearEquationSolver<storm::RationalNumber>>(storm::utility::solver::getLpSolverFactory<storm::RationalNumber>());
271 } else if (method == MinMaxMethod::Acyclic) {
272 result = std::make_unique<AcyclicMinMaxLinearEquationSolver<storm::RationalNumber>>();
273 } else if (method == MinMaxMethod::Topological) {
274 result = std::make_unique<TopologicalMinMaxLinearEquationSolver<storm::RationalNumber>>();
275 } else {
276 STORM_LOG_THROW(false, storm::exceptions::InvalidSettingsException, "Unsupported technique.");
277 }
278 result->setRequirementsChecked(this->isRequirementsCheckedSet());
279 return result;
280}
281
283
286
290
294} // namespace storm::solver
SolverEnvironment & solver()
storm::solver::MinMaxMethod const & getMethod() const
MinMaxSolverEnvironment & minMax()
virtual std::unique_ptr< MinMaxLinearEquationSolver< ValueType, SolutionType > > create(Environment const &env) const override
MinMaxLinearEquationSolverRequirements getRequirements(Environment const &env, bool hasUniqueSolution=false, bool hasNoEndComponents=false, boost::optional< storm::solver::OptimizationDirection > const &direction=boost::none, bool hasInitialScheduler=false, bool trackScheduler=false) const
Retrieves the requirements of the solver that would be created when calling create() right now.
std::unique_ptr< MinMaxLinearEquationSolver< ValueType, SolutionType > > create(Environment const &env, storm::storage::SparseMatrix< ValueType > const &matrix) const
A class representing the interface that all min-max linear equation solvers shall implement.
virtual void setSchedulerFixedForRowGroup(storm::storage::BitVector &&schedulerFixedForRowGroup)
Sets the states for which the choices are fixed.
void unsetOptimizationDirection()
Unsets the optimization direction to use for calls to methods that do not explicitly provide one.
storm::storage::Scheduler< ValueType > computeScheduler() const
Retrieves the generated scheduler.
void setHasUniqueSolution(bool value=true)
Sets whether the solution to the min max equation system is known to be unique.
void setUncertaintyIsRobust(bool robust)
Set whether uncertainty should be interpreted adverserially (robust) or not.
bool solveEquations(Environment const &env, OptimizationDirection d, std::vector< SolutionType > &x, std::vector< ValueType > const &b) const
Solves the equation system x = min/max(A*x + b) given by the parameters.
virtual void clearCache() const
Clears the currently cached data that has been stored during previous calls of the solver.
void setOptimizationDirection(OptimizationDirection direction)
Sets an optimization direction to use for calls to methods that do not explicitly provide one.
MinMaxLinearEquationSolver(OptimizationDirectionSetting direction=OptimizationDirectionSetting::Unset)
void setInitialScheduler(std::vector< uint_fast64_t > &&choices)
Sets a valid initial scheduler that is required by some solvers (see requirements of solvers).
bool hasScheduler() const
Retrieves whether the solver generated a scheduler.
std::vector< uint_fast64_t > const & getSchedulerChoices() const
Retrieves the generated (deterministic) choices of the optimal scheduler.
std::vector< uint_fast64_t > const & getInitialScheduler() const
Retrieves the initial scheduler if one was set.
void setRequirementsChecked(bool value=true)
Notifies the solver that the requirements for solving equations have been checked.
bool hasUniqueSolution() const
Retrieves whether the solution to the min max equation system is assumed to be unique.
virtual MinMaxLinearEquationSolverRequirements getRequirements(Environment const &env, boost::optional< storm::solver::OptimizationDirection > const &direction=boost::none, bool const &hasInitialScheduler=false) const
Retrieves the requirements of this solver for solving equations with the current settings.
void setHasNoEndComponents(bool value=true)
Sets whether the min max equation system is known to not have any end components.
bool isUncertaintyRobust() const
Is the uncertainty to be interpreted robustly (adverserially) or not?
bool isTrackSchedulerSet() const
Retrieves whether this solver is set to generate schedulers.
bool hasNoEndComponents() const
Retrieves whether the min max equation system is known to not have any end components.
void setCachingEnabled(bool value)
Sets whether some of the generated data during solver calls should be cached.
bool isRequirementsCheckedSet() const
Retrieves whether the solver is aware that the requirements were checked.
bool isCachingEnabled() const
Retrieves whether some of the generated data during solver calls should be cached.
bool hasInitialScheduler() const
Returns true iff an initial scheduler is set.
void setTrackScheduler(bool trackScheduler=true)
Sets whether schedulers are generated when solving equation systems.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
void setChoice(SchedulerChoice< ValueType > const &choice, uint_fast64_t modelState, uint_fast64_t memoryState=0)
Sets the choice defined by the scheduler for the given state.
Definition Scheduler.cpp:37
A class that holds a possibly non-square matrix in the compressed row storage format.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
#define STORM_LOG_WARN_COND_DEBUG(cond, message)
Definition macros.h:18
OptimizationDirection convert(OptimizationDirectionSetting s)
bool isSet(OptimizationDirectionSetting s)