Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BisimulationTestHelper.h
Go to the documentation of this file.
1#pragma once
2
3#include "storm-config.h"
4#include "test/storm_gtest.h"
5
6#include <map>
7#include <memory>
8#include <optional>
9#include <string>
10#include <vector>
11
16#include "storm/api/builder.h"
31
36
39
41 Options options;
42 // Tests use exact bisimulation unless they deliberately opt into a positive tolerance.
44 return options;
45}
46
48 Options options;
50 // Tests use exact bisimulation unless they deliberately opt into a positive tolerance.
52 return options;
53}
54
59template<typename ModelType, typename ValueType = typename ModelType::ValueType>
60std::shared_ptr<ModelType> buildModel(storm::storage::SparseMatrix<ValueType> matrix, std::map<std::string, std::vector<uint64_t>> const& labels,
61 std::vector<ValueType> const& stateRewards = {}, std::vector<ValueType> const& stateActionRewards = {}) {
62 uint64_t const numStates = matrix.getColumnCount();
63 storm::models::sparse::StateLabeling labeling(numStates);
64 labeling.addLabel("init");
65 labeling.addLabelToState("init", 0);
66 for (auto const& [label, states] : labels) {
67 labeling.addLabel(label);
68 for (uint64_t const state : states) {
69 labeling.addLabelToState(label, state);
70 }
71 }
72 storm::storage::sparse::ModelComponents<ValueType> components(std::move(matrix), std::move(labeling));
73 if constexpr (std::is_same_v<ModelType, storm::models::sparse::Ctmc<ValueType>>) {
74 components.rateTransitions = true;
75 }
76 if (!stateRewards.empty() || !stateActionRewards.empty()) {
77 components.rewardModels.emplace("rew", storm::models::sparse::StandardRewardModel<ValueType>(
78 stateRewards.empty() ? std::nullopt : std::optional<std::vector<ValueType>>(stateRewards),
79 stateActionRewards.empty() ? std::nullopt : std::optional<std::vector<ValueType>>(stateActionRewards)));
80 }
81 return std::make_shared<ModelType>(std::move(components));
82}
83
88template<typename ValueType = double>
89std::shared_ptr<storm::models::sparse::MarkovAutomaton<ValueType>> buildMarkovAutomaton(storm::storage::SparseMatrix<ValueType> matrix,
90 storm::storage::BitVector markovianStates,
91 std::map<std::string, std::vector<uint64_t>> const& labels) {
92 uint64_t const numStates = matrix.getColumnCount();
93 storm::models::sparse::StateLabeling labeling(numStates);
94 labeling.addLabel("init");
95 labeling.addLabelToState("init", 0);
96 for (auto const& [label, states] : labels) {
97 labeling.addLabel(label);
98 for (uint64_t const state : states) {
99 labeling.addLabelToState(label, state);
100 }
101 }
102 storm::storage::sparse::ModelComponents<ValueType> components(std::move(matrix), std::move(labeling));
103 components.markovianStates = std::move(markovianStates);
104 components.rateTransitions = true; // The rows of the Markovian states hold rates, which the model turns into probabilities plus exit rates.
105 return std::make_shared<storm::models::sparse::MarkovAutomaton<ValueType>>(std::move(components));
106}
107
111template<typename ValueType>
112ValueType checkFormula(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::shared_ptr<storm::logic::Formula const> const& formula) {
114 return result->template asExplicitQuantitativeCheckResult<ValueType>()[*model->getInitialStates().begin()];
115}
116
121template<typename ValueType>
122ValueType checkFormula(std::shared_ptr<storm::models::sparse::Model<ValueType>> const& model, std::string const& formulaString) {
123 return checkFormula<ValueType>(model, storm::parser::FormulaParser().parseSingleFormulaFromString(formulaString));
124}
125
130template<typename ValueType>
132 std::shared_ptr<storm::models::sparse::Model<ValueType>> model;
133 std::vector<std::shared_ptr<storm::logic::Formula const>> formulas;
134};
135
140 bool allLabels = false; // If set, the full state space is built with all labels of the program. Otherwise, the formulas may restrict the
141 // exploration, e.g. by making the target states of a reachability formula absorbing.
142 bool choiceLabels = false; // If set, the choice labeling is built.
143 bool choiceOrigins = false; // If set, the choice origins are built.
144 bool stateValuations = false; // If set, the state valuations are built.
145};
146
147template<typename ValueType>
148PrismInput<ValueType> buildFromPrism(std::string const& prismFile, std::string const& formulaString, BuildOptions const& buildOptions = {}) {
149 // Preprocessing substitutes the constants. This is also where the declared type of a constant is taken into account, e.g. `const double x = 1/500;` only
150 // becomes 0.002 instead of the integer division 0 after preprocessing.
151 auto const program = storm::parser::PrismParser::parse(prismFile, true).preprocess();
152 PrismInput<ValueType> result;
154 auto builderOptions = buildOptions.allLabels ? storm::builder::BuilderOptions(false, true) : storm::builder::BuilderOptions(result.formulas, program);
155 if (buildOptions.choiceLabels) {
156 builderOptions.setBuildChoiceLabels();
157 }
158 if (buildOptions.choiceOrigins) {
159 builderOptions.setBuildChoiceOrigins();
160 }
161 if (buildOptions.stateValuations) {
162 builderOptions.setBuildStateValuations();
163 }
164 result.model = storm::api::buildSparseModel<ValueType>(program, builderOptions);
165 return result;
166}
167
168} // namespace storm::test::bisimulation
void addLabel(std::string const &label)
Adds a new label to the labelings.
Base class for all sparse models.
Definition Model.h:34
This class manages the labeling of the state space with a number of (atomic) labels.
void addLabelToState(std::string const &label, storm::storage::sparse::state_type state)
Adds a label to a given state.
static storm::prism::Program parse(std::string const &filename, bool prismCompatability=false)
Parses the given file into the PRISM storage classes assuming it complies with the PRISM syntax.
Program preprocess(std::map< storm::expressions::Variable, storm::expressions::Expression > const &constantDefinitions) const
Preprocesses the program by defining the given constant definitions, substituting constants and formu...
Definition Program.cpp:1170
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
A class that holds a possibly non-square matrix in the compressed row storage format.
index_type getColumnCount() const
Returns the number of columns of the matrix.
storm::modelchecker::CheckTask< storm::logic::Formula, ValueType > createTask(std::shared_ptr< const storm::logic::Formula > const &formula, bool onlyInitialStatesRelevant=false)
std::vector< storm::jani::Property > parsePropertiesForPrismProgram(std::string const &inputString, storm::prism::Program const &program, boost::optional< std::set< std::string > > const &propertyFilter)
std::shared_ptr< storm::models::sparse::Model< ValueType > > buildSparseModel(storm::storage::SymbolicModelDescription const &model, storm::builder::BuilderOptions const &options, typename storm::builder::ExplicitModelBuilder< ValueType >::Options const &explorationOptions=typename storm::builder::ExplicitModelBuilder< ValueType >::Options())
Definition builder.h:117
std::vector< std::shared_ptr< storm::logic::Formula const > > extractFormulasFromProperties(std::vector< storm::jani::Property > const &properties)
std::unique_ptr< storm::modelchecker::CheckResult > verifyWithSparseEngine(storm::Environment const &env, std::shared_ptr< storm::models::sparse::Dtmc< ValueType > > const &dtmc, storm::modelchecker::CheckTask< storm::logic::Formula, ValueType > const &task, bool preferEliminationChecker=false)
StateLabelPreservation
The way state labels will be preserved by the bisimulation.
Definition Options.h:14
Shared helpers for the bisimulation tests, cf.
storm::bisimulation::StateLabelPreservation StateLabelPreservation
ValueType checkFormula(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::shared_ptr< storm::logic::Formula const > const &formula)
storm::bisimulation::Options Options
std::shared_ptr< storm::models::sparse::MarkovAutomaton< ValueType > > buildMarkovAutomaton(storm::storage::SparseMatrix< ValueType > matrix, storm::storage::BitVector markovianStates, std::map< std::string, std::vector< uint64_t > > const &labels)
Builds a Markov automaton.
std::shared_ptr< ModelType > buildModel(storm::storage::SparseMatrix< ValueType > matrix, std::map< std::string, std::vector< uint64_t > > const &labels, std::vector< ValueType > const &stateRewards={}, std::vector< ValueType > const &stateActionRewards={})
Builds a deterministic model with the given (probability or rate) matrix.
PrismInput< ValueType > buildFromPrism(std::string const &prismFile, std::string const &formulaString, BuildOptions const &buildOptions={})
ValueType zero()
Definition constants.cpp:26
BisimulationType bisimulationType
Definition Options.h:32
std::optional< storm::RationalNumber > tolerance
Definition Options.h:35
boost::optional< storm::storage::BitVector > markovianStates
What to build from a PRISM program in addition to what the formulas require.
The model built from the given PRISM file together with the formulas that the minimization has to pre...
std::vector< std::shared_ptr< storm::logic::Formula const > > formulas
std::shared_ptr< storm::models::sparse::Model< ValueType > > model