-
Notifications
You must be signed in to change notification settings - Fork 0
/
planning.py
executable file
·66 lines (57 loc) · 2.33 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
"""Planning (Chapters 10-11)
"""
from .utils import Expr
class Action:
"""
Defines an action schema using preconditions and effects
Use this to describe actions in PDDL
action is an Expr where variables are given as arguments(args)
Precondition and effect are both lists with positive and negated literals
Example:
precond_pos = [expr("Human(person)"), expr("Hungry(Person)")]
precond_neg = [expr("Eaten(food)")]
effect_add = [expr("Eaten(food)")]
effect_rem = [expr("Hungry(person)")]
eat = Action(expr("Eat(person, food)"), [precond_pos, precond_neg], [effect_add, effect_rem])
"""
def __init__(self, action, precond, effect):
self.name = action.op
self.args = action.args
self.precond_pos = set(precond[0])
self.precond_neg = set(precond[1])
self.effect_add = set(effect[0])
self.effect_rem = set(effect[1])
def __call__(self, kb, args):
return self.act(kb, args)
def __str__(self):
return "{}{!s}".format(self.name, self.args)
def substitute(self, e, args):
"""Replaces variables in expression with their respective Propostional symbol"""
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):
"""Checks if the precondition is satisfied in the current state"""
# check for positive clauses
for clause in self.precond_pos:
if self.substitute(clause, args) not in kb.clauses:
return False
# check for negative clauses
for clause in self.precond_neg:
if self.substitute(clause, args) in kb.clauses:
return False
return True
def act(self, kb, args):
"""Executes the action on the state's kb"""
# check if the preconditions are satisfied
if not self.check_precond(kb, args):
raise Exception("Action pre-conditions not satisfied")
# remove negative literals
for clause in self.effect_rem:
kb.retract(self.substitute(clause, args))
# add positive literals
for clause in self.effect_add:
kb.tell(self.substitute(clause, args))