You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
13 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/exceptions/InvalidArgumentException.h"
  4. #include "src/storage/dd/CuddDdManager.h"
  5. #include "src/storage/dd/CuddDd.h"
  6. #include "src/storage/dd/DdMetaVariable.h"
  7. TEST(CuddDdManager, Constants) {
  8. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  9. storm::dd::Dd<storm::dd::DdType::CUDD> zero;
  10. ASSERT_NO_THROW(zero = manager->getZero());
  11. EXPECT_EQ(0, zero.getNonZeroCount());
  12. EXPECT_EQ(1, zero.getLeafCount());
  13. EXPECT_EQ(1, zero.getNodeCount());
  14. EXPECT_EQ(0, zero.getMin());
  15. EXPECT_EQ(0, zero.getMax());
  16. storm::dd::Dd<storm::dd::DdType::CUDD> one;
  17. ASSERT_NO_THROW(one = manager->getOne());
  18. EXPECT_EQ(1, one.getNonZeroCount());
  19. EXPECT_EQ(1, one.getLeafCount());
  20. EXPECT_EQ(1, one.getNodeCount());
  21. EXPECT_EQ(1, one.getMin());
  22. EXPECT_EQ(1, one.getMax());
  23. storm::dd::Dd<storm::dd::DdType::CUDD> two;
  24. ASSERT_NO_THROW(two = manager->getConstant(2));
  25. EXPECT_EQ(1, two.getNonZeroCount());
  26. EXPECT_EQ(1, two.getLeafCount());
  27. EXPECT_EQ(1, two.getNodeCount());
  28. EXPECT_EQ(2, two.getMin());
  29. EXPECT_EQ(2, two.getMax());
  30. }
  31. TEST(CuddDdManager, AddGetMetaVariableTest) {
  32. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  33. ASSERT_NO_THROW(manager->addMetaVariable("x", 1, 9));
  34. EXPECT_EQ(1, manager->getNumberOfMetaVariables());
  35. std::vector<std::string> names = {"x", "x'"};
  36. ASSERT_THROW(manager->addMetaVariablesInterleaved(names, 0, 3), storm::exceptions::InvalidArgumentException);
  37. names = {"y", "y"};
  38. ASSERT_THROW(manager->addMetaVariablesInterleaved(names, 0, 3), storm::exceptions::InvalidArgumentException);
  39. names = {"y", "y'"};
  40. ASSERT_NO_THROW(manager->addMetaVariablesInterleaved(names, 0, 3));
  41. EXPECT_EQ(3, manager->getNumberOfMetaVariables());
  42. EXPECT_FALSE(manager->hasMetaVariable("x'"));
  43. EXPECT_TRUE(manager->hasMetaVariable("y'"));
  44. std::set<std::string> metaVariableSet = {"x", "y", "y'"};
  45. EXPECT_EQ(metaVariableSet, manager->getAllMetaVariableNames());
  46. ASSERT_THROW(storm::dd::DdMetaVariable<storm::dd::DdType::CUDD> const& metaVariableX = manager->getMetaVariable("x'"), storm::exceptions::InvalidArgumentException);
  47. ASSERT_NO_THROW(storm::dd::DdMetaVariable<storm::dd::DdType::CUDD> const& metaVariableX = manager->getMetaVariable("x"));
  48. }
  49. TEST(CuddDdManager, EncodingTest) {
  50. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  51. manager->addMetaVariable("x", 1, 9);
  52. storm::dd::Dd<storm::dd::DdType::CUDD> encoding;
  53. ASSERT_THROW(encoding = manager->getEncoding("x", 0), storm::exceptions::InvalidArgumentException);
  54. ASSERT_THROW(encoding = manager->getEncoding("x", 10), storm::exceptions::InvalidArgumentException);
  55. ASSERT_NO_THROW(encoding = manager->getEncoding("x", 4));
  56. EXPECT_EQ(1, encoding.getNonZeroCount());
  57. EXPECT_EQ(6, encoding.getNodeCount());
  58. EXPECT_EQ(2, encoding.getLeafCount());
  59. }
  60. TEST(CuddDdManager, RangeTest) {
  61. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  62. ASSERT_NO_THROW(manager->addMetaVariable("x", 1, 9));
  63. storm::dd::Dd<storm::dd::DdType::CUDD> range;
  64. ASSERT_THROW(range = manager->getRange("y"), storm::exceptions::InvalidArgumentException);
  65. ASSERT_NO_THROW(range = manager->getRange("x"));
  66. EXPECT_EQ(9, range.getNonZeroCount());
  67. EXPECT_EQ(2, range.getLeafCount());
  68. EXPECT_EQ(6, range.getNodeCount());
  69. }
  70. TEST(CuddDdManager, IdentityTest) {
  71. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  72. manager->addMetaVariable("x", 1, 9);
  73. storm::dd::Dd<storm::dd::DdType::CUDD> range;
  74. ASSERT_THROW(range = manager->getIdentity("y"), storm::exceptions::InvalidArgumentException);
  75. ASSERT_NO_THROW(range = manager->getIdentity("x"));
  76. EXPECT_EQ(9, range.getNonZeroCount());
  77. EXPECT_EQ(10, range.getLeafCount());
  78. EXPECT_EQ(21, range.getNodeCount());
  79. }
  80. TEST(CuddDdMetaVariable, AccessorTest) {
  81. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  82. manager->addMetaVariable("x", 1, 9);
  83. EXPECT_EQ(1, manager->getNumberOfMetaVariables());
  84. ASSERT_NO_THROW(storm::dd::DdMetaVariable<storm::dd::DdType::CUDD> const& metaVariableX = manager->getMetaVariable("x"));
  85. storm::dd::DdMetaVariable<storm::dd::DdType::CUDD> const& metaVariableX = manager->getMetaVariable("x");
  86. EXPECT_EQ(1, metaVariableX.getLow());
  87. EXPECT_EQ(9, metaVariableX.getHigh());
  88. EXPECT_EQ("x", metaVariableX.getName());
  89. EXPECT_EQ(manager, metaVariableX.getDdManager());
  90. EXPECT_EQ(4, metaVariableX.getNumberOfDdVariables());
  91. }
  92. TEST(CuddDd, OperatorTest) {
  93. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  94. manager->addMetaVariable("x", 1, 9);
  95. EXPECT_TRUE(manager->getZero() == manager->getZero());
  96. EXPECT_FALSE(manager->getZero() == manager->getOne());
  97. EXPECT_FALSE(manager->getZero() != manager->getZero());
  98. EXPECT_TRUE(manager->getZero() != manager->getOne());
  99. storm::dd::Dd<storm::dd::DdType::CUDD> dd1 = manager->getOne();
  100. storm::dd::Dd<storm::dd::DdType::CUDD> dd2 = manager->getOne();
  101. storm::dd::Dd<storm::dd::DdType::CUDD> dd3 = dd1 + dd2;
  102. EXPECT_TRUE(dd3 == manager->getConstant(2));
  103. dd3 += manager->getZero();
  104. EXPECT_TRUE(dd3 == manager->getConstant(2));
  105. dd3 = dd1 && manager->getConstant(3);
  106. EXPECT_TRUE(dd1 == manager->getOne());
  107. dd3 = dd1 * manager->getConstant(3);
  108. EXPECT_TRUE(dd3 == manager->getConstant(3));
  109. dd3 *= manager->getConstant(2);
  110. EXPECT_TRUE(dd3 == manager->getConstant(6));
  111. dd3 = dd1 - dd2;
  112. EXPECT_TRUE(dd3 == manager->getZero());
  113. dd3 -= manager->getConstant(-2);
  114. EXPECT_TRUE(dd3 == manager->getConstant(2));
  115. dd3 /= manager->getConstant(2);
  116. EXPECT_TRUE(dd3 == manager->getOne());
  117. dd3.complement();
  118. EXPECT_TRUE(dd3 == manager->getZero());
  119. dd1 = !dd3;
  120. EXPECT_TRUE(dd1 == manager->getOne());
  121. dd3 = dd1 || dd2;
  122. EXPECT_TRUE(dd3 == manager->getOne());
  123. dd1 = manager->getIdentity("x");
  124. dd2 = manager->getConstant(5);
  125. dd3 = dd1.equals(dd2);
  126. EXPECT_EQ(1, dd3.getNonZeroCount());
  127. storm::dd::Dd<storm::dd::DdType::CUDD> dd4 = dd1.notEquals(dd2);
  128. EXPECT_TRUE(dd4 == !dd3);
  129. dd3 = dd1.less(dd2);
  130. EXPECT_EQ(11, dd3.getNonZeroCount());
  131. dd3 = dd1.lessOrEqual(dd2);
  132. EXPECT_EQ(12, dd3.getNonZeroCount());
  133. dd3 = dd1.greater(dd2);
  134. EXPECT_EQ(4, dd3.getNonZeroCount());
  135. dd3 = dd1.greaterOrEqual(dd2);
  136. EXPECT_EQ(5, dd3.getNonZeroCount());
  137. dd3 = (manager->getEncoding("x", 2)).ite(dd2, dd1);
  138. dd4 = dd3.less(dd2);
  139. EXPECT_EQ(10, dd4.getNonZeroCount());
  140. dd4 = dd3.minimum(dd1);
  141. dd4 *= manager->getEncoding("x", 2);
  142. dd4.sumAbstract({"x"});
  143. EXPECT_EQ(2, dd4.getValue());
  144. dd4 = dd3.maximum(dd1);
  145. dd4 *= manager->getEncoding("x", 2);
  146. dd4.sumAbstract({"x"});
  147. EXPECT_EQ(5, dd4.getValue());
  148. dd1 = manager->getConstant(0.01);
  149. dd2 = manager->getConstant(0.01 + 1e-6);
  150. EXPECT_TRUE(dd1.equalModuloPrecision(dd2, 1e-6, false));
  151. EXPECT_FALSE(dd1.equalModuloPrecision(dd2, 1e-6));
  152. }
  153. TEST(CuddDd, AbstractionTest) {
  154. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  155. manager->addMetaVariablesInterleaved({"x", "x'"}, 1, 9);
  156. storm::dd::Dd<storm::dd::DdType::CUDD> dd1;
  157. storm::dd::Dd<storm::dd::DdType::CUDD> dd2;
  158. storm::dd::Dd<storm::dd::DdType::CUDD> dd3;
  159. dd1 = manager->getIdentity("x");
  160. dd2 = manager->getConstant(5);
  161. dd3 = dd1.equals(dd2);
  162. EXPECT_EQ(1, dd3.getNonZeroCount());
  163. ASSERT_THROW(dd3.existsAbstract({"x'"}), storm::exceptions::InvalidArgumentException);
  164. ASSERT_NO_THROW(dd3.existsAbstract({"x"}));
  165. EXPECT_EQ(1, dd3.getNonZeroCount());
  166. EXPECT_EQ(1, dd3.getMax());
  167. dd3 = dd1.equals(dd2);
  168. dd3 *= manager->getConstant(3);
  169. EXPECT_EQ(1, dd3.getNonZeroCount());
  170. ASSERT_THROW(dd3.existsAbstract({"x'"}), storm::exceptions::InvalidArgumentException);
  171. ASSERT_NO_THROW(dd3.existsAbstract({"x"}));
  172. EXPECT_TRUE(dd3 == manager->getZero());
  173. dd3 = dd1.equals(dd2);
  174. dd3 *= manager->getConstant(3);
  175. ASSERT_THROW(dd3.sumAbstract({"x'"}), storm::exceptions::InvalidArgumentException);
  176. ASSERT_NO_THROW(dd3.sumAbstract({"x"}));
  177. EXPECT_EQ(1, dd3.getNonZeroCount());
  178. EXPECT_EQ(3, dd3.getMax());
  179. dd3 = dd1.equals(dd2);
  180. dd3 *= manager->getConstant(3);
  181. ASSERT_THROW(dd3.minAbstract({"x'"}), storm::exceptions::InvalidArgumentException);
  182. ASSERT_NO_THROW(dd3.minAbstract({"x"}));
  183. EXPECT_EQ(0, dd3.getNonZeroCount());
  184. EXPECT_EQ(0, dd3.getMax());
  185. dd3 = dd1.equals(dd2);
  186. dd3 *= manager->getConstant(3);
  187. ASSERT_THROW(dd3.maxAbstract({"x'"}), storm::exceptions::InvalidArgumentException);
  188. ASSERT_NO_THROW(dd3.maxAbstract({"x"}));
  189. EXPECT_EQ(1, dd3.getNonZeroCount());
  190. EXPECT_EQ(3, dd3.getMax());
  191. }
  192. TEST(CuddDd, SwapTest) {
  193. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  194. manager->addMetaVariablesInterleaved({"x", "x'"}, 1, 9);
  195. manager->addMetaVariable("z", 2, 8);
  196. storm::dd::Dd<storm::dd::DdType::CUDD> dd1;
  197. storm::dd::Dd<storm::dd::DdType::CUDD> dd2;
  198. dd1 = manager->getIdentity("x");
  199. ASSERT_THROW(dd1.swapVariables({std::make_pair("x", "z")}), storm::exceptions::InvalidArgumentException);
  200. ASSERT_NO_THROW(dd1.swapVariables({std::make_pair("x", "x'")}));
  201. EXPECT_TRUE(dd1 == manager->getIdentity("x'"));
  202. }
  203. TEST(CuddDd, MultiplyMatrixTest) {
  204. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  205. manager->addMetaVariablesInterleaved({"x", "x'"}, 1, 9);
  206. storm::dd::Dd<storm::dd::DdType::CUDD> dd1 = manager->getIdentity("x").equals(manager->getIdentity("x'"));
  207. storm::dd::Dd<storm::dd::DdType::CUDD> dd2 = manager->getRange("x'");
  208. storm::dd::Dd<storm::dd::DdType::CUDD> dd3;
  209. dd1 *= manager->getConstant(2);
  210. ASSERT_NO_THROW(dd3 = dd1.multiplyMatrix(dd2, {"x'"}));
  211. ASSERT_NO_THROW(dd3.swapVariables({std::make_pair("x", "x'")}));
  212. EXPECT_TRUE(dd3 == dd2 * manager->getConstant(2));
  213. }
  214. TEST(CuddDd, GetSetValueTest) {
  215. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  216. manager->addMetaVariable("x", 1, 9);
  217. storm::dd::Dd<storm::dd::DdType::CUDD> dd1 = manager->getOne();
  218. ASSERT_NO_THROW(dd1.setValue("x", 4, 2));
  219. EXPECT_EQ(2, dd1.getLeafCount());
  220. std::map<std::string, int_fast64_t> metaVariableToValueMap;
  221. metaVariableToValueMap.emplace("x", 1);
  222. EXPECT_EQ(1, dd1.getValue(metaVariableToValueMap));
  223. metaVariableToValueMap.clear();
  224. metaVariableToValueMap.emplace("x", 4);
  225. EXPECT_EQ(2, dd1.getValue(metaVariableToValueMap));
  226. }
  227. TEST(CuddDd, ForwardIteratorTest) {
  228. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  229. manager->addMetaVariable("x", 1, 9);
  230. manager->addMetaVariable("y", 0, 3);
  231. storm::dd::Dd<storm::dd::DdType::CUDD> dd;
  232. ASSERT_NO_THROW(dd = manager->getRange("x"));
  233. storm::dd::DdForwardIterator<storm::dd::DdType::CUDD> it, ite;
  234. ASSERT_NO_THROW(it = dd.begin());
  235. ASSERT_NO_THROW(ite = dd.end());
  236. std::pair<storm::expressions::SimpleValuation, double> valuationValuePair;
  237. uint_fast64_t numberOfValuations = 0;
  238. while (it != ite) {
  239. ASSERT_NO_THROW(valuationValuePair = *it);
  240. ASSERT_NO_THROW(++it);
  241. ++numberOfValuations;
  242. }
  243. EXPECT_EQ(9, numberOfValuations);
  244. dd = manager->getRange("x");
  245. dd = dd.ite(manager->getOne(), manager->getOne());
  246. ASSERT_NO_THROW(it = dd.begin());
  247. ASSERT_NO_THROW(ite = dd.end());
  248. numberOfValuations = 0;
  249. while (it != ite) {
  250. ASSERT_NO_THROW(valuationValuePair = *it);
  251. ASSERT_NO_THROW(++it);
  252. ++numberOfValuations;
  253. }
  254. EXPECT_EQ(16, numberOfValuations);
  255. ASSERT_NO_THROW(it = dd.begin(false));
  256. ASSERT_NO_THROW(ite = dd.end());
  257. numberOfValuations = 0;
  258. while (it != ite) {
  259. ASSERT_NO_THROW(valuationValuePair = *it);
  260. ASSERT_NO_THROW(++it);
  261. ++numberOfValuations;
  262. }
  263. EXPECT_EQ(1, numberOfValuations);
  264. }