Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
feasibility.cpp
Go to the documentation of this file.
2#include <optional>
3
8
11
25
26namespace storm::pars {
27
28template<typename ConstantType>
30 std::stringstream valuationStr;
31 bool first = true;
32 for (auto const& v : result.valuation) {
33 if (first) {
34 first = false;
35 } else {
36 valuationStr << ", ";
37 }
38 valuationStr << v.first << "=" << v.second;
39 }
40 if (success) {
41 STORM_PRINT_AND_LOG("Result at initial state: " << result.value << " ( approx. " << storm::utility::convertNumber<double>(result.value) << ") at ["
42 << valuationStr.str() << "].\n");
43 } else {
44 STORM_PRINT_AND_LOG("No satisfying result found.\n");
45 }
46 STORM_PRINT_AND_LOG("Time for model checking: " << watch << ".\n");
47}
48
49std::shared_ptr<FeasibilitySynthesisTask const> createFeasibilitySynthesisTaskFromSettings(
50 std::shared_ptr<storm::logic::Formula const> const& formula, std::vector<storm::storage::ParameterRegion<storm::RationalFunction>> const& regions) {
51 STORM_LOG_THROW(formula->isRewardOperatorFormula() || formula->isProbabilityOperatorFormula(), storm::exceptions::NotSupportedException,
52 "We only support reward- and probability operator formulas.");
53 STORM_LOG_THROW(regions.size() <= 1, storm::exceptions::NotSupportedException, "Storm only supports one or zero regions.");
54
55 std::shared_ptr<storm::logic::Formula const> formulaNoBound;
56 if (formula->asOperatorFormula().hasBound()) {
57 std::shared_ptr<storm::logic::Formula> formulaWithoutBounds = formula->clone();
58 formulaWithoutBounds->asOperatorFormula().removeBound();
59 formulaNoBound = formulaWithoutBounds->asSharedPointer();
60 } else {
61 formulaNoBound = formula;
62 }
63
64 FeasibilitySynthesisTask t(formulaNoBound);
66
67 if (formula->asOperatorFormula().hasBound()) {
68 t.setBound(formula->asOperatorFormula().getBound());
69
70 STORM_LOG_THROW(!feasibilitySettings.isParameterDirectionSet(), storm::exceptions::NotSupportedException,
71 "With a bound, the direction for the parameters is inferred from the bound.");
72 STORM_LOG_THROW(!feasibilitySettings.hasOptimalValueGuaranteeBeenSet(), storm::exceptions::NotSupportedException,
73 "When a bound is given, the guarantee is that this bound will be satisfied by a solution.");
74 } else {
75 if (feasibilitySettings.hasOptimalValueGuaranteeBeenSet()) {
76 t.setMaximalAllowedGap(storm::utility::convertNumber<storm::RationalNumber>(feasibilitySettings.getOptimalValueGuarantee()));
77 t.setMaximalAllowedGapIsRelative(!feasibilitySettings.isAbsolutePrecisionSet());
78 }
79 STORM_LOG_THROW(feasibilitySettings.isParameterDirectionSet(), storm::exceptions::NotSupportedException,
80 "Without a bound, the direction for the parameters must be explicitly given.");
81 t.setOptimizationDirection(feasibilitySettings.getParameterDirection());
82 }
83 if (regions.size() == 1) {
84 t.setRegion(regions.front());
85 }
86 return std::make_shared<FeasibilitySynthesisTask const>(std::move(t));
87}
88
89template<typename ValueType>
91 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
92 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings) {
94
95 STORM_PRINT_AND_LOG("Find feasible solution for " << task->getFormula());
96 if (task->isRegionSet()) {
97 STORM_PRINT_AND_LOG(" within region " << task->getRegion());
98 }
99 if (monotonicitySettings.useMonotonicity) {
100 STORM_PRINT_AND_LOG(" and using monotonicity ...");
101 }
103
104 if (feasibilitySettings.getFeasibilityMethod() == storm::pars::FeasibilityMethod::GD) {
105 runFeasibilityWithGD(env, model, task, omittedParameters, monotonicitySettings);
106 } else if (feasibilitySettings.getFeasibilityMethod() == storm::pars::FeasibilityMethod::PLA) {
107 runFeasibilityWithPLA(env, model, task, omittedParameters, monotonicitySettings);
108 } else {
109 STORM_LOG_ASSERT(feasibilitySettings.getFeasibilityMethod() == storm::pars::FeasibilityMethod::SCP, "Remaining method must be SCP.");
110 runFeasibilityWithSCP(env, model, task, omittedParameters, monotonicitySettings);
111 }
112}
113
114template<typename ValueType>
116 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
117 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings) {
119
120 STORM_LOG_THROW(model->isOfType(storm::models::ModelType::Dtmc), storm::exceptions::NotSupportedException,
121 "Gradient descent is currently only supported for DTMCs.");
122 std::shared_ptr<storm::models::sparse::Dtmc<ValueType>> dtmc = model->template as<storm::models::sparse::Dtmc<ValueType>>();
123 STORM_LOG_THROW(task->getFormula().isProbabilityOperatorFormula() || task->getFormula().isRewardOperatorFormula(), storm::exceptions::NotSupportedException,
124 "Input formula needs to be either a probability operator formula or a reward operator formula.");
125 STORM_LOG_THROW(task->isBoundSet(), storm::exceptions::NotImplementedException, "GD (right now) requires an explicitly given bound.");
126 STORM_LOG_THROW(task->getMaximalAllowedGap() == std::nullopt, storm::exceptions::NotSupportedException,
127 "GD cannot provide guarantees on the optimality of the solution..");
128
129 if (omittedParameters && !omittedParameters->empty()) {
130 // TODO get rid of std::cout here
131 std::cout << "Parameters ";
132 for (auto const& entry : *omittedParameters) {
133 std::cout << entry << " ";
134 }
135 std::cout << "are inconsequential.";
136 if (derSettings.areInconsequentialParametersOmitted()) {
137 std::cout << " They will be omitted in the found instantiation.\n";
138 } else {
139 std::cout << " They will be set to 0.5 in the found instantiation. To omit them, set the flag --omit-inconsequential-params.\n";
140 }
141 }
142
143 boost::optional<derivative::GradientDescentConstraintMethod> constraintMethod = derSettings.getConstraintMethod();
144 if (!constraintMethod) {
145 STORM_LOG_ERROR("Unknown Gradient Descent Constraint method: " << derSettings.getConstraintMethodAsString());
146 return;
147 }
148
149 boost::optional<derivative::GradientDescentMethod> method = derSettings.getGradientDescentMethod();
150 if (!method) {
151 STORM_LOG_ERROR("Unknown Gradient Descent method: " << derSettings.getGradientDescentMethodAsString());
152 return;
153 }
154
155 std::optional<std::map<typename utility::parametric::VariableType<ValueType>::type, typename utility::parametric::CoefficientType<ValueType>::type>>
156 startPoint;
157
158 std::optional<storage::ParameterRegion<storm::RationalFunction>> region;
159 if (task->isRegionSet()) {
160 region = task->getRegion();
161 // Gradient descent requires a graph-preserving region.
162 establishGraphPreservation<ValueType>(model, {*region}, true);
163 }
164
165 STORM_PRINT("Finding an extremum using Gradient Descent\n");
166 storm::utility::Stopwatch derivativeWatch(true);
168 env, *dtmc, *method, derSettings.getLearningRate(), derSettings.getAverageDecay(), derSettings.getSquaredAverageDecay(), derSettings.getMiniBatchSize(),
169 derSettings.getTerminationEpsilon(), startPoint, *constraintMethod, region, derSettings.isPrintJsonSet());
170
171 gdsearch.setup(env, task);
172 auto result = gdsearch.gradientDescent();
173 // TODO check what happens if no feasible solution is found
174 if (!derSettings.areInconsequentialParametersOmitted() && omittedParameters) {
175 for (RationalFunctionVariable const& param : *omittedParameters) {
176 if (startPoint) {
177 result.valuation[param] = startPoint->at(param);
178 } else {
179 result.valuation[param] = utility::convertNumber<RationalFunction::CoeffType>(0.5);
180 }
181 }
182 }
183 derivativeWatch.stop();
184
185 if (derSettings.isPrintJsonSet()) {
186 gdsearch.printRunAsJson();
187 }
188
189 if (task->isBoundSet()) {
190 printFeasibilityResult(task->getBound().isSatisfied(result.value), result, derivativeWatch);
191 } else {
192 printFeasibilityResult(true, result, derivativeWatch);
193 }
194}
195
196template<typename ValueType>
198 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
199 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings) {
200 STORM_LOG_THROW(task->isRegionSet(), storm::exceptions::NotSupportedException, "PLA requires an explicitly given region.");
201 storm::solver::OptimizationDirection direction = task->getOptimizationDirection();
202
203 // TODO handle omittedParameterss
205 auto engine = regionVerificationSettings.getRegionCheckEngine();
206
207 auto const graphPreservationStatus =
209
210 auto regionSplittingStrategy = storm::modelchecker::RegionSplittingStrategy();
211
212 regionSplittingStrategy.heuristic = regionVerificationSettings.getRegionSplittingHeuristic();
213 regionSplittingStrategy.estimateKind = regionVerificationSettings.getRegionSplittingEstimateMethod();
214 if (regionVerificationSettings.isSplittingThresholdSet()) {
215 regionSplittingStrategy.maxSplitDimensions = regionVerificationSettings.getSplittingThreshold();
216 }
217
218 if (task->isBoundSet()) {
219 storm::utility::Stopwatch watch(true);
221 storm::api::createTask<ValueType>(task->getFormula().asSharedPointer(), true),
222 engine,
223 regionSplittingStrategy,
225 {},
226 true,
227 graphPreservationStatus};
228 auto valueValuation = storm::api::computeExtremalValue<ValueType>(env, settings, task->getRegion(), direction, storm::utility::zero<ValueType>(),
229 !task->isMaxGapRelative(), task->getBound().getInvertedBound());
230 watch.stop();
231
232 printFeasibilityResult(task->getBound().isSatisfied(valueValuation.value), valueValuation, watch);
233 } else {
234 STORM_LOG_THROW(task->getMaximalAllowedGap() != std::nullopt, storm::exceptions::NotSupportedException,
235 "Without a bound, PLA requires an explicit target in form of a guarantee.");
236
237 ValueType precision = storm::utility::convertNumber<ValueType>(task->getMaximalAllowedGap().value());
238 storm::utility::Stopwatch watch(true);
240 storm::api::createTask<ValueType>(task->getFormula().asSharedPointer(), true),
241 engine,
242 regionSplittingStrategy,
244 {},
245 true,
246 graphPreservationStatus};
247 auto valueValuation = storm::api::computeExtremalValue<ValueType>(env, settings, task->getRegion(), direction, storm::utility::zero<ValueType>(),
248 !task->isMaxGapRelative(), std::nullopt);
249 watch.stop();
250
251 printFeasibilityResult(true, valueValuation, watch);
252 }
253}
254
255template<typename ValueType>
257 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
258 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings) {
259 // Formula and bound checks are done by SparseScpInstantiationSearcher::run().
260 STORM_LOG_THROW(model->isOfType(storm::models::ModelType::Mdp) || model->isOfType(storm::models::ModelType::Dtmc), storm::exceptions::NotSupportedException,
261 "SCP is only supported for MDPs and DTMCs.");
262 // TODO handle omittedParameters.
263 STORM_LOG_THROW(task->isRegionSet(), storm::exceptions::NotSupportedException, "SCP requires an explicitly given region.");
264 // SCP requires a graph-preserving region.
265 establishGraphPreservation<ValueType>(model, {task->getRegion()}, true);
266
269 options.trustRegionInitial = scpSettings.getTrustRegionInitial();
270 options.trustRegionFactor = scpSettings.getTrustRegionFactor();
271 options.trustRegionMax = scpSettings.getTrustRegionMax();
272 options.trustRegionMinExcess = scpSettings.getTrustRegionMinExcess();
273 options.maxIterations = scpSettings.getMaxIterations();
274 options.initialStateBias = scpSettings.getInitialStateBias();
275 options.useMaxViolationObjective = !scpSettings.isSumViolationObjectiveSet();
276
277 auto run = [&]<typename ConstantType>() {
278 storm::utility::Stopwatch watch(true);
280 if (model->isOfType(storm::models::ModelType::Mdp)) {
281 auto mdp = model->template as<storm::models::sparse::Mdp<ValueType>>();
282 result = storm::pars::scp::SparseScpInstantiationSearcher<storm::models::sparse::Mdp<ValueType>, ConstantType>(*mdp, options).run(env, task);
283 } else {
284 auto dtmc = model->template as<storm::models::sparse::Dtmc<ValueType>>();
285 result = storm::pars::scp::SparseScpInstantiationSearcher<storm::models::sparse::Dtmc<ValueType>, ConstantType>(*dtmc, options).run(env, task);
286 }
287 watch.stop();
289 };
290 if (scpSettings.isExactSet()) {
291 run.template operator()<storm::RationalNumber>();
292 } else {
293 run.template operator()<double>();
294 }
295}
296
298 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
299 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings);
300
302 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
303 boost::optional<std::set<RationalFunctionVariable>> omittedParameters, storm::api::MonotonicitySetting monotonicitySettings);
304
306 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
307 boost::optional<std::set<RationalFunctionVariable>> omittedParameters,
308 storm::api::MonotonicitySetting monotonicitySettings);
309
311 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task,
312 boost::optional<std::set<RationalFunctionVariable>> omittedParameters,
313 storm::api::MonotonicitySetting monotonicitySettings);
314
315} // namespace storm::pars
void setup(Environment const &env, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task)
This will setup the matrices used for computing the derivatives by constructing the SparseDerivativeI...
storm::pars::FeasibilityResult< ConstantType > gradientDescent()
Perform Gradient Descent.
Base class for all sparse models.
Definition Model.h:34
void setOptimizationDirection(storm::solver::OptimizationDirection const &dir)
void setMaximalAllowedGap(storm::RationalNumber const &maxGap)
void setBound(storm::logic::Bound const &bound)
void setRegion(storm::storage::ParameterRegion< storm::RationalFunction > const &region)
Finds a parameter valuation satisfying an unbounded reachability threshold property (probability or r...
A class that provides convenience operations to display run times.
Definition Stopwatch.h:13
void stop()
Stop stopwatch and add measured time to total time.
Definition Stopwatch.cpp:42
#define STORM_LOG_ERROR(message)
Definition logging.h:36
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
storm::modelchecker::CheckTask< storm::logic::Formula, ValueType > createTask(std::shared_ptr< const storm::logic::Formula > const &formula, bool onlyInitialStatesRelevant=false)
storm::pars::FeasibilityResult< storm::RationalNumber > computeExtremalValue(Environment const &env, RefinementOptions< ValueType > settings, storm::storage::ParameterRegion< ValueType > const &region, storm::solver::OptimizationDirection const &dir, std::optional< ValueType > const &precision, bool absolutePrecision, std::optional< storm::logic::Bound > const &boundInvariant)
Finds the extremal value in the given region.
Definition region.h:334
storm::pars::modelchecker::MonotonicityOptions MonotonicitySetting
Definition region.h:45
storm::pars::modelchecker::RegionRefinementOptions< ValueType > RefinementOptions
Definition region.h:48
@ RobustParameterLifting
Parameter lifting approach based on robust markov models instead of generating nondeterminism.
void runFeasibilityWithSCP(storm::Environment const &env, std::shared_ptr< storm::models::sparse::Model< ValueType > > model, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task, boost::optional< std::set< RationalFunctionVariable > > omittedParameters, storm::api::MonotonicitySetting monotonicitySettings)
void printFeasibilityResult(bool success, storm::pars::FeasibilityResult< ConstantType > const &result, storm::utility::Stopwatch const &watch)
storm::pars::analysis::GraphPreservationStatus establishGraphPreservation(std::shared_ptr< storm::models::ModelBase > const &model, std::vector< storm::storage::ParameterRegion< ValueType > > const &regions, bool graphPreservationRequired)
Determines to what extent the regions are known to be well-defined and graph-preserving.
void runFeasibilityWithGD(storm::Environment const &env, std::shared_ptr< storm::models::sparse::Model< ValueType > > model, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task, boost::optional< std::set< RationalFunctionVariable > > omittedParameters, storm::api::MonotonicitySetting monotonicitySettings)
void performFeasibility(storm::Environment const &env, std::shared_ptr< storm::models::sparse::Model< ValueType > > model, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task, boost::optional< std::set< RationalFunctionVariable > > omittedParameters, storm::api::MonotonicitySetting monotonicitySettings)
void runFeasibilityWithPLA(storm::Environment const &env, std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::shared_ptr< storm::pars::FeasibilitySynthesisTask const > const &task, boost::optional< std::set< RationalFunctionVariable > > omittedParameters, storm::api::MonotonicitySetting monotonicitySettings)
std::shared_ptr< FeasibilitySynthesisTask const > createFeasibilitySynthesisTaskFromSettings(std::shared_ptr< storm::logic::Formula const > const &formula, std::vector< storm::storage::ParameterRegion< storm::RationalFunction > > const &regions)
SettingsType const & getModule()
Get module.
ValueType zero()
Definition constants.cpp:26
TargetType convertNumber(SourceType const &number)
carl::Variable RationalFunctionVariable
#define STORM_PRINT_AND_LOG(message)
Definition print.h:20
#define STORM_PRINT(message)
Define the macros that print information to stdout and optionally also log it.
Definition print.h:14
The result of a feasibility synthesis: a parameter valuation and the value at the initial state under...
std::map< storm::RationalFunctionVariable, storm::RationalFunctionCoefficient > valuation
storm::utility::ExtendedValueType< ConstantType > value
Configuration for the SCP feasibility search (SparseScpInstantiationSearcher).
Definition ScpOptions.h:16
double trustRegionMinExcess
Termination threshold on (radius - 1).
Definition ScpOptions.h:28
double trustRegionMax
Upper cap on the trust region radius.
Definition ScpOptions.h:25
double initialStateBias
Weight of the initial state's value in the LP objective. It is dominated by the violation objective a...
Definition ScpOptions.h:34
double trustRegionInitial
Initial trust region radius (delta_0).
Definition ScpOptions.h:18
bool useMaxViolationObjective
If true, the LP objective minimizes the maximum per-state constraint violation; if false,...
Definition ScpOptions.h:36
uint64_t maxIterations
Maximum number of SCP iterations before giving up.
Definition ScpOptions.h:31
double trustRegionFactor
Multiplicative factor (gamma) by which the trust region grows after an accepted step and shrinks afte...
Definition ScpOptions.h:22
The outcome of an SCP search at ConstantType precision (double or storm::RationalNumber): the best po...
Definition ScpResult.h:20