Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SylvanDdTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
4#include <carl/util/stringparser.h>
5
13
14class Sylvan {
15 public:
16 static void checkLibraryAvailable() {
17#ifndef STORM_HAVE_SYLVAN
18 GTEST_SKIP() << "Library Sylvan not available.";
19#endif
20 }
21
23};
24
25template<typename TestType>
26class SylvanDd : public ::testing::Test {
27 public:
28 void SetUp() override {
29 TestType::checkLibraryAvailable();
30 }
31
32 static const storm::dd::DdType DdType = TestType::DdType;
33};
34
35typedef ::testing::Types<Sylvan> TestingTypes;
37
38TYPED_TEST(SylvanDd, AddSharpenTest) {
39 const storm::dd::DdType DdType = TestFixture::DdType;
40 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
41 manager->execute([&]() {
42 std::pair<storm::expressions::Variable, storm::expressions::Variable> x = manager->addMetaVariable("x", 1, 9);
43
44 storm::dd::Add<DdType, double> dd = manager->template getAddOne<double>();
45 ASSERT_NO_THROW(dd.setValue(x.first, 4, 1.89999999));
46 ASSERT_EQ(2ul, dd.getLeafCount());
47
49
50 std::map<storm::expressions::Variable, int_fast64_t> metaVariableToValueMap;
51 metaVariableToValueMap.emplace(x.first, 4);
52
53 sharpened = dd.sharpenKwekMehlhorn(1);
54 ASSERT_EQ(storm::utility::convertNumber<storm::RationalNumber>(std::string("9/5")), sharpened.getValue(metaVariableToValueMap));
55
56 sharpened = dd.sharpenKwekMehlhorn(2);
57 ASSERT_EQ(storm::utility::convertNumber<storm::RationalNumber>(std::string("19/10")), sharpened.getValue(metaVariableToValueMap));
58 });
59}
60
61TYPED_TEST(SylvanDd, AddRationalSharpenTest) {
62 const storm::dd::DdType DdType = TestFixture::DdType;
63 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
64 manager->execute([&]() {
65 std::pair<storm::expressions::Variable, storm::expressions::Variable> x = manager->addMetaVariable("x", 1, 9);
66
67 storm::dd::Add<DdType, storm::RationalNumber> dd = manager->template getAddOne<storm::RationalNumber>();
68 ASSERT_NO_THROW(dd.setValue(x.first, 4, storm::utility::convertNumber<storm::RationalNumber>(1.89999999)));
69 ASSERT_EQ(2ul, dd.getLeafCount());
70
72
73 std::map<storm::expressions::Variable, int_fast64_t> metaVariableToValueMap;
74 metaVariableToValueMap.emplace(x.first, 4);
75
76 sharpened = dd.sharpenKwekMehlhorn(1);
77 ASSERT_EQ(storm::utility::convertNumber<storm::RationalNumber>(std::string("9/5")), sharpened.getValue(metaVariableToValueMap));
78
79 sharpened = dd.sharpenKwekMehlhorn(2);
80 ASSERT_EQ(storm::utility::convertNumber<storm::RationalNumber>(std::string("19/10")), sharpened.getValue(metaVariableToValueMap));
81 });
82}
83
84TYPED_TEST(SylvanDd, AddToRationalTest) {
85 const storm::dd::DdType DdType = TestFixture::DdType;
86 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
87 manager->execute([&]() {
88 std::pair<storm::expressions::Variable, storm::expressions::Variable> x = manager->addMetaVariable("x", 1, 9);
89
90 storm::dd::Add<DdType, double> dd = manager->template getAddOne<double>();
91 ASSERT_NO_THROW(dd.setValue(x.first, 4, 0.4));
92 ASSERT_EQ(2ul, dd.getLeafCount());
93
94 storm::dd::Add<DdType, storm::RationalNumber> rationalDd = dd.template toValueType<storm::RationalNumber>();
95
96 std::map<storm::expressions::Variable, int_fast64_t> metaVariableToValueMap;
97 metaVariableToValueMap.emplace(x.first, 4);
98
99 ASSERT_EQ(storm::utility::convertNumber<storm::RationalNumber>(std::string("3602879701896397/9007199254740992")),
100 rationalDd.getValue(metaVariableToValueMap));
101 });
102}
103
104TYPED_TEST(SylvanDd, RationalFunctionConstants) {
105 const storm::dd::DdType DdType = TestFixture::DdType;
106 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
107 manager->execute([&]() {
109 ASSERT_NO_THROW(zero = manager->template getAddZero<storm::RationalFunction>());
110
111 EXPECT_EQ(0ul, zero.getNonZeroCount());
112 EXPECT_EQ(1ul, zero.getLeafCount());
113 EXPECT_EQ(1ul, zero.getNodeCount());
114
116 ASSERT_NO_THROW(one = manager->template getAddOne<storm::RationalFunction>());
117
118 EXPECT_EQ(0ul, one.getNonZeroCount());
119 EXPECT_EQ(1ul, one.getLeafCount());
120 EXPECT_EQ(1ul, one.getNodeCount());
121
123 storm::RationalFunction constantTwo(2);
124
125 ASSERT_NO_THROW(two = manager->template getConstant<storm::RationalFunction>(constantTwo));
126
127 EXPECT_EQ(0ul, two.getNonZeroCount());
128 EXPECT_EQ(1ul, two.getLeafCount());
129 EXPECT_EQ(1ul, two.getNodeCount());
130
132 std::shared_ptr<storm::RawPolynomialCache> cache = std::make_shared<storm::RawPolynomialCache>();
133 carl::StringParser parser;
134 parser.setVariables({"x", "y", "z"});
135
137 storm::RationalFunction(storm::Polynomial(parser.template parseMultivariatePolynomial<storm::RationalFunctionCoefficient>("2*x+x*y"), cache));
139 storm::RationalFunction(storm::Polynomial(parser.template parseMultivariatePolynomial<storm::RationalFunctionCoefficient>("z"), cache),
140 storm::Polynomial(parser.template parseMultivariatePolynomial<storm::RationalFunctionCoefficient>("2*y"), cache));
141
142 storm::RationalFunction rationalFunction = storm::RationalFunction(partA + partB);
143
144 ASSERT_NO_THROW(function = manager->template getConstant<storm::RationalFunction>(rationalFunction));
145
146 EXPECT_EQ(0ul, function.getNonZeroCount());
147 EXPECT_EQ(1ul, function.getLeafCount());
148 EXPECT_EQ(1ul, function.getNodeCount());
149 });
150}
151
152TYPED_TEST(SylvanDd, RationalFunctionToDouble) {
153 const storm::dd::DdType DdType = TestFixture::DdType;
154 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
155
156 manager->execute([&]() {
157 std::pair<storm::expressions::Variable, storm::expressions::Variable> xExpr;
158 std::pair<storm::expressions::Variable, storm::expressions::Variable> yExpr;
159 std::pair<storm::expressions::Variable, storm::expressions::Variable> zExpr;
160 ASSERT_NO_THROW(xExpr = manager->addMetaVariable("x", 0, 1));
161 ASSERT_NO_THROW(yExpr = manager->addMetaVariable("y", 0, 1));
162 ASSERT_NO_THROW(zExpr = manager->addMetaVariable("z", 0, 1));
163
164 storm::dd::Bdd<DdType> bddX0 = manager->getEncoding(xExpr.first, 0);
165 storm::dd::Bdd<DdType> bddX1 = manager->getEncoding(xExpr.first, 1);
166 storm::dd::Bdd<DdType> bddY0 = manager->getEncoding(yExpr.first, 0);
167 storm::dd::Bdd<DdType> bddY1 = manager->getEncoding(yExpr.first, 1);
168 storm::dd::Bdd<DdType> bddZ0 = manager->getEncoding(zExpr.first, 0);
169 storm::dd::Bdd<DdType> bddZ1 = manager->getEncoding(zExpr.first, 1);
170
172 ((bddX0 && (bddY0 && bddZ0)).template toAdd<storm::RationalFunction>() *
173 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(-1)))) +
174 ((bddX0 && (bddY0 && bddZ1)).template toAdd<storm::RationalFunction>() *
175 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(0)))) +
176 ((bddX0 && (bddY1 && bddZ0)).template toAdd<storm::RationalFunction>() *
177 manager->template getConstant<storm::RationalFunction>(
178 storm::RationalFunction(storm::RationalFunctionCoefficient(1) / storm::RationalFunctionCoefficient(2)))) +
179 ((bddX0 && (bddY1 && bddZ1)).template toAdd<storm::RationalFunction>() *
180 manager->template getConstant<storm::RationalFunction>(
181 storm::RationalFunction(storm::RationalFunctionCoefficient(1) / storm::RationalFunctionCoefficient(3)))) +
182 ((bddX1 && (bddY0 && bddZ0)).template toAdd<storm::RationalFunction>() *
183 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(100000)))) +
184 ((bddX1 && (bddY0 && bddZ1)).template toAdd<storm::RationalFunction>() *
185 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(3)))) +
186 ((bddX1 && (bddY1 && bddZ0)).template toAdd<storm::RationalFunction>() *
187 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(4)))) +
188 ((bddX1 && (bddY1 && bddZ1)).template toAdd<storm::RationalFunction>() *
189 manager->template getConstant<storm::RationalFunction>(storm::RationalFunction(storm::RationalFunctionCoefficient(0))));
190 EXPECT_EQ(6ul, complexAdd.getNonZeroCount());
191 EXPECT_EQ(7ul, complexAdd.getLeafCount());
192 EXPECT_EQ(14ul, complexAdd.getNodeCount());
193
194 storm::dd::Add<DdType, double> doubleAdd = complexAdd.template toValueType<double>();
195
196 EXPECT_EQ(6ul, doubleAdd.getNonZeroCount());
197 EXPECT_EQ(7ul, doubleAdd.getLeafCount());
198 EXPECT_EQ(14ul, doubleAdd.getNodeCount());
199
200 storm::dd::Add<DdType, double> comparisonAdd =
201 ((bddX0 && (bddY0 && bddZ0)).template toAdd<double>() * manager->template getConstant<double>(-1.0)) +
202 ((bddX0 && (bddY0 && bddZ1)).template toAdd<double>() * manager->template getConstant<double>(0.0)) +
203 ((bddX0 && (bddY1 && bddZ0)).template toAdd<double>() * manager->template getConstant<double>(0.5)) +
204 ((bddX0 && (bddY1 && bddZ1)).template toAdd<double>() * manager->template getConstant<double>(0.33333333333333333333)) +
205 ((bddX1 && (bddY0 && bddZ0)).template toAdd<double>() * manager->template getConstant<double>(100000.0)) +
206 ((bddX1 && (bddY0 && bddZ1)).template toAdd<double>() * manager->template getConstant<double>(3.0)) +
207 ((bddX1 && (bddY1 && bddZ0)).template toAdd<double>() * manager->template getConstant<double>(4.0)) +
208 ((bddX1 && (bddY1 && bddZ1)).template toAdd<double>() * manager->template getConstant<double>(0.0));
209
210 EXPECT_TRUE(comparisonAdd == doubleAdd);
211 });
212}
213
214TYPED_TEST(SylvanDd, RationalFunctionEncodingTest) {
215 const storm::dd::DdType DdType = TestFixture::DdType;
216 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
217 manager->execute([&]() {
218 std::pair<storm::expressions::Variable, storm::expressions::Variable> x = manager->addMetaVariable("x", 1, 9);
219
220 storm::dd::Bdd<DdType> encoding;
221 STORM_SILENT_ASSERT_THROW(encoding = manager->getEncoding(x.first, 0), storm::exceptions::InvalidArgumentException);
222 STORM_SILENT_ASSERT_THROW(encoding = manager->getEncoding(x.first, 10), storm::exceptions::InvalidArgumentException);
223 ASSERT_NO_THROW(encoding = manager->getEncoding(x.first, 4));
224 EXPECT_EQ(1ul, encoding.getNonZeroCount());
225
226 // As a BDD, this DD has one only leaf, because there does not exist a 0-leaf, and (consequently) one node less
227 // than the MTBDD.
228 EXPECT_EQ(5ul, encoding.getNodeCount());
229 EXPECT_EQ(1ul, encoding.getLeafCount());
230
232 ASSERT_NO_THROW(add = encoding.template toAdd<storm::RationalFunction>());
233
234 // As an MTBDD, the 0-leaf is there, so the count is actually 2 and the node count is 6.
235 EXPECT_EQ(6ul, add.getNodeCount());
236 EXPECT_EQ(2ul, add.getLeafCount());
237 });
238}
239
240TYPED_TEST(SylvanDd, RationalFunctionIdentityTest) {
241 const storm::dd::DdType DdType = TestFixture::DdType;
242 std::shared_ptr<storm::dd::DdManager<DdType>> manager(new storm::dd::DdManager<DdType>());
243 manager->execute([&]() {
244 std::pair<storm::expressions::Variable, storm::expressions::Variable> x = manager->addMetaVariable("x", 1, 9);
245
247 ASSERT_NO_THROW(identity = manager->template getIdentity<storm::RationalFunction>(x.first));
248
249 EXPECT_EQ(9ul, identity.getNonZeroCount());
250 EXPECT_EQ(10ul, identity.getLeafCount());
251 EXPECT_EQ(21ul, identity.getNodeCount());
252 });
253}
TYPED_TEST_SUITE(SylvanDd, TestingTypes,)
TYPED_TEST(SylvanDd, AddSharpenTest)
::testing::Types< Sylvan > TestingTypes
static const storm::dd::DdType DdType
void SetUp() override
static const storm::dd::DdType DdType
Definition GraphTest.cpp:42
static void checkLibraryAvailable()
Add< LibraryType, storm::RationalNumber > sharpenKwekMehlhorn(uint64_t precision) const
Retrieves the function that sharpens all values in the current ADD with the Kwek-Mehlhorn algorithm.
Definition Add.cpp:144
ValueType getValue(std::map< storm::expressions::Variable, int_fast64_t > const &metaVariableToValueMap=std::map< storm::expressions::Variable, int_fast64_t >()) const
Retrieves the value of the function when all meta variables are assigned the values of the given mapp...
Definition Add.cpp:501
virtual uint_fast64_t getNonZeroCount() const override
Retrieves the number of encodings that are mapped to a non-zero value.
Definition Add.cpp:444
virtual uint_fast64_t getNodeCount() const override
Retrieves the number of nodes necessary to represent the DD.
Definition Add.cpp:458
virtual uint_fast64_t getLeafCount() const override
Retrieves the number of leaves of the ADD.
Definition Add.cpp:453
void setValue(storm::expressions::Variable const &metaVariable, int_fast64_t variableValue, ValueType const &targetValue)
Sets the function values of all encodings that have the given value of the meta variable to the given...
Definition Add.cpp:473
virtual uint_fast64_t getNonZeroCount() const override
Retrieves the number of encodings that are mapped to a non-zero value.
Definition Bdd.cpp:507
virtual uint_fast64_t getNodeCount() const override
Retrieves the number of nodes necessary to represent the DD.
Definition Bdd.cpp:521
virtual uint_fast64_t getLeafCount() const override
Retrieves the number of leaves of the DD.
Definition Bdd.cpp:516
carl::RationalFunction< Polynomial, true > RationalFunction
::testing::Types< Cudd, Sylvan > TestingTypes
Definition GraphTest.cpp:59
#define STORM_SILENT_ASSERT_THROW(statement, expected_exception)
Definition storm_gtest.h:26