Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun find-all (item sequence &rest keyword-args
- &key (test #'eql) test-not &allow-other-keys)
- "Find all elements of a sequence that match item, according to
- the given lambda-list keyword test (or test-not). Doesn't alter
- sequence."
- (if test-not
- (apply #'remove item sequence
- :test-not (complement test-not) keyword-args)
- (apply #'remove item sequence
- :test (complement test) keyword-args)))
- (defvar *initial-state* nil
- "The current state -- a list of conditions.")
- (defvar *scenario-and-requirements* nil
- "A list of actions and the requirements for that action, along with
- the logical consequences of that action.")
- (defstruct action-container
- "An action with its requirements and logical consequences."
- (action nil) (preconds nil) (result-of-action nil) (no-longer-true nil))
- (defparameter *school-scenario*
- (list
- (make-action-container
- :action 'drive-son-to-school
- :preconds '(son-at-home car-works)
- :result-of-action '(son-at-school)
- :no-longer-true '(son-at-home))
- (make-action-container
- :action 'shop-installs-battery
- :preconds '(car-needs-battery shop-knows-problem shop-has-money)
- :result-of-action '(car-works))
- (make-action-container
- :action 'tell-shop-problem
- :preconds '(in-communication-with-shop)
- :result-of-action '(shop-knows-problem))
- (make-action-container
- :action 'telephone-shop
- :preconds '(know-phone-number)
- :result-of-action '(in-communication-with-shop))
- (make-action-container
- :action 'look-up-number
- :preconds '(have-phone-book)
- :result-of-action '(know-phone-number))
- (make-action-container
- :action 'give-shop-money
- :preconds '(have-money)
- :result-of-action '(shop-has-money)
- :no-longer-true '(have-money))))
- (defun GPS (*initial-state* goal *scenario-and-requirements*)
- "General Problem Solver: achieve all goals using *scenario-and-requirements*."
- (if (every #'goal-or-precond-found? goal) 'solved))
- (defun goal-or-precond-found? (goal-or-precond)
- "A goal is achieved if it already holds,
- or if there is an appropriate op for it that is applicable. Returns a
- action-container."
- (or (member goal-or-precond *initial-state*)
- (some #'pass-the-precond ; encapsulate this under a function
- (member-of-result-of-action? goal-or-precond
- *scenario-and-requirements*))))
- (defun member-of-result-of-action? (goal-or-precond *scenario-and-requirements*)
- "Returns an individual action-container, or a list of them."
- (find-all goal-or-precond *scenario-and-requirements*
- :test #'actual-member-result-of-action))
- (defun actual-member-result-of-action (goal-or-precond individual-action-container)
- "An op is appropriate to a goal if it is in its add list."
- (member goal-or-precond (action-container-result-of-action
- individual-action-container)))
- (defun pass-the-precond (individual-action-container)
- "Print a message and update *initial-state* if op is applicable."
- (when (every #'goal-or-precond-found?
- (action-container-preconds individual-action-container))
- (print (action-container-action individual-action-container))
- (setf *initial-state*
- (set-difference *initial-state*
- (action-container-no-longer-true
- individual-action-container)))
- (setf *initial-state*
- (union *initial-state*
- (action-container-result-of-action individual-action-container)))
- t))
- (defun all-goals-found? (goals)
- "Try to achieve each goal. Then verify they still hold."
- (and (every #'goal-or-precond-found? goals)
- (subsetp goals *initial-state*)))
- (gps '(son-at-home car-works)
- '(son-at-school)
- *school-scenario*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement