;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/domains/map-coloring.lisp

;;;; Map Coloring and N-Queens as Binary CSPs

;;; These problems use the CSP structure of search/algorithms/csp-4e.lisp.
;;; (The 1995 n-queens CSP-PROBLEM is in nqueens.lisp.)

(defun different-values-constraint (var1 val1 var2 val2)
  "A constraint saying that two neighboring variables differ in value."
  (declare (ignore var1 var2))
  (not (equal val1 val2)))

(defun make-map-coloring-csp (colors neighbors)
  "A CSP for coloring a map so that adjacent regions have different colors.
  NEIGHBORS is an alist ((region . adjacent-regions) ...) or a string in the
  format of PARSE-NEIGHBORS."
  (when (stringp neighbors)
    (setf neighbors (parse-neighbors neighbors)))
  (make-csp :variables (mapcar #'car neighbors) :domain colors
	    :neighbors neighbors
	    :constraint-fn #'different-values-constraint))

(defun parse-neighbors (string)
  "Convert a string of the form \"X: Y Z; Y: Z\" into an alist mapping each
  region (a symbol) to its neighbors; adjacency is symmetric, so \"X: Y\"
  makes Y a neighbor of X too.  Regions keep the order of first mention."
  (let ((alist nil))
    (flet ((entry (region)
	     (or (assoc region alist)
		 (let ((new (list region)))
		   (setf alist (nconc alist (list new)))
		   new))))
      (dolist (spec (csp-split-string string '(#\;)))
	(let* ((parts (csp-split-string spec '(#\:)))
	       (tokens (csp-split-string (first parts)
					 '(#\Space #\Tab #\Newline))))
	  (when tokens
	    (let* ((a (intern (string-upcase (first tokens))))
		   (a-entry (entry a)))
	      (dolist (b-name (csp-split-string (or (second parts) "")
						'(#\Space #\Tab #\Newline)))
		(let* ((b (intern (string-upcase b-name)))
		       (b-entry (entry b)))
		  (setf (cdr a-entry) (append (cdr a-entry) (list b)))
		  (setf (cdr b-entry) (append (cdr b-entry) (list a))))))))))
    alist))

(defun csp-split-string (string separators)
  "Split STRING at the characters in SEPARATORS, dropping empty pieces."
  (let ((pieces nil) (start 0))
    (loop for i from 0 to (length string) do
	  (when (or (= i (length string))
		    (member (char string i) separators))
	    (when (> i start)
	      (push (subseq string start i) pieces))
	    (setf start (+ i 1))))
    (nreverse pieces)))

;;;; The Maps

(defparameter *australia-neighbors*
  "SA: WA NT Q NSW V; NT: WA Q; NSW: Q V; T: "
  "The map of Australia [Fig. 6.1].")

(defparameter *usa-neighbors*
  "WA: OR ID; OR: ID NV CA; CA: NV AZ; NV: ID UT AZ; ID: MT WY UT;
   UT: WY CO AZ; MT: ND SD WY; WY: SD NE CO; CO: NE KA OK NM; NM: OK TX AZ;
   ND: MN SD; SD: MN IA NE; NE: IA MO KA; KA: MO OK; OK: MO AR TX;
   TX: AR LA; MN: WI IA; IA: WI IL MO; MO: IL KY TN AR; AR: MS TN LA;
   LA: MS; WI: MI IL; IL: IN KY; IN: OH KY; MS: TN AL; AL: TN GA FL;
   MI: OH IN; OH: PA WV KY; KY: WV VA TN; TN: VA NC GA; GA: NC SC FL;
   PA: NY NJ DE MD WV; WV: MD VA; VA: MD DC NC; NC: SC; NY: VT MA CT NJ;
   NJ: DE; DE: MD; MD: DC; VT: NH MA; MA: NH RI CT; CT: RI; ME: NH;
   HI: ; AK: "
  "The map of the states of the USA.")

(defparameter *france-neighbors*
  "AL: LO FC; AQ: MP LI PC; AU: LI CE BO RA LR MP; BO: CE IF CA FC RA
   AU; BR: NB PL; CA: IF PI LO FC BO; CE: PL NB NH IF BO AU LI PC; FC: BO
   CA LO AL RA; IF: NH PI CA BO CE; LI: PC CE AU MP AQ; LO: CA AL FC; LR:
   MP AU RA PA; MP: AQ LI AU LR; NB: NH CE PL BR; NH: PI IF CE NB; NO:
   PI; PA: LR RA; PC: PL CE LI AQ; PI: NH NO CA IF; PL: BR NB CE PC; RA:
   AU BO FC PA LR"
  "The map of the regions of France.")

(defun make-australia-csp (&optional (colors '(R G B)))
  "Color the map of Australia [Fig. 6.1]."
  (make-map-coloring-csp colors *australia-neighbors*))

(defun make-usa-csp (&optional (colors '(R G B Y)))
  (make-map-coloring-csp colors *usa-neighbors*))

(defun make-france-csp (&optional (colors '(R G B Y)))
  (make-map-coloring-csp colors *france-neighbors*))

;;;; N-Queens

(defun queen-constraint (var1 val1 var2 val2)
  "Queens in columns VAR1 and VAR2, rows VAL1 and VAL2, do not attack each
  other (or are the same queen)."
  (or (= var1 var2)
      (and (/= val1 val2)
	   (/= (+ var1 val1) (+ var2 val2))
	   (/= (- var1 val1) (- var2 val2)))))

(defun make-queens-csp (&optional (n 8))
  "The n-queens problem as a CSP: variable i is the row of the queen in
  column i."
  (let ((columns (iota n)))
    (make-csp :variables columns :domain columns
	      :neighbors (mapcar #'(lambda (i) (cons i (remove i columns)))
				 columns)
	      :constraint-fn #'queen-constraint)))
