AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
heuristic_search_state_information.h
1#ifndef PROBFD_ALGORITHMS_HEURISTIC_SEARCH_STATE_INFORMATION_H
2#define PROBFD_ALGORITHMS_HEURISTIC_SEARCH_STATE_INFORMATION_H
3
4#include "probfd/algorithms/state_properties.h"
5#include "probfd/algorithms/types.h"
6
7#include "probfd/storage/per_state_storage.h"
8
9#include "probfd/transition.h"
10#include "probfd/types.h"
11
12#include <cassert>
13#include <cstdint>
14#include <optional>
15
17
18template <typename, bool StorePolicy = false>
19struct StatesPolicy {};
20
21template <typename Action>
22struct StatesPolicy<Action, true> {
23 std::optional<Action> policy = std::nullopt;
24
25 std::optional<Action> get_policy() const { return policy; }
26
27 bool update_policy(const std::optional<Action>& a)
28 {
29 bool changed = policy != a;
30 policy = a;
31 return changed;
32 }
33
34 bool update_policy(const std::optional<Transition<Action>>& transition)
35 {
36 return update_policy(
37 transition.transform([](auto& t) { return t.action; }));
38 }
39
40 bool update_policy(std::nullopt_t)
41 {
42 return update_policy(std::optional<Action>{});
43 }
44};
45
46struct StateFlags {
47 static constexpr uint8_t INITIALIZED = 1;
48 static constexpr uint8_t TERMINAL = 2;
49 static constexpr uint8_t GOAL = 4;
50 static constexpr uint8_t FRINGE = 5;
51 static constexpr uint8_t MASK = 7;
52 static constexpr uint8_t BITS = 3;
53
54 uint8_t info = 0;
55
56 [[nodiscard]]
57 bool is_value_initialized() const
58 {
59 return (info & MASK) != 0;
60 }
61
62 [[nodiscard]]
63 bool is_terminal() const
64 {
65 return (info & MASK) == TERMINAL;
66 }
67
68 [[nodiscard]]
69 bool is_goal_state() const
70 {
71 return (info & MASK) == GOAL;
72 }
73
74 [[nodiscard]]
75 bool is_goal_or_terminal() const
76 {
77 return is_terminal() || is_goal_state();
78 }
79
80 [[nodiscard]]
81 bool is_on_fringe() const
82 {
83 return (info & MASK) == FRINGE;
84 }
85
86 void set_goal()
87 {
88 assert(!is_value_initialized());
89 info = (info & ~MASK) | GOAL;
90 }
91
92 void set_on_fringe()
93 {
94 // FRET may demote to fringe state when collapsing a trap
95 // assert(!is_value_initialized());
96 info = (info & ~MASK) | FRINGE;
97 }
98
99 void set_terminal()
100 {
101 assert(!is_goal_or_terminal());
102 info = (info & ~MASK) | TERMINAL;
103 }
104
105 void removed_from_fringe()
106 {
107 assert(is_value_initialized() && !is_goal_or_terminal());
108 info = (info & ~MASK) | INITIALIZED;
109 }
110};
111
112template <typename Action, bool StorePolicy_, bool UseInterval_>
113struct PerStateBaseInformation
114 : public StatesPolicy<Action, StorePolicy_>
115 , public StateFlags {
116 static constexpr bool StorePolicy = StorePolicy_;
117 static constexpr bool UseInterval = UseInterval_;
118
120
122 [[nodiscard]]
123 bool bounds_agree() const
124 {
125 static_assert(UseInterval, "No interval available!");
126 return value.bounds_approximately_equal();
127 }
128
129 [[nodiscard]]
130 value_t get_value() const
131 {
132 if constexpr (UseInterval) {
133 return value.lower;
134 } else {
135 return value;
136 }
137 }
138
139 [[nodiscard]]
140 Interval get_bounds() const
141 {
142 if constexpr (UseInterval) {
143 return value;
144 } else {
145 return Interval(value, INFINITE_VALUE);
146 }
147 }
148};
149
150} // namespace probfd::algorithms::heuristic_search
151
152#endif // PROBFD_ALGORITHMS_HEURISTIC_SEARCH_STATE_INFORMATION_H
Namespace dedicated to the MDP h search base implementation.
Definition heuristic_search_base.h:47
std::conditional_t< UseInterval, Interval, value_t > AlgorithmValue
Convenience value type alias for algorithms selecting interval iteration behaviour based on a templat...
Definition types.h:14
double value_t
Typedef for the state value type.
Definition aliases.h:7