Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SchedulerTrackingHelper.cpp
Go to the documentation of this file.
2
6
7namespace storm::solver::helper {
8
9template<typename ValueType, storm::OptimizationDirection Dir>
11 public:
12 SchedulerTrackingBackend(std::vector<uint64_t>& schedulerStorage,
13 std::vector<typename ValueIterationOperator<ValueType, false>::IndexType> const& rowGroupIndices, bool applyUpdates)
14 : schedulerStorage(schedulerStorage), applyUpdates(applyUpdates), rowGroupIndices(rowGroupIndices) {
15 // intentionally empty
16 }
18 isConverged = true;
19 }
20
21 void firstRow(ValueType&& value, uint64_t rowGroup, uint64_t row) {
22 currChoice = row - rowGroupIndices[rowGroup];
23 best = std::move(value);
24 }
25
26 void nextRow(ValueType&& value, uint64_t rowGroup, uint64_t row) {
27 if (best &= value) {
28 currChoice = row - rowGroupIndices[rowGroup];
29 } else if (*best == value) {
30 // For rows that are equally good, we prefer the previously selected one.
31 // This is necessary, e.g., for policy iteration correctness.
32 if (uint64_t rowChoice = row - rowGroupIndices[rowGroup]; rowChoice == schedulerStorage[rowGroup]) {
33 currChoice = rowChoice;
34 }
35 }
36 }
37
38 void applyUpdate(ValueType& currValue, uint64_t rowGroup) {
39 if (applyUpdates) {
40 currValue = std::move(*best);
41 }
42 auto& choice = schedulerStorage[rowGroup];
43 if (isConverged) {
44 isConverged = choice == currChoice;
45 }
46 choice = currChoice;
47 }
48
49 void endOfIteration() const {}
50
51 bool converged() const {
52 return isConverged;
53 }
54
55 bool constexpr abort() const {
56 return false;
57 }
58
59 private:
60 std::vector<uint64_t>& schedulerStorage;
61 bool const applyUpdates;
62 std::vector<typename ValueIterationOperator<ValueType, false>::IndexType> const& rowGroupIndices;
63
64 bool isConverged;
66 uint64_t currChoice;
67};
68
69template<typename ValueType, typename SolutionType>
71 : viOperator(viOperator) {
72 // Intentionally left empty
73}
74
75template<typename ValueType, typename SolutionType>
76template<storm::OptimizationDirection Dir, storm::OptimizationDirection RobustDir>
77bool SchedulerTrackingHelper<ValueType, SolutionType>::computeScheduler(std::vector<SolutionType>& operandIn, std::vector<ValueType> const& offsets,
78 std::vector<uint64_t>& schedulerStorage, std::vector<SolutionType>* operandOut) const {
79 bool const applyUpdates = operandOut != nullptr;
80 SchedulerTrackingBackend<SolutionType, Dir> backend(schedulerStorage, viOperator->getRowGroupIndices(), applyUpdates);
81 if (applyUpdates) {
82 return viOperator->template applyRobust<RobustDir>(*operandOut, operandIn, offsets, backend);
83 } else {
84 return viOperator->template applyInPlaceRobust<RobustDir>(operandIn, offsets, backend);
85 }
86}
87
88template<typename ValueType, typename SolutionType>
89bool SchedulerTrackingHelper<ValueType, SolutionType>::computeScheduler(std::vector<SolutionType>& operandIn, std::vector<ValueType> const& offsets,
90 storm::OptimizationDirection const& dir, std::vector<uint64_t>& schedulerStorage,
91 bool robust, std::vector<SolutionType>* operandOut) const {
92 if (maximize(dir)) {
93 if (robust) {
94 return computeScheduler<storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Minimize>(operandIn, offsets, schedulerStorage,
95 operandOut);
96 } else {
97 return computeScheduler<storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Maximize>(operandIn, offsets, schedulerStorage,
98 operandOut);
99 }
100 } else {
101 if (robust) {
102 return computeScheduler<storm::OptimizationDirection::Minimize, OptimizationDirection::Maximize>(operandIn, offsets, schedulerStorage, operandOut);
103 } else {
104 return computeScheduler<storm::OptimizationDirection::Minimize, OptimizationDirection::Minimize>(operandIn, offsets, schedulerStorage, operandOut);
105 }
106 }
107}
108
112
113} // 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) const
Computes the optimal choices from the given solution.
SchedulerTrackingHelper(std::shared_ptr< ValueIterationOperator< ValueType, false, 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
bool constexpr maximize(OptimizationDirection d)