Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.72 KB | None | 0 0
  1. (defun find-all (item sequence &rest keyword-args
  2.          &key (test #'eql) test-not &allow-other-keys)
  3.   "Find all elements of a sequence that match item, according to
  4. the given lambda-list keyword test (or test-not).  Doesn't alter
  5. sequence."
  6.   (if test-not
  7.       (apply #'remove item sequence
  8.          :test-not (complement test-not) keyword-args)
  9.       (apply #'remove item sequence
  10.          :test (complement test) keyword-args)))
  11.  
  12.  
  13. (defvar *initial-state* nil
  14.   "The current state -- a list of conditions.")
  15.  
  16.  
  17. (defvar *scenario-and-requirements* nil
  18.   "A list of actions and the requirements for that action, along with
  19. the logical consequences of that action.")
  20.  
  21.  
  22. (defstruct action-container
  23.   "An action with its requirements and logical consequences."
  24.   (action nil) (preconds nil) (result-of-action nil) (no-longer-true nil))
  25.  
  26.  
  27. (defparameter *school-scenario*
  28.   (list
  29.    (make-action-container
  30.     :action 'drive-son-to-school
  31.     :preconds '(son-at-home car-works)
  32.     :result-of-action '(son-at-school)
  33.     :no-longer-true '(son-at-home))
  34.    (make-action-container
  35.     :action 'shop-installs-battery
  36.     :preconds '(car-needs-battery shop-knows-problem shop-has-money)
  37.     :result-of-action '(car-works))
  38.    (make-action-container
  39.     :action 'tell-shop-problem
  40.     :preconds '(in-communication-with-shop)
  41.     :result-of-action '(shop-knows-problem))
  42.    (make-action-container
  43.     :action 'telephone-shop
  44.     :preconds '(know-phone-number)
  45.     :result-of-action '(in-communication-with-shop))
  46.    (make-action-container
  47.     :action 'look-up-number
  48.     :preconds '(have-phone-book)
  49.     :result-of-action '(know-phone-number))
  50.    (make-action-container
  51.     :action 'give-shop-money
  52.     :preconds '(have-money)
  53.     :result-of-action '(shop-has-money)
  54.     :no-longer-true '(have-money))))
  55.  
  56.  
  57. (defun GPS (*initial-state* goal *scenario-and-requirements*)
  58.   "General Problem Solver: achieve all goals using *scenario-and-requirements*."
  59.   (if (every #'goal-or-precond-found? goal) 'solved))
  60.  
  61.  
  62. (defun goal-or-precond-found? (goal-or-precond)
  63.   "A goal is achieved if it already holds,
  64. or if there is an appropriate op for it that is applicable.  Returns a
  65. action-container."
  66.   (or (member goal-or-precond *initial-state*)
  67.       (some #'pass-the-precond  ; encapsulate this under a function
  68.         (member-of-result-of-action? goal-or-precond
  69.                      *scenario-and-requirements*))))
  70.  
  71.  
  72. (defun member-of-result-of-action? (goal-or-precond *scenario-and-requirements*)
  73.   "Returns an individual action-container, or a list of them."
  74.   (find-all goal-or-precond *scenario-and-requirements*
  75.         :test #'actual-member-result-of-action))
  76.  
  77.  
  78. (defun actual-member-result-of-action (goal-or-precond individual-action-container)
  79.   "An op is appropriate to a goal if it is in its add list."
  80.   (member goal-or-precond (action-container-result-of-action
  81.                individual-action-container)))
  82.  
  83.  
  84. (defun pass-the-precond (individual-action-container)
  85.   "Print a message and update *initial-state* if op is applicable."
  86.   (when (every #'goal-or-precond-found?
  87.            (action-container-preconds individual-action-container))
  88.     (print (action-container-action individual-action-container))
  89.     (setf *initial-state*
  90.       (set-difference *initial-state*
  91.               (action-container-no-longer-true
  92.                individual-action-container)))
  93.     (setf *initial-state*
  94.       (union *initial-state*
  95.          (action-container-result-of-action individual-action-container)))
  96.   t))
  97.  
  98.  
  99. (defun all-goals-found? (goals)
  100.     "Try to achieve each goal.  Then verify they still hold."
  101.   (and (every #'goal-or-precond-found? goals)
  102.        (subsetp goals *initial-state*)))
  103.  
  104.  
  105. (gps '(son-at-home car-works)
  106.      '(son-at-school)
  107.      *school-scenario*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement