Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. (defvar *state* nil “The current state: a list of conditions.”)
  2.  
  3. (defvar *ops* nil “A list of available operators.”)
  4.  
  5. (defstruct op “An operation”
  6.  
  7. (action nil) (preconds nil) (add-list nil) (del-list nil))
  8.  
  9. (defun GPS (*state* goals *ops*)
  10.  
  11. “General Problem Solver: achieve ail goals using *ops*.”
  12.  
  13. (if (every #’achieve goals) ‘solved))
  14.  
  15. (defun achieve (goal)
  16.  
  17. “A goal is achieved if it already holds,
  18.  
  19. or if there is an appropriate op for it that is applicable.”
  20.  
  21. (or (member goal *state*)
  22.  
  23. (some #’apply-op
  24.  
  25. (find-all goal *ops* :test #’appropriate-p))))
  26.  
  27. (defun appropriate-p (goal op)
  28.  
  29. “An op is appropriate to a goal if it is in its add list.”
  30.  
  31. (member goal (op-add-list op)))
  32.  
  33. (defun apply-op (op)
  34.  
  35. “Print a message and update *state* if op is applicable.”
  36.  
  37. (when (every #’achieve (op-preconds op))
  38.  
  39. (print (list ‘executing (op-action op)))
  40.  
  41. (setf *state* (set-difference *state* (op-del-list op)))
  42.  
  43. (setf *state* (union *state* (op-add-list op)))
  44.  
  45. t))
  46.  
  47. (defparameter *school-ops*
  48.  
  49. (list
  50.  
  51. (make-op :action ‘drive-son-to-school
  52.  
  53. :preconds ‘(son-at-home car-works)
  54.  
  55. :add-list ‘(son-at-school)
  56.  
  57. :del-list ‘(son-at-home))
  58.  
  59. (make-op :action ‘shop-installs-battery
  60.  
  61. :preconds ‘(car-needs-battery shop-knows-problem shop-has-money)
  62.  
  63. :add-list ‘(car-works))
  64.  
  65. (make-op :action ‘tel 1-shop-problem
  66.  
  67. :preconds ‘(in-communication-with-shop)
  68.  
  69. :add-list ‘(shop-knows-problem))
  70.  
  71. (make-op raction ‘telephone-shop
  72.  
  73. :preconds ‘(know-phone-number)
  74.  
  75. :add-list ‘(in-communication-with-shop))
  76.  
  77. (make-op .-action ‘look-up-number
  78.  
  79. :preconds ‘(have-phone-book)
  80.  
  81. :add-list ‘(know-phone-number))
  82.  
  83. (make-op :action ‘give-shop-money
  84.  
  85. :preconds ‘(have-money)
  86.  
  87. :add-list ‘(shop-has-money)
  88.  
  89. :del-list ‘(have-money))))
  90.  
  91. > (gps ‘(son-at-home car-needs-battery have-money have-phone-book)
  92.  
  93. ‘(son-at-school)
  94.  
  95. *school-ops*)
  96.  
  97. (EXECUTING LOOK-UP-NUMBER)
  98.  
  99. (EXECUTING TELEPHONE-SHOP)
  100.  
  101. (EXECUTING TELL-SHOP-PROBLEM)
  102.  
  103. (EXECUTING GIVE-SHOP-MONEY)
  104.  
  105. (EXECUTING SHOP-INSTALLS-BATTERY)
  106.  
  107. (EXECUTING DRIVE-SON-TO-SCHOOL)
  108.  
  109. SOLVED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement