diff --git a/planning.py b/planning.py index 60247a7bc..92f4f773e 100644 --- a/planning.py +++ b/planning.py @@ -7,7 +7,7 @@ class PDLL: """ - PDLL used to deine a search problem + PDLL used to define a search problem It stores states in a knowledge base consisting of first order logic statements The conjunction of these logical statements completely define a state """ @@ -61,7 +61,11 @@ def __call__(self, kb, args): def substitute(self, e, args): """Replaces variables in expression with their respective Propostional symbol""" - new_args = [args[i] for x in e.args for i in range(len(self.args)) if self.args[i] == x] + new_args = list(e.args) + for num, x in enumerate(e.args): + for i in range(len(self.args)): + if self.args[i] == x: + new_args[num] = args[i] return Expr(e.op, *new_args) def check_precond(self, kb, args): @@ -123,8 +127,8 @@ def goal_test(kb): effect_rem = [expr("In(c, p)")] unload = Action(expr("Unload(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem]) - # Load - # Used used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function + # Fly + # Used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function precond_pos = [expr("At(p, f)"), expr("Plane(p)"), expr("Airport(f)"), expr("Airport(to)")] precond_neg = [] effect_add = [expr("At(p, to)")] @@ -132,3 +136,42 @@ def goal_test(kb): fly = Action(expr("Fly(p, f, to)"), [precond_pos, precond_neg], [effect_add, effect_rem]) return PDLL(init, [load, unload, fly], goal_test) + + +def spare_tire(): + init = [expr('Tire(Flat)'), + expr('Tire(Spare)'), + expr('At(Flat, Axle)'), + expr('At(Spare, Trunk)')] + + def goal_test(kb): + required = [expr('At(Spare, Axle)'), expr('At(Flat, Ground)')] + for q in required: + if kb.ask(q) is False: + return False + return True + + ##Actions + #Remove + precond_pos = [expr("At(obj, loc)")] + precond_neg = [] + effect_add = [expr("At(obj, Ground)")] + effect_rem = [expr("At(obj, loc)")] + remove = Action(expr("Remove(obj, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem]) + + #PutOn + precond_pos = [expr("Tire(t)"), expr("At(t, Ground)")] + precond_neg = [expr("At(Flat, Axle)")] + effect_add = [expr("At(t, Axle)")] + effect_rem = [expr("At(t, Ground)")] + put_on = Action(expr("PutOn(t, Axle)"), [precond_pos, precond_neg], [effect_add, effect_rem]) + + #LeaveOvernight + precond_pos = [] + precond_neg = [] + effect_add = [] + effect_rem = [expr("At(Spare, Ground)"), expr("At(Spare, Axle)"), expr("At(Spare, Trunk)"), + expr("At(Flat, Ground)"), expr("At(Flat, Axle)"), expr("At(Flat, Trunk)")] + leave_overnight = Action(expr("LeaveOvernight"), [precond_pos, precond_neg], [effect_add, effect_rem]) + + return PDLL(init, [remove, put_on, leave_overnight], goal_test)