-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredicate_manager.cpp
400 lines (342 loc) · 12.1 KB
/
predicate_manager.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <assert.h>
#include <cctype>
#include <algorithm>
#include "VALfiles/TimSupport.h"
#include "VALfiles/TypedAnalyser.h"
#include "predicate_manager.h"
#include "action_manager.h"
#include "type_manager.h"
#include "formula.h"
///#define MYPOP_PREDICATE_COMMENTS
namespace MyPOP {
//Predicate::Predicate(const std::string& name)
Predicate::Predicate(const std::string& name, const std::vector<const Type*>& types, bool is_static)
: name_(name), types_(&types), is_static_(is_static), can_substitute_(NULL)
{
}
Predicate::~Predicate()
{
delete types_;
if (can_substitute_ != NULL)
{
delete[] can_substitute_;
}
}
void Predicate::makeStatic(bool make_static)
{
if (make_static == is_static_)
return;
is_static_ = make_static;
// std::cout << "Make static: " << *this << " : " << make_static << std::endl;
}
bool Predicate::canSubstitute(const Predicate& predicate) const
{
if (can_substitute_ == NULL)
{
if (predicate.getName() != name_)
return false;
if (predicate.getArity() != getArity())
return false;
// Check if the types match up.
for (unsigned int i = 0; i < getArity(); i++)
{
const Type* this_type = (*types_)[i];
const Type* other_type = (*predicate.types_)[i];
if (this_type != other_type && !this_type->isSupertypeOf(*other_type))
{
return false;
}
}
return true;
}
else
{
return can_substitute_[predicate.getId()];
}
}
bool Predicate::operator==(const Predicate& predicate) const
{
if (predicate.getName() != name_)
return false;
if (predicate.getArity() != getArity())
return false;
// Check if the types match up.
for (unsigned int i = 0; i < getArity(); i++)
{
const Type* this_type = (*types_)[i];
const Type* other_type = (*predicate.types_)[i];
if (this_type != other_type)
return false;
}
return true;
}
bool Predicate::operator!=(const Predicate& predicate) const
{
return !(predicate == *this);
}
void Predicate::initCache(const std::vector<Predicate*>& all_predicates)
{
bool* can_substitute_tmp = new bool[all_predicates.size()];
memset(can_substitute_tmp, false, sizeof(bool) * all_predicates.size());
for (std::vector<Predicate*>::const_iterator ci = all_predicates.begin(); ci != all_predicates.end(); ci++)
{
unsigned int index = std::distance(all_predicates.begin(), ci);
can_substitute_tmp[index] = canSubstitute(**ci);
}
can_substitute_ = can_substitute_tmp;
}
std::ostream& operator<<(std::ostream& os, const Predicate& predicate)
{
os << "(" << predicate.name_;
for (std::vector<const Type*>::const_iterator ci = predicate.types_->begin(); ci != predicate.types_->end(); ci++)
{
os << " " << (*ci)->getName();
}
os << ")";
if (predicate.is_static_)
{
os << "[static]";
}
return os;
}
PredicateManager::PredicateManager(const TypeManager& type_manager)
: type_manager_(&type_manager)
{
}
PredicateManager::~PredicateManager()
{
/* for (std::map<std::string, std::vector<const Type*>* >::iterator i = general_predicates_.begin(); i != general_predicates_.end(); i++)
{
std::string name = (*i).first;
if (getGeneralPredicate(name) != NULL)
{
delete (*i).second;
}
}*/
}
void PredicateManager::processPredicates(const VAL::pred_decl_list& predicates)
{
/**
* Prior to calling this function, TIM analysis is done on the domain. To retrieve this information
* we need to cast the predicate symbol of the VAL::pred_decl instances into the TIM equivalent one.
* Than we can proceed with detecting its properties.
* A predicate symbol can contain more than a single TIM analysis as the predicate might have different
* behavious based on the type of its variables. Therefor we construct a separate predicate for every
* possible combination.
* Note: We can reduce this, if we know that the behaviour is equal, but we leave this for future work.
*/
for (VAL::pred_decl_list::const_iterator ci = predicates.begin(); ci != predicates.end(); ci++)
{
VAL::pred_decl* predicate_declaration = *ci;
VAL::holding_pred_symbol* hps = HPS(predicate_declaration->getPred());
const std::string& raw_predicate_name = hps->getName();
std::string predicate_name(raw_predicate_name);
std::transform(raw_predicate_name.begin(), raw_predicate_name.end(), predicate_name.begin(), (int(*)(int))std::tolower);
assert (raw_predicate_name.size() > 0);
assert (predicate_name.size() > 0);
for (VAL::holding_pred_symbol::PIt i = hps->pBegin();i != hps->pEnd();++i)
{
const TIM::TIMpredSymbol* tps = static_cast<const TIM::TIMpredSymbol*>(*i);
// tps->write(std::cout);
// std::cout << "\nIs definitely static " << tps->isDefinitelyStatic() << tps->isStatic() << std::endl;
// std::cout << ", ";
// Build a list of all types this predicate holds.
std::vector<const Type*>* types = new std::vector<const Type*>();
for (VAL::Types::const_iterator tim_pred_ci = tps->tcBegin(); tim_pred_ci != tps->tcEnd(); tim_pred_ci++)
{
const VAL::pddl_typed_symbol* pts = *tim_pred_ci;
const Type* type = type_manager_->getType(pts->type->getName());
assert(type != NULL);
types->push_back(type);
}
// Check if this predicate is static.
bool is_static = tps->isDefinitelyStatic();
Predicate* predicate = new Predicate(predicate_name, *types, is_static);
// Store this to our table.
predicate_map_[std::make_pair(predicate_name, *types)] = predicate;
addManagableObject(predicate);
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "Predicate: " << *predicate << std::endl;
#endif
// Find the most generalised types.
std::vector<const Type*>* general_types = NULL;
std::map<std::string, std::vector<const Type*>* >::const_iterator type_ci = general_predicates_.find(predicate_name);
if (type_ci == general_predicates_.end())
{
assert (predicate_name.size() > 0);
general_types = new std::vector<const Type*>();
general_predicates_[predicate_name] = general_types;
}
else
{
general_types = (*type_ci).second;
}
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "Find super types for: " << predicate_name << std::endl;
#endif
if (general_types->size() == 0)
{
for (unsigned int i = 0; i < types->size(); i++)
{
const Type* current_type = (*types)[i];
general_types->push_back(current_type);
}
}
else
{
for (unsigned int i = 0; i < types->size(); i++)
{
const Type* current_type = (*types)[i];
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "Check " << i << "th type" << std::endl;
std::cout << *current_type << std::endl;
std::cout << " v.s. current general type: " << std::endl;
std::cout << *(*general_types)[i] << std::endl;
#endif
if ((*general_types)[i]->isSubtypeOf(*current_type))
{
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "- " << *(*general_types)[i] << " is a subtype of " << *current_type << std::endl;
#endif
(*general_types)[i] = current_type;
}
else if ((*general_types)[i] != current_type && !current_type->isSubtypeOf(*(*general_types)[i]))
{
// If this type is not the same nor the subtype, we must find for the supertype both are members of.
const Type* super_type = (*general_types)[i]->getSupertype();
///std::cout << "The super type of the general type " << *(*general_types)[i] << " is " << *super_type << std::endl;
while (super_type != NULL && !current_type->isSubtypeOf(*super_type) && current_type != super_type)
{
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "- " << "Is: " << *super_type << " a super type of " << *current_type << "?" << std::endl;
#endif
super_type = super_type->getSupertype();
#ifdef MYPOP_PREDICATE_COMMENTS
if (super_type != NULL)
{
std::cout << "- New super type: " << *super_type << std::endl;
}
else
{
std::cout << "- No supertype found!" << std::endl;
}
#endif
}
assert (super_type != NULL);
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "- " << "Super type!!!" << *super_type << std::endl;
#endif
(*general_types)[i] = super_type;
}
else
{
#ifdef MYPOP_PREDICATE_COMMENTS
std::cout << "- The same." << std::endl;
#endif
}
}
}
#ifdef MYPOP_PREDICATE_COMMENTS
if (getGeneralPredicate(predicate_name) != NULL)
{
std::cout << "Generalised types for predicate: " << *predicate << ": " << *getGeneralPredicate(predicate_name) << std::endl;
}
#endif
}
}
// Make sure all the generalised predicates have been created.
for (std::map<std::string, std::vector<const Type*>* >::const_iterator ci = general_predicates_.begin(); ci != general_predicates_.end(); ci++)
{
std::string name = (*ci).first;
assert (name.size() > 0);
if (getGeneralPredicate(name) == NULL)
{
assert (predicate_map_.count(std::make_pair(name, *(*ci).second)) == 0);
Predicate* predicate = new Predicate(name, *(*ci).second, false);
predicate_map_[std::make_pair(name, *(*ci).second)] = predicate;
addManagableObject(predicate);
}
else if (getPredicate(name, *(*ci).second) == NULL)
{
Predicate* predicate = new Predicate(name, *(*ci).second, false);
addManagableObject(predicate);
}
}
}
void PredicateManager::checkStaticPredicates(const ActionManager& action_manager)
{
for (std::map<std::pair<std::string, std::vector<const Type*> >, Predicate*>::const_iterator ci = predicate_map_.begin(); ci != predicate_map_.end(); ci++)
{
Predicate* predicate = (*ci).second;
// Check if there exists an operator who affects this predicate.
// NOTE: We do not check if this operator can be applied in the current domain, leave that for another time :).
bool appears_in_effects = false;
for (std::vector<Action*>::const_iterator ci = action_manager.getManagableObjects().begin(); !appears_in_effects && ci != action_manager.getManagableObjects().end(); ci++)
{
const Action* action = *ci;
for (std::vector<const Atom*>::const_iterator ci = action->getEffects().begin(); ci != action->getEffects().end(); ci++)
{
const Atom* effect = *ci;
// Check if the effect can be linked to the predicate.
if (effect->getPredicate().getName() != predicate->getName())
{
continue;
}
// Compare types, if the effect affects any subtype of the predicates the predicate cannot be static.
bool types_are_compatible = true;
for (unsigned int i = 0; i < predicate->getTypes().size(); i++)
{
const Type* predicate_type = predicate->getTypes()[i];
const Type* effect_type = effect->getPredicate().getTypes()[i];
if (!predicate_type->isEqual(*effect_type) && !effect_type->isSubtypeOf(*predicate_type) && !predicate_type->isSubtypeOf(*effect_type))
{
types_are_compatible = false;
break;
}
}
if (types_are_compatible)
{
// std::cout << "Predicate: " << *predicate << " is affected by ";
// effect->print(std::cout);
// std::cout << std::endl;
appears_in_effects = true;
break;
}
}
}
predicate->makeStatic(!appears_in_effects);
}
}
const Predicate* PredicateManager::getPredicate(const std::string& name, const std::vector<const Type*>& types) const
{
// std::cout << "Find predicate " << name << " ";
// for (std::vector<const Type*>::const_iterator ci = types.begin(); ci != types.end(); ci++)
// {
// std::cout << **ci << ", ";
// }
assert (name.size() > 0);
// std::cout << std::endl;
std::string lower_case_name(name);
std::transform(name.begin(), name.end(), lower_case_name.begin(), (int(*)(int))std::tolower);
map<std::pair<std::string, std::vector<const Type*> >, Predicate*>::const_iterator ci = predicate_map_.find(std::make_pair(lower_case_name, types));
if (ci == predicate_map_.end())
{
return NULL;
//assert (false);
}
return (*ci).second;
}
const Predicate* PredicateManager::getGeneralPredicate(const std::string& name) const
{
// std::cout << "Find general predicate " << name << std::endl;
std::string predicate_name(name);
std::transform(name.begin(), name.end(), predicate_name.begin(), (int(*)(int))std::tolower);
// std::cout << name << " -> " << predicate_name << std::endl;
std::map<std::string, std::vector<const Type*>* >::const_iterator type_ci = general_predicates_.find(predicate_name);
if (type_ci == general_predicates_.end())
{
return NULL;
}
return getPredicate(predicate_name, *(*type_ci).second);
}
}