planning.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Planning (Chapters 10-11)
  2. """
  3. from .utils import Expr
  4. class Action:
  5. """
  6. Defines an action schema using preconditions and effects
  7. Use this to describe actions in PDDL
  8. action is an Expr where variables are given as arguments(args)
  9. Precondition and effect are both lists with positive and negated literals
  10. Example:
  11. precond_pos = [expr("Human(person)"), expr("Hungry(Person)")]
  12. precond_neg = [expr("Eaten(food)")]
  13. effect_add = [expr("Eaten(food)")]
  14. effect_rem = [expr("Hungry(person)")]
  15. eat = Action(expr("Eat(person, food)"), [precond_pos, precond_neg], [effect_add, effect_rem])
  16. """
  17. def __init__(self, action, precond, effect):
  18. self.name = action.op
  19. self.args = action.args
  20. self.precond_pos = precond[0]
  21. self.precond_neg = precond[1]
  22. self.effect_add = effect[0]
  23. self.effect_rem = effect[1]
  24. def __call__(self, kb, args):
  25. return self.act(kb, args)
  26. def __str__(self):
  27. return "{}{!s}".format(self.name, self.args)
  28. def substitute(self, e, args):
  29. """Replaces variables in expression with their respective Propostional symbol"""
  30. new_args = list(e.args)
  31. for num, x in enumerate(e.args):
  32. for i in range(len(self.args)):
  33. if self.args[i] == x:
  34. new_args[num] = args[i]
  35. return Expr(e.op, *new_args)
  36. def check_precond(self, kb, args):
  37. """Checks if the precondition is satisfied in the current state"""
  38. # check for positive clauses
  39. for clause in self.precond_pos:
  40. if self.substitute(clause, args) not in kb.clauses:
  41. return False
  42. # check for negative clauses
  43. for clause in self.precond_neg:
  44. if self.substitute(clause, args) in kb.clauses:
  45. return False
  46. return True
  47. def act(self, kb, args):
  48. """Executes the action on the state's kb"""
  49. # check if the preconditions are satisfied
  50. if not self.check_precond(kb, args):
  51. raise Exception("Action pre-conditions not satisfied")
  52. # remove negative literals
  53. for clause in self.effect_rem:
  54. kb.retract(self.substitute(clause, args))
  55. # add positive literals
  56. for clause in self.effect_add:
  57. kb.tell(self.substitute(clause, args))