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