;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/domains/erratic-vacuum.lisp

;;;; The Erratic Vacuum World [Section 4.3.1]

;;; A state is a list (location status-of-A status-of-B), e.g. (A dirty
;;; clean).  The actions are LEFT, RIGHT and SUCK.  SUCK is erratic: on a
;;; dirty square it cleans the square and sometimes the other one too; on a
;;; clean square it sometimes deposits dirt.  The goal is that both squares
;;; are clean.

(defstructure (erratic-vacuum-problem
	       (:include nondeterministic-problem
			 (initial-state '(A dirty dirty))))
  "The erratic vacuum world, starting by default in state 1 of [Fig. 4.9].")

(defmethod goal-test ((problem erratic-vacuum-problem) state)
  (and (eq (second state) 'clean) (eq (third state) 'clean)))

(defmethod nondeterministic-actions ((problem erratic-vacuum-problem) state)
  (declare-ignore state)
  '(suck right left))

(defmethod nondeterministic-results ((problem erratic-vacuum-problem)
				     state action)
  (destructuring-bind (loc a b) state
    (flet ((with-status (loc status other)
	     ;; The state where LOC has STATUS and the other square OTHER
	     (if (eq loc 'A) (list 'A status other) (list 'B other status))))
      (let ((here (if (eq loc 'A) a b))
	    (there (if (eq loc 'A) b a)))
	(case action
	  (right (list (list 'B a b)))
	  (left  (list (list 'A a b)))
	  (suck  (if (eq here 'dirty)
		     (remove-duplicates
		      (list (with-status loc 'clean there)
			    (with-status loc 'clean 'clean))
		      :test #'equal)
		   (list (with-status loc 'clean there)
			 (with-status loc 'dirty there))))
	  (t nil))))))
