AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
ao_search_impl.h
1#ifndef GUARD_INCLUDE_PROBFD_ALGORITHMS_AO_SEARCH_H
2#error "This file should only be included from ao_search.h"
3#endif
4
5#include <ostream>
6
7#include "downward/utils/countdown_timer.h"
8
10
11inline void Statistics::print(std::ostream& out) const
12{
13 out << " Iterations: " << iterations << std::endl;
14}
15
16template <typename State, typename Action, typename StateInfo>
17void AOBase<State, Action, StateInfo>::print_additional_statistics(
18 std::ostream& out) const
19{
20 statistics_.print(out);
21}
22
23template <typename State, typename Action, typename StateInfo>
24void AOBase<State, Action, StateInfo>::backpropagate_tip_value(
25 this auto& self,
26 MDPType& mdp,
27 std::vector<Transition<Action>>& transitions,
28 StateInfo& state_info,
29 utils::CountdownTimer& timer)
30{
31 self.push_parents_to_queue(state_info);
32
33 while (!self.queue_.empty()) {
34 timer.throw_if_expired();
35
36 auto elem = self.queue_.top();
37 self.queue_.pop();
38
39 auto& info = self.state_infos_[elem.state_id];
40 assert(!info.is_on_fringe());
41 assert(!info.is_goal_state());
42 assert(!info.is_goal_or_terminal() || info.is_solved());
43
44 if (info.is_solved()) {
45 // has been handled already
46 continue;
47 }
48
49 assert(info.is_marked());
50 info.unmark();
51
52 const State state = mdp.get_state(elem.state_id);
53
54 ClearGuard _(transitions);
55 self.generate_non_tip_transitions(mdp, state, transitions);
56
57 bool value_changed =
58 self.update_value_check_solved(mdp, state, transitions, info);
59
60 if (info.is_solved() || value_changed) {
61 self.push_parents_to_queue(info);
62 }
63 }
64}
65
66template <typename State, typename Action, typename StateInfo>
67void AOBase<State, Action, StateInfo>::backpropagate_update_order(
68 StateID tip,
69 StateInfo& tip_info,
70 unsigned update_order,
71 utils::CountdownTimer& timer)
72{
73 tip_info.update_order = update_order;
74 queue_.emplace(update_order, tip);
75
76 do {
77 timer.throw_if_expired();
78
79 auto elem = queue_.top();
80 queue_.pop();
81
82 auto& info = this->state_infos_[elem.state_id];
83 if (info.update_order > elem.update_order) {
84 continue;
85 }
86
87 std::erase_if(info.get_parents(), [this, elem](StateID state_id) {
88 auto& pinfo = this->state_infos_[state_id];
89 if (pinfo.is_solved()) {
90 return true;
91 }
92
93 if (pinfo.update_order <= elem.update_order) {
94 pinfo.update_order = elem.update_order + 1;
95 queue_.emplace(elem.update_order + 1, state_id);
96 }
97
98 return false;
99 });
100 } while (!queue_.empty());
101}
102
103template <typename State, typename Action, typename StateInfo>
104void AOBase<State, Action, StateInfo>::push_parents_to_queue(StateInfo& info)
105{
106 auto& parents = info.get_parents();
107 for (StateID parent : parents) {
108 auto& pinfo = this->state_infos_[parent];
109
110 if constexpr (!StateInfo::StorePolicy) {
111 if (info.is_solved()) {
112 assert(pinfo.unsolved > 0 || pinfo.is_solved());
113 --pinfo.unsolved;
114 }
115 }
116
117 if (pinfo.is_marked()) continue;
118
119 pinfo.mark();
120 queue_.emplace(pinfo.update_order, parent);
121 }
122
123 if (info.is_solved()) {
124 utils::release_vector_memory(parents);
125 }
126}
127
128} // namespace probfd::algorithms::ao_search
Namespace dedicated to the AO* family of MDP algorithms.
Definition ao_search.h:14