Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
GradientDescentInstantiationSearcher.h
Go to the documentation of this file.
1#pragma once
2
3#include <boost/variant.hpp>
4#include <map>
5#include <memory>
6
16#include "storm/logic/Formula.h"
21
22namespace storm {
23namespace derivative {
24template<typename FunctionType, typename ConstantType>
26 public:
28
50 ConstantType learningRate = 0.1, ConstantType averageDecay = 0.9, ConstantType squaredAverageDecay = 0.999, uint_fast64_t miniBatchSize = 32,
51 ConstantType terminationEpsilon = 1e-6,
52 std::optional<
54 startPoint = std::nullopt,
56 std::optional<storage::ParameterRegion<FunctionType>> region = std::nullopt, bool recordRun = false)
57 : env(env),
58 model(model),
59 derivativeEvaluationHelper(std::make_unique<SparseDerivativeInstantiationModelChecker<FunctionType, ConstantType>>(model)),
60 instantiationModelChecker(
61 std::make_unique<modelchecker::SparseDtmcInstantiationModelChecker<models::sparse::Dtmc<FunctionType>, ConstantType>>(env, model)),
62 startPoint(startPoint),
63 miniBatchSize(miniBatchSize),
64 terminationEpsilon(terminationEpsilon),
65 constraintMethod(constraintMethod),
66 region(region),
67 recordRun(recordRun) {
68 STORM_LOG_ERROR_COND(region == std::nullopt || (constraintMethod == GradientDescentConstraintMethod::PROJECT ||
70 "Specifying a region is only supported if you are constraining by projection.");
71 // TODO should we put this in subclasses?
72 switch (method) {
74 Adam adam;
75 adam.learningRate = learningRate;
76 adam.averageDecay = averageDecay;
77 adam.averageDecay = averageDecay;
78 adam.squaredAverageDecay = squaredAverageDecay;
79 gradientDescentType = adam;
80 break;
81 }
83 RAdam radam;
84 radam.learningRate = learningRate;
85 radam.averageDecay = averageDecay;
86 radam.squaredAverageDecay = squaredAverageDecay;
87 gradientDescentType = radam;
88 break;
89 }
91 RmsProp rmsProp;
92 rmsProp.learningRate = learningRate;
93 rmsProp.averageDecay = averageDecay;
94 gradientDescentType = rmsProp;
95 break;
96 }
99 Plain plain;
100 plain.learningRate = learningRate;
101 gradientDescentType = plain;
102 if (method == GradientDescentMethod::PLAIN_SIGN) {
103 useSignsOnly = true;
104 } else {
105 useSignsOnly = false;
106 }
107 break;
108 }
111 Momentum momentum;
112 momentum.learningRate = learningRate;
113 // TODO Document this
114 momentum.momentumTerm = averageDecay;
115 gradientDescentType = momentum;
117 useSignsOnly = true;
118 } else {
119 useSignsOnly = false;
120 }
121 break;
122 }
125 Nesterov nesterov;
126 nesterov.learningRate = learningRate;
127 // TODO Document this
128 nesterov.momentumTerm = averageDecay;
129 gradientDescentType = nesterov;
131 useSignsOnly = true;
132 } else {
133 useSignsOnly = false;
134 }
135 break;
136 }
137 }
138 }
139
148 void setup(Environment const& env, std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> const& task) {
149 this->env = env;
150 this->parameters = storm::models::sparse::getProbabilityParameters(model);
151 this->synthesisTask = task;
152 STORM_LOG_ASSERT(task->getFormula().isProbabilityOperatorFormula() || task->getFormula().isRewardOperatorFormula(),
153 "Formula must be either a reward or a probability operator formula.");
154
155 std::shared_ptr<storm::logic::Formula> formulaWithoutBounds = task->getFormula().clone();
156 formulaWithoutBounds->asOperatorFormula().removeBound();
157 this->currentFormulaNoBound = formulaWithoutBounds->asSharedPointer();
158
159 if (task->getFormula().isRewardOperatorFormula()) {
160 auto rewardParameters = storm::models::sparse::getRewardParameters(model);
161 this->parameters.insert(rewardParameters.begin(), rewardParameters.end());
162 }
163
164 this->currentCheckTaskNoBound = std::make_unique<storm::modelchecker::CheckTask<storm::logic::Formula, FunctionType>>(*currentFormulaNoBound);
165 this->currentCheckTaskNoBoundConstantType =
166 std::make_unique<storm::modelchecker::CheckTask<storm::logic::Formula, ConstantType>>(*currentFormulaNoBound);
167
168 instantiationModelChecker->specifyFormula(*this->currentCheckTaskNoBound);
169 derivativeEvaluationHelper->specifyFormula(env, *this->currentCheckTaskNoBound);
170 }
171
176
180 void printRunAsJson();
181
186 std::map<typename utility::parametric::VariableType<FunctionType>::type, typename utility::parametric::CoefficientType<FunctionType>::type> position;
188 };
189
192 std::vector<VisualizationPoint> getVisualizationWalk();
193
194 private:
195 void resetDynamicValues();
196
197 Environment env;
198 std::shared_ptr<storm::pars::FeasibilitySynthesisTask const> synthesisTask;
199 std::unique_ptr<modelchecker::CheckTask<storm::logic::Formula, FunctionType>> currentCheckTaskNoBound;
200 std::unique_ptr<modelchecker::CheckTask<storm::logic::Formula, ConstantType>> currentCheckTaskNoBoundConstantType;
201 std::shared_ptr<storm::logic::Formula const> currentFormulaNoBound;
202
204 std::set<typename utility::parametric::VariableType<FunctionType>::type> parameters;
205 const std::unique_ptr<storm::derivative::SparseDerivativeInstantiationModelChecker<FunctionType, ConstantType>> derivativeEvaluationHelper;
206 std::unique_ptr<storm::analysis::MonotonicityHelper<FunctionType, ConstantType>> monotonicityHelper;
207 const std::unique_ptr<modelchecker::SparseDtmcInstantiationModelChecker<models::sparse::Dtmc<FunctionType>, ConstantType>> instantiationModelChecker;
208 std::optional<std::map<typename utility::parametric::VariableType<FunctionType>::type, typename utility::parametric::CoefficientType<FunctionType>::type>>
209 startPoint;
210 const uint_fast64_t miniBatchSize;
211 const ConstantType terminationEpsilon;
212 const GradientDescentConstraintMethod constraintMethod;
213 const std::optional<storage::ParameterRegion<FunctionType>> region;
214
215 // This is for visualizing data
216 const bool recordRun;
217 std::vector<VisualizationPoint> walk;
218
219 // Gradient Descent types and data that belongs to them, with hyperparameters and running data.
220 struct Adam {
221 ConstantType averageDecay;
222 ConstantType squaredAverageDecay;
223 ConstantType learningRate;
224 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> decayingStepAverageSquared;
225 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> decayingStepAverage;
226 };
227 struct RAdam {
228 ConstantType averageDecay;
229 ConstantType squaredAverageDecay;
230 ConstantType learningRate;
231 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> decayingStepAverageSquared;
232 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> decayingStepAverage;
233 };
234 struct RmsProp {
235 ConstantType averageDecay;
236 ConstantType learningRate;
237 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> rootMeanSquare;
238 };
239 struct Plain {
240 ConstantType learningRate;
241 };
242 struct Momentum {
243 ConstantType learningRate;
244 ConstantType momentumTerm;
245 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> pastStep;
246 };
247 struct Nesterov {
248 ConstantType learningRate;
249 ConstantType momentumTerm;
250 std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType> pastStep;
251 };
252 typedef boost::variant<Adam, RAdam, RmsProp, Plain, Momentum, Nesterov> GradientDescentType;
253 GradientDescentType gradientDescentType;
254 // Only respected by some Gradient Descent methods, the ones that have a "sign" version in the GradientDescentMethod enum
255 bool useSignsOnly;
256
257 ConstantType logarithmicBarrierTerm;
258
259 ExtendedConstantType stochasticGradientDescent(
261 ConstantType doStep(
264 const std::map<typename utility::parametric::VariableType<FunctionType>::type, ConstantType>& gradient, uint_fast64_t stepNum);
265 ConstantType constantTypeSqrt(ConstantType input) {
266 return storm::utility::sqrt(input);
267 }
268
269 utility::Stopwatch stochasticWatch;
270 utility::Stopwatch batchWatch;
271 utility::Stopwatch startingPointCalculationWatch;
272};
273} // namespace derivative
274} // namespace storm
storm::models::sparse::Dtmc< double > Dtmc
storm::utility::ExtendedValueType< ConstantType > ExtendedConstantType
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...
std::vector< VisualizationPoint > getVisualizationWalk()
Get the visualization walk that is recorded if recordRun is set to true in the constructor (false by ...
GradientDescentInstantiationSearcher(Environment const &env, storm::models::sparse::Dtmc< FunctionType > const &model, GradientDescentMethod method=GradientDescentMethod::ADAM, ConstantType learningRate=0.1, ConstantType averageDecay=0.9, ConstantType squaredAverageDecay=0.999, uint_fast64_t miniBatchSize=32, ConstantType terminationEpsilon=1e-6, std::optional< std::map< typename utility::parametric::VariableType< FunctionType >::type, typename utility::parametric::CoefficientType< FunctionType >::type > > startPoint=std::nullopt, GradientDescentConstraintMethod constraintMethod=GradientDescentConstraintMethod::PROJECT_WITH_GRADIENT, std::optional< storage::ParameterRegion< FunctionType > > region=std::nullopt, bool recordRun=false)
The GradientDescentInstantiationSearcher can find extrema and feasible instantiations in pMCs,...
storm::pars::FeasibilityResult< ConstantType > gradientDescent()
Perform Gradient Descent.
This class represents a discrete-time Markov chain.
Definition Dtmc.h:16
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:68
GradientDescentMethod
GradientDescentMethod is the method of Gradient Descent the GradientDescentInstantiationSearcher shal...
GradientDescentConstraintMethod
GradientDescentConstraintMethod is the method for mitigating constraints that the GradientDescentInst...
std::set< storm::RationalFunctionVariable > getRewardParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rewards.
Definition Model.cpp:736
std::set< storm::RationalFunctionVariable > getProbabilityParameters(Model< storm::RationalFunction > const &model)
Get all probability parameters occurring on transitions.
Definition Model.cpp:732
std::conditional_t< storm::NumberTraits< ValueType >::HasInfinity, ValueType, ExtendedNumber< ValueType > > ExtendedValueType
The type to use for values that may be infinite.
ValueType sqrt(ValueType const &number)
The contract of a DD backend.
A point in the Gradient Descent walk, recorded if recordRun is set to true in the constructor (false ...
std::map< typename utility::parametric::VariableType< FunctionType >::type, typename utility::parametric::CoefficientType< FunctionType >::type > position
The result of a feasibility synthesis: a parameter valuation and the value at the initial state under...