Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SchedulerTrackingHelper.cpp
Go to the documentation of this file.
2
8
9namespace storm::solver::helper {
10
11template<typename ValueType, storm::OptimizationDirection Dir>
13 public:
14 SchedulerTrackingBackend(std::vector<uint64_t>& schedulerStorage,
15 std::vector<typename ValueIterationOperator<ValueType, false>::IndexType> const& rowGroupIndices, bool applyUpdates)
16 : schedulerStorage(schedulerStorage), applyUpdates(applyUpdates), rowGroupIndices(rowGroupIndices) {
17 // intentionally empty
18 }
20 isConverged = true;
21 }
22
23 void firstRow(ValueType&& value, uint64_t rowGroup, uint64_t row) {
24 currChoice = row - rowGroupIndices[rowGroup];
25 best = std::move(value);
26 }
27
28 void nextRow(ValueType&& value, uint64_t rowGroup, uint64_t row) {
29 if (best &= value) {
30 currChoice = row - rowGroupIndices[rowGroup];
31 } else if (*best == value) {
32 // For rows that are equally good, we prefer the previously selected one.
33 // This is necessary, e.g., for policy iteration correctness.
34 if (uint64_t rowChoice = row - rowGroupIndices[rowGroup]; rowChoice == schedulerStorage[rowGroup]) {
35 currChoice = rowChoice;
36 }
37 }
38 }
39
40 void applyUpdate(ValueType& currValue, uint64_t rowGroup) {
41 if (applyUpdates) {
42 currValue = std::move(*best);
43 }
44 auto& choice = schedulerStorage[rowGroup];
45 if (isConverged) {
46 isConverged = choice == currChoice;
47 }
48 choice = currChoice;
49 }
50
51 void endOfIteration() const {}
52
53 bool converged() const {
54 return isConverged;
55 }
56
57 bool constexpr abort() const {
58 return false;
59 }
60
61 private:
62 std::vector<uint64_t>& schedulerStorage;
63 bool const applyUpdates;
64 std::vector<typename ValueIterationOperator<ValueType, false>::IndexType> const& rowGroupIndices;
65
66 bool isConverged;
68 uint64_t currChoice;
69};
70
71template<typename ValueType, typename SolutionType, bool TrivialRowGrouping>
77
78template<typename ValueType, typename SolutionType, bool TrivialRowGrouping>
79template<storm::OptimizationDirection Dir, storm::OptimizationDirection RobustDir>
81 std::vector<ValueType> const& offsets,
82 std::vector<uint64_t>& schedulerStorage,
83 std::vector<SolutionType>* operandOut,
84 boost::optional<std::vector<uint64_t>> const& robustIndices) const {
85 bool const applyUpdates = operandOut != nullptr;
86 if constexpr (TrivialRowGrouping && std::is_same<SolutionType, double>::value) {
87 STORM_LOG_ASSERT(robustIndices, "Tracking scheduler with trivial row grouping but no robust indices => there is no scheduler.");
88 RobustSchedulerTrackingBackend<SolutionType, RobustDir, TrivialRowGrouping> backend(schedulerStorage, *robustIndices, applyUpdates);
89 if (applyUpdates) {
90 return viOperator->template applyRobust<RobustDir>(*operandOut, operandIn, offsets, backend);
91 } else {
92 return viOperator->template applyInPlaceRobust<RobustDir>(operandIn, offsets, backend);
93 }
94 } else {
95 STORM_LOG_WARN_COND(!robustIndices, "Only tracking nondeterminism, not ordering of intervals.");
96 SchedulerTrackingBackend<SolutionType, Dir> backend(schedulerStorage, viOperator->getRowGroupIndices(), applyUpdates);
97 if (applyUpdates) {
98 return viOperator->template applyRobust<RobustDir>(*operandOut, operandIn, offsets, backend);
99 } else {
100 return viOperator->template applyInPlaceRobust<RobustDir>(operandIn, offsets, backend);
101 }
102 }
103}
104
105template<typename ValueType, typename SolutionType, bool TrivialRowGrouping>
107 std::vector<ValueType> const& offsets,
109 std::vector<uint64_t>& schedulerStorage, bool robust,
110 std::vector<SolutionType>* operandOut,
111 boost::optional<std::vector<uint64_t>> const& robustIndices) const {
112 // TODO this currently assumes antagonistic intervals <-> !TrivialRowGrouping
113 if (maximize(dir)) {
114 if (robust && !TrivialRowGrouping) {
115 return computeScheduler<storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Minimize>(operandIn, offsets, schedulerStorage,
116 operandOut, robustIndices);
117 } else {
118 return computeScheduler<storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Maximize>(operandIn, offsets, schedulerStorage,
119 operandOut, robustIndices);
120 }
121 } else {
122 if (robust && !TrivialRowGrouping) {
123 return computeScheduler<storm::OptimizationDirection::Minimize, OptimizationDirection::Maximize>(operandIn, offsets, schedulerStorage, operandOut,
124 robustIndices);
125 } else {
126 return computeScheduler<storm::OptimizationDirection::Minimize, OptimizationDirection::Minimize>(operandIn, offsets, schedulerStorage, operandOut,
127 robustIndices);
128 }
129 }
130}
131
136
137} // namespace storm::solver::helper
SchedulerTrackingBackend(std::vector< uint64_t > &schedulerStorage, std::vector< typename ValueIterationOperator< ValueType, false >::IndexType > const &rowGroupIndices, bool applyUpdates)
void applyUpdate(ValueType &currValue, uint64_t rowGroup)
void firstRow(ValueType &&value, uint64_t rowGroup, uint64_t row)
void nextRow(ValueType &&value, uint64_t rowGroup, uint64_t row)
Helper class to extract optimal scheduler choices from a MinMax equation system solution.
bool computeScheduler(std::vector< SolutionType > &operandIn, std::vector< ValueType > const &offsets, storm::OptimizationDirection const &dir, std::vector< uint64_t > &schedulerStorage, bool robust, std::vector< SolutionType > *operandOut=nullptr, boost::optional< std::vector< uint64_t > > const &robustIndices=boost::none) const
Computes the optimal choices from the given solution.
SchedulerTrackingHelper(std::shared_ptr< ValueIterationOperator< ValueType, TrivialRowGrouping, SolutionType > > viOperator)
Initializes this helper with the given value iteration operator.
This class represents the Value Iteration Operator (also known as Bellman operator).
Stores and manages an extremal (maximal or minimal) value.
Definition Extremum.h:15
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:38
bool constexpr maximize(OptimizationDirection d)