AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
transition_system.h
1#ifndef MERGE_AND_SHRINK_TRANSITION_SYSTEM_H
2#define MERGE_AND_SHRINK_TRANSITION_SYSTEM_H
3
4#include "downward/merge_and_shrink/types.h"
5
6#include "downward/utils/collections.h"
7
8#include <compare>
9#include <iostream>
10#include <memory>
11#include <sstream>
12#include <string>
13#include <utility>
14#include <vector>
15
16namespace utils {
17class LogProxy;
18}
19
20namespace merge_and_shrink {
21class Distances;
22class Labels;
23
24struct Transition {
25 int src;
26 int target;
27
28 Transition(int src, int target)
29 : src(src)
30 , target(target)
31 {
32 }
33
34 friend auto operator<=>(const Transition&, const Transition&) = default;
35};
36
37std::ostream& operator<<(std::ostream& os, const Transition& trans);
38
39using LabelGroup = std::vector<int>;
40
41/*
42 Class for representing groups of labels with equivalent transitions in a
43 transition system. See also documentation for TransitionSystem.
44
45 The local label is in a consistent state if label_group and transitions
46 are sorted and unique.
47*/
48class LocalLabelInfo {
49 // The sorted set of labels with identical transitions in a transition
50 // system.
51 LabelGroup label_group;
52 std::vector<Transition> transitions;
53 // The cost is the minimum cost over all labels in label_group.
54 int cost;
55
56public:
57 LocalLabelInfo(
58 LabelGroup&& label_group,
59 std::vector<Transition>&& transitions,
60 int cost)
61 : label_group(std::move(label_group))
62 , transitions(std::move(transitions))
63 , cost(cost)
64 {
65 assert(is_consistent());
66 }
67
68 void add_label(int label, int label_cost);
69
70 /*
71 Remove the labels in old_labels from label_group and add new_label to it.
72 Requires old_labels to be sorted.
73 */
74 void apply_same_cost_label_mapping(
75 int new_label,
76 const std::vector<int>& old_labels);
77
78 // Remove the labels in old_labels. Requires old_labels to be sorted.
79 void remove_labels(const std::vector<int>& old_labels);
80
81 void recompute_cost(const Labels& labels);
82 void replace_transitions(std::vector<Transition>&& new_transitions);
83
84 /*
85 The given local label must have identical transitions. Its labels are
86 moved into this local label info. The given local label is then
87 invalidated.
88 */
89 void merge_local_label_info(LocalLabelInfo& local_label_info);
90
91 // Empty all data structures.
92 void deactivate();
93
94 // A local label is active as long as it represents labels (in label_group).
95 bool is_active() const { return !label_group.empty(); }
96
97 const LabelGroup& get_label_group() const { return label_group; }
98
99 const std::vector<Transition>& get_transitions() const
100 {
101 return transitions;
102 }
103
104 int get_cost() const { return cost; }
105
106 bool is_consistent() const;
107};
108
109/*
110 Iterator class for TransitionSystem which provides access to the active
111 entries of into local_label_infos.
112*/
113class TransitionSystemConstIterator {
114 std::vector<LocalLabelInfo>::const_iterator it;
115 std::vector<LocalLabelInfo>::const_iterator end_it;
116
117 void advance_to_next_valid_index();
118
119public:
120 TransitionSystemConstIterator(
121 std::vector<LocalLabelInfo>::const_iterator it,
122 std::vector<LocalLabelInfo>::const_iterator end_it);
123 TransitionSystemConstIterator& operator++();
124
125 const LocalLabelInfo& operator*() const { return *it; }
126
127 bool operator==(const TransitionSystemConstIterator& rhs) const
128 {
129 return it == rhs.it;
130 }
131
132 bool operator!=(const TransitionSystemConstIterator& rhs) const
133 {
134 return it != rhs.it;
135 }
136};
137
138class TransitionSystem {
139private:
140 /*
141 The following two attributes are only used for output.
142
143 - num_variables: total number of variables in the factored
144 transition system
145
146 - incorporated_variables: variables that contributed to this
147 transition system
148 */
149 const int num_variables;
150 std::vector<int> incorporated_variables;
151
152 const Labels& labels;
153 /*
154 All locally equivalent labels are grouped together, and their
155 transitions are only stored once for every such group. Each such group
156 is represented by a local label (LocalLabelInfo). Local labels can be
157 mapped back to the set of labels they represent. Their cost is
158 the minimum cost of all represented labels.
159 */
160 std::vector<int> label_to_local_label;
161 std::vector<LocalLabelInfo> local_label_infos;
162
163 int num_states;
164 std::vector<bool> goal_states;
165 int init_state;
166
167 /*
168 Check if two or more local labels are equivalent to each other,
169 and if so, merge them and store their transitions only once.
170 */
171 void compute_equivalent_local_labels();
172
173 // Statistics and output
174 int compute_total_transitions() const;
175 std::string get_description() const;
176
177 /*
178 The transitions for every group of locally equivalent labels are
179 sorted (by source, by target) and there are no duplicates.
180 */
181 bool are_local_labels_consistent() const;
182
183 /*
184 The mapping label_to_local_label is consistent with local_label_infos.
185 */
186 bool is_label_mapping_consistent() const;
187 void dump_label_mapping() const;
188
189public:
190 TransitionSystem(
191 int num_variables,
192 std::vector<int>&& incorporated_variables,
193 const Labels& labels,
194 std::vector<int>&& label_to_local_label,
195 std::vector<LocalLabelInfo>&& local_label_infos,
196 int num_states,
197 std::vector<bool>&& goal_states,
198 int init_state);
199 TransitionSystem(const TransitionSystem& other);
200 ~TransitionSystem();
201 /*
202 Factory method to construct the merge of two transition systems.
203
204 Invariant: the children ts1 and ts2 must be solvable.
205 (It is a bug to merge an unsolvable transition system.)
206 */
207 static std::unique_ptr<TransitionSystem> merge(
208 const Labels& labels,
209 const TransitionSystem& ts1,
210 const TransitionSystem& ts2,
211 utils::LogProxy& log);
212
213 /*
214 Applies the given state equivalence relation to the transition system.
215 abstraction_mapping is a mapping from old states to new states, and it
216 must be consistent with state_equivalence_relation in the sense that
217 old states are only mapped to the same new state if they are in the same
218 equivalence class as specified in state_equivalence_relation.
219 */
220 void apply_abstraction(
221 const StateEquivalenceRelation& state_equivalence_relation,
222 const std::vector<int>& abstraction_mapping,
223 utils::LogProxy& log);
224
225 /*
226 Applies the given label mapping, mapping old to new label numbers. This
227 potentially means regrouping or adding new local labels.
228 */
229 void apply_label_reduction(
230 const std::vector<std::pair<int, std::vector<int>>>& label_mapping,
231 bool only_equivalent_labels);
232
233 TransitionSystemConstIterator begin() const
234 {
235 return TransitionSystemConstIterator(
236 local_label_infos.begin(),
237 local_label_infos.end());
238 }
239
240 TransitionSystemConstIterator end() const
241 {
242 return TransitionSystemConstIterator(
243 local_label_infos.end(),
244 local_label_infos.end());
245 }
246
247 /*
248 Method to identify the transition system in output.
249 Print "Atomic transition system #x: " for atomic transition systems,
250 where x is the variable. For composite transition systems, print
251 "Transition system (x/y): " for a transition system containing x
252 out of y variables.
253 */
254 std::string tag() const;
255
256 bool is_valid() const;
257
258 bool is_solvable(const Distances& distances) const;
259 void dump_dot_graph(utils::LogProxy& log) const;
260 void dump_labels_and_transitions(utils::LogProxy& log) const;
261 void statistics(utils::LogProxy& log) const;
262
263 int get_size() const { return num_states; }
264
265 int get_init_state() const { return init_state; }
266
267 bool is_goal_state(int state) const { return goal_states[state]; }
268
269 const std::vector<int>& get_incorporated_variables() const
270 {
271 return incorporated_variables;
272 }
273};
274} // namespace merge_and_shrink
275
276#endif
STL namespace.