AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
exhaustive_ao_impl.h
1#ifndef GUARD_INCLUDE_PROBFD_ALGORITHMS_EXHAUSTIVE_AO_H
2#error "This file should only be included from exhaustive_ao.h"
3#endif
4
5#include "probfd/algorithms/open_list.h"
6
7#include "downward/utils/countdown_timer.h"
8
10
11template <typename State, typename Action, bool UseInterval>
12ExhaustiveAOSearch<State, Action, UseInterval>::ExhaustiveAOSearch(
13 std::shared_ptr<PolicyPickerType> policy_chooser,
14 std::shared_ptr<OpenListType> open_list)
15 : Base(policy_chooser)
16 , open_list_(open_list)
17{
18}
19
20template <typename State, typename Action, bool UseInterval>
21Interval ExhaustiveAOSearch<State, Action, UseInterval>::do_solve(
22 MDPType& mdp,
23 EvaluatorType& heuristic,
24 param_type<State> initial_state,
25 ProgressReport& progress,
26 double max_time)
27{
28 utils::CountdownTimer timer(max_time);
29
30 StateID initstateid = mdp.get_state_id(initial_state);
31 const auto& state_info = this->state_infos_[initstateid];
32
33 open_list_->push(initstateid);
34
35 progress.register_bound("v", [&state_info]() {
36 return as_interval(state_info.value);
37 });
38
39 progress.register_print([&](std::ostream& out) {
40 out << "i=" << this->statistics_.iterations;
41 });
42
43 do {
44 timer.throw_if_expired();
45 progress.print();
46
47 assert(!this->open_list_->empty());
48 StateID stateid = open_list_->pop();
49 auto& info = this->state_infos_[stateid];
50
51 if (!info.is_on_fringe() || info.is_solved()) {
52 continue;
53 }
54
55 ++this->statistics_.iterations;
56
57 const State state = mdp.get_state(stateid);
58 const value_t termination_cost =
59 mdp.get_termination_info(state).get_cost();
60
61 ClearGuard _(transitions_);
62 this->expand_and_initialize(mdp, heuristic, state, info, transitions_);
63
64 const auto value =
65 this->compute_bellman(mdp, stateid, transitions_, termination_cost);
66 bool value_changed = this->update_value(info, value);
67
68 // Terminal state
69 if (info.is_solved()) {
70 assert(transitions_.empty());
71 this->backpropagate_tip_value(mdp, transitions_, info, timer);
72 continue;
73 }
74
75 unsigned min_order = std::numeric_limits<unsigned>::max();
76
77 for (const auto& [op, dist] : transitions_) {
78 for (auto& [succid, prob] : dist) {
79 auto& succ_info = this->state_infos_[succid];
80 if (succ_info.is_solved()) continue;
81
82 open_list_->push(stateid, op, prob, succid);
83
84 if (succ_info.is_marked()) continue;
85
86 succ_info.mark();
87 succ_info.add_parent(stateid);
88 min_order = std::min(min_order, succ_info.update_order);
89 ++info.unsolved;
90 }
91 }
92
93 if (info.unsolved == 0) {
94 transitions_.clear();
95 info.set_solved();
96 this->backpropagate_tip_value(mdp, transitions_, info, timer);
97 continue;
98 }
99
100 for (const auto& transition : transitions_) {
101 for (StateID succ_id : transition.successor_dist.support()) {
102 this->state_infos_[succ_id].unmark();
103 }
104 }
105
106 assert(min_order < std::numeric_limits<unsigned>::max());
107 this->backpropagate_update_order(stateid, info, min_order + 1, timer);
108
109 if (value_changed) {
110 transitions_.clear();
111 this->backpropagate_tip_value(mdp, transitions_, info, timer);
112 }
113 } while (!state_info.is_solved());
114
115 return state_info.get_bounds();
116}
117
118template <typename State, typename Action, bool UseInterval>
119bool ExhaustiveAOSearch<State, Action, UseInterval>::update_value_check_solved(
120 MDPType& mdp,
121 param_type<State> state,
122 std::vector<Transition<Action>> transitions,
123 StateInfo& info)
124{
125 assert(!info.is_solved());
126
127 const value_t termination_cost = mdp.get_termination_info(state).get_cost();
128
129 const auto value = this->compute_bellman(
130 mdp,
131 mdp.get_state_id(state),
132 transitions,
133 termination_cost);
134 bool value_changed = this->update_value(info, value);
135
136 if (info.unsolved == 0) {
137 info.set_solved();
138 }
139
140 return value_changed;
141}
142
143} // namespace probfd::algorithms::exhaustive_ao
I do not know this algorithm.
Definition exhaustive_ao.h:13
Interval as_interval(value_t lower_bound)
Returns the interval with the given lower bound and infinte upper bound.
double value_t
Typedef for the state value type.
Definition aliases.h:7