62template<
typename ValueType>
72 std::vector<std::map<typename storm::utility::parametric::VariableType<ValueType>::type,
73 std::vector<typename storm::utility::parametric::CoefficientType<ValueType>::type>>>
79template<
template<
typename,
typename>
class ModelCheckerType,
typename ModelType,
typename ValueType,
typename SolveValueType =
double>
82 ModelCheckerType<ValueType, SolveValueType> derivativeModelchecker(model);
85 for (
auto const& property : input.
properties) {
89 derivativeModelchecker.specifyFormula(
Environment(), checkTask);
94 std::vector<typename storm::utility::parametric::VariableType<ValueType>::type> parameters;
95 std::vector<typename std::vector<typename storm::utility::parametric::CoefficientType<ValueType>::type>::const_iterator> iterators;
96 std::vector<typename std::vector<typename storm::utility::parametric::CoefficientType<ValueType>::type>::const_iterator> iteratorEnds;
102 iteratorEnds.clear();
104 for (
auto const& entry : product) {
105 parameters.push_back(entry.first);
106 iterators.push_back(entry.second.cbegin());
107 iteratorEnds.push_back(entry.second.cend());
113 for (uint64_t i = 0; i < parameters.size(); ++i) {
114 valuation[parameters[i]] = *iterators[i];
118 std::unique_ptr<storm::modelchecker::CheckResult> originalResult = originalModelchecker.
check(
Environment(), valuation);
119 valuationWatch.
stop();
121 boost::optional<std::vector<SolveValueType>> valueVector = boost::none;
122 if (originalResult) {
123 valueVector = originalResult->template asExplicitQuantitativeCheckResult<SolveValueType>().getValueVector();
129 for (
auto const& parameter : parameters) {
131 std::unique_ptr<storm::modelchecker::CheckResult> result = derivativeModelchecker.check(
Environment(), valuation, parameter, valueVector);
132 valuationWatch.
stop();
141 for (uint64_t i = 0; i < parameters.size(); ++i) {
143 if (iterators[i] == iteratorEnds[i]) {
145 iterators[i] = product.at(parameters[i]).cbegin();
148 if (i == parameters.size() - 1) {
164template<
template<
typename,
typename>
class ModelCheckerType,
typename ModelType,
typename ValueType,
typename SolveValueType = double,
bool Derivative =
false>
167 ModelCheckerType<ModelType, SolveValueType>
modelchecker(model);
169 for (
auto const& property : input.
properties) {
177 std::vector<typename storm::utility::parametric::VariableType<ValueType>::type> parameters;
178 std::vector<typename std::vector<typename storm::utility::parametric::CoefficientType<ValueType>::type>::const_iterator> iterators;
179 std::vector<typename std::vector<typename storm::utility::parametric::CoefficientType<ValueType>::type>::const_iterator> iteratorEnds;
185 iteratorEnds.clear();
187 for (
auto const& entry : product) {
188 parameters.push_back(entry.first);
189 iterators.push_back(entry.second.cbegin());
190 iteratorEnds.push_back(entry.second.cend());
196 for (uint64_t i = 0; i < parameters.size(); ++i) {
197 valuation[parameters[i]] = *iterators[i];
202 valuationWatch.
stop();
209 for (uint64_t i = 0; i < parameters.size(); ++i) {
211 if (iterators[i] == iteratorEnds[i]) {
213 iterators[i] = product.at(parameters[i]).cbegin();
216 if (i == parameters.size() - 1) {
232template<
typename ValueType,
typename SolveValueType =
double>
237 ValueType, SolveValueType>(*model->template as<storm::models::sparse::Dtmc<ValueType>>(), input, samples);
239 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"Sampling the derivative is currently only supported for DTMCs.");
243template<
typename ValueType,
typename SolveValueType =
double>
248 SolveValueType>(*model->template as<storm::models::sparse::Dtmc<ValueType>>(), input, samples);
251 SolveValueType>(*model->template as<storm::models::sparse::Ctmc<ValueType>>(), input, samples);
254 SolveValueType>(*model->template as<storm::models::sparse::Mdp<ValueType>>(), input, samples);
256 STORM_LOG_THROW(
false, storm::exceptions::NotSupportedException,
"Sampling is currently only supported for DTMCs, CTMCs and MDPs.");
260template<
typename ValueType>
262 STORM_LOG_THROW(!model || model->isSparseModel(), storm::exceptions::NotSupportedException,
"Sampling is only supported for sparse models.");
265 if (sampleString.empty()) {
270 std::set<typename storm::utility::parametric::VariableType<ValueType>::type> modelParameters;
274 modelParameters.insert(rewParameters.begin(), rewParameters.end());
276 std::vector<std::string> cartesianProducts;
277 boost::split(cartesianProducts, sampleString, boost::is_any_of(
";"));
278 for (
auto& product : cartesianProducts) {
279 boost::trim(product);
282 std::vector<std::string> valuesForVariables;
283 boost::split(valuesForVariables, product, boost::is_any_of(
","));
284 for (
auto& values : valuesForVariables) {
288 std::set<typename storm::utility::parametric::VariableType<ValueType>::type> encounteredParameters;
291 for (
auto const& varValues : valuesForVariables) {
292 auto equalsPosition = varValues.find(
"=");
293 STORM_LOG_THROW(equalsPosition != varValues.npos, storm::exceptions::WrongFormatException,
"Incorrect format of samples.");
294 std::string variableName = varValues.substr(0, equalsPosition);
295 boost::trim(variableName);
296 std::string values = varValues.substr(equalsPosition + 1);
299 bool foundParameter =
false;
301 for (
auto const& parameter : modelParameters) {
302 std::stringstream parameterStream;
303 parameterStream << parameter;
304 if (parameterStream.str() == variableName) {
305 foundParameter =
true;
306 theParameter = parameter;
307 encounteredParameters.insert(parameter);
310 STORM_LOG_THROW(foundParameter, storm::exceptions::WrongFormatException,
"Unknown parameter '" << variableName <<
"'.");
312 std::vector<std::string> splitValues;
313 boost::split(splitValues, values, boost::is_any_of(
":"));
314 STORM_LOG_THROW(!splitValues.empty(), storm::exceptions::WrongFormatException,
"Expecting at least one value per parameter.");
316 auto& list = newCartesianProduct[theParameter];
318 for (
auto& value : splitValues) {
324 STORM_LOG_THROW(encounteredParameters == modelParameters, storm::exceptions::WrongFormatException,
325 "Variables for all parameters are required when providing samples.");
331template<
typename ValueType>
333 std::string
const& instantiationString) {
335 "Gradient descent is currently only supported for DTMCs.");
336 std::shared_ptr<storm::models::sparse::Dtmc<ValueType>> dtmc = model->template as<storm::models::sparse::Dtmc<ValueType>>();
339 auto formula = formulas[0];
341 STORM_LOG_THROW(formula->isProbabilityOperatorFormula() || formula->isRewardOperatorFormula(), storm::exceptions::NotSupportedException,
342 "Input formula needs to be either a probability operator formula or a reward operator formula.");
346 boost::optional<std::string> rewardModel = boost::none;
347 if (formula->isRewardOperatorFormula()) {
348 if (formula->asRewardOperatorFormula().hasRewardModelName()) {
349 rewardModel = std::string(formula->asRewardOperatorFormula().getRewardModelName());
351 rewardModel = std::string(
"");
354 vars.insert(rewardParameter);
362 for (
auto const& pair : keyValue) {
363 auto variable = carl::VariablePool::getInstance().findVariableWithName(pair.first);
365 instantiation.emplace(variable, value);
371 std::shared_ptr<storm::logic::Formula> formulaWithoutBound;
373 formulaWithoutBound = std::make_shared<storm::logic::ProbabilityOperatorFormula>(
377 formulaWithoutBound = std::make_shared<storm::logic::RewardOperatorFormula>(formulas[0]->asRewardOperatorFormula().getSubformula().asSharedPointer());
383 for (
auto const& parameter : vars) {
384 std::cout <<
"Derivative w.r.t. " << parameter <<
": ";
387 std::cout << *result <<
'\n';
std::unique_ptr< modelchecker::ExplicitQuantitativeCheckResult< ConstantType > > check(Environment const &env, storm::utility::parametric::Valuation< FunctionType > const &valuation, typename utility::parametric::VariableType< FunctionType >::type const ¶meter, boost::optional< std::vector< ConstantType > > const &valueVector=boost::none)
check calculates the deriative of the model w.r.t.
void specifyFormula(Environment const &env, modelchecker::CheckTask< logic::Formula, FunctionType > const &checkTask)
specifyFormula specifies a CheckTask.
bool isRewardModelSet() const
Retrieves whether a reward model was set.
Class to efficiently check a formula on a parametric model with different parameter instantiations.
virtual std::unique_ptr< CheckResult > check(Environment const &env, storm::utility::parametric::Valuation< typename SparseModelType::ValueType > const &valuation) override
void specifyFormula(CheckTask< storm::logic::Formula, typename SparseModelType::ValueType > const &checkTask)
Base class for all sparse models.
A class that provides convenience operations to display run times.
void restart()
Reset the stopwatch and immediately start it.
void stop()
Stop stopwatch and add measured time to total time.
#define STORM_LOG_THROW(cond, exception, message)
storm::modelchecker::CheckTask< storm::logic::Formula, ValueType > createTask(std::shared_ptr< const storm::logic::Formula > const &formula, bool onlyInitialStatesRelevant=false)
std::vector< std::shared_ptr< storm::logic::Formula const > > extractFormulasFromProperties(std::vector< storm::jani::Property > const &properties)
void printModelCheckingProperty(storm::jani::Property const &property)
std::set< storm::RationalFunctionVariable > getRewardParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rewards.
std::set< storm::RationalFunctionVariable > getProbabilityParameters(Model< storm::RationalFunction > const &model)
Get all probability parameters occurring on transitions.
void verifyPropertiesAtSamplePoints(ModelType const &model, cli::SymbolicInput const &input, SampleInformation< ValueType > const &samples)
SampleInformation< ValueType > parseSamples(std::shared_ptr< storm::models::ModelBase > const &model, std::string const &sampleString, bool graphPreserving)
void verifyPropertiesAtSamplePointsWithSparseEngineDerivatives(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, cli::SymbolicInput const &input, SampleInformation< ValueType > const &samples)
void sampleDerivatives(std::shared_ptr< storm::models::sparse::Model< ValueType > > model, cli::SymbolicInput const &input, std::string const &instantiationString)
void verifyPropertiesAtSamplePointsWithSparseEngine(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, cli::SymbolicInput const &input, SampleInformation< ValueType > const &samples)
void printInitialStatesResult(std::unique_ptr< storm::modelchecker::CheckResult > const &result, storm::utility::Stopwatch *watch, const storm::utility::parametric::Valuation< ValueType > *valuation)
void verifyPropertiesAtSamplePointsDerivative(ModelType const &model, cli::SymbolicInput const &input, SampleInformation< ValueType > const &samples)
std::unordered_map< std::string, std::string > parseKeyValueString(std::string const &keyValueString)
std::map< typename VariableType< FunctionType >::type, typename CoefficientType< FunctionType >::type > Valuation
TargetType convertNumber(SourceType const &number)
#define STORM_PRINT_AND_LOG(message)