AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
domain_transition_graph.h
1#ifndef HEURISTICS_DOMAIN_TRANSITION_GRAPH_H
2#define HEURISTICS_DOMAIN_TRANSITION_GRAPH_H
3
4#include "downward/task_proxy.h"
5
6#include <cassert>
7#include <unordered_map>
8#include <vector>
9
10namespace cea_heuristic {
11class ContextEnhancedAdditiveHeuristic;
12}
13
14namespace cg_heuristic {
15class CGHeuristic;
16}
17
18namespace domain_transition_graph {
19struct LocalAssignment;
20struct ValueNode;
21struct ValueTransition;
22struct ValueTransitionLabel;
23class DomainTransitionGraph;
24
25// Note: We do not use references but pointers to refer to the "parents" of
26// transitions and nodes. This is because these structures could not be
27// put into vectors otherwise.
28
29class DTGFactory {
30 using DTGs = std::vector<std::unique_ptr<DomainTransitionGraph>>;
31 const TaskProxy& task_proxy;
32 bool collect_transition_side_effects;
33 std::function<bool(int, int)> pruning_condition;
34
35 std::vector<utils::HashMap<std::pair<int, int>, int>> transition_index;
36 std::vector<std::unordered_map<int, int>> global_to_local_var;
37
38 void allocate_graphs_and_nodes(DTGs& dtgs);
39 void initialize_index_structures(int num_dtgs);
40 void create_transitions(DTGs& dtgs);
41 void process_effect(
42 const EffectProxy& eff,
43 const AxiomOrOperatorProxy& op,
44 DTGs& dtgs);
45 void update_transition_condition(
46 const FactProxy& fact,
47 DomainTransitionGraph* dtg,
48 std::vector<LocalAssignment>& condition);
49 void extend_global_to_local_mapping_if_necessary(
50 DomainTransitionGraph* dtg,
51 int global_var);
52 void revert_new_local_vars(
53 DomainTransitionGraph* dtg,
54 unsigned int first_local_var);
55 ValueTransition*
56 get_transition(int origin, int target, DomainTransitionGraph* dtg);
57 void simplify_transitions(DTGs& dtgs);
58 void simplify_labels(std::vector<ValueTransitionLabel>& labels);
59 void collect_all_side_effects(DTGs& dtgs);
60 void collect_side_effects(
61 DomainTransitionGraph* dtg,
62 std::vector<ValueTransitionLabel>& labels);
63 AxiomOrOperatorProxy get_op_for_label(const ValueTransitionLabel& label);
64
65public:
66 DTGFactory(
67 const TaskProxy& task_proxy,
68 bool collect_transition_side_effects,
69 const std::function<bool(int, int)>& pruning_condition);
70
71 DTGs build_dtgs();
72};
73
74struct LocalAssignment {
75 short local_var;
76 short value;
77
78 LocalAssignment(int var, int val)
79 : local_var(var)
80 , value(val)
81 {
82 // Check overflow.
83 assert(local_var == var);
84 assert(value == val);
85 }
86};
87
88struct ValueTransitionLabel {
89 int op_id;
90 bool is_axiom;
91 std::vector<LocalAssignment> precond;
92 std::vector<LocalAssignment> effect;
93
94 ValueTransitionLabel(
95 int op_id,
96 bool axiom,
97 const std::vector<LocalAssignment>& precond,
98 const std::vector<LocalAssignment>& effect)
99 : op_id(op_id)
100 , is_axiom(axiom)
101 , precond(precond)
102 , effect(effect)
103 {
104 }
105};
106
107struct ValueTransition {
108 ValueNode* target;
109 std::vector<ValueTransitionLabel> labels;
110
111 ValueTransition(ValueNode* targ)
112 : target(targ)
113 {
114 }
115
116 void simplify(const TaskProxy& task_proxy);
117};
118
119struct ValueNode {
120 DomainTransitionGraph* parent_graph;
121 int value;
122 std::vector<ValueTransition> transitions;
123
124 std::vector<int> distances;
125 std::vector<ValueTransitionLabel*> helpful_transitions;
126 std::vector<int> children_state;
127 ValueNode* reached_from;
128 ValueTransitionLabel* reached_by;
129
130 ValueNode(DomainTransitionGraph* parent, int val)
131 : parent_graph(parent)
132 , value(val)
133 , reached_from(nullptr)
134 , reached_by(nullptr)
135 {
136 }
137};
138
139class DomainTransitionGraph {
140 friend class cg_heuristic::CGHeuristic;
141 friend class cea_heuristic::ContextEnhancedAdditiveHeuristic;
142 friend class DTGFactory;
143
144 int var;
145 std::vector<ValueNode> nodes;
146
147 int last_helpful_transition_extraction_time;
148
149 std::vector<int> local_to_global_child;
150 // used for mapping variables in conditions to their global index
151 // (only needed for initializing child_state for the start node?)
152
153 DomainTransitionGraph(
154 const DomainTransitionGraph& other); // copying forbidden
155public:
156 DomainTransitionGraph(int var_index, int node_count);
157};
158} // namespace domain_transition_graph
159
160#endif