Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Model.cpp
Go to the documentation of this file.
2
3#include <boost/algorithm/string/join.hpp>
4#include <boost/algorithm/string/split.hpp>
5
13#include "storm/io/export.h"
23
24namespace storm {
25namespace models {
26namespace sparse {
27
28template<typename ValueType, typename RewardModelType>
30 : storm::models::Model<ValueType>(modelType),
31 transitionMatrix(components.transitionMatrix),
32 stateLabeling(components.stateLabeling),
33 rewardModels(components.rewardModels),
34 choiceLabeling(components.choiceLabeling),
35 stateValuations(components.stateValuations),
36 choiceOrigins(components.choiceOrigins) {
37 assertValidityOfComponents(components);
38}
39
40template<typename ValueType, typename RewardModelType>
42 : storm::models::Model<ValueType>(modelType),
43 transitionMatrix(std::move(components.transitionMatrix)),
44 stateLabeling(std::move(components.stateLabeling)),
45 rewardModels(std::move(components.rewardModels)),
46 choiceLabeling(std::move(components.choiceLabeling)),
47 stateValuations(std::move(components.stateValuations)),
48 choiceOrigins(std::move(components.choiceOrigins)) {
49 assertValidityOfComponents(components);
50}
51
52template<typename ValueType, typename RewardModelType>
55 // More costly checks are only asserted to avoid doing them in release mode.
56 ValueType const stochasticTolerance =
57 isExact() ? storm::utility::zero<ValueType>()
58 : storm::utility::convertNumber<ValueType>(storm::settings::getModule<storm::settings::modules::GeneralSettings>().getPrecision());
59
60 uint64_t stateCount = this->getNumberOfStates();
61 uint64_t choiceCount = this->getTransitionMatrix().getRowCount();
62
63 // general components for all model types.
64 STORM_LOG_THROW(this->getTransitionMatrix().getColumnCount() == stateCount, storm::exceptions::IllegalArgumentException,
65 "Invalid column count of transition matrix.");
67 components.rateTransitions || this->hasParameters() || this->hasUncertainty() || this->getTransitionMatrix().isProbabilistic(stochasticTolerance),
68 "The matrix is not probabilistic.");
69 if (this->hasUncertainty()) {
70 STORM_LOG_ASSERT(this->getTransitionMatrix().hasOnlyPositiveEntries(), "Not all entries are (strictly) positive.");
71 }
72 STORM_LOG_THROW(this->getStateLabeling().getNumberOfItems() == stateCount, storm::exceptions::IllegalArgumentException,
73 "Invalid item count (" << this->getStateLabeling().getNumberOfItems() << ") of state labeling (states: " << stateCount << ").");
74 for (auto const& rewardModel : this->getRewardModels()) {
75 STORM_LOG_THROW(!rewardModel.second.hasStateRewards() || rewardModel.second.getStateRewardVector().size() == stateCount,
76 storm::exceptions::IllegalArgumentException,
77 "Invalid size (" << rewardModel.second.getStateRewardVector().size() << ") of state reward vector (states:" << stateCount << ").");
79 !rewardModel.second.hasStateActionRewards() || rewardModel.second.getStateActionRewardVector().size() == choiceCount,
80 storm::exceptions::IllegalArgumentException,
81 "Invalid size (" << rewardModel.second.getStateActionRewardVector().size() << ") of state action reward vector (expected:" << choiceCount << ").");
83 !rewardModel.second.hasTransitionRewards() || rewardModel.second.getTransitionRewardMatrix().isSubmatrixOf(this->getTransitionMatrix()),
84 "The transition reward matrix is not a submatrix of the transition matrix, i.e. there are rewards for transitions that do not exist.");
85 }
87 !this->hasChoiceLabeling() || this->getChoiceLabeling().getNumberOfItems() == choiceCount, storm::exceptions::IllegalArgumentException,
88 "Invalid choice count of choice labeling (choices: " << choiceCount << " vs. labeling:" << this->getChoiceLabeling().getNumberOfItems() << ").");
90 !this->hasStateValuations() || this->getStateValuations().getNumberOfStates() == stateCount, storm::exceptions::IllegalArgumentException,
91 "Invalid state count for state valuations (states: " << stateCount << " vs. valuations:" << this->getStateValuations().getNumberOfStates() << ").");
93 !this->hasChoiceOrigins() || this->getChoiceOrigins()->getNumberOfChoices() == choiceCount, storm::exceptions::IllegalArgumentException,
94 "Invalid choice count for choice origins. (choices: " << choiceCount << " vs. origins:" << this->getChoiceOrigins()->getNumberOfChoices() << ").");
95
96 // Branch on type of nondeterminism
97 if (this->isOfType(ModelType::Dtmc) || this->isOfType(ModelType::Ctmc)) {
98 STORM_LOG_THROW(this->getTransitionMatrix().hasTrivialRowGrouping(), storm::exceptions::IllegalArgumentException,
99 "Can not create deterministic model: Transition matrix has non-trivial row grouping.");
100 STORM_LOG_THROW(stateCount == this->getTransitionMatrix().getRowCount(), storm::exceptions::IllegalArgumentException,
101 "Can not create deterministic model: Number of rows of transition matrix does not match state count.");
102 STORM_LOG_THROW(stateCount == this->getTransitionMatrix().getColumnCount(), storm::exceptions::IllegalArgumentException,
103 "Can not create deterministic model: Number of columns of transition matrix does not match state count.");
104 STORM_LOG_ERROR_COND(!components.player1Matrix.is_initialized(), "Player 1 matrix given for a model that is no stochastic game (will be ignored).");
105 } else if (this->isOfType(ModelType::Mdp) || this->isOfType(ModelType::MarkovAutomaton) || this->isOfType(ModelType::Pomdp) ||
106 this->isOfType(ModelType::Smg)) {
107 STORM_LOG_THROW(stateCount == this->getTransitionMatrix().getRowGroupCount(), storm::exceptions::IllegalArgumentException,
108 "Can not create nondeterministic model: Number of row groups ("
109 << this->getTransitionMatrix().getRowGroupCount() << ") of transition matrix does not match state count (" << stateCount << ").");
110 STORM_LOG_THROW(stateCount == this->getTransitionMatrix().getColumnCount(), storm::exceptions::IllegalArgumentException,
111 "Can not create nondeterministic model: Number of columns of transition matrix does not match state count.");
112 STORM_LOG_ERROR_COND(!components.player1Matrix.is_initialized(), "Player 1 matrix given for a model that is no stochastic game (will be ignored).");
113 } else if (this->isOfType(ModelType::S2pg)) {
114 STORM_LOG_THROW(components.player1Matrix.is_initialized(), storm::exceptions::IllegalArgumentException,
115 "No player 1 matrix given for stochastic game.");
116 STORM_LOG_ASSERT(components.player1Matrix->isProbabilistic(0),
117 "Can not create stochastic game: There is a row in the p1 matrix with not exactly one entry.");
118 STORM_LOG_THROW(stateCount == components.player1Matrix->getRowGroupCount(), storm::exceptions::IllegalArgumentException,
119 "Can not create stochastic game: Number of row groups of p1 matrix does not match state count.");
120 STORM_LOG_THROW(this->getTransitionMatrix().getRowGroupCount() == components.player1Matrix->getColumnCount(),
121 storm::exceptions::IllegalArgumentException,
122 "Can not create stochastic game: Number of row groups of p2 matrix does not match column count of p1 matrix.");
123 } else {
124 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "Invalid model type.");
125 }
126
127 // Branch on continuous/discrete timing
128 if (this->isOfType(ModelType::Ctmc) || this->isOfType(ModelType::MarkovAutomaton)) {
129 STORM_LOG_THROW(components.rateTransitions || components.exitRates.is_initialized(), storm::exceptions::IllegalArgumentException,
130 "Can not create continuous time model: no rates are given.");
131 STORM_LOG_THROW(!components.exitRates.is_initialized() || components.exitRates->size() == stateCount, storm::exceptions::IllegalArgumentException,
132 "Size of exit rate vector does not match state count.");
133 STORM_LOG_THROW(this->isOfType(ModelType::Ctmc) || components.markovianStates.is_initialized(), storm::exceptions::IllegalArgumentException,
134 "Can not create Markov Automaton: no Markovian states given.");
135 } else {
136 STORM_LOG_WARN_COND(!components.rateTransitions && !components.exitRates.is_initialized(),
137 "Rates specified for discrete-time model. The rates will be ignored.");
138 }
139 STORM_LOG_WARN_COND(this->isOfType(ModelType::MarkovAutomaton) || !components.markovianStates.is_initialized(),
140 "Markovian states given for a model that is not a Markov automaton (will be ignored).");
141
142 // Treat stochastic multiplayer games
143 if (this->isOfType(ModelType::Smg)) {
144 STORM_LOG_THROW(components.statePlayerIndications.is_initialized(), storm::exceptions::IllegalArgumentException,
145 "Can not create stochastic multiplayer game: Missing player indications.");
146 // playerNameToIndexMap is optional.
148 stateCount == components.statePlayerIndications->size(), storm::exceptions::IllegalArgumentException,
149 "Size of state player indications (" << components.statePlayerIndications->size() << ") of SMG does not match state count (" << stateCount << ").");
150 } else {
151 STORM_LOG_WARN_COND(!components.statePlayerIndications.is_initialized(),
152 "statePlayerIndications given for a model that is not a stochastic multiplayer game (will be ignored).");
153 STORM_LOG_WARN_COND(!components.playerNameToIndexMap.is_initialized(),
154 "playerNameToIndexMap given for a model that is not a stochastic multiplayer game (will be ignored).");
155 }
156}
157
158template<typename ValueType, typename RewardModelType>
162
163template<typename ValueType, typename RewardModelType>
165 return this->getTransitionMatrix().getColumnCount();
166}
167
168template<typename ValueType, typename RewardModelType>
170 return this->getTransitionMatrix().getNonzeroEntryCount();
171}
173template<typename ValueType, typename RewardModelType>
175 return this->getTransitionMatrix().getRowCount();
176}
177
178template<typename ValueType, typename RewardModelType>
180 return this->getStates("init");
181}
182
183template<typename ValueType, typename RewardModelType>
185 return this->getStateLabeling().setStates("init", states);
187
188template<typename ValueType, typename RewardModelType>
190 return stateLabeling.getStates(label);
191}
192
193template<typename ValueType, typename RewardModelType>
194bool Model<ValueType, RewardModelType>::hasLabel(std::string const& label) const {
195 return stateLabeling.containsLabel(label);
196}
197
198template<typename ValueType, typename RewardModelType>
200 return transitionMatrix;
201}
202
203template<typename ValueType, typename RewardModelType>
208template<typename ValueType, typename RewardModelType>
209bool Model<ValueType, RewardModelType>::hasRewardModel(std::string const& rewardModelName) const {
210 return this->rewardModels.find(rewardModelName) != this->rewardModels.end();
211}
212
213template<typename ValueType, typename RewardModelType>
215 STORM_LOG_ASSERT(this->hasRewardModel(rewardModelName), "Model has no reward model.");
216 return this->rewardModels.find(rewardModelName)->second;
217}
219template<typename ValueType, typename RewardModelType>
220RewardModelType const& Model<ValueType, RewardModelType>::getRewardModel(std::string const& rewardModelName) const {
221 auto it = this->rewardModels.find(rewardModelName);
222 if (it == this->rewardModels.end()) {
223 if (rewardModelName.empty()) {
224 if (this->hasUniqueRewardModel()) {
225 return this->getUniqueRewardModel();
226 } else {
227 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException,
228 "Unable to refer to default reward model, because there is no default model or it is not unique.");
229 }
230 } else {
231 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "The requested reward model '" << rewardModelName << "' does not exist.");
233 }
234 return it->second;
235}
236
237template<typename ValueType, typename RewardModelType>
239 auto it = this->rewardModels.find(rewardModelName);
240 if (it == this->rewardModels.end()) {
241 if (rewardModelName.empty()) {
242 if (this->hasUniqueRewardModel()) {
243 return this->getUniqueRewardModel();
244 } else {
245 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException,
246 "Unable to refer to default reward model, because there is no default model or it is not unique.");
248 } else {
249 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "The requested reward model '" << rewardModelName << "' does not exist.");
250 }
251 }
252 return it->second;
253}
255template<typename ValueType, typename RewardModelType>
256void Model<ValueType, RewardModelType>::addRewardModel(std::string const& rewardModelName, RewardModelType const& newRewardModel) {
257 if (this->hasRewardModel(rewardModelName)) {
258 STORM_LOG_THROW(!(this->hasRewardModel(rewardModelName)), storm::exceptions::IllegalArgumentException,
259 "A reward model with the given name '" << rewardModelName << "' already exists.");
260 }
261 STORM_LOG_ASSERT(newRewardModel.isCompatible(this->getNumberOfStates(), this->getTransitionMatrix().getRowCount()), "New reward model is not compatible.");
262 this->rewardModels.emplace(rewardModelName, newRewardModel);
263}
264
265template<typename ValueType, typename RewardModelType>
266bool Model<ValueType, RewardModelType>::removeRewardModel(std::string const& rewardModelName) {
267 auto it = this->rewardModels.find(rewardModelName);
268 bool res = (it != this->rewardModels.end());
269 if (res) {
270 this->rewardModels.erase(it->first);
271 }
272 return res;
273}
274
275template<typename ValueType, typename RewardModelType>
276void Model<ValueType, RewardModelType>::restrictRewardModels(std::set<std::string> const& keptRewardModels) {
277 std::set<std::string> removedRewardModels;
278 for (auto const& rewModel : this->getRewardModels()) {
279 if (keptRewardModels.find(rewModel.first) == keptRewardModels.end()) {
280 removedRewardModels.insert(rewModel.first);
281 }
282 }
283 for (auto const& rewModelName : removedRewardModels) {
284 this->removeRewardModel(rewModelName);
285 }
286}
287
288template<typename ValueType, typename RewardModelType>
290 return this->getNumberOfRewardModels() == 1;
291}
292
293template<typename ValueType, typename RewardModelType>
295 STORM_LOG_THROW(this->getNumberOfRewardModels() == 1, storm::exceptions::IllegalFunctionCallException, "The reward model is not unique.");
296 return this->rewardModels.begin()->first;
298
299template<typename ValueType, typename RewardModelType>
301 return !this->rewardModels.empty();
302}
303
304template<typename ValueType, typename RewardModelType>
306 STORM_LOG_THROW(this->getNumberOfRewardModels() == 1, storm::exceptions::IllegalFunctionCallException, "The reward model is not unique.");
307 return this->rewardModels.cbegin()->second;
308}
309
310template<typename ValueType, typename RewardModelType>
312 STORM_LOG_THROW(this->getNumberOfRewardModels() == 1, storm::exceptions::IllegalFunctionCallException, "The reward model is not unique.");
313 return this->rewardModels.begin()->second;
314}
315
316template<typename ValueType, typename RewardModelType>
318 return this->rewardModels.size();
320template<typename ValueType, typename RewardModelType>
324
325template<typename ValueType, typename RewardModelType>
329
330template<typename ValueType, typename RewardModelType>
332 return static_cast<bool>(choiceLabeling);
333}
334
335template<typename ValueType, typename RewardModelType>
339
340template<typename ValueType, typename RewardModelType>
341std::optional<storm::models::sparse::ChoiceLabeling> const& Model<ValueType, RewardModelType>::getOptionalChoiceLabeling() const {
342 return choiceLabeling;
343}
344
345template<typename ValueType, typename RewardModelType>
346std::optional<storm::models::sparse::ChoiceLabeling>& Model<ValueType, RewardModelType>::getOptionalChoiceLabeling() {
347 return choiceLabeling;
348}
349
350template<typename ValueType, typename RewardModelType>
352 return static_cast<bool>(stateValuations);
354
355template<typename ValueType, typename RewardModelType>
359
360template<typename ValueType, typename RewardModelType>
361std::optional<storm::storage::sparse::StateValuations> const& Model<ValueType, RewardModelType>::getOptionalStateValuations() const {
362 return stateValuations;
364
365template<typename ValueType, typename RewardModelType>
366std::optional<storm::storage::sparse::StateValuations>& Model<ValueType, RewardModelType>::getOptionalStateValuations() {
367 return stateValuations;
368}
369
370template<typename ValueType, typename RewardModelType>
372 return static_cast<bool>(choiceOrigins);
374
375template<typename ValueType, typename RewardModelType>
376std::shared_ptr<storm::storage::sparse::ChoiceOrigins> const& Model<ValueType, RewardModelType>::getChoiceOrigins() const {
377 return choiceOrigins.value();
378}
379
380template<typename ValueType, typename RewardModelType>
381std::optional<std::shared_ptr<storm::storage::sparse::ChoiceOrigins>> const& Model<ValueType, RewardModelType>::getOptionalChoiceOrigins() const {
382 return choiceOrigins;
384
385template<typename ValueType, typename RewardModelType>
386std::optional<std::shared_ptr<storm::storage::sparse::ChoiceOrigins>>& Model<ValueType, RewardModelType>::getOptionalChoiceOrigins() {
387 return choiceOrigins;
388}
389
390template<typename ValueType, typename RewardModelType>
392 this->printModelInformationHeaderToStream(out);
393 this->printModelInformationFooterToStream(out);
395
396template<typename ValueType, typename RewardModelType>
398 std::size_t seed = 0;
399 boost::hash_combine(seed, transitionMatrix.hash());
400 boost::hash_combine(seed, stateLabeling.hash());
401 for (auto const& rewModel : rewardModels) {
402 boost::hash_combine(seed, rewModel.second.hash());
403 }
404 if (choiceLabeling) {
405 boost::hash_combine(seed, choiceLabeling->hash());
406 }
407 if (stateValuations) {
408 boost::hash_combine(seed, stateValuations->hash());
409 }
410 if (choiceOrigins) {
411 boost::hash_combine(seed, choiceOrigins.value()->hash());
413 return seed;
414}
415
416template<typename ValueType, typename RewardModelType>
418 out << "-------------------------------------------------------------- \n";
419 out << "Model type: \t" << this->getType() << " (sparse)\n";
420 out << "States: \t" << this->getNumberOfStates() << '\n';
421 out << "Transitions: \t" << this->getNumberOfTransitions() << '\n';
422}
423
424template<typename ValueType, typename RewardModelType>
426 this->printRewardModelsInformationToStream(out);
427 out << "State Labels: \t";
428 this->getStateLabeling().printLabelingInformationToStream(out);
429 out << "Choice Labels: \t";
430 if (this->hasChoiceLabeling()) {
431 this->getChoiceLabeling().printLabelingInformationToStream(out);
432 } else {
433 out << "none\n";
434 }
435 out << "-------------------------------------------------------------- \n";
436}
437
438template<typename ValueType, typename RewardModelType>
440 if (this->rewardModels.size()) {
441 std::vector<std::string> rewardModelNames;
442 std::for_each(this->rewardModels.cbegin(), this->rewardModels.cend(),
443 [&rewardModelNames](typename std::pair<std::string, RewardModelType> const& nameRewardModelPair) {
444 if (nameRewardModelPair.first.empty()) {
445 rewardModelNames.push_back("(default)");
446 } else {
447 rewardModelNames.push_back(nameRewardModelPair.first);
448 }
449 });
450 out << "Reward Models: " << boost::join(rewardModelNames, ", ") << '\n';
451 } else {
452 out << "Reward Models: none\n";
453 }
454}
455
456template<typename ValueType, typename RewardModelType>
457void Model<ValueType, RewardModelType>::writeDotToStream(std::ostream& outStream, size_t maxWidthLabel, bool includeLabeling,
458 storm::storage::BitVector const* subsystem, std::vector<ValueType> const* firstValue,
459 std::vector<ValueType> const* secondValue, std::vector<uint_fast64_t> const* stateColoring,
460 std::vector<std::string> const* colors, std::vector<uint_fast64_t>*, bool finalizeOutput) const {
461 outStream << "digraph model {\n";
462
463 // Write all states to the stream.
464 for (uint_fast64_t state = 0, highestStateIndex = this->getNumberOfStates() - 1; state <= highestStateIndex; ++state) {
465 if (subsystem == nullptr || subsystem->get(state)) {
466 outStream << "\t" << state;
467 if (includeLabeling || firstValue != nullptr || secondValue != nullptr || stateColoring != nullptr || hasStateValuations()) {
468 outStream << " [ ";
469
470 // If we need to print some extra information, do so now.
471 if (includeLabeling || firstValue != nullptr || secondValue != nullptr || hasStateValuations()) {
472 outStream << "label = \"" << state;
473 if (hasStateValuations()) {
474 std::string stateInfo = getStateValuations().getStateInfo(state);
475 std::vector<std::string> results;
476 boost::split(results, stateInfo, [](char c) { return c == ','; });
477 storm::io::outputFixedWidth(outStream, results, maxWidthLabel);
478 }
479 outStream << ": ";
480
481 // Now print the state labeling to the stream if requested.
482 if (includeLabeling) {
483 outStream << "{";
484 storm::io::outputFixedWidth(outStream, this->getLabelsOfState(state), maxWidthLabel);
485 outStream << "}";
486 }
487
488 outStream << this->additionalDotStateInfo(state);
489
490 // If we are to include some values for the state as well, we do so now.
491 if (firstValue != nullptr || secondValue != nullptr) {
492 outStream << " [";
493 if (firstValue != nullptr) {
494 outStream << (*firstValue)[state];
495 if (secondValue != nullptr) {
496 outStream << ", ";
497 }
498 }
499 if (secondValue != nullptr) {
500 outStream << (*secondValue)[state];
501 }
502 outStream << "]";
503 }
504 outStream << "\"";
505
506 // Now, we color the states if there were colors given.
507 if (stateColoring != nullptr && colors != nullptr) {
508 outStream << ", ";
509 outStream << " style = filled, fillcolor = " << (*colors)[(*stateColoring)[state]];
510 }
511 }
512 outStream << " ]";
513 }
514 outStream << ";\n";
515 }
516 }
517
518 // If this methods has not been called from a derived class, we want to close the digraph here.
519 if (finalizeOutput) {
520 outStream << "}\n";
521 }
522}
523
524template<typename ValueType, typename RewardModelType>
525void Model<ValueType, RewardModelType>::writeJsonToStream(std::ostream& outStream) const {
526 STORM_LOG_WARN_COND(this->getNumberOfStates() < 10000 && this->getNumberOfTransitions() < 100000,
527 "Exporting a large model to json. This might take some time and will result in a very large file.");
528 using JsonValueType = storm::RationalNumber;
530 for (uint64_t state = 0; state < getNumberOfStates(); ++state) {
531 storm::json<JsonValueType> stateChoicesJson;
532 stateChoicesJson["id"] = state;
533 if (hasStateValuations()) {
534 stateChoicesJson["s"] = getStateValuations().template toJson<JsonValueType>(state);
535 }
536 auto labels = getLabelsOfState(state);
537 stateChoicesJson["lab"] = labels;
538 storm::json<JsonValueType> stateRewardsJson;
539 for (auto const& rm : rewardModels) {
540 if (rm.second.hasStateRewards()) {
541 auto const& r = rm.second.getStateReward(state);
542 if (!storm::utility::isZero(r)) {
543 stateRewardsJson[rm.first] = storm::utility::to_string(r);
544 }
545 }
546 }
547 if (!stateRewardsJson.empty()) {
548 stateChoicesJson["rew"] = std::move(stateRewardsJson);
549 }
550
551 // For CTMCs we need to scale the transition probabilities as the transition matrix contains rates.
552 // This is not the case for MA.
553 auto rateForProbabilityScaling = storm::utility::one<ValueType>();
554 if (this->isOfType(storm::models::ModelType::Ctmc)) {
555 auto const& ctmc = this->template as<storm::models::sparse::Ctmc<ValueType, RewardModelType>>();
556 rateForProbabilityScaling = ctmc->getExitRateVector()[state];
557 stateChoicesJson["rate"] = storm::utility::to_string(rateForProbabilityScaling);
558 } else if (this->isOfType(storm::models::ModelType::MarkovAutomaton)) {
559 auto const& ma = this->template as<storm::models::sparse::MarkovAutomaton<ValueType, RewardModelType>>();
560 if (ma->isMarkovianState(state)) {
561 stateChoicesJson["rate"] = storm::utility::to_string(ma->getExitRate(state)); // Only export rate for Markovian states
562 }
563 }
564
565 storm::json<JsonValueType> choicesJson;
566 for (uint64_t choiceIndex = getTransitionMatrix().getRowGroupIndices()[state]; choiceIndex < getTransitionMatrix().getRowGroupIndices()[state + 1];
567 ++choiceIndex) {
569 if (hasChoiceOrigins() && getChoiceOrigins()->getIdentifier(choiceIndex) != getChoiceOrigins()->getIdentifierForChoicesWithNoOrigin()) {
570 choiceJson["origin"] = getChoiceOrigins()->getChoiceAsJson(choiceIndex);
571 }
572 if (hasChoiceLabeling()) {
573 auto choiceLabels = getChoiceLabeling().getLabelsOfChoice(choiceIndex);
574 if (!choiceLabels.empty()) {
575 choiceJson["lab"] = choiceLabels;
576 }
577 }
578 choiceJson["id"] = choiceIndex;
579 storm::json<JsonValueType> choiceRewardsJson;
580 for (auto const& rm : rewardModels) {
581 if (rm.second.hasStateActionRewards()) {
582 auto r = rm.second.getStateActionReward(choiceIndex);
583 if (!storm::utility::isZero(r)) {
584 choiceRewardsJson[rm.first] = storm::utility::to_string(r);
585 }
586 }
587 }
588 if (!choiceRewardsJson.empty()) {
589 choiceRewardsJson["rew"] = std::move(choiceRewardsJson);
590 }
592 for (auto const& entry : transitionMatrix.getRow(choiceIndex)) {
594 successor["id"] = entry.getColumn();
595 successor["prob"] = storm::utility::to_string<ValueType>(entry.getValue() / rateForProbabilityScaling);
596 successors.push_back(successor);
597 }
598 choiceJson["succ"] = std::move(successors);
599 choicesJson.push_back(choiceJson);
600 }
601 stateChoicesJson["c"] = std::move(choicesJson);
602 output.push_back(std::move(stateChoicesJson));
603 }
604 outStream << storm::dumpJson(output);
605}
606
607template<>
609 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Json export not implemented for this model type.");
610}
611
612template<typename ValueType, typename RewardModelType>
614 return "";
615}
616
617template<typename ValueType, typename RewardModelType>
619 return this->stateLabeling.getLabelsOfState(state);
620}
621
622template<typename ValueType, typename RewardModelType>
624 for (auto const& entry : this->getTransitionMatrix().getRowGroup(state)) {
625 if (entry.getColumn() != state) {
626 return false;
627 }
628 if (!storm::utility::isOne(entry.getValue())) {
629 return false;
630 }
631 }
632 return true;
633}
634
635template<typename ValueType, typename RewardModelType>
637 return true;
638}
639
640template<typename ValueType, typename RewardModelType>
642 return std::is_same<ValueType, storm::RationalFunction>::value;
643}
644
645template<typename ValueType, typename RewardModelType>
647 return std::is_same<ValueType, storm::Interval>::value;
648}
649
650template<typename ValueType, typename RewardModelType>
652 if (!this->supportsParameters()) {
653 return false;
654 }
655 // Check for parameters
656 for (auto const& entry : this->getTransitionMatrix()) {
657 if (!storm::utility::isConstant(entry.getValue())) {
658 return true;
659 }
660 }
661 // Only constant values present
662 return false;
663}
664
665template<typename ValueType, typename RewardModelType>
667 if (!this->supportsUncertainty()) {
668 return false;
669 }
670 // Check for intervals
671 for (auto const& entry : this->getTransitionMatrix()) {
672 if (!storm::utility::isConstant(entry.getValue())) {
673 return true;
674 }
675 }
676 // Only constant values present
677 return false;
678}
679
680template<typename ValueType, typename RewardModelType>
684
685template<typename ValueType, typename RewardModelType>
686std::unordered_map<std::string, RewardModelType>& Model<ValueType, RewardModelType>::getRewardModels() {
687 return this->rewardModels;
688}
689
690template<typename ValueType, typename RewardModelType>
691std::unordered_map<std::string, RewardModelType> const& Model<ValueType, RewardModelType>::getRewardModels() const {
692 return this->rewardModels;
693}
694
695std::set<storm::RationalFunctionVariable> getProbabilityParameters(Model<storm::RationalFunction> const& model) {
697}
698
699std::set<storm::RationalFunctionVariable> getRewardParameters(Model<storm::RationalFunction> const& model) {
700 std::set<storm::RationalFunctionVariable> result;
701 for (auto rewModel : model.getRewardModels()) {
702 std::set<storm::RationalFunctionVariable> tmp = getRewardModelParameters(rewModel.second);
703 result.insert(tmp.begin(), tmp.end());
704 }
705 return result;
706}
707
708std::set<storm::RationalFunctionVariable> getRateParameters(Model<storm::RationalFunction> const& model) {
710 auto const& ctmc = model.template as<storm::models::sparse::Ctmc<storm::RationalFunction>>();
711 return storm::utility::vector::getVariables(ctmc->getExitRateVector());
713 auto const& ma = model.template as<storm::models::sparse::MarkovAutomaton<storm::RationalFunction>>();
714 return storm::utility::vector::getVariables(ma->getExitRates());
715 } else {
716 return {};
717 }
718}
719
720std::set<storm::RationalFunctionVariable> getAllParameters(Model<storm::RationalFunction> const& model) {
721 std::set<storm::RationalFunctionVariable> parameters = getProbabilityParameters(model);
722 std::set<storm::RationalFunctionVariable> rewardParameters = getRewardParameters(model);
723 parameters.insert(rewardParameters.begin(), rewardParameters.end());
724 std::set<storm::RationalFunctionVariable> rateParameters = getRewardParameters(model);
725 parameters.insert(rateParameters.begin(), rateParameters.end());
726 return parameters;
727}
728
729template class Model<double>;
731template class Model<storm::RationalNumber>;
732template class Model<storm::Interval>;
733template class Model<storm::RationalFunction>;
734} // namespace sparse
735} // namespace models
736} // namespace storm
virtual bool isExact() const
Checks whether the model is exact.
Definition ModelBase.cpp:62
virtual bool hasParameters() const
Checks whether the model has parameters.
Definition ModelBase.cpp:58
virtual bool isSparseModel() const
Checks whether the model is a sparse model.
Definition ModelBase.cpp:11
bool isOfType(storm::models::ModelType const &modelType) const
Checks whether the model is of the given type.
Definition ModelBase.cpp:27
virtual bool supportsUncertainty() const
Does it support uncertainty (e.g., via interval-valued entries).
Definition ModelBase.cpp:23
virtual bool supportsParameters() const
Checks whether the model supports parameters.
Definition ModelBase.cpp:54
This class manages the labeling of the choice space with a number of (atomic) labels.
Base class for all sparse models.
Definition Model.h:32
storm::models::sparse::ChoiceLabeling const & getChoiceLabeling() const
Retrieves the labels for the choices of the model.
Definition Model.cpp:336
RewardModelType const & getUniqueRewardModel() const
Retrieves the unique reward model, if there exists exactly one.
Definition Model.cpp:305
bool hasRewardModel() const
Retrieves whether the model has at least one reward model.
Definition Model.cpp:300
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:199
virtual std::size_t hash() const
Definition Model.cpp:397
Model(Model< ValueType, RewardModelType > const &other)=default
void setInitialStates(storm::storage::BitVector const &states)
Overwrites the initial states of the model.
Definition Model.cpp:184
void printModelInformationFooterToStream(std::ostream &out) const
Prints the information footer (reward models, labels and size in memory) of the model to the specifie...
Definition Model.cpp:425
virtual void printModelInformationToStream(std::ostream &out) const override
Prints information about the model to the specified stream.
Definition Model.cpp:391
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:691
void restrictRewardModels(std::set< std::string > const &keptRewardModels)
Removes all reward models whose name is not in the given set.
Definition Model.cpp:276
bool hasStateValuations() const
Retrieves whether this model was build with state valuations.
Definition Model.cpp:351
storm::storage::BitVector const & getStates(std::string const &label) const
Returns the sets of states labeled with the given label.
Definition Model.cpp:189
bool removeRewardModel(std::string const &rewardModelName)
Removes the reward model with the given name from the model.
Definition Model.cpp:266
void printRewardModelsInformationToStream(std::ostream &out) const
Prints information about the reward models to the specified stream.
Definition Model.cpp:439
std::set< std::string > getLabelsOfState(storm::storage::sparse::state_type state) const
Retrieves the set of labels attached to the given state.
Definition Model.cpp:618
std::optional< std::shared_ptr< storm::storage::sparse::ChoiceOrigins > > const & getOptionalChoiceOrigins() const
Retrieves an optional value that contains the choice origins if there are some.
Definition Model.cpp:381
RewardModelType & rewardModel(std::string const &rewardModelName)
Definition Model.cpp:214
virtual uint_fast64_t getNumberOfChoices() const override
Returns the number of choices ine the model.
Definition Model.cpp:174
storm::storage::sparse::StateValuations const & getStateValuations() const
Retrieves the valuations of the states of the model.
Definition Model.cpp:356
void addRewardModel(std::string const &rewardModelName, RewardModelType const &rewModel)
Adds a reward model to the model.
Definition Model.cpp:256
std::shared_ptr< storm::storage::sparse::ChoiceOrigins > const & getChoiceOrigins() const
Retrieves the origins of the choices of the model.
Definition Model.cpp:376
storm::storage::SparseMatrix< ValueType > getBackwardTransitions() const
Retrieves the backward transition relation of the model, i.e.
Definition Model.cpp:159
bool hasChoiceLabeling() const
Retrieves whether this model has a labeling of the choices.
Definition Model.cpp:331
uint_fast64_t getNumberOfRewardModels() const
Retrieves the number of reward models associated with this model.
Definition Model.cpp:317
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:321
void printModelInformationHeaderToStream(std::ostream &out) const
Prints the information header (number of states and transitions) of the model to the specified stream...
Definition Model.cpp:417
std::optional< storm::models::sparse::ChoiceLabeling > const & getOptionalChoiceLabeling() const
Retrieves an optional value that contains the choice labeling if there is one.
Definition Model.cpp:341
CRewardModelType RewardModelType
Definition Model.h:35
virtual bool hasUniqueRewardModel() const override
Retrieves whether the model has a unique reward model.
Definition Model.cpp:289
std::optional< storm::storage::sparse::StateValuations > const & getOptionalStateValuations() const
Retrieves an optional value that contains the state valuations if there are some.
Definition Model.cpp:361
virtual uint_fast64_t getNumberOfTransitions() const override
Returns the number of (non-zero) transitions of the model.
Definition Model.cpp:169
bool hasChoiceOrigins() const
Retrieves whether this model was build with choice origins.
Definition Model.cpp:371
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:164
bool hasLabel(std::string const &label) const
Retrieves whether the given label is a valid label in this model.
Definition Model.cpp:194
RewardModelType const & getRewardModel(std::string const &rewardModelName) const
Retrieves the reward model with the given name, if one exists.
Definition Model.cpp:220
virtual std::string const & getUniqueRewardModelName() const override
Retrieves the name of the unique reward model, if there exists exactly one.
Definition Model.cpp:294
storm::storage::BitVector const & getInitialStates() const
Retrieves the initial states of the model.
Definition Model.cpp:179
This class manages the labeling of the state space with a number of (atomic) labels.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
A class that holds a possibly non-square matrix in the compressed row storage format.
const_iterator end(index_type row) const
Retrieves an iterator that points past the end of the given row.
storm::storage::SparseMatrix< value_type > transpose(bool joinGroups=false, bool keepZeros=false) const
Transposes the matrix.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:52
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void outputFixedWidth(std::ostream &stream, Container const &output, size_t maxWidth=30)
Output list of strings with linebreaks according to fixed width.
Definition export.h:60
std::set< storm::RationalFunctionVariable > getRateParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rates.
Definition Model.cpp:708
std::set< storm::RationalFunctionVariable > getRewardParameters(Model< storm::RationalFunction > const &model)
Get all parameters occurring in rewards.
Definition Model.cpp:699
std::set< storm::RationalFunctionVariable > getRewardModelParameters(StandardRewardModel< storm::RationalFunction > const &rewModel)
std::set< storm::RationalFunctionVariable > getProbabilityParameters(Model< storm::RationalFunction > const &model)
Get all probability parameters occurring on transitions.
Definition Model.cpp:695
std::set< storm::RationalFunctionVariable > getAllParameters(Model< storm::RationalFunction > const &model)
Get all parameters (probability, rewards, and rates) occurring in the model.
Definition Model.cpp:720
std::set< storm::RationalFunctionVariable > getVariables(SparseMatrix< storm::RationalFunction > const &matrix)
std::set< storm::RationalFunctionVariable > getVariables(std::vector< storm::RationalFunction > const &vector)
bool isOne(ValueType const &a)
Definition constants.cpp:34
bool isConstant(ValueType const &)
bool isZero(ValueType const &a)
Definition constants.cpp:39
std::string to_string(ValueType const &value)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:10
std::string dumpJson(storm::json< ValueType > const &j, bool compact)
Dumps the given json object, producing a String.
boost::optional< storm::storage::BitVector > markovianStates
boost::optional< std::vector< storm::storage::PlayerIndex > > statePlayerIndications
boost::optional< storm::storage::SparseMatrix< storm::storage::sparse::state_type > > player1Matrix
boost::optional< std::map< std::string, storm::storage::PlayerIndex > > playerNameToIndexMap
boost::optional< std::vector< ValueType > > exitRates