/
my_air_cargo_problems.py
377 lines (347 loc) · 14.4 KB
/
my_air_cargo_problems.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
from aimacode.logic import PropKB
from aimacode.planning import Action
from aimacode.search import (
Node, Problem,
)
from aimacode.utils import expr
from lp_utils import (
FluentState, encode_state, decode_state,
)
from my_planning_graph import PlanningGraph
from functools import lru_cache
class AirCargoProblem(Problem):
def __init__(self, cargos, planes, airports, initial: FluentState, goal: list):
"""
:param cargos: list of str
cargos in the problem
:param planes: list of str
planes in the problem
:param airports: list of str
airports in the problem
:param initial: FluentState object
positive and negative literal fluents (as expr) describing initial state
:param goal: list of expr
literal fluents required for goal test
"""
self.state_map = initial.pos + initial.neg
self.initial_state_TF = encode_state(initial, self.state_map)
Problem.__init__(self, self.initial_state_TF, goal=goal)
self.cargos = cargos
self.planes = planes
self.airports = airports
self.actions_list = self.get_actions()
def get_actions(self):
"""
This method creates concrete actions (no variables) for all actions in the problem
domain action schema and turns them into complete Action objects as defined in the
aimacode.planning module. It is computationally expensive to call this method directly;
however, it is called in the constructor and the results cached in the `actions_list` property.
Returns:
----------
list<Action>
list of Action objects
"""
# TODO create concrete Action objects based on the domain action schema for: Load, Unload, and Fly
# concrete actions definition: specific literal action that does not include variables as with the schema
# for example, the action schema 'Load(c, p, a)' can represent the concrete actions 'Load(C1, P1, SFO)'
# or 'Load(C2, P2, JFK)'. The actions for the planning problem must be concrete because the problems in
# forward search and Planning Graphs must use Propositional Logic
def load_actions():
"""Create all concrete Load actions and return a list
:return: list of Action objects
"""
loads = []
# create all load ground actions from the domain Load action
for a in self.airports:
for p in self.planes:
for c in self.cargos:
precond_pos = [
expr("At({}, {})".format(c, a)), # cargo at airport
expr("At({}, {})".format(p, a)) # plane at airport
]
precond_neg = []
effect_add = [expr("In({}, {})".format(c, p))] # In Plane
effect_rem = [expr("At({}, {})".format(c, a))] # cargo NOT At airport
load = Action(expr("Load({}, {}, {})".format(c, p, a)),
[precond_pos, precond_neg],
[effect_add, effect_rem])
loads.append(load)
return loads
def unload_actions():
"""Create all concrete Unload actions and return a list
:return: list of Action objects
"""
unloads = []
# create all Unload ground actions from the domain Unload action
for a in self.airports:
for p in self.planes:
for c in self.cargos:
precond_pos = [
expr("In({}, {})".format(c, p)), # cargo in plane
expr("At({}, {})".format(p, a)) # plane at airport
]
precond_neg = []
effect_add = [expr("At({}, {})".format(c, a))] # Cargo at Airport
effect_rem = [expr("In({}, {})".format(c, p))] # Cargo NOT in plane
unload = Action(expr("Unload({}, {}, {})".format(c, p, a)),
[precond_pos, precond_neg],
[effect_add, effect_rem])
unloads.append(unload)
return unloads
def fly_actions():
"""Create all concrete Fly actions and return a list
:return: list of Action objects
"""
flys = []
for fr in self.airports:
for to in self.airports:
if fr != to:
for p in self.planes:
precond_pos = [expr("At({}, {})".format(p, fr)),
]
precond_neg = []
effect_add = [expr("At({}, {})".format(p, to))]
effect_rem = [expr("At({}, {})".format(p, fr))]
fly = Action(expr("Fly({}, {}, {})".format(p, fr, to)),
[precond_pos, precond_neg],
[effect_add, effect_rem])
flys.append(fly)
return flys
return load_actions() + unload_actions() + fly_actions()
def actions(self, state: str) -> list:
""" Return the actions that can be executed in the given state.
:param state: str
state represented as T/F string of mapped fluents (state variables)
e.g. 'FTTTFF'
:return: list of Action objects
"""
possible_actions = []
kb = PropKB()
decoded = decode_state(state, self.state_map)
# if we feed decoded.sentence() in, the clauses will also
# include all the negatives like At(C1, SFO) but also ~At(C1, JFK)
# approach adapted from example_have_cake.py
kb.tell(decoded.pos_sentence())
for action in self.actions_list:
is_possible = True
# loop through the action's POSITIVE precondition clauses
for pos_clause in action.precond_pos:
if pos_clause not in kb.clauses:
is_possible = False
# loop through the action's NEGATIVE precondition clauses
for neg_clause in action.precond_neg:
if neg_clause in kb.clauses:
is_possible = False
if is_possible:
possible_actions.append(action)
return possible_actions
def result(self, state: str, action: Action):
""" Return the state that results from executing the given
action in the given state. The action must be one of
self.actions(state).
:param state: state entering node
:param action: Action applied
:return: resulting state after action
"""
# approach adapted from example_have_cake.py
new_state = FluentState([], [])
old_state = decode_state(state, self.state_map)
for fluent in old_state.pos:
# if the action did not remove old positive fluent, keep it
if fluent not in action.effect_rem:
new_state.pos.append(fluent)
for fluent in action.effect_add:
# if we haven't added the resulting fluent to new state, add it
if fluent not in new_state.pos:
new_state.pos.append(fluent)
for fluent in old_state.neg:
# if the old negative fluent hasn't been added, keep it
if fluent not in action.effect_add:
new_state.neg.append(fluent)
for fluent in action.effect_rem:
# if the fluent being removed is not already in the new stat's neg list, add
if fluent not in new_state.neg:
new_state.neg.append(fluent)
return encode_state(new_state, self.state_map)
def goal_test(self, state: str) -> bool:
""" Test the state to see if goal is reached
:param state: str representing state
:return: bool
"""
kb = PropKB()
kb.tell(decode_state(state, self.state_map).pos_sentence())
for clause in self.goal:
if clause not in kb.clauses:
return False
return True
def h_1(self, node: Node):
# note that this is not a true heuristic
h_const = 1
return h_const
@lru_cache(maxsize=8192)
def h_pg_levelsum(self, node: Node):
"""This heuristic uses a planning graph representation of the problem
state space to estimate the sum of all actions that must be carried
out from the current state in order to satisfy each individual goal
condition.
"""
pg = PlanningGraph(self, node.state)
pg_levelsum = pg.h_levelsum()
return pg_levelsum
@lru_cache(maxsize=8192)
def h_ignore_preconditions(self, node: Node):
"""This heuristic estimates the minimum number of actions that must be
carried out from the current state in order to satisfy all of the goal
conditions by ignoring the preconditions required for an action to be
executed.
"""
kb = PropKB()
decoded = decode_state(node.state, self.state_map)
kb.tell(decoded.pos_sentence())
current_clauses = kb.clauses
# Russell-Norvig 382: "Almost implies the number of steps required to solve a relaxed problem
# is the number of unsatisfied goals"
# following gets the number of goal clauses not satisfied,
# works for independent goals
# ref: https://ai-nd.slack.com/archives/C3TPR3RCG/p1502854124000011
unsatified_goals = [goal_clause for goal_clause in self.goal if goal_clause not in current_clauses]
return len(unsatified_goals)
# Init(At(C1, SFO) ∧ At(C2, JFK)
# ∧ At(P1, SFO) ∧ At(P2, JFK)
# ∧ Cargo(C1) ∧ Cargo(C2)
# ∧ Plane(P1) ∧ Plane(P2)
# ∧ Airport(JFK) ∧ Airport(SFO))
# Goal(At(C1, JFK) ∧ At(C2, SFO))
def air_cargo_p1() -> AirCargoProblem:
cargos = ['C1', 'C2']
planes = ['P1', 'P2']
airports = ['JFK', 'SFO']
pos = [expr('At(C1, SFO)'),
expr('At(C2, JFK)'),
expr('At(P1, SFO)'),
expr('At(P2, JFK)'),
]
neg = [expr('At(C2, SFO)'),
expr('In(C2, P1)'),
expr('In(C2, P2)'),
expr('At(C1, JFK)'),
expr('In(C1, P1)'),
expr('In(C1, P2)'),
expr('At(P1, JFK)'),
expr('At(P2, SFO)'),
]
init = FluentState(pos, neg)
goal = [expr('At(C1, JFK)'),
expr('At(C2, SFO)'),
]
return AirCargoProblem(cargos, planes, airports, init, goal)
#Init(At(C1, SFO) ∧ At(C2, JFK) ∧ At(C3, ATL)
# ∧ At(P1, SFO) ∧ At(P2, JFK) ∧ At(P3, ATL)
# ∧ Cargo(C1) ∧ Cargo(C2) ∧ Cargo(C3)
# ∧ Plane(P1) ∧ Plane(P2) ∧ Plane(P3)
# ∧ Airport(JFK) ∧ Airport(SFO) ∧ Airport(ATL))
# Goal(At(C1, JFK) ∧ At(C2, SFO) ∧ At(C3, SFO))
def air_cargo_p2() -> AirCargoProblem:
cargos = ['C1', 'C2', 'C3']
planes = ['P1', 'P2', 'P3']
airports = ['JFK', 'SFO', 'ATL']
pos = [expr('At(C1, SFO)'),
expr('At(C2, JFK)'),
expr('At(C3, ATL)'),
expr('At(P1, SFO)'),
expr('At(P2, JFK)'),
expr('At(P3, ATL)')
]
neg = [# NOT states of C1
expr('At(C1, JFK)'),
expr('At(C1, ATL)'),
expr('In(C1, P1)'),
expr('In(C1, P2)'),
expr('In(C1, P3)'),
# NOT states of C2
expr('At(C2, SFO)'),
expr('At(C2, ATL)'),
expr('In(C2, P1)'),
expr('In(C2, P2)'),
expr('In(C2, P3)'),
# NOT states of C3
expr('At(C3, JFK)'),
expr('At(C3, SFO)'),
expr('In(C3, P1)'),
expr('In(C3, P2)'),
expr('In(C3, P3)'),
# NOT states for P1
expr('At(P1, JFK)'),
expr('At(P1, ATL)'),
# NOT states for P2
expr('At(P2, SFO)'),
expr('At(P2, ATL)'),
# NOT states for P3
expr('At(P3, JFK)'),
expr('At(P3, SFO)')
]
init = FluentState(pos, neg)
# Goal(At(C1, JFK) ∧ At(C2, SFO) ∧ At(C3, SFO))
goal = [expr('At(C1, JFK)'),
expr('At(C2, SFO)'),
expr('At(C3, SFO)')
]
return AirCargoProblem(cargos, planes, airports, init, goal)
# Init(At(C1, SFO) ∧ At(C2, JFK) ∧ At(C3, ATL) ∧ At(C4, ORD)
# ∧ At(P1, SFO) ∧ At(P2, JFK)
# ∧ Cargo(C1) ∧ Cargo(C2) ∧ Cargo(C3) ∧ Cargo(C4)
# ∧ Plane(P1) ∧ Plane(P2)
# ∧ Airport(JFK) ∧ Airport(SFO) ∧ Airport(ATL) ∧ Airport(ORD))
# Goal(At(C1, JFK) ∧ At(C3, JFK) ∧ At(C2, SFO) ∧ At(C4, SFO))
def air_cargo_p3() -> AirCargoProblem:
cargos = ['C1', 'C2', 'C3','C4']
planes = ['P1', 'P2']
airports = ['SFO','JFK','ATL','ORD']
pos = [expr('At(C1, SFO)'),
expr('At(C2, JFK)'),
expr('At(C3, ATL)'),
expr('At(C4, ORD)'),
expr('At(P1, SFO)'),
expr('At(P2, JFK)')
]
neg = [# NOT states of C1
expr('At(C1, JFK)'),
expr('At(C1, ATL)'),
expr('At(C1, ORD)'),
expr('In(C1, P1)'),
expr('In(C1, P2)'),
# NOT states of C2
expr('At(C2, SFO)'),
expr('At(C2, ATL)'),
expr('At(C2, ORD)'),
expr('In(C2, P1)'),
expr('In(C2, P2)'),
# NOT states of C3
expr('At(C3, SFO)'),
expr('At(C3, JFK)'),
expr('At(C3, ORD)'),
expr('In(C3, P1)'),
expr('In(C3, P2)'),
# NOT states of C4
expr('At(C4, SFO)'),
expr('At(C4, JFK)'),
expr('At(C4, ATL)'),
expr('In(C4, P1)'),
expr('In(C4, P2)'),
# NOT states for P1
expr('At(P1, JFK)'),
expr('At(P1, ATL)'),
expr('At(P1, ORD)'),
# NOT states for P2
expr('At(P2, SFO)'),
expr('At(P2, ATL)'),
expr('At(P2, ORD)')
]
init = FluentState(pos, neg)
# Goal(At(C1, JFK) ∧ At(C3, JFK) ∧ At(C2, SFO) ∧ At(C4, SFO))
goal = [expr('At(C1, JFK)'),
expr('At(C3, JFK)'),
expr('At(C2, SFO)'),
expr('At(C4, SFO)')
]
return AirCargoProblem(cargos, planes, airports, init, goal)