Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SolutionBoundsTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
2#include "test/storm_gtest.h"
3
7
8TEST(SolutionBoundsTest, PrecisionNeedsBothSides) {
10 bounds.lower = std::vector<double>{0.5};
11 EXPECT_FALSE(bounds.isWithinPrecision({0.5}, 1.0, false));
12 EXPECT_FALSE(bounds.isWithinPrecision({0.5}, 1.0, true));
13}
14
15TEST(SolutionBoundsTest, PrecisionRejectsMismatchingSizes) {
17 bounds.setExact(std::vector<double>{0.5});
18 STORM_SILENT_EXPECT_THROW(bounds.isWithinPrecision({0.5, 0.5}, 1.0, false), storm::exceptions::InvalidArgumentException);
19}
20
21TEST(SolutionBoundsTest, AbsolutePrecisionComparesTheDistanceToTheValue) {
23 bounds.lower = std::vector<double>{0.0, 1.0};
24 bounds.upper = std::vector<double>{0.1, 1.05};
25 // Both values sit at the midpoint, so the distance is half of the width.
26 EXPECT_TRUE(bounds.isWithinPrecision({0.05, 1.025}, 0.05, false));
27 EXPECT_FALSE(bounds.isWithinPrecision({0.05, 1.025}, 0.04, false));
28 // A value off center is only as precise as its farther side allows.
29 EXPECT_FALSE(bounds.isWithinPrecision({0.1, 1.025}, 0.05, false));
30}
31
32TEST(SolutionBoundsTest, RelativePrecisionScalesWithTheValue) {
34 bounds.lower = std::vector<double>{10.0};
35 bounds.upper = std::vector<double>{10.1};
36 EXPECT_TRUE(bounds.isWithinPrecision({10.05}, 0.01, true));
37 EXPECT_FALSE(bounds.isWithinPrecision({10.05}, 0.004, true));
38 // The same distance is far too large to be a relative statement about a value this small.
39 bounds.lower = std::vector<double>{0.001};
40 bounds.upper = std::vector<double>{0.101};
41 EXPECT_FALSE(bounds.isWithinPrecision({0.051}, 0.005, true));
42}
43
44TEST(SolutionBoundsTest, RelativePrecisionRejectsBoundsAroundZero) {
46 bounds.lower = std::vector<double>{-0.001};
47 bounds.upper = std::vector<double>{0.001};
48 // The enclosed value may be zero, which no distance but zero is within a relative precision of.
49 EXPECT_FALSE(bounds.isWithinPrecision({0.0}, 0.5, true));
50 EXPECT_TRUE(bounds.isWithinPrecision({0.0}, 0.01, false));
51 bounds.setExact(std::vector<double>{0.0});
52 EXPECT_TRUE(bounds.isWithinPrecision({0.0}, 0.5, true));
53}
54
55TEST(SolutionBoundsTest, RelativePrecisionWithExtendedRationalNumbers) {
57 auto const rational = [](int numerator, int denominator) {
58 return ExtendedRationalNumber(storm::utility::convertNumber<storm::RationalNumber>(numerator) /
60 };
62 bounds.lower = std::vector<ExtendedRationalNumber>{rational(10, 1), rational(-101, 10)};
63 bounds.upper = std::vector<ExtendedRationalNumber>{rational(101, 10), rational(-10, 1)};
64 // Negative bounds are scaled by their absolute value just like positive ones.
65 EXPECT_TRUE(bounds.isWithinPrecision({rational(201, 20), rational(-201, 20)}, rational(1, 100), true));
66 EXPECT_FALSE(bounds.isWithinPrecision({rational(201, 20), rational(-201, 20)}, rational(1, 250), true));
67 // An infinite bound is never within a finite distance of the value.
68 bounds.upper = std::vector<ExtendedRationalNumber>{ExtendedRationalNumber::infinity(), rational(-10, 1)};
69 EXPECT_FALSE(bounds.isWithinPrecision({rational(201, 20), rational(-201, 20)}, rational(1, 100), true));
70}
TEST(SolutionBoundsTest, PrecisionNeedsBothSides)
TargetType convertNumber(SourceType const &number)
storm::utility::ExtendedValueType< RationalNumber > ExtendedRationalNumber
The rational number type extended with the two infinities.
#define STORM_SILENT_EXPECT_THROW(statement, expected_exception)
Definition storm_gtest.h:21
Sound lower and upper bounds on a solution vector.
std::optional< std::vector< ValueType > > lower
std::optional< std::vector< ValueType > > upper
bool isWithinPrecision(std::vector< ValueType > const &values, ValueType const &precision, bool relative) const
Returns true if both bounds are set and neither is further from its value than the given precision al...
void setExact(std::vector< ValueType > const &values)
Sets both bounds to the given values, i.e.