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