Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Extremum.h
Go to the documentation of this file.
1#pragma once
2
3#include <limits>
4#include <optional>
5#include <type_traits>
6
10
11namespace storm::utility {
12
16template<storm::OptimizationDirection Dir, typename ValueType>
17class Extremum {
18 public:
19 Extremum() = default;
20 Extremum(ValueType const& value);
21 Extremum(ValueType&& value);
22 Extremum(Extremum const&) = default;
23 Extremum(Extremum&&) = default;
24 Extremum& operator=(Extremum const&) = default;
26 ~Extremum() = default;
27
32 Extremum& operator=(ValueType const& value);
33
38 Extremum& operator=(ValueType&& value);
39
44 bool better(ValueType const& value) const;
45
50 template<typename ExtendedType>
51 requires(std::is_same_v<ExtendedType, storm::utility::ExtendedValueType<ValueType>> &&
52 !std::is_same_v<storm::utility::ExtendedValueType<ValueType>, ValueType>)
53 bool better(ExtendedType const& value) const {
54 if constexpr (storm::solver::minimize(Dir)) {
55 return value < extremalValue;
56 } else {
57 static_assert(storm::solver::maximize(Dir));
58 return value > extremalValue;
59 }
60 }
61
67 bool operator&=(Extremum const& other);
68
74 bool operator&=(Extremum&& other);
75
81 bool operator&=(ValueType const& value);
82
88 bool operator&=(ValueType&& value);
89
93 bool empty() const;
94
99 ValueType const& operator*() const;
100
105 ValueType& operator*();
106
110 std::optional<ValueType> getOptionalValue() const;
111
116
121 template<typename ExtendedType>
122 requires(std::is_same_v<ExtendedType, storm::utility::ExtendedValueType<ValueType>> &&
123 !std::is_same_v<storm::utility::ExtendedValueType<ValueType>, ValueType>)
124 bool operator&=(ExtendedType const& value) {
125 if (better(value)) {
126 extremalValue = value;
127 return true;
128 }
129 return false;
130 }
131
135 void reset();
136
137 private:
139 static bool const StoresPlainValues = std::is_same_v<storm::utility::ExtendedValueType<ValueType>, ValueType>;
140 static_assert(!StoresPlainValues || std::numeric_limits<ValueType>::has_infinity, "NumberTraits claims an infinity that numeric_limits cannot provide.");
141
144 if constexpr (StoresPlainValues) {
145 // Taken from numeric_limits rather than from storm::utility so that it stays a compile time constant.
146 if constexpr (storm::solver::minimize(Dir)) {
147 return std::numeric_limits<ValueType>::infinity();
148 } else {
149 static_assert(storm::solver::maximize(Dir));
150 return -std::numeric_limits<ValueType>::infinity();
151 }
152 } else {
153 if constexpr (storm::solver::minimize(Dir)) {
155 } else {
156 static_assert(storm::solver::maximize(Dir));
157 return storm::utility::ExtendedValueType<ValueType>::negativeInfinity();
158 }
159 }
160 }
161
162 storm::utility::ExtendedValueType<ValueType> extremalValue{baseValue()};
163};
164
165template<typename ValueType>
167template<typename ValueType>
169
170} // namespace storm::utility
Stores and manages an extremal (maximal or minimal) value.
Definition Extremum.h:17
bool operator&=(Extremum const &other)
Updates the stored value, if the given extremal value is better.
Definition Extremum.cpp:43
ValueType & operator*()
Definition Extremum.cpp:103
bool operator&=(ValueType const &value)
Updates the stored value, if the given value is better.
Definition Extremum.cpp:61
bool operator&=(ValueType &&value)
Updates the stored value, if the given value is better.
Definition Extremum.cpp:70
ValueType const & operator*() const
Definition Extremum.cpp:93
std::optional< ValueType > getOptionalValue() const
Definition Extremum.cpp:113
bool better(ValueType const &value) const
Definition Extremum.cpp:33
bool operator&=(Extremum &&other)
Updates the stored value, if the given extremal value is better.
Definition Extremum.cpp:52
Extremum(Extremum &&)=default
Extremum(ValueType &&value)
Definition Extremum.cpp:16
Extremum & operator=(ValueType const &value)
Sets the extremum to the given value.
Definition Extremum.cpp:21
storm::utility::ExtendedValueType< ValueType > const & getExtendedValue() const
Definition Extremum.cpp:121
Extremum(Extremum const &)=default
Extremum & operator=(ValueType &&value)
Sets the extremum to the given value.
Definition Extremum.cpp:27
Extremum(ValueType const &value)
Definition Extremum.cpp:11
void reset()
Forgets the extremal value so that this represents the extremum over an empty set.
Definition Extremum.cpp:126
Extremum & operator=(Extremum &&)=default
bool better(ExtendedType const &value) const
Definition Extremum.h:53
Extremum & operator=(Extremum const &)=default
bool constexpr maximize(OptimizationDirection d)
bool constexpr minimize(OptimizationDirection d)
Extremum< storm::OptimizationDirection::Maximize, ValueType > Maximum
Definition Extremum.h:166
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
Definition Extremum.h:168
std::conditional_t< storm::NumberTraits< ValueType >::HasInfinity, ValueType, ExtendedNumber< ValueType > > ExtendedValueType
The type to use for values that may be infinite.