AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
qualitative_reachability_analysis.h
1#ifndef PROBFD_PREPROCESSING_QUALITATIVE_REACHABILITY_ANALYSIS_H
2#define PROBFD_PREPROCESSING_QUALITATIVE_REACHABILITY_ANALYSIS_H
3
4#include "probfd/quotients/quotient_system.h"
5
6#include "probfd/storage/per_state_storage.h"
7
8#include "probfd/utils/iterators.h"
9
10#include "probfd/evaluator.h"
11#include "probfd/mdp.h"
12#include "probfd/type_traits.h"
13
14#include "downward/utils/countdown_timer.h"
15#include "downward/utils/timer.h"
16
17#include <cassert>
18#include <deque>
19#include <iostream>
20#include <iterator>
21#include <limits>
22#include <type_traits>
23#include <vector>
24
25namespace probfd::preprocessing {
26
27struct QRStatistics {
28 unsigned long long goals = 0;
29 unsigned long long terminals = 0;
30 unsigned long long selfloops = 0;
31
32 unsigned long long sccs1 = 0;
33 unsigned long long sccsk = 0;
34 unsigned long long sccs1_dead = 0;
35 unsigned long long sccsk_dead = 0;
36 unsigned long long ones = 0;
37
38 utils::Timer time;
39
40 void print(std::ostream& out) const;
41};
42
43namespace internal {
44
45struct StateInfo {
46 enum { NEW, ONSTACK, CLOSED };
47 static constexpr uint32_t UNDEF = std::numeric_limits<uint32_t>::max() >> 3;
48
49 unsigned explored : 1 = 0;
50 unsigned dead : 1 = 1; // dead end flag
51 unsigned solvable : 1 = 0; // solvable state flag
52 unsigned stackid : 29 = UNDEF;
53
54 [[nodiscard]]
55 auto get_status() const;
56};
57
58struct StackInfo {
59 struct ParentTransition {
60 unsigned parent_idx;
61 unsigned parent_transition_idx;
62 };
63
64 // A transition is active if it is not going to a state with goal
65 // probability less than one. This information is iteratively refined in a
66 // fixpoint iteration as more states with this property are found.
67
68 struct TransitionFlags {
69 bool is_active_exiting : 1; // Is the transition active and an SCC exit?
70 bool is_active : 1; // Is the transition active?
71 };
72
73 StateID stateid;
74
75 unsigned active_exit_transitions = 0; // Number of active exit transitions.
76 unsigned active_transitions = 0; // Number of active transitions.
77
78 std::vector<TransitionFlags> transition_flags;
79 std::vector<ParentTransition> parents;
80
81 explicit StackInfo(StateID sid);
82};
83
84} // namespace internal
85
93template <typename State, typename Action>
97
98 using StateInfo = internal::StateInfo;
99 using StackInfo = internal::StackInfo;
100
101 struct ExpansionInfo {
102 StateID state_id;
103 StackInfo& stack_info;
104 StateInfo& state_info;
105
106 // Tarjan's SCC algorithm: stack id and lowlink
107 unsigned stck;
108 unsigned lstck;
109
110 // Temporary transition flags
111 bool exits_only_solvable : 1 = true;
112 bool exits_scc : 1 = false;
113 bool transitions_in_scc : 1 = false;
114
115 // Mutable info
116 std::vector<Action> aops; // Remaining unexpanded operators
117 Distribution<StateID> transition; // Currently expanded transition
118 // Next state to expand
119 typename Distribution<StateID>::const_iterator successor;
120
121 explicit ExpansionInfo(
122 StateID state_id,
123 StackInfo& stack_info,
124 StateInfo& state_info,
125 unsigned int stck);
126
131 bool next_action(MDPType& mdp);
132 bool forward_non_self_loop(MDPType& mdp, const State& state);
133 bool next_successor();
134
135 StateID get_current_successor();
136 };
137
138 const bool expand_goals_;
139
140 storage::PerStateStorage<StateInfo> state_infos_;
141 std::deque<ExpansionInfo> expansion_queue_;
142 std::deque<StackInfo> stack_;
143
144 QRStatistics stats_;
145
146public:
147 explicit QualitativeReachabilityAnalysis(bool expand_goals);
148
149 void run_analysis(
150 MDPType& mdp,
151 const EvaluatorType* pruning_function,
152 param_type<State> source_state,
153 std::output_iterator<StateID> auto dead_out,
154 std::output_iterator<StateID> auto unsolvable_out,
155 std::output_iterator<StateID> auto solvable_out,
156 double max_time = std::numeric_limits<double>::infinity());
157
158private:
159 void push_state(StateID state_id, StateInfo& state_info);
160
161 bool initialize(
162 MDPType& mdp,
163 const EvaluatorType* pruning_function,
164 ExpansionInfo& exp_info);
165
166 bool push_successor(
167 MDPType& mdp,
168 ExpansionInfo& e,
169 utils::CountdownTimer& timer);
170
171 void scc_found(
172 unsigned int stack_idx,
173 std::output_iterator<StateID> auto dead_out,
174 std::output_iterator<StateID> auto unsolvable_out,
175 std::output_iterator<StateID> auto solvable_out,
176 utils::CountdownTimer& timer);
177};
178
179} // namespace probfd::preprocessing
180
181#define GUARD_INCLUDE_PROBFD_PREPROCESSING_QUALITATIVE_REACHABILITY_ANALYSIS_H
182#include "probfd/preprocessing/qualitative_reachability_analysis_impl.h"
183#undef GUARD_INCLUDE_PROBFD_PREPROCESSING_QUALITATIVE_REACHABILITY_ANALYSIS_H
184
185#endif // PROBFD_PREPROCESSING_QUALITATIVE_REACHABILITY_ANALYSIS_H
A convenience class that represents a finite probability distribution.
Definition task_state_space.h:27
Algorithm that computes all dead-ends and states with a goal probability of one which are reachable f...
Definition qualitative_reachability_analysis.h:94
This namespace contains preprocessing algorithms for SSPs.
Definition end_component_decomposition.h:24
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
A StateID represents a state within a StateIDMap. Just like Fast Downward's StateID type,...
Definition types.h:22