-
Notifications
You must be signed in to change notification settings - Fork 0
/
planning.py
executable file
·614 lines (495 loc) · 18.7 KB
/
planning.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
"""
Various classes and functions to model automated planning.
Based on code from:
- https://github.com/aimacode/aima-python/blob/c587f2c429b9dec199f190c3453cd269b6b6bbd1/planning.py
- https://github.com/aimacode/aima-python/blob/c587f2c429b9dec199f190c3453cd269b6b6bbd1/utils.py
- https://github.com/aimacode/aima-python/blob/c587f2c429b9dec199f190c3453cd269b6b6bbd1/logic.py
"""
import itertools
import collections
###
### PLANNING
###
class PlanningProblem:
"""
Planning Domain Definition Language (PlanningProblem) 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 defines a state.
"""
def __init__(self, initial, goals, actions):
self.initial = self.convert(initial)
self.goals = self.convert(goals)
self.actions = actions
def convert(self, clauses):
"""Converts strings into exprs"""
if not isinstance(clauses, Expr):
if len(clauses) > 0:
clauses = expr(clauses)
else:
clauses = []
try:
clauses = conjuncts(clauses)
except AttributeError:
pass
new_clauses = []
for clause in clauses:
new_clauses.append(clause)
return new_clauses
def goal_test(self):
"""Checks if the goals have been reached"""
kb = FolKB(self.initial)
return first(fol_bc_and(kb,self.goals,{})) is not False
def act(self, action):
"""
Performs the action given as argument.
Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)')
"""
action_name = action.op
args = action.args
list_action = first(a for a in self.actions if a.name == action_name)
if list_action is None:
raise Exception("Action '{}' not found".format(action_name))
if not list_action.check_precond(self.initial, args):
raise Exception("Action '{}' pre-conditions not satisfied".format(action))
self.initial = list_action(self.initial, args).clauses
class Action:
"""
Defines an action schema using preconditions and effects.
Use this to describe actions in PlanningProblem.
action is an Expr where variables are given as arguments(args).
Precondition and effect are both lists with positive and negative literals.
Negative preconditions and effects are defined by negating a clause using the operator '~'
Example:
precond = [expr("Human(person)"), expr("Hungry(Person)"), expr("~Eaten(food)")]
effect = [expr("Eaten(food)"), expr("Hungry(person)")]
eat = Action(expr("Eat(person, food)"), precond, effect)
"""
def __init__(self, action, precond, effect):
if isinstance(action, str):
action = expr(action)
self.name = action.op
self.args = action.args
self.precond = self.convert(precond)
self.effect = self.convert(effect)
def __call__(self, kb, args):
return self.act(kb, args)
def __repr__(self):
return '{}'.format(Expr(self.name, *self.args))
def convert(self, clauses):
"""Converts strings into Exprs"""
if isinstance(clauses, Expr):
clauses = conjuncts(clauses)
elif isinstance(clauses, str):
if len(clauses) > 0:
clauses = expr(clauses)
try:
clauses = conjuncts(clauses)
except AttributeError:
pass
return clauses
def substitute(self, e, args):
"""Replaces variables in expression with their respective Propositional symbol"""
new_args = list(e.args)
for num, x in enumerate(e.args):
for i, _ in enumerate(self.args):
if self.args[i] == x:
new_args[num] = args[i]
return Expr(e.op, *new_args)
def check_precond(self, kb, args):
"""Checks if the precondition is satisfied in the current state"""
if isinstance(kb, list):
kb = FolKB(kb)
for clause in self.precond:
if clause.op == '~':
new_clause = clause.args[0];
if kb.ask(self.substitute(new_clause, args)) is not False:
return False
else:
if kb.ask(self.substitute(clause, args)) is False:
# if self.substitute(clause, args) not in kb.clauses:
return False
return True
def act(self, kb, args):
"""Executes the action on the state's knowledge base"""
if isinstance(kb, list):
kb = FolKB(kb)
if not self.check_precond(kb, args):
raise Exception('Action pre-conditions not satisfied')
for clause in self.effect:
if clause.op == '~':
new_clause = clause.args[0];
if kb.ask(self.substitute(new_clause, args)) is not False:
kb.retract(self.substitute(new_clause, args))
else:
kb.tell(self.substitute(clause, args))
return kb
###
### EXPRESSIONS
###
class Expr:
def __init__(self, op, *args):
self.op = str(op)
self.args = args
# Operator overloads
def __and__(self, rhs):
return Expr('&', self, rhs)
def __or__(self, rhs):
"""Allow both P | Q, and P |'==>'| Q."""
if isinstance(rhs, Expr):
return Expr('|', self, rhs)
else:
return PartialExpr(rhs, self)
def __invert__(self):
return Expr('~', self)
# Reverse operator overloads
def __rand__(self, lhs):
return Expr('&', lhs, self)
def __ror__(self, lhs):
return Expr('|', lhs, self)
def __call__(self, *args):
"""Call: if 'f' is a Symbol, then f(0) == Expr('f', 0)."""
if self.args:
raise ValueError('Can only do a call for a Symbol, not an Expr')
else:
return Expr(self.op, *args)
# Equality and repr
def __eq__(self, other):
"""x == y' evaluates to True or False; does not build an Expr."""
return isinstance(other, Expr) and self.op == other.op and self.args == other.args
def __hash__(self):
return hash(self.op) ^ hash(self.args)
def __repr__(self):
op = self.op
args = [str(arg) for arg in self.args]
if op.isidentifier(): # f(x) or f(x, y)
return '{}({})'.format(op, ', '.join(args)) if args else op
elif len(args) == 1: # -x or -(x + 1)
return op + args[0]
else: # (x - y)
opp = (' ' + op + ' ')
return '(' + opp.join(args) + ')'
class PartialExpr:
"""Given 'P |'==>'| Q, first form PartialExpr('==>', P), then combine with Q."""
def __init__(self, op, lhs):
self.op, self.lhs = op, lhs
def __or__(self, rhs):
return Expr(self.op, self.lhs, rhs)
def __repr__(self):
return "PartialExpr('{}', {})".format(self.op, self.lhs)
infix_ops = '==> <== <=>'.split()
def expr_handle_infix_ops(x):
"""Given a str, return a new str with ==> replaced by |'==>'|, etc.
>>> expr_handle_infix_ops('P ==> Q')
"P |'==>'| Q"
"""
for op in infix_ops:
x = x.replace(op, '|' + repr(op) + '|')
return x
def expr(x):
"""Shortcut to create an Expr. x is a str in which:
identifiers are automatically defined as Symbols.
"""
return eval(expr_handle_infix_ops(x), defaultkeydict(Symbol)) if isinstance(x, str) else x
def first(iterable, default=None):
"""Return the first element of an iterable; or default."""
return next(iter(iterable), default)
def extend(s, var, val):
"""Copy dict s and extend it by setting var to val; return copy."""
try: # Python 3.5 and later
return eval('{**s, var: val}')
except SyntaxError: # Python 3.4
s2 = s.copy()
s2[var] = val
return s2
class defaultkeydict(collections.defaultdict):
"""Like defaultdict, but the default_factory is a function of the key.
>>> d = defaultkeydict(len); d['four']
4
"""
def __missing__(self, key):
self[key] = result = self.default_factory(key)
return result
def Symbol(name):
"""A Symbol is just an Expr with no args."""
return Expr(name)
###
### LOGIC
###
class KB:
"""A knowledge base to which you can tell and ask sentences.
To create a KB, first subclass this class and implement
tell, ask_generator, and retract. Why ask_generator instead of ask?
The book is a bit vague on what ask means --
For a Propositional Logic KB, ask(P & Q) returns True or False, but for an
FOL KB, something like ask(Brother(x, y)) might return many substitutions
such as {x: Cain, y: Abel}, {x: Abel, y: Cain}, {x: George, y: Jeb}, etc.
So ask_generator generates these one at a time, and ask either returns the
first one or returns False."""
def __init__(self, sentence=None):
if sentence:
self.tell(sentence)
def tell(self, sentence):
"""Add the sentence to the KB."""
raise NotImplementedError
def ask(self, query):
"""Return a substitution that makes the query true, or, failing that, return False."""
return first(self.ask_generator(query), default=False)
def ask_generator(self, query):
"""Yield all the substitutions that make query true."""
raise NotImplementedError
def retract(self, sentence):
"""Remove sentence from the KB."""
raise NotImplementedError
def associate(op, args):
"""Given an associative op, return an expression with the same
meaning as Expr(op, *args), but flattened -- that is, with nested
instances of the same op promoted to the top level.
>>> associate('&', [(A&B),(B|C),(B&C)])
(A & B & (B | C) & B & C)
>>> associate('|', [A|(B|(C|(A&B)))])
(A | B | C | (A & B))
"""
args = dissociate(op, args)
if len(args) == 0:
return _op_identity[op]
elif len(args) == 1:
return args[0]
else:
return Expr(op, *args)
_op_identity = {'&': True, '|': False, '+': 0, '*': 1}
def dissociate(op, args):
"""Given an associative op, return a flattened list result such
that Expr(op, *result) means the same as Expr(op, *args).
>>> dissociate('&', [A & B])
[A, B]
"""
result = []
def collect(subargs):
for arg in subargs:
if arg.op == op:
collect(arg.args)
else:
result.append(arg)
collect(args)
return result
def conjuncts(s):
"""Return a list of the conjuncts in the sentence s.
>>> conjuncts(A & B)
[A, B]
>>> conjuncts(A | B)
[(A | B)]
"""
return dissociate('&', [s])
def disjuncts(s):
"""Return a list of the disjuncts in the sentence s.
>>> disjuncts(A | B)
[A, B]
>>> disjuncts(A & B)
[(A & B)]
"""
return dissociate('|', [s])
class FolKB(KB):
"""A knowledge base consisting of first-order definite clauses.
>>> kb0 = FolKB([expr('Farmer(Mac)'), expr('Rabbit(Pete)'),
... expr('(Rabbit(r) & Farmer(f)) ==> Hates(f, r)')])
>>> kb0.tell(expr('Rabbit(Flopsie)'))
>>> kb0.retract(expr('Rabbit(Pete)'))
>>> kb0.ask(expr('Hates(Mac, x)'))[x]
Flopsie
>>> kb0.ask(expr('Wife(Pete, x)'))
False
"""
def __init__(self, clauses=None):
super().__init__()
self.clauses = [] # inefficient: no indexing
if clauses:
for clause in clauses:
self.tell(clause)
def tell(self, sentence):
if is_definite_clause(sentence):
self.clauses.append(sentence)
else:
raise Exception('Not a definite clause: {}'.format(sentence))
def ask_generator(self, query):
return fol_bc_ask(self, query)
def retract(self, sentence):
self.clauses.remove(sentence)
def fetch_rules_for_goal(self, goal):
return self.clauses
def fol_bc_ask(kb, query):
"""
[Figure 9.6]
A simple backward-chaining algorithm for first-order logic.
KB should be an instance of FolKB, and query an atomic sentence.
"""
return fol_bc_or(kb, query, {})
def fol_bc_or(kb, goal, theta):
for rule in kb.fetch_rules_for_goal(goal):
lhs, rhs = parse_definite_clause(standardize_variables(rule))
for theta1 in fol_bc_and(kb, lhs, unify_mm(rhs, goal, theta)):
yield theta1
def fol_bc_and(kb, goals, theta):
if theta is None:
pass
elif not goals:
yield theta
else:
first, rest = goals[0], goals[1:]
for theta1 in fol_bc_or(kb, subst(theta, first), theta):
for theta2 in fol_bc_and(kb, rest, theta1):
yield theta2
def is_definite_clause(s):
"""Returns True for exprs s of the form A & B & ... & C ==> D,
where all literals are positive. In clause form, this is
~A | ~B | ... | ~C | D, where exactly one clause is positive.
>>> is_definite_clause(expr('Farmer(Mac)'))
True
"""
if is_symbol(s.op):
return True
elif s.op == '==>':
antecedent, consequent = s.args
return is_symbol(consequent.op) and all(is_symbol(arg.op) for arg in conjuncts(antecedent))
else:
return False
def parse_definite_clause(s):
"""Return the antecedents and the consequent of a definite clause."""
assert is_definite_clause(s)
if is_symbol(s.op):
return [], s
else:
antecedent, consequent = s.args
return conjuncts(antecedent), consequent
def standardize_variables(sentence, dic=None):
"""Replace all the variables in sentence with new variables."""
if dic is None:
dic = {}
if not isinstance(sentence, Expr):
return sentence
elif is_var_symbol(sentence.op):
if sentence in dic:
return dic[sentence]
else:
v = Expr('v_{}'.format(next(standardize_variables.counter)))
dic[sentence] = v
return v
else:
return Expr(sentence.op, *[standardize_variables(a, dic) for a in sentence.args])
standardize_variables.counter = itertools.count()
def subst(s, x):
"""Substitute the substitution s into the expression x.
>>> subst({x: 42, y:0}, F(x) + y)
(F(42) + 0)
"""
if isinstance(x, list):
return [subst(s, xi) for xi in x]
elif isinstance(x, tuple):
return tuple([subst(s, xi) for xi in x])
elif not isinstance(x, Expr):
return x
elif is_var_symbol(x.op):
return s.get(x, x)
else:
return Expr(x.op, *[subst(s, arg) for arg in x.args])
def is_symbol(s):
"""A string s is a symbol if it starts with an alphabetic char.
>>> is_symbol('R2D2')
True
"""
return isinstance(s, str) and s[:1].isalpha()
def is_var_symbol(s):
"""A logic variable symbol is an initial-lowercase string.
>>> is_var_symbol('EXE')
False
"""
return is_symbol(s) and s[0].islower()
def is_prop_symbol(s):
"""A proposition logic symbol is an initial-uppercase string.
>>> is_prop_symbol('exe')
False
"""
return is_symbol(s) and s[0].isupper()
def is_variable(x):
"""A variable is an Expr with no args and a lowercase symbol as the op."""
return isinstance(x, Expr) and not x.args and x.op[0].islower()
def unify_mm(x, y, s={}):
"""Unify expressions x,y with substitution s using an efficient rule-based
unification algorithm by Martelli & Montanari; return a substitution that
would make x,y equal, or None if x,y can not unify. x and y can be
variables (e.g. Expr('x')), constants, lists, or Exprs.
>>> unify_mm(x, 3, {})
{x: 3}
"""
set_eq = extend(s, x, y)
s = set_eq.copy()
while True:
trans = 0
for x, y in set_eq.items():
if x == y:
# if x = y this mapping is deleted (rule b)
del s[x]
elif not is_variable(x) and is_variable(y):
# if x is not a variable and y is a variable, rewrite it as y = x in s (rule a)
if s.get(y, None) is None:
s[y] = x
del s[x]
else:
# if a mapping already exist for variable y then apply
# variable elimination (there is a chance to apply rule d)
s[x] = vars_elimination(y, s)
elif not is_variable(x) and not is_variable(y):
# in which case x and y are not variables, if the two root function symbols
# are different, stop with failure, else apply term reduction (rule c)
if x.op is y.op and len(x.args) == len(y.args):
term_reduction(x, y, s)
del s[x]
else:
return None
elif isinstance(y, Expr):
# in which case x is a variable and y is a function or a variable (e.g. F(z) or y),
# if y is a function, we must check if x occurs in y, then stop with failure, else
# try to apply variable elimination to y (rule d)
if occur_check(x, y, s):
return None
s[x] = vars_elimination(y, s)
if y == s.get(x):
trans += 1
else:
trans += 1
if trans == len(set_eq):
# if no transformation has been applied, stop with success
return s
set_eq = s.copy()
def term_reduction(x, y, s):
"""Apply term reduction to x and y if both are functions and the two root function
symbols are equals (e.g. F(x1, x2, ..., xn) and F(x1', x2', ..., xn')) by returning
a new mapping obtained by replacing x: y with {x1: x1', x2: x2', ..., xn: xn'}
"""
for i in range(len(x.args)):
if x.args[i] in s:
s[s.get(x.args[i])] = y.args[i]
else:
s[x.args[i]] = y.args[i]
def vars_elimination(x, s):
"""Apply variable elimination to x: if x is a variable and occurs in s, return
the term mapped by x, else if x is a function recursively applies variable
elimination to each term of the function."""
if not isinstance(x, Expr):
return x
if is_variable(x):
return s.get(x, x)
return Expr(x.op, *[vars_elimination(arg, s) for arg in x.args])
def occur_check(var, x, s):
"""Return true if variable var occurs anywhere in x
(or in subst(s, x), if s has a binding for x)."""
if var == x:
return True
elif is_variable(x) and x in s:
return occur_check(var, s[x], s)
elif isinstance(x, Expr):
return (occur_check(var, x.op, s) or
occur_check(var, x.args, s))
elif isinstance(x, (list, tuple)):
return first(e for e in x if occur_check(var, e, s))
else:
return False