Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ExpressionEvalutionTest.cpp
Go to the documentation of this file.
8#include "test/storm_gtest.h"
9
10TEST(ExpressionEvaluation, NaiveEvaluation) {
11 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
12
16 ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
17 ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
18 ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
19
21
22 storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
23
24 eval.setRationalValue(z, 5.5);
25 eval.setBooleanValue(x, true);
26 for (int_fast64_t i = 0; i < 1000; ++i) {
27 eval.setIntegerValue(y, 3 + i);
28 EXPECT_NEAR(8.5 + i, iteExpression.evaluateAsDouble(&eval), 1e-6);
29 }
30
31 eval.setBooleanValue(x, false);
32 for (int_fast64_t i = 0; i < 1000; ++i) {
33 double zValue = i / static_cast<double>(10);
34 eval.setRationalValue(z, zValue);
35 EXPECT_NEAR(3 * zValue, iteExpression.evaluateAsDouble(&eval), 1e-6);
36 }
37}
38
39TEST(ExpressionEvaluation, ExprTkEvaluation) {
40 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
41
45 ASSERT_NO_THROW(x = manager->declareBooleanVariable("x"));
46 ASSERT_NO_THROW(y = manager->declareIntegerVariable("y"));
47 ASSERT_NO_THROW(z = manager->declareRationalVariable("z"));
48
49 storm::expressions::Expression iteExpression = storm::expressions::ite(x, y + z, manager->integer(3) * z);
51
52 eval.setRationalValue(z, 5.5);
53 eval.setBooleanValue(x, true);
54 for (int_fast64_t i = 0; i < 1000; ++i) {
55 eval.setIntegerValue(y, 3 + i);
56 EXPECT_NEAR(8.5 + i, eval.asRational(iteExpression), 1e-6);
57 }
58
59 eval.setBooleanValue(x, false);
60 for (int_fast64_t i = 0; i < 1000; ++i) {
61 double zValue = i / static_cast<double>(10);
62 eval.setRationalValue(z, zValue);
63 EXPECT_NEAR(3 * zValue, eval.asRational(iteExpression), 1e-6);
64 }
65}
66
67TEST(ExpressionEvaluation, NegativeModulo) {
68 std::shared_ptr<storm::expressions::ExpressionManager> manager(new storm::expressions::ExpressionManager());
69
70 storm::parser::ExpressionCreator creator(*manager);
71 std::unordered_map<std::string, storm::expressions::Expression> mapping;
72 creator.setIdentifierMapping(mapping);
73
74 storm::expressions::Expression n = manager->integer(-1);
75 storm::expressions::Expression mod = manager->integer(4);
76
77 bool pass = true;
79
80 auto positiveModulo = [](int a, int b) { return a >= 0 ? a % b : (a % b) + b; };
81
82 int expectedInt = positiveModulo(-1, 4);
83 double expectedDouble(expectedInt);
84
86 int result1 = evaluator.asInt(expr);
87 int result2 = expr.evaluateAsInt();
88 double result3 = evaluator.asRational(expr);
89 double result4 = expr.evaluateAsDouble();
90
91 EXPECT_EQ(result1, expectedInt);
92 EXPECT_EQ(result2, expectedInt);
93 EXPECT_NEAR(result3, expectedDouble, 1e-6);
94 EXPECT_NEAR(result4, expectedDouble, 1e-6);
95}
TEST(ExpressionEvaluation, NaiveEvaluation)
int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
double evaluateAsDouble(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
This class is responsible for managing a set of typed variables and all expressions using these varia...
void setIntegerValue(storm::expressions::Variable const &variable, int_fast64_t value) override
void setBooleanValue(storm::expressions::Variable const &variable, bool value) override
int_fast64_t asInt(Expression const &expression) const override
void setRationalValue(storm::expressions::Variable const &variable, double value) override
double asRational(Expression const &expression) const override
A simple implementation of the valuation interface.
virtual void setIntegerValue(Variable const &integerVariable, int_fast64_t value) override
Sets the value of the given integer variable to the provided value.
virtual void setBooleanValue(Variable const &booleanVariable, bool value) override
Sets the value of the given boolean variable to the provided value.
virtual void setRationalValue(Variable const &rationalVariable, double value) override
Sets the value of the given boolean variable to the provided value.
void setIdentifierMapping(qi::symbols< char, storm::expressions::Expression > const *identifiers_)
Sets an identifier mapping that is used to determine valid variables in the expression.
storm::expressions::Expression createPowerModuloLogarithmExpression(storm::expressions::Expression const &e1, storm::expressions::OperatorType const &operatorType, storm::expressions::Expression const &e2, bool &pass) const
Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)