-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoloured_graph.cpp
348 lines (292 loc) · 10.3 KB
/
coloured_graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "coloured_graph.h"
#include "predicate_manager.h"
#include "term_manager.h"
#include "fc_planner.h"
namespace MyPOP
{
ColouredGraphNodeGoal::ColouredGraphNodeGoal(const GroundedAtom& grounded_atom)
: grounded_atom_(&grounded_atom)
{
}
bool ColouredGraphNodeGoal::isEquivalentTo(const GroundedAtom& grounded_atom) const
{
return &grounded_atom == grounded_atom_;
}
bool ColouredGraphNodeGoal::isEquivalentTo(const ColouredGraphNodeGoal& atom) const
{
return isEquivalentTo(*atom.grounded_atom_);
}
ColouredGraphNodePredicates::ColouredGraphNodePredicates(const Predicate& predicate, int invariable_index, const std::vector<const Object*>& objects)
: predicates_(&predicate), invariables_(invariable_index)
{
objects_.insert(objects_.end(), objects.begin(), objects.end());
}
bool ColouredGraphNodePredicates::isEquivalentTo(const Predicate& predicate, int invariables, const std::vector<const Object*>& objects) const
{
if (predicate.getName() != predicates_->getName() || predicate.getArity() != predicates_->getArity() || invariables != invariables_ || objects_.size() != objects.size())
{
return false;
}
for (unsigned int i = 0; i < objects.size(); ++i)
{
if ((int)i != invariables && objects[i] != objects_[i])
{
return false;
}
}
return true;
}
bool ColouredGraphNodePredicates::isEquivalentTo(const ColouredGraphNodePredicates& other) const
{
return isEquivalentTo(*other.predicates_, other.invariables_, other.objects_);
}
void ColouredGraphNodeObjects::addObject(const Object& object)
{
objects_.push_back(&object);
}
void ColouredGraphNodeObjects::addEdge(const ColouredGraphNodePredicates& edge)
{
for (std::vector<const ColouredGraphNodePredicates*>::const_iterator ci = predicate_edges_.begin(); ci != predicate_edges_.end(); ++ci)
{
const ColouredGraphNodePredicates* cgn = *ci;
if (cgn == &edge)
{
return;
}
}
predicate_edges_.push_back(&edge);
}
void ColouredGraphNodeObjects::addEdge(const ColouredGraphNodeGoal& edge)
{
for (std::vector<const ColouredGraphNodeGoal*>::const_iterator ci = goal_edges_.begin(); ci != goal_edges_.end(); ++ci)
{
const ColouredGraphNodeGoal* cgn = *ci;
if (cgn == &edge)
{
return;
}
}
goal_edges_.push_back(&edge);
}
ColouredGraphNodeObjects* ColouredGraph::getNode(const Object& object) const
{
std::map<const Object*, ColouredGraphNodeObjects*>::const_iterator ci = mapped_objects_.find(&object);
if (ci == mapped_objects_.end())
{
return NULL;
}
return (*ci).second;
}
bool ColouredGraphNodeObjects::isSymmetricalTo(const ColouredGraphNodeObjects& other) const
{
if (predicate_edges_.size() != other.predicate_edges_.size() || goal_edges_.size() != other.goal_edges_.size())
{
return false;
}
for (std::vector<const ColouredGraphNodePredicates*>::const_iterator ci = predicate_edges_.begin(); ci != predicate_edges_.end(); ++ci)
{
const ColouredGraphNodePredicates* lhs_cgnp = *ci;
bool found_match = false;
for (std::vector<const ColouredGraphNodePredicates*>::const_iterator ci = other.predicate_edges_.begin(); ci != other.predicate_edges_.end(); ++ci)
{
const ColouredGraphNodePredicates* rhs_cgnp = *ci;
if (lhs_cgnp->isEquivalentTo(*rhs_cgnp))
{
found_match = true;
break;
}
}
if (!found_match)
{
return false;
}
}
for (std::vector<const ColouredGraphNodeGoal*>::const_iterator ci = goal_edges_.begin(); ci != goal_edges_.end(); ++ci)
{
const ColouredGraphNodeGoal* lhs_cgng = *ci;
bool found_match = false;
for (std::vector<const ColouredGraphNodeGoal*>::const_iterator ci = other.goal_edges_.begin(); ci != other.goal_edges_.end(); ++ci)
{
const ColouredGraphNodeGoal* rhs_cgng = *ci;
if (lhs_cgng->isEquivalentTo(*rhs_cgng))
{
found_match = true;
break;
}
}
if (!found_match)
{
return false;
}
}
return true;
}
std::ostream& operator<<(std::ostream& os, const ColouredGraphNodeObjects& coloured_graph_node_object)
{
os << "{";
for (std::vector<const Object*>::const_iterator ci = coloured_graph_node_object.getObjects().begin(); ci != coloured_graph_node_object.getObjects().end(); ++ci)
{
os << **ci << ", ";
}
os << "}";
return os;
}
ColouredGraph::ColouredGraph(const std::multimap<const Object*, const Object*>& symmetrical_objects)
{
std::set<const Object*> processed_objects;
for (std::multimap<const Object*, const Object*>::const_iterator ci = symmetrical_objects.begin(); ci != symmetrical_objects.end(); ++ci)
{
const Object* lhs_object = (*ci).first;
if (processed_objects.count(lhs_object) != 0)
{
continue;
}
processed_objects.insert(lhs_object);
ColouredGraphNodeObjects* cgno = new ColouredGraphNodeObjects();
all_object_edges_.push_back(cgno);
std::pair<std::multimap<const Object*, const Object*>::const_iterator, std::multimap<const Object*, const Object*>::const_iterator> m_ci = symmetrical_objects.equal_range(lhs_object);
for (std::multimap<const Object*, const Object*>::const_iterator ci = m_ci.first; ci != m_ci.second; ++ci)
{
cgno->addObject(*(*ci).second);
mapped_objects_.insert(std::make_pair((*ci).second, cgno));
}
}
}
ColouredGraph::~ColouredGraph()
{
for (std::vector<ColouredGraphNodeObjects*>::const_iterator ci = all_object_edges_.begin(); ci != all_object_edges_.end(); ++ci)
{
delete *ci;
}
for (std::vector<ColouredGraphNodePredicates*>::const_iterator ci = predicate_nodes_.begin(); ci != predicate_nodes_.end(); ++ci)
{
delete *ci;
}
for (std::vector<ColouredGraphNodeGoal*>::const_iterator ci = goal_nodes_.begin(); ci != goal_nodes_.end(); ++ci)
{
delete *ci;
}
}
ColouredGraphNodePredicates& ColouredGraph::createPredicateNode(const Predicate& predicate, int invariable, const std::vector<const Object*>& objects)
{
for (std::vector<ColouredGraphNodePredicates*>::const_iterator ci = predicate_nodes_.begin(); ci != predicate_nodes_.end(); ++ci)
{
ColouredGraphNodePredicates* cgnp = *ci;
if (cgnp->isEquivalentTo(predicate, invariable, objects))
{
return *cgnp;
}
}
ColouredGraphNodePredicates* cgnp = new ColouredGraphNodePredicates(predicate, invariable, objects);
predicate_nodes_.push_back(cgnp);
return *cgnp;
}
ColouredGraphNodeGoal& ColouredGraph::createGoalNode(const GroundedAtom& goal_atom)
{
for (std::vector<ColouredGraphNodeGoal*>::const_iterator ci = goal_nodes_.begin(); ci != goal_nodes_.end(); ++ci)
{
ColouredGraphNodeGoal* cgnp = *ci;
if (cgnp->isEquivalentTo(goal_atom))
{
return *cgnp;
}
}
ColouredGraphNodeGoal* cgnp = new ColouredGraphNodeGoal(goal_atom);
goal_nodes_.push_back(cgnp);
return *cgnp;
}
bool ColouredGraph::isSymmetricalTo(const ColouredGraph& other) const
{
if (other.mapped_objects_.size() != mapped_objects_.size())
{
return false;
}
std::set<const ColouredGraphNodeObjects*> rhs_mapped_objects;
//std::map<const ColouredGraphNodeObjects*, const ColouredGraphNodeObjects*> mappings, dummy_mappings;
bool are_symmetrical = findSymmetry(rhs_mapped_objects, other);//, mappings, dummy_mappings);
/*
if (are_symmetrical)
{
for (std::map<const ColouredGraphNodeObjects*, const ColouredGraphNodeObjects*>::const_iterator ci = mappings.begin(); ci != mappings.end(); ++ci)
{
std::cout << *(*ci).first << " >>>>-----> " << *(*ci).second << std::endl;
}
}
*/
return are_symmetrical;
}
bool ColouredGraph::findSymmetry(const std::set<const ColouredGraphNodeObjects*>& rhs_object_mappings, const ColouredGraph& other) const//, std::map<const ColouredGraphNodeObjects*, const ColouredGraphNodeObjects*>& mappings, const std::map<const ColouredGraphNodeObjects*, const ColouredGraphNodeObjects*>& tmp_mappings) const
{
if (rhs_object_mappings.size() == mapped_objects_.size())
{
// mappings = tmp_mappings;
return true;
}
// Find an object that has not been mapped yet.
std::map<const Object*, ColouredGraphNodeObjects*>::const_iterator ci = mapped_objects_.begin();
for (unsigned int i = 0; i < rhs_object_mappings.size(); ++i)
{
++ci;
}
const ColouredGraphNodeObjects* lhs_object = (*ci).second;
// Find a set of objects that have the same number of objects and the same references.
for (std::map<const Object*, ColouredGraphNodeObjects*>::const_iterator ci = other.mapped_objects_.begin(); ci != other.mapped_objects_.end(); ++ci)
{
const ColouredGraphNodeObjects* rhs_object = (*ci).second;
if (rhs_object_mappings.count(rhs_object) != 0)
{
continue;
}
if (lhs_object->getObjects().size() != rhs_object->getObjects().size())
{
continue;
}
if (lhs_object->isSymmetricalTo(*rhs_object))
{
//std::set<const ColouredGraphNodeObjects*> new_lhs_object_mappings(lhs_object_mappings);
//new_lhs_object_mappings.insert(lhs_object);
std::set<const ColouredGraphNodeObjects*> new_rhs_object_mappings(rhs_object_mappings);
new_rhs_object_mappings.insert(rhs_object);
//std::map<const ColouredGraphNodeObjects*, const ColouredGraphNodeObjects*> new_mappings(tmp_mappings);
//new_mappings[lhs_object] = rhs_object;
//if (findSymmetry(new_lhs_object_mappings, new_rhs_object_mappings, other, mappings, new_mappings))
if (findSymmetry(new_rhs_object_mappings, other))
{
return true;
}
}
}
return false;
}
std::ostream& operator<<(std::ostream& os, const ColouredGraph& coloured_graph)
{
for (std::map<const Object*, ColouredGraphNodeObjects*>::const_iterator ci = coloured_graph.mapped_objects_.begin(); ci != coloured_graph.mapped_objects_.end(); ++ci)
{
ColouredGraphNodeObjects* cgno = (*ci).second;
os << "{";
for (std::vector<const Object*>::const_iterator ci = cgno->getObjects().begin(); ci != cgno->getObjects().end(); ++ci)
{
os << **ci << ", ";
}
os <<"}";
os << std::endl << "Predicates: " << std::endl;
for (std::vector<const ColouredGraphNodePredicates*>::const_iterator ci = cgno->getPredicateEdges().begin(); ci != cgno->getPredicateEdges().end(); ++ci)
{
const ColouredGraphNodePredicates* cgnp = *ci;
os << "* "<< cgnp->getPredicate() << "(" << cgnp->getInvariables() << ")" << std::endl;
os << "{";
for (std::vector<const Object*>::const_iterator ci = cgnp->getObjects().begin(); ci != cgnp->getObjects().end(); ++ci)
{
os << **ci << ",";
}
os << "}" << std::endl;
}
os << std::endl << "Goals: " << std::endl;
for (std::vector<const ColouredGraphNodeGoal*>::const_iterator ci = cgno->getGoalEdges().begin(); ci != cgno->getGoalEdges().end(); ++ci)
{
os << "* "<< (*ci)->getGroundedAtom() << std::endl;
}
}
return os;
}
};