;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/domains/peak-finding.lisp

;;;; Finding the Highest Peak of a Grid

;;; The GRID is a list of rows of numbers, and a state is a cell (x y) of
;;; it, with x the row and y the column.  The moves are those of
;;; DIRECTIONS (see grid-problem.lisp) that stay on the grid.  The local
;;; search algorithms minimize h, so h is minus the value of the cell.

(defstructure (peak-finding-problem (:include problem))
  "Find a cell of GRID with the highest value."
  (grid '((0)))
  (directions *directions4*))

(defun peak-value (problem state)
  "The value of the cell STATE."
  (nth (second state) (nth (first state) (peak-finding-problem-grid problem))))

(defmethod successors ((problem peak-finding-problem) state)
  "Move to each neighboring cell of the grid."
  (let ((n (length (peak-finding-problem-grid problem)))
	(m (length (first (peak-finding-problem-grid problem))))
	(result nil))
    (dolist (d (peak-finding-problem-directions problem) (nreverse result))
      (let ((cell (list (+ (first state) (second d))
			(+ (second state) (third d)))))
	(when (and (< -1 (first cell) n) (< -1 (second cell) m))
	  (push (cons (first d) cell) result))))))

(defmethod h-cost ((problem peak-finding-problem) state)
  "Minus the value of the cell, since the searches minimize h."
  (- (peak-value problem state)))
