;;; File: agents/test.lisp -*- Mode: Lisp; -*-

(deftest agents
  "Test agents in the vacuum and wumpus worlds." 
  "Here is how to run an environment, in this case the vacuum world."
  "We specify the maximum number of steps, but that is optional:"
  ((run-environment (make-vacuum-world :max-steps 10)))
  "You can turn off the display (with :stream nil), and just see the results:"
  ((run-environment (make-vacuum-world :stream nil)))
  "You can customize several things, such as the agent(s) in the world."
  "By default, a vacuum-world has a random-agent; we can change that to"
  "a slightly smarter agent with the :ASPEC (Agent SPECification) keyword:"
  ((run-environment (make-vacuum-world :stream nil 
				       :aspec '(reactive-vacuum-agent))))
  "We can change the probability of dirt in each cell using the :CSPEC"
  "keyword (Custom SPECification).  It allows a complex language for"
  "specifying objects and where they go."
  ((run-environment (make-vacuum-world :cspec '((at all (P 0.9 dirt)))
				       :max-steps 10)))
  "Finally, we can compare 2 or more agents over a number of trials:"
  ((agent-trials 'make-vacuum-world 
		 '(reactive-vacuum-agent random-vacuum-agent) :n 10))
  "Now for the wumpus world"
  ((run-environment (make-wumpus-world :max-steps 10)))

  "The two-square vacuum world of [Fig. 2.2], with the table-driven agent"
  "[Fig. 2.7], the simple reflex agent [Fig. 2.10] and the model-based"
  "reflex agent [Fig. 2.12]."
  ((setq p (make-table-driven-agent-program *two-square-vacuum-table*)))
  ((funcall p '(A clean)) 'right)
  ((funcall p '(B dirty)) 'suck)
  ((funcall (make-simple-reflex-agent-program *two-square-vacuum-rules*)
	    '(A dirty))
   'suck)
  ((setq env (make-two-square-vacuum-world
	      :status (list (cons 'A 'dirty) (cons 'B 'dirty)) :location 'A
	      :aspec '(table-driven-vacuum-agent))))
  ((run-environment env))
  ((two-square-vacuum-clean? env) *)
  ((run-environment (make-two-square-vacuum-world
		     :stream nil :aspec '(simple-reflex-vacuum-agent)))
   (two-square-vacuum-clean? *))
  "The model-based agent stops (NO-OP) once it knows both squares are clean."
  ((run-environment (make-two-square-vacuum-world
		     :stream nil :aspec '(model-based-vacuum-agent)))
   (and (two-square-vacuum-clean? *)
	(eq (agent-action (first (environment-agents *))) 'no-op)))
  ((agent-trials 'make-two-square-vacuum-world
		 '(table-driven-vacuum-agent simple-reflex-vacuum-agent
		   model-based-vacuum-agent)
		 :n 5))
  ((typep (make-two-square-vacuum-world) 'two-square-vacuum-world) t)
  ((typep (make-vacuum-world) 'vacuum-world) t)

  "The random agent program picks any of its actions:"
  ((setq p (make-random-agent-program '(left right suck))))
  ((loop repeat 30 collect (funcall p '(A dirty)))
   (and (subsetp * '(left right suck))
	(> (length (remove-duplicates *)) 1)))
  ((run-environment (make-two-square-vacuum-world
		     :stream nil :aspec '(random-two-square-vacuum-agent)))
   (<= 0 (agent-score (first (environment-agents *))) 20))
  "The model-based vacuum program keeps the status of both squares:"
  ((setq p (make-model-based-vacuum-program)))
  ((mapcar p '((A dirty) (A clean) (B dirty) (B clean)))
   '(suck right suck no-op))
  "A model-based reflex program whose state counts the percepts:"
  ((setq p (make-model-based-reflex-agent-program
	    (list (cons #'(lambda (n) (>= n 3)) 'stop)
		  (cons #'(lambda (n) (declare (ignore n)) t) 'go))
	    #'(lambda (n action percept model)
		(declare (ignore action percept model))
		(+ n 1))
	    0)))
  ((mapcar p '(a b c d)) '(go go stop stop))

  "A grid environment (here a 4x4 vacuum world): walls on the edges, and"
  "the agent starts in (1 1) and bumps into the wall at (3 1)."
  ((setq genv (make-vacuum-world :size (@ 4 4) :cspec nil :stream nil
				 :aspec '(reactive-vacuum-agent))))
  ((progn (initialize genv) (typep genv 'grid-environment)) t)
  ((find-object-if #'obstacle-p (@ 0 0) genv) *)
  ((find-object-if #'obstacle-p (@ 3 1) genv) *)
  ((free-loc? (@ 2 2) genv) t)
  ((setq body (agent-body (first (environment-agents genv)))))
  ((object-loc body) '(1 1))
  ((progn (setf (object-heading body) (@ 1 0)) (forward genv body)) '(2 1))
  ((forward genv body) nil)
  ((list (object-loc body) (object-bump body)) '((2 1) bump))

  "The wumpus world: with a pit in [2,1], the agent in [1,1] feels a breeze,"
  "and no stench from the wumpus in [1,3]."
  ((setq wenv (make-wumpus-world
	       :stream nil :aspec '(aimless-wumpus-agent)
	       :bspec '((at edge wall) (at (2 1) pit) (at (1 3) wumpus)
			(at (2 3) gold)))))
  ((progn (initialize wenv) (typep wenv 'wumpus-world)) t)
  ((get-percept wenv (first (environment-agents wenv)))
   '(nil breeze nil nil nil))
  )
