AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
landmark_graph.h
1#ifndef LANDMARKS_LANDMARK_GRAPH_H
2#define LANDMARKS_LANDMARK_GRAPH_H
3
4#include "downward/landmarks/landmark.h"
5
6#include "downward/task_proxy.h"
7
8#include "downward/utils/hash.h"
9#include "downward/utils/memory.h"
10
11#include <cassert>
12#include <list>
13#include <map>
14#include <set>
15#include <unordered_map>
16#include <unordered_set>
17#include <vector>
18
19namespace landmarks {
20enum class EdgeType {
21 /*
22 NOTE: The code relies on the fact that larger numbers are stronger in the
23 sense that, e.g., every greedy-necessary ordering is also natural and
24 reasonable. (It is a sad fact of terminology that necessary is indeed a
25 special case of greedy-necessary, i.e., every necessary ordering is
26 greedy-necessary, but not vice versa.
27 */
28 NECESSARY = 3,
29 GREEDY_NECESSARY = 2,
30 NATURAL = 1,
31 REASONABLE = 0
32};
33
34class LandmarkNode {
35 int id;
36 Landmark landmark;
37
38public:
39 LandmarkNode(Landmark&& landmark)
40 : id(-1)
41 , landmark(std::move(landmark))
42 {
43 }
44
45 std::unordered_map<LandmarkNode*, EdgeType> parents;
46 std::unordered_map<LandmarkNode*, EdgeType> children;
47
48 int get_id() const { return id; }
49
50 // TODO: Should possibly not be changeable
51 void set_id(int new_id)
52 {
53 assert(id == -1 || new_id == id);
54 id = new_id;
55 }
56
57 // TODO: Remove this function once the LM-graph is constant after creation.
58 Landmark& get_landmark() { return landmark; }
59
60 const Landmark& get_landmark() const { return landmark; }
61};
62
63class LandmarkGraph {
64public:
65 /*
66 TODO: get rid of this by removing get_nodes() and instead offering
67 functions begin() and end() with an iterator class, so users of the
68 LandmarkGraph can do loops like this:
69 for (const LandmarkNode &n : graph) {...}
70 */
71 using Nodes = std::vector<std::unique_ptr<LandmarkNode>>;
72
73private:
74 int num_conjunctive_landmarks;
75 int num_disjunctive_landmarks;
76
77 utils::HashMap<FactPair, LandmarkNode*> simple_landmarks_to_nodes;
78 utils::HashMap<FactPair, LandmarkNode*> disjunctive_landmarks_to_nodes;
79 Nodes nodes;
80
81 void remove_node_occurrences(LandmarkNode* node);
82
83public:
84 /* This is needed only by landmark graph factories and will disappear
85 when moving landmark graph creation there. */
86 LandmarkGraph();
87
88 // needed by both landmarkgraph-factories and non-landmarkgraph-factories
89 const Nodes& get_nodes() const { return nodes; }
90 // needed by both landmarkgraph-factories and non-landmarkgraph-factories
91 int get_num_landmarks() const { return nodes.size(); }
92 /* This is needed only by landmark graph factories and will disappear
93 when moving landmark graph creation there. */
94 int get_num_disjunctive_landmarks() const
95 {
96 return num_disjunctive_landmarks;
97 }
98 /* This is needed only by landmark graph factories and will disappear
99 when moving landmark graph creation there. */
100 int get_num_conjunctive_landmarks() const
101 {
102 return num_conjunctive_landmarks;
103 }
104 /* This is needed only by landmark graph factories and will disappear
105 when moving landmark graph creation there. */
106 int get_num_edges() const;
107
108 // only needed by non-landmarkgraph-factories
109 LandmarkNode* get_node(int index) const;
110 // only needed by non-landmarkgraph-factories
111 LandmarkNode* get_node(const FactPair& fact) const;
112 /* This is needed only by landmark graph factories and will disappear
113 when moving landmark graph creation there. */
114 LandmarkNode& get_simple_landmark(const FactPair& fact) const;
115 /* This is needed only by landmark graph factories and will disappear
116 when moving landmark graph creation there. */
117 LandmarkNode& get_disjunctive_landmark(const FactPair& fact) const;
118
119 /* This is needed only by landmark graph factories and will disappear
120 when moving landmark graph creation there. It is not needed by
121 HMLandmarkFactory*/
122 bool contains_simple_landmark(const FactPair& lm) const;
123 /* Only used internally. */
124 bool contains_disjunctive_landmark(const FactPair& lm) const;
125 /* This is needed only by landmark graph factories and will disappear
126 when moving landmark graph creation there. It is not needed by
127 HMLandmarkFactory*/
128 bool contains_overlapping_disjunctive_landmark(
129 const std::set<FactPair>& lm) const;
130 /* This is needed only by landmark graph factories and will disappear
131 when moving landmark graph creation there. */
132 bool
133 contains_identical_disjunctive_landmark(const std::set<FactPair>& lm) const;
134 /* This is needed only by landmark graph factories and will disappear
135 when moving landmark graph creation there. It is not needed by
136 HMLandmarkFactory*/
137 bool contains_landmark(const FactPair& fact) const;
138
139 /* This is needed only by landmark graph factories and will disappear
140 when moving landmark graph creation there. */
141 LandmarkNode& add_landmark(Landmark&& landmark);
142 /* This is needed only by landmark graph factories and will disappear
143 when moving landmark graph creation there. */
144 void remove_node(LandmarkNode* node);
145 void remove_node_if(
146 const std::function<bool(const LandmarkNode&)>& remove_node_condition);
147
148 /* This is needed only by landmark graph factories and will disappear
149 when moving landmark graph creation there. */
150 void set_landmark_ids();
151};
152} // namespace landmarks
153
154#endif
STL namespace.