-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphGeneration.py
404 lines (331 loc) · 13.1 KB
/
GraphGeneration.py
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
401
402
403
404
from util import *
from logical_classes import *
def expand(objects, state, domain):
"""Finds all the possible actions that can be taken
from the current state.
Args:
state: state Dictionary containing the information of the state.
domain: The representation of the domain file with all of the actions and predicates.
Returns:
list of tuples, that are the action name with the parameters and the state that is reach with
that action taken
"""
actions = domain['actions']
# for act in actions:
# print(act, "\n")
possible_actions = get_possible_actions(state, actions)
# print(objects)
effect_list = []
for key in possible_actions:
for possibility in possible_actions[key]:
# print("possibility:")
# print(key, effect_to_string(possibility), "\n")
# print(len(possibility), len(get_domain_action(actions, key).parameters))
valid = True
for item in possibility:
if possibility.count(item) > 1:
valid = False
break
if valid:
next_effect = compute_effect(possibility, get_domain_action(actions, key))
if contains_unbound_parameters(next_effect[0]):
additional_effects = compute_additional_effects(objects, next_effect[0][1:], next_effect, get_domain_action(actions, key))
for additional_effect in additional_effects:
effect_list.append(additional_effect)
else:
effect_list.append(compute_effect(possibility, get_domain_action(actions, key)))
# for effect in effect_list:
# print(action_params_to_string(effect[0]))
state_list = generate_new_states(state, effect_list)
# print("ORIGINAL STATE")
# print(state_to_string(state))
# print()
# for state in state_list:
# print("NEW STATE:")
# print(action_params_to_string(state[0]) + ": \n" + state_to_string(state[1]))
# print()
# print(len(state_list))
# print(len(objects))
return state_list
#Get Binding Methods
def get_possible_actions(predicates, actions):
action_dict = dict()
for act in actions:
# print("\n", act.name)
precondition = act.precondition
filtered_predicates = filter_predicates(predicates, precondition)
bindings_list = get_bindings(filtered_predicates, precondition)
# for binding in bindings_list:
# print(binding)
# print()
matched = match_conditions(precondition, bindings_list, predicates)
# print(act.name)
# print(effect_to_string(act.precondition))
# for match in matched:
# print(effect_to_string(match))
# print()
action_dict[act.name] = matched
return action_dict
def get_bindings(predicates, precondition):
total_list = []
for condition in precondition:
bindings_list = ask(condition, predicates)
if bindings_list != False:
total_list.append(bindings_list)
# print()
# print(condition.name)
# print(bindings_list)
# print()
# print(total_list)
return total_list
def match_conditions(precondition, bindings_list, predicates):
# print("\nPRECONDITION:" , effect_to_string(precondition))
if len(bindings_list) == 1:
new_preconditions = []
last_condition = precondition[0]
# print(len(precondition))
if not contains_variables(last_condition):
contained = get_predicate(last_condition, predicates)
# print("BASE CASE:", contained, "Condition", last_condition)
# print("\nInstantiated Condition: ", effect_to_string([last_condition]))
valid = True
if contained == False:
valid = not last_condition.value
if valid:
return [precondition]
else:
for binding in bindings_list[0]:
if contains_variables(last_condition):
last_condition = instantiate(last_condition, binding, last_condition.value)
else:
last_condition = last_condition
contained = get_predicate(last_condition, predicates)
# print("BASE CASE:", contained, "Condition", last_condition)
# print("\nInstantiated Condition: ", effect_to_string([last_condition]))
valid = True
if contained == False:
valid = not last_condition.value
if valid and (not contains_variables(last_condition)):
new_preconditions.append([last_condition])
if new_preconditions != []:
return new_preconditions
else:
return False #False means no bindings
possible_conditions = []
for binding in bindings_list[0]:
if contains_variables(precondition[0]):
first_precondition = instantiate(precondition[0], binding, precondition[0].value)
else:
first_precondition = precondition[0]
contained = get_predicate(first_precondition, predicates)
# print("RECURSIVE:", contained, "Condition", first_precondition)
valid = True
if contained == False:
valid = not first_precondition.value
if valid and (not contains_variables(first_precondition)):
new_precondition = []
for condition in precondition[1:]:
new_precondition.append(instantiate(condition, binding, condition.value))
# print("\nInstantiated Condition: ", effect_to_string(new_precondition))
bound_precondition_list = match_conditions(new_precondition, bindings_list[1:], predicates)
if bound_precondition_list != False:
for bound_precondition in bound_precondition_list:
# print(bound_precondition)
bound_precondition.insert(0, first_precondition)
if bound_precondition not in possible_conditions:
possible_conditions.append(bound_precondition)
return possible_conditions
def ask(condition, predicates):
bindingsList = ListOfBindings()
for pred in predicates:
binding = match(pred, condition)
if binding != False:
bindingsList.add_bindings(binding)
if bindingsList.list_of_bindings != []:
return bindingsList
else:
return False
# Effect Methods
def compute_effect(parameters, action, bindings = False):
effect = action.effect
if not bindings:
bindings = Bindings()
precondition = action.precondition
# print(action.name)
# print("\nPARAMS", parameters)
# print("Precondition", action.precondition)
for i in range(len(precondition)):
for j in range(len(precondition[i].args)):
# print("PRECONDITION:", precondition[i])
# print("PARAMETERS:" , parameters[i])
bindings.add_binding(precondition[i].args[j].term, parameters[i].args[j].term)
# print("New Action")
# print("Parameters: ", parameters)
# print()
# print("Precondition: ", precondition)
# print()
# print("Bindings: ", bindings)
# print()
params = action.parameters
input = [action.name]
for param in params:
binding = bindings[param.term.element]
if binding == None:
input.append(param.term.element)
else:
input.append(bindings[param.term.element])
new_effect = []
for predicate in effect:
new_effect.append(instantiate(predicate, bindings, predicate.value))
# print("General Effect: ", effect)
# print()
# print("Computed Effect: ", new_effect)
# print()
return (input, new_effect)
def compute_additional_effects(objects, parameters, current_effect, action):
# print(current_effect)
filtered_objects = filter_objects(objects, parameters)
# print()
# print("PARAMETERS:\n", parameters)
# print("OBJECTS:\n", filtered_objects)
# print()
effect = current_effect[1]
unbound_parameters = []
for predicate in effect:
for term in predicate.args:
if is_var(term):
if term not in unbound_parameters:
unbound_parameters.append(term)
if not unbound_parameters:
return [current_effect]
else:
# print()
# print("Unbound Parameters")
# print(unbound_parameters)
# print("Objects:")
# print(filtered_objects)
# print()
possible_bindings = []
for param in unbound_parameters:
for ob in filtered_objects:
# print(ob)
bindings = Bindings()
bindings.add_binding(param.term, ob.term)
possible_bindings.append(bindings)
# print(binding)
# print()
# print()
# print(possible_bindings)
# print()
# print("Objects:\n", filtered_objects)
# print("\nEffect: \n", effect)
new_effects = []
for binding in possible_bindings:
# print("BINDING:", binding)
new_effect = []
for predicate in effect:
new_effect.append(instantiate(predicate, binding, predicate.value))
new_input = [action.name]
for term in parameters:
bind = binding[term]
if bind == None:
new_input.append(term)
else:
new_input.append(binding[term])
new_effects.append((new_input, new_effect))
new_new_effects = []
for possible_effect in new_effects:
new_new_effect_list = compute_additional_effects(filtered_objects, possible_effect[0][1:], possible_effect, action)
for new_new_effect in new_new_effect_list:
new_new_effects.append(new_new_effect)
# print()
# print(new_effects)
# print()
return new_new_effects
def generate_new_states(state, effects):
# print(state)
state_list = []
for effect in effects:
new_state = copy_state(state)
# print()
# print(effect[1])
for predicate in effect[1]:
# print(predicate)
if predicate.value and (not (predicate in state)):
new_state.append(predicate)
elif (not predicate.value) and (get_predicate(predicate, state)):
predicate.value = not predicate.value
# print(predicate)
if predicate in new_state:
new_state.remove(predicate)
predicate.value = not predicate.value
state_list.append((effect[0], new_state))
return state_list
#Helper Methods....
def get_domain_action(actions, key):
for action in actions:
if action.name == key:
return action
return None
def filter_predicates(predicates, precondition):
names = []
for condition in precondition:
names.append(condition.name)
filtered = []
for predicate in predicates:
if predicate.name in names:
filtered.append(predicate)
return filtered
def filter_objects(objects, parameters):
filtered_objects = []
for ob in objects:
if ob.term.element not in parameters:
filtered_objects.append(ob)
return filtered_objects
def get_predicate(predicate, predicates):
for pred in predicates:
if pred.name == predicate.name and pred.args == predicate.args:
return pred
return False
def contains_unbound_parameters(parameters):
unbound_parameters = False
for term in parameters:
unbound_parameters = ("?" in term)
if unbound_parameters:
break
return unbound_parameters
def contains_variables(condition):
# print("\nCondition", condition)
for term in condition.args:
if is_var(term):
return True
return False
def copy_state(state):
new_state = []
for predicate in state:
new_state.append(predicate)
return new_state
def state_to_string(state):
string = ""
for predicate in state:
string += predicate_to_string(predicate) + "\n"
return string
def action_params_to_string(effect):
string = "(" + str(effect[0])
for predicate in effect[1:]:
string += " " + str(predicate)
return string + ")"
def effect_to_string(effect):
string = ""
for predicate in effect:
if not predicate.value:
string += "(not " + predicate_to_string(predicate) + ") "
else:
string += predicate_to_string(predicate) + " "
return string.strip()
def predicate_to_string(predicate):
string = "(" + predicate.name
for arg in predicate.args:
string += " " + arg.term.element
string += ")"
return string