AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
depth_first_heuristic_search.h
1#ifndef PROBFD_ALGORITHMS_DEPTH_FIRST_HEURISTIC_SEARCH_H
2#define PROBFD_ALGORITHMS_DEPTH_FIRST_HEURISTIC_SEARCH_H
3
4#include "probfd/algorithms/heuristic_search_base.h"
5
6#include <deque>
7#include <iostream>
8#include <type_traits>
9#include <vector>
10
11// Forward Declarations
12namespace utils {
13class CountdownTimer;
14}
15
18
19enum class BacktrackingUpdateType { DISABLED, ON_DEMAND, SINGLE };
20
21namespace internal {
22
23struct Statistics {
24 unsigned long long iterations = 0;
25 unsigned long long forward_updates = 0;
26 unsigned long long backtracking_updates = 0;
27 unsigned long long convergence_updates = 0;
28 unsigned long long convergence_value_iterations = 0;
29
30 void print(std::ostream& out) const;
31};
32
33template <typename Action, bool UseInterval>
34struct PerStateInformation
35 : public heuristic_search::
36 PerStateBaseInformation<Action, true, UseInterval> {
37 using Base = heuristic_search::PerStateBaseInformation<Action, true, UseInterval>;
38
39public:
40 static constexpr uint8_t SOLVED = 1 << Base::BITS;
41 static constexpr uint8_t MASK = 1 << Base::BITS;
42 static constexpr uint8_t BITS = Base::BITS + 1;
43
44 [[nodiscard]]
45 bool is_solved() const
46 {
47 return (this->info & SOLVED) != 0 || this->is_goal_or_terminal();
48 }
49
50 void set_solved() { this->info |= SOLVED; }
51
52 void unset_solved() { this->info &= ~SOLVED; }
53
54 void clear() { this->info &= ~MASK; }
55};
56
57struct LocalStateInfo {
58 enum { NEW = 0, ONSTACK = 1, CLOSED = 2 };
59
60 uint8_t status = NEW;
61 unsigned index = std::numeric_limits<unsigned>::max();
62 unsigned lowlink = std::numeric_limits<unsigned>::max();
63
64 void open(unsigned stack_index)
65 {
66 index = stack_index;
67 lowlink = stack_index;
68 }
69};
70
71struct ExpansionInfo {
72 explicit ExpansionInfo(StateID state)
73 : stateid(state)
74 {
75 }
76
77 const StateID stateid;
78
79 std::vector<StateID> successors;
80
81 bool solved : 1 = true;
82 bool value_converged : 1 = true;
83
84 bool next_successor();
85 [[nodiscard]]
86 StateID get_current_successor() const;
87};
88
89} // namespace internal
90
99template <typename State, typename Action, bool UseInterval>
102 State,
103 Action,
104 internal::PerStateInformation<Action, UseInterval>> {
105 using Base =
106 typename HeuristicDepthFirstSearch::FRETHeuristicSearchAlgorithm;
107
108public:
109 using StateInfo = typename Base::StateInfo;
110 using AlgorithmValueType = Base::AlgorithmValueType;
111
112private:
113 using MDP = typename Base::MDPType;
114 using Evaluator = typename Base::EvaluatorType;
115
116 using PolicyPicker = typename Base::PolicyPicker;
117
118 using Statistics = internal::Statistics;
119 using ExpansionInfo = internal::ExpansionInfo;
120 using LocalStateInfo = internal::LocalStateInfo;
121
122 // Algorithm parameters
123 const bool forward_updates_;
124 const BacktrackingUpdateType backtrack_update_type_;
125 const bool cutoff_tip_;
126 const bool cutoff_inconsistent_;
127 const bool terminate_exploration_on_cutoff_;
128 const bool label_solved_;
129
130 // Algorithm state
131 storage::StateHashMap<LocalStateInfo> local_state_infos_;
132 std::vector<StateID> visited_;
133 std::deque<ExpansionInfo> expansion_queue_;
134 std::deque<StateID> stack_;
135
136 Statistics statistics_;
137
138 // Re-used buffer
139 std::vector<Transition<Action>> transitions_;
140 std::vector<AlgorithmValueType> qvalues_;
141
142public:
144 std::shared_ptr<PolicyPicker> policy_chooser,
145 bool forward_updates,
146 BacktrackingUpdateType backtrack_update_type,
147 bool cutoff_tip,
148 bool cutoff_inconsistent,
149 bool terminate_exploration_on_cutoff,
150 bool label_solved);
151
152 void reset_search_state() override;
153
154protected:
155 Interval do_solve(
156 MDP& mdp,
157 Evaluator& heuristic,
158 param_type<State> state,
159 ProgressReport& progress,
160 double max_time) override;
161
162 void print_additional_statistics(std::ostream& out) const override;
163
164private:
165 void solve_with_vi_termination(
166 MDP& mdp,
167 Evaluator& heuristic,
168 StateID stateid,
169 ProgressReport& progress,
170 utils::CountdownTimer& timer);
171
172 void solve_without_vi_termination(
173 MDP& mdp,
174 Evaluator& heuristic,
175 StateID stateid,
176 ProgressReport& progress,
177 utils::CountdownTimer& timer);
178
179 template <bool GetVisited>
180 bool policy_exploration(
181 MDP& mdp,
182 Evaluator& heuristic,
183 StateID state,
184 utils::CountdownTimer& timer);
185
186 bool advance(MDP& mdp, ExpansionInfo& einfo, StateInfo& state_info);
187
188 bool push_successor(
189 MDP& mdp,
190 ExpansionInfo& einfo,
191 StateInfo& sinfo,
192 LocalStateInfo& lsinfo,
193 utils::CountdownTimer& timer);
194
195 void push(StateID stateid);
196
197 bool initialize(
198 MDP& mdp,
199 Evaluator& heuristic,
200 ExpansionInfo& einfo,
201 StateInfo& sinfo);
202
203 bool value_iteration(
204 MDP& mdp,
205 const std::ranges::input_range auto& range,
206 utils::CountdownTimer& timer);
207
208 std::pair<bool, bool> vi_step(
209 MDP& mdp,
210 const std::ranges::input_range auto& range,
211 utils::CountdownTimer& timer,
212 unsigned long long& stat_counter);
213};
214
215} // namespace probfd::algorithms::heuristic_depth_first_search
216
217#define GUARD_INCLUDE_PROBFD_ALGORITHMS_HEURISTIC_DEPTH_FIRST_SEARCH_H
218#include "probfd/algorithms/depth_first_heuristic_search_impl.h"
219#undef GUARD_INCLUDE_PROBFD_ALGORITHMS_HEURISTIC_DEPTH_FIRST_SEARCH_H
220
221#endif // PROBFD_ALGORITHMS_DEPTH_FIRST_HEURISTIC_SEARCH_H
A registry for print functions related to search progress.
Definition progress_report.h:33
Implementation of the depth-first heuristic search algorithm family steinmetz:etal:icaps-16.
Definition depth_first_heuristic_search.h:104
void print_additional_statistics(std::ostream &out) const override
Prints additional statistics to the output stream.
Definition depth_first_heuristic_search_impl.h:91
void reset_search_state() override
Resets the h search algorithm object to a clean state.
Definition depth_first_heuristic_search_impl.h:58
Heuristics search algorithm that can be used within FRET.
Definition heuristic_search_base.h:368
Namespace dedicated to Depth-First Heuristic Search.
Definition depth_first_heuristic_search.h:17
typename std::conditional_t< is_cheap_to_copy_v< T >, T, const T & > param_type
Alias template defining the best way to pass a parameter of a given type.
Definition type_traits.h:25
Represents a closed interval over the extended reals as a pair of lower and upper bound.
Definition interval.h:12
A StateID represents a state within a StateIDMap. Just like Fast Downward's StateID type,...
Definition types.h:22