Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61ef267

Browse files
opensourcewarenorvig
authored andcommitted
Planning (aimacode#253)
* Minor docstring changes * Added Spare Tire Problem * Fixed a bug in substitute method of class Action * Fixed minor typo in comment
1 parent 5574d77 commit 61ef267

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

planning.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class PDLL:
99
"""
10-
PDLL used to deine a search problem
10+
PDLL used to define a search problem
1111
It stores states in a knowledge base consisting of first order logic statements
1212
The conjunction of these logical statements completely define a state
1313
"""
@@ -61,7 +61,11 @@ def __call__(self, kb, args):
6161

6262
def substitute(self, e, args):
6363
"""Replaces variables in expression with their respective Propostional symbol"""
64-
new_args = [args[i] for x in e.args for i in range(len(self.args)) if self.args[i] == x]
64+
new_args = list(e.args)
65+
for num, x in enumerate(e.args):
66+
for i in range(len(self.args)):
67+
if self.args[i] == x:
68+
new_args[num] = args[i]
6569
return Expr(e.op, *new_args)
6670

6771
def check_precond(self, kb, args):
@@ -123,12 +127,51 @@ def goal_test(kb):
123127
effect_rem = [expr("In(c, p)")]
124128
unload = Action(expr("Unload(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem])
125129

126-
# Load
127-
# Used used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function
130+
# Fly
131+
# Used 'f' instead of 'from' because 'from' is a python keyword and expr uses eval() function
128132
precond_pos = [expr("At(p, f)"), expr("Plane(p)"), expr("Airport(f)"), expr("Airport(to)")]
129133
precond_neg = []
130134
effect_add = [expr("At(p, to)")]
131135
effect_rem = [expr("At(p, f)")]
132136
fly = Action(expr("Fly(p, f, to)"), [precond_pos, precond_neg], [effect_add, effect_rem])
133137

134138
return PDLL(init, [load, unload, fly], goal_test)
139+
140+
141+
def spare_tire():
142+
init = [expr('Tire(Flat)'),
143+
expr('Tire(Spare)'),
144+
expr('At(Flat, Axle)'),
145+
expr('At(Spare, Trunk)')]
146+
147+
def goal_test(kb):
148+
required = [expr('At(Spare, Axle)'), expr('At(Flat, Ground)')]
149+
for q in required:
150+
if kb.ask(q) is False:
151+
return False
152+
return True
153+
154+
##Actions
155+
#Remove
156+
precond_pos = [expr("At(obj, loc)")]
157+
precond_neg = []
158+
effect_add = [expr("At(obj, Ground)")]
159+
effect_rem = [expr("At(obj, loc)")]
160+
remove = Action(expr("Remove(obj, loc)"), [precond_pos, precond_neg], [effect_add, effect_rem])
161+
162+
#PutOn
163+
precond_pos = [expr("Tire(t)"), expr("At(t, Ground)")]
164+
precond_neg = [expr("At(Flat, Axle)")]
165+
effect_add = [expr("At(t, Axle)")]
166+
effect_rem = [expr("At(t, Ground)")]
167+
put_on = Action(expr("PutOn(t, Axle)"), [precond_pos, precond_neg], [effect_add, effect_rem])
168+
169+
#LeaveOvernight
170+
precond_pos = []
171+
precond_neg = []
172+
effect_add = []
173+
effect_rem = [expr("At(Spare, Ground)"), expr("At(Spare, Axle)"), expr("At(Spare, Trunk)"),
174+
expr("At(Flat, Ground)"), expr("At(Flat, Axle)"), expr("At(Flat, Trunk)")]
175+
leave_overnight = Action(expr("LeaveOvernight"), [precond_pos, precond_neg], [effect_add, effect_rem])
176+
177+
return PDLL(init, [remove, put_on, leave_overnight], goal_test)

0 commit comments

Comments
 (0)