Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
CompressedStateTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <memory>
5#include <random>
6#include <vector>
7
12
13namespace {
14
15// Creates variables with a random layout: Variables are placed consecutively (so many of them cross bucket boundaries)
16storm::generator::VariableInformation createRandomLayout(storm::expressions::ExpressionManager& manager, std::mt19937_64& rng, uint64_t targetNumberOfBits) {
18 uint64_t offset = 0;
19 uint64_t index = 0;
20 while (offset < targetNumberOfBits) {
21 auto const name = "v" + std::to_string(index++);
22 switch (rng() % 3) {
23 case 0: {
24 info.booleanVariables.emplace_back(manager.declareBooleanVariable(name), offset, false, true);
25 offset += 1;
26 break;
27 }
28 case 1: {
29 // Width 0 is possible for locations with a single value.
30 uint64_t const width = rng() % 9;
31 info.locationVariables.emplace_back(manager.declareIntegerVariable(name), (uint64_t(1) << width) - 1, offset, width, true);
32 offset += width;
33 break;
34 }
35 default: {
36 // Widths up to 52 bits are exactly representable by the (double based) evaluator
37 uint64_t const width = 1 + rng() % 52;
38 int64_t const lowerBound = static_cast<int64_t>(rng() % 20) - 10;
39 info.integerVariables.emplace_back(manager.declareIntegerVariable(name), lowerBound,
40 lowerBound + static_cast<int64_t>((uint64_t(1) << width) - 1), offset, width);
41 offset += width;
42 break;
43 }
44 }
45 }
46 info.totalBitOffset = offset;
47 return info;
48}
49
50storm::generator::CompressedState randomState(std::mt19937_64& rng, uint64_t numberOfBits) {
51 storm::generator::CompressedState state(numberOfBits);
52 for (uint64_t i = 0; i < numberOfBits; ++i) {
53 state.set(i, rng() % 2 == 0);
54 }
55 return state;
56}
57
58// Makes a state that agrees with the given one except for a few random bits
59storm::generator::CompressedState perturbState(storm::generator::CompressedState state, std::mt19937_64& rng, uint64_t numberOfFlips) {
60 for (uint64_t i = 0; i < numberOfFlips; ++i) {
61 uint64_t const bit = rng() % state.size();
62 state.set(bit, !state.get(bit));
63 }
64 return state;
65}
66
67void expectSameValues(storm::generator::VariableInformation const& info, storm::expressions::ExpressionEvaluator<double> const& expected,
69 for (auto const& v : info.locationVariables) {
70 EXPECT_EQ(expected.asInt(v.variable.getExpression()), actual.asInt(v.variable.getExpression())) << v.variable.getName();
71 }
72 for (auto const& v : info.booleanVariables) {
73 EXPECT_EQ(expected.asBool(v.variable.getExpression()), actual.asBool(v.variable.getExpression())) << v.variable.getName();
74 }
75 for (auto const& v : info.integerVariables) {
76 EXPECT_EQ(expected.asInt(v.variable.getExpression()), actual.asInt(v.variable.getExpression())) << v.variable.getName();
77 }
78}
79
80} // namespace
81
82TEST(CompressedStateTest, UnpackStateDifferenceIntoEvaluator) {
83 std::mt19937_64 rng(1234);
84 // The bit sizes cover a single bucket, a few buckets and more than 16 buckets (which requires heap memory).
85 for (uint64_t targetBits : {10ul, 64ul, 100ul, 300ul, 1200ul}) {
86 for (int layoutIteration = 0; layoutIteration < 20; ++layoutIteration) {
87 auto manager = std::make_shared<storm::expressions::ExpressionManager>();
88 auto const info = createRandomLayout(*manager, rng, targetBits);
89 // The evaluators must be created after declaring all variables
92
93 auto current = randomState(rng, info.totalBitOffset);
95 for (int step = 0; step < 30; ++step) {
97 switch (step % 4) {
98 case 0:
99 next = current; // no difference at all
100 break;
101 case 1:
102 next = perturbState(current, rng, 1);
103 break;
104 case 2:
105 next = perturbState(current, rng, 5);
106 break;
107 default:
108 next = randomState(rng, info.totalBitOffset);
109 break;
110 }
111 storm::generator::unpackStateDifferenceIntoEvaluator<double>(next, current, info, incremental);
113 expectSameValues(info, fullyUnpacked, incremental);
114 if (::testing::Test::HasFailure()) {
115 return;
116 }
117 current = next;
118 }
119 }
120 }
121}
TEST(CompressedStateTest, UnpackStateDifferenceIntoEvaluator)
This class is responsible for managing a set of typed variables and all expressions using these varia...
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
size_t size() const
Retrieves the number of bits this bit vector can store.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
template void unpackStateIntoEvaluator< double >(CompressedState const &state, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< double > &evaluator)
template void unpackStateDifferenceIntoEvaluator< double >(CompressedState const &newState, CompressedState const &oldState, VariableInformation const &variableInformation, storm::expressions::ExpressionEvaluator< double > &evaluator)
storm::storage::BitVector CompressedState
SettingsManager const & manager()
Retrieves the settings manager.
uint_fast64_t totalBitOffset
The total bit offset over all variables.
std::vector< IntegerVariableInformation > integerVariables
The integer variables.
std::vector< LocationVariableInformation > locationVariables
The location variables.
std::vector< BooleanVariableInformation > booleanVariables
The boolean variables.