|
7 | 7 |
|
8 | 8 | class PDLL:
|
9 | 9 | """
|
10 |
| - PDLL used to deine a search problem |
| 10 | + PDLL used to define a search problem |
11 | 11 | It stores states in a knowledge base consisting of first order logic statements
|
12 | 12 | The conjunction of these logical statements completely define a state
|
13 | 13 | """
|
@@ -61,7 +61,11 @@ def __call__(self, kb, args):
|
61 | 61 |
|
62 | 62 | def substitute(self, e, args):
|
63 | 63 | """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] |
65 | 69 | return Expr(e.op, *new_args)
|
66 | 70 |
|
67 | 71 | def check_precond(self, kb, args):
|
@@ -123,12 +127,51 @@ def goal_test(kb):
|
123 | 127 | effect_rem = [expr("In(c, p)")]
|
124 | 128 | unload = Action(expr("Unload(c, p, a)"), [precond_pos, precond_neg], [effect_add, effect_rem])
|
125 | 129 |
|
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 |
128 | 132 | precond_pos = [expr("At(p, f)"), expr("Plane(p)"), expr("Airport(f)"), expr("Airport(to)")]
|
129 | 133 | precond_neg = []
|
130 | 134 | effect_add = [expr("At(p, to)")]
|
131 | 135 | effect_rem = [expr("At(p, f)")]
|
132 | 136 | fly = Action(expr("Fly(p, f, to)"), [precond_pos, precond_neg], [effect_add, effect_rem])
|
133 | 137 |
|
134 | 138 | 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