Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExportCheckResultTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <filesystem>
5#include <fstream>
6#include <random>
7#include <sstream>
8
13#include "storm/api/builder.h"
14#include "storm/api/export.h"
26
27namespace {
28
29// From state 1 the target is never reached, so the expected reward is infinite there and in state 0, which reaches
30// state 1 with probability 1/2. States 2 and 3 have a finite expected reward of 1 and 0, respectively.
31//
32// Every state is initial: a program that pins the initial state down instead has its initial states enumerated by an
33// SMT solver, which this test would then need a Storm built with one to run at all. Which states are initial makes no
34// difference to what is checked below, since the check result and its export cover all of them either way.
35std::string const modelDescription = R"(
36dtmc
37
38module main
39 s : [0..3];
40 [] s=0 -> 1/2 : (s'=1) + 1/2 : (s'=2);
41 [] s=1 -> 1 : (s'=1);
42 [] s=2 -> 1 : (s'=3);
43 [] s=3 -> 1 : (s'=3);
44endmodule
45
46init
47 true
48endinit
49
50label "target" = s=3;
51
52rewards "steps"
53 s<3 : 1;
54endrewards
55)";
56
57std::filesystem::path getTemporaryFilePath() {
58 std::error_code ec;
59 auto tmpDir = std::filesystem::temp_directory_path(ec);
60 EXPECT_EQ(0, ec.value()) << "Unable to get temporary directory for check result export test: " << ec.message();
61 std::random_device rd;
62 std::filesystem::path result;
63 do {
64 result = tmpDir / std::filesystem::path("storm_check_result_export_test_" + std::to_string(rd()) + ".json");
65 } while (std::filesystem::exists(result));
66 return result;
67}
68
74template<typename ValueType>
75void runExportTest(storm::Environment const& env) {
76 storm::prism::Program program = storm::parser::PrismParser::parseFromString(modelDescription, "testfile");
77 auto formulas = storm::api::extractFormulasFromProperties(storm::api::parsePropertiesForPrismProgram("R{\"steps\"}=? [F \"target\"]", program));
78 ASSERT_EQ(1ull, formulas.size());
79 auto model = storm::api::buildSparseModel<ValueType>(program, formulas)->template as<storm::models::sparse::Dtmc<ValueType>>();
80 ASSERT_EQ(4ull, model->getNumberOfStates());
81
83 std::unique_ptr<storm::modelchecker::CheckResult> result = checker.check(env, *formulas[0]);
84 ASSERT_TRUE(result->isExplicitQuantitativeCheckResult());
85 auto const& values = result->template asExplicitQuantitativeCheckResult<ValueType>().getValueVector();
86 ASSERT_EQ(4ull, values.size());
87
88 // Two states cannot reach the target, so their expected reward is infinite. Without those, the export below would
89 // not exercise anything.
90 uint64_t numberOfInfiniteValues = 0;
91 for (auto const& value : values) {
92 if (storm::utility::isInfinity(value)) {
93 ++numberOfInfiniteValues;
94 }
95 }
96 ASSERT_EQ(2ull, numberOfInfiniteValues);
97
98 auto const path = getTemporaryFilePath();
99 ASSERT_NO_THROW(storm::api::exportCheckResultToJson<ValueType>(model, result, path.string()));
100 std::ifstream exportedStream(path);
101 ASSERT_TRUE(exportedStream.good());
102 storm::json<double> exported;
103 ASSERT_NO_THROW(exportedStream >> exported);
104 exportedStream.close();
105 std::filesystem::remove(path);
106
107 ASSERT_TRUE(exported.is_array());
108 ASSERT_EQ(values.size(), exported.size());
109 for (auto const& entry : exported) {
110 ASSERT_TRUE(entry.count("s") == 1 && entry.count("v") == 1);
111 uint64_t const state = entry.at("s").template get<uint64_t>();
112 ASSERT_LT(state, values.size());
113 if (storm::utility::isInfinity(values[state])) {
114 // JSON has no infinity of its own, so an infinite value is exported as this string.
115 ASSERT_TRUE(entry.at("v").is_string()) << "State " << state << " has an infinite value, but was exported as " << entry.at("v").dump() << ".";
116 EXPECT_EQ("inf", entry.at("v").template get<std::string>());
117 } else if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
118 ASSERT_TRUE(entry.at("v").is_string());
119 EXPECT_EQ(storm::utility::getFinite(values[state]), storm::utility::convertNumber<ValueType>(entry.at("v").template get<std::string>()));
120 } else {
121 ASSERT_TRUE(entry.at("v").is_number()) << "State " << state << " has the finite value " << values[state] << ", but was exported as "
122 << entry.at("v").dump() << ".";
123 EXPECT_EQ(storm::utility::convertNumber<double>(storm::utility::getFinite(values[state])), entry.at("v").template get<double>());
124 }
125 }
126}
127} // namespace
128
133template<typename ValueType>
135 typedef storm::utility::ExtendedValueType<ValueType> ExtendedValueType;
139
140 storm::json<ValueType> exported;
141 ASSERT_NO_THROW(exported = result.toJson());
142 ASSERT_TRUE(exported.is_array());
143 ASSERT_EQ(3ull, exported.size());
144
145 if constexpr (std::is_same_v<ValueType, storm::RationalNumber>) {
146 ASSERT_TRUE(exported[0].at("v").is_string());
147 EXPECT_EQ("1", exported[0].at("v").template get<std::string>());
148 } else {
149 EXPECT_TRUE(exported[0].at("v").is_number());
150 }
151 // JSON has no infinity of its own, and the two of them must not be exported as the same thing.
152 ASSERT_TRUE(exported[1].at("v").is_string()) << "The negative infinity was exported as " << exported[1].at("v").dump() << ".";
153 EXPECT_EQ("-inf", exported[1].at("v").template get<std::string>());
154 ASSERT_TRUE(exported[2].at("v").is_string()) << "The positive infinity was exported as " << exported[2].at("v").dump() << ".";
155 EXPECT_EQ("inf", exported[2].at("v").template get<std::string>());
156}
157
158TEST(ExportCheckResultTest, NegativeInfinityDouble) {
160}
161
162TEST(ExportCheckResultTest, NegativeInfinityExact) {
164}
165
166TEST(ExportCheckResultTest, ExactValuesAndBoundsRoundTrip) {
167 std::string const tenTo400 = "1" + std::string(400, '0');
168 std::vector<std::string> const expectedValues{"1/3", "-1/3", "9007199254740993", tenTo400, "1/" + tenTo400, "0", "1"};
169 std::vector<storm::RationalNumber> values;
170 for (auto const& value : expectedValues) {
172 }
174 result.setValuesExact();
175
176 auto exported = storm::json<double>::parse(storm::dumpJson(result.toJson()));
177 ASSERT_EQ(values.size(), exported.size());
178 for (std::size_t i = 0; i < values.size(); ++i) {
179 EXPECT_EQ(i, exported[i].at("s").get<uint64_t>());
180 for (auto const* key : {"v", "lb", "ub"}) {
181 ASSERT_TRUE(exported[i].at(key).is_string());
182 EXPECT_EQ(expectedValues[i], exported[i].at(key).get<std::string>());
183 }
184 }
185}
186
192TEST(ExportCheckResultTest, PrintsTheApproximationOfAnExactValue) {
194 storm::RationalNumber const third = storm::RationalNumber(1) / storm::RationalNumber(3);
195 storm::modelchecker::ExplicitQuantitativeCheckResult<storm::RationalNumber> result(std::vector<ExtendedValueType>{ExtendedValueType(third)});
196
197 std::stringstream out;
198 result.writeToStream(out);
199 EXPECT_NE(std::string::npos, out.str().find("1/3")) << "The exact value is missing from " << out.str() << ".";
200 EXPECT_NE(std::string::npos, out.str().find("(approx.")) << "The approximation is missing from " << out.str() << ".";
201}
202
206TEST(ExportCheckResultTest, PrintsNoApproximationOfAnInexactValue) {
207 storm::modelchecker::ExplicitQuantitativeCheckResult<double> result(std::vector<double>{0.25});
208
209 std::stringstream out;
210 result.writeToStream(out);
211 EXPECT_EQ(std::string::npos, out.str().find("(approx.")) << "An approximation was printed for " << out.str() << ".";
212}
213
217TEST(ExportCheckResultTest, RangeOutputShowsBounds) {
218 std::vector<double> values(10, 0.5);
219 values.front() = 0.25;
221 std::vector<double> lower(10, 0.4);
222 lower.front() = 0.2;
223 result.setLowerBounds(storm::utility::widen(std::move(lower)));
224
225 std::stringstream out;
226 result.writeToStream(out);
227 EXPECT_EQ("[0.25, 0.5] (range). Solution bounds: [0.2, ?].", out.str());
228}
229
233TEST(ExportCheckResultTest, ExactSolutionIsAnnouncedInsteadOfItsBounds) {
234 storm::modelchecker::ExplicitQuantitativeCheckResult<double> result(std::vector<double>{0.25});
235 result.setValuesExact();
236
237 std::stringstream out;
238 result.writeToStream(out);
239 EXPECT_EQ("0.25. Exact solution.", out.str());
240}
241
242TEST(ExportCheckResultTest, RangeOutputAnnouncesAnExactSolution) {
243 std::vector<double> values(10, 0.5);
244 values.front() = 0.25;
246 result.setValuesExact();
247
248 std::stringstream out;
249 result.writeToStream(out);
250 EXPECT_EQ("[0.25, 0.5] (range). Exact solution.", out.str());
251}
252
253TEST(ExportCheckResultTest, InfiniteRewardDouble) {
255 runExportTest<double>(env);
256}
257
258TEST(ExportCheckResultTest, InfiniteRewardExact) {
260 env.solver().setLinearEquationSolverType(storm::solver::EquationSolverType::Eigen);
261 env.solver().eigen().setMethod(storm::solver::EigenLinearEquationSolverMethod::SparseLU);
262 runExportTest<storm::RationalNumber>(env);
263}
TEST(ExportCheckResultTest, NegativeInfinityDouble)
void runNegativeInfinityExportTest()
No model checker hands out a negative infinity today, so this builds the check result directly.
void setMethod(storm::solver::EigenLinearEquationSolverMethod value, bool isSetFromDefault=false)
SolverEnvironment & solver()
void setLinearEquationSolverType(storm::solver::EquationSolverType const &value, bool isSetFromDefault=false)
EigenSolverEnvironment & eigen()
A quantitative check result over the states of a sparse model.
virtual std::ostream & writeToStream(std::ostream &out) const override
void setLowerBounds(vector_type lowerBounds)
Sets sound bounds on the actual values.
storm::json< ValueType > toJson(std::optional< storm::storage::sparse::Valuations > const &stateValuations=std::nullopt, std::optional< storm::models::sparse::StateLabeling > const &stateLabels=std::nullopt) const
Finite rational values and bounds are JSON strings such as "1/3" and "1"; finite floating-point value...
void setValuesExact()
States that the values of this result are the exact ones, i.e.
static storm::prism::Program parseFromString(std::string const &input, std::string const &filename, bool prismCompatability=false)
Parses the given input stream into the PRISM storage classes assuming it complies with the PRISM synt...
std::vector< storm::jani::Property > parsePropertiesForPrismProgram(std::string const &inputString, storm::prism::Program const &program, boost::optional< std::set< std::string > > const &propertyFilter)
std::vector< std::shared_ptr< storm::logic::Formula const > > extractFormulasFromProperties(std::vector< storm::jani::Property > const &properties)
void exportCheckResultToJson(std::shared_ptr< storm::models::sparse::Model< ValueType > > const &model, std::unique_ptr< storm::modelchecker::CheckResult > const &checkResult, std::string const &filename)
Definition export.h:129
std::shared_ptr< storm::models::sparse::Model< ValueType > > buildSparseModel(storm::languages::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:118
ExtendedValueType< ValueType > positiveInfinity()
std::vector< ExtendedValueType< ValueType > > widen(std::vector< ValueType > &&values)
Widens a vector of finite values into the extended value type.
ExtendedValueType< ValueType > negativeInfinity()
std::conditional_t< storm::NumberTraits< ValueType >::HasInfinity, ValueType, ExtendedNumber< ValueType > > ExtendedValueType
The type to use for values that may be infinite.
ValueType one()
Definition constants.cpp:21
bool isInfinity(ValueType const &a)
ValueType const & getFinite(ExtendedNumber< ValueType > const &value)
TargetType convertNumber(SourceType const &number)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11
std::string dumpJson(storm::json< ValueType > const &j, bool compact)
Dumps the given json object, producing a String.