Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Extremum.cpp
Go to the documentation of this file.
2
5
6namespace storm::utility {
7
8template<storm::OptimizationDirection Dir, typename ValueType>
9Extremum<Dir, ValueType>::Extremum(ValueType const& value) : data({value}) {
10 if constexpr (!SupportsInfinity) {
11 data.empty = false;
12 }
13}
14
15template<storm::OptimizationDirection Dir, typename ValueType>
16Extremum<Dir, ValueType>::Extremum(ValueType&& value) : data({std::move(value)}) {
17 if constexpr (!SupportsInfinity) {
18 data.empty = false;
19 }
20}
21
22template<storm::OptimizationDirection Dir, typename ValueType>
24 data.value = value;
25 if constexpr (!SupportsInfinity) {
26 data.empty = false;
27 }
28 return *this;
29}
31template<storm::OptimizationDirection Dir, typename ValueType>
33 data.value = std::move(value);
34 if constexpr (!SupportsInfinity) {
35 data.empty = false;
36 }
37 return *this;
38}
39
40template<storm::OptimizationDirection Dir, typename ValueType>
41bool Extremum<Dir, ValueType>::better(ValueType const& value) const {
42 if constexpr (!SupportsInfinity) {
43 if (data.empty) {
44 return true;
45 }
46 }
47 if constexpr (storm::solver::minimize(Dir)) {
48 return value < data.value;
49 } else {
50 static_assert(storm::solver::maximize(Dir));
51 return value > data.value;
52 }
53}
54
55template<storm::OptimizationDirection Dir, typename ValueType>
57 if (other.empty()) {
58 return false;
59 }
60 return *this &= *other;
61}
62
63template<storm::OptimizationDirection Dir, typename ValueType>
65 if (other.empty()) {
66 return false;
67 }
68 return *this &= std::move(*other);
69}
71template<storm::OptimizationDirection Dir, typename ValueType>
72bool Extremum<Dir, ValueType>::operator&=(ValueType const& value) {
73 if (better(value)) {
74 data.value = value;
75 if constexpr (!SupportsInfinity) {
76 data.empty = false;
77 }
78 return true;
79 }
80 return false;
82
83template<storm::OptimizationDirection Dir, typename ValueType>
84bool Extremum<Dir, ValueType>::operator&=(ValueType&& value) {
85 if (better(value)) {
86 data.value = std::move(value);
87 if constexpr (!SupportsInfinity) {
88 data.empty = false;
89 }
90 return true;
91 }
92 return false;
93}
94
95template<storm::OptimizationDirection Dir, typename ValueType>
97 if constexpr (SupportsInfinity) {
98 return data.value == data.baseValue();
99 } else {
100 return data.empty;
101 }
102}
103
104template<storm::OptimizationDirection Dir, typename ValueType>
105ValueType const& Extremum<Dir, ValueType>::operator*() const {
106 STORM_LOG_ASSERT(!empty(), "tried to get empty extremum.");
107 return data.value;
108}
109
110template<storm::OptimizationDirection Dir, typename ValueType>
112 STORM_LOG_ASSERT(!empty(), "tried to get empty extremum.");
113 return data.value;
114}
115
116template<storm::OptimizationDirection Dir, typename ValueType>
117std::optional<ValueType> Extremum<Dir, ValueType>::getOptionalValue() const {
118 if (empty()) {
119 return {};
120 } else {
121 return data.value;
122 }
123}
124
125template<storm::OptimizationDirection Dir, typename ValueType>
127 if constexpr (SupportsInfinity) {
128 data.value = data.baseValue();
129 } else {
130 data.empty = true;
131 }
132}
133
138
139} // namespace storm::utility
Stores and manages an extremal (maximal or minimal) value.
Definition Extremum.h:15
bool operator&=(Extremum const &other)
Updates the stored value, if the given extremal value is better.
Definition Extremum.cpp:56
ValueType const & operator*() const
Definition Extremum.cpp:105
std::optional< ValueType > getOptionalValue() const
Definition Extremum.cpp:117
bool better(ValueType const &value) const
Definition Extremum.cpp:41
void reset()
Forgets the extremal value so that this represents the extremum over an empty set.
Definition Extremum.cpp:126
Extremum & operator=(Extremum const &)=default
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
bool constexpr maximize(OptimizationDirection d)
bool constexpr minimize(OptimizationDirection d)