;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/agents.lisp ;;;; Making Problem-Solving Agents and Environments ;;; This file defines problem-solving agents that use search algorithms to ;;; find a solution to a problem, and then execute the appropriate actions, ;;; one at a time. Of course, we also have to define the environments that ;;; these agents execute in. Call the function RUN-PROBLEM to have an agent ;;; solve a problem and execute the list of actions that leads to the ;;; solution. Or call SEARCH-TRIALS to see how a set of algorithms compare ;;; in a particular domain. ;;;; Top-Level Function (defun run-problem (problem &key (display 'unchanged) (max-steps 1000) (algorithm (select-searcher problem))) "Run a problem-solving-agent in an environment. The agent will try to solve the problem, and then execute the actions that make up the solution." (setf (problem-num-expanded problem) 0) (run-eval-environment (problem->environment problem :algorithm algorithm) :display display :max-steps max-steps)) ;;;; Auxiliary Functions (defun problem->environment (problem &key (algorithm (select-searcher problem))) "Convert a problem into an environment. Then we can pass the environment to RUN-ENVIRONMENT, and the agent will search for a solution and execute it." (let ((agent (make-problem-solving-agent problem :algorithm algorithm))) (make-environment :percept-fn #'(lambda (agent env) (declare (ignore agent)) (environment-state env)) :update-fn #'(lambda (env) (setf (environment-state env) (cdr (assoc (agent-action agent) (funcall (problem-successor-fn problem) (environment-state env)) :test #'equal)))) :termination-fn #'(lambda (env) (solved? problem env)) :state (problem-initial-state problem) :agents (list agent) :name (problem-domain problem)))) (defun make-problem-solving-agent (problem &key (name "A") (algorithm (select-searcher problem))) (let* ((actions nil) (program #'(lambda (percept) (declare (ignore percept)) ; These agents ignore percepts! (when (null actions) (setf actions (solution-actions (funcall algorithm problem)))) (pop actions)))) (make-agent :name name :program program)))