;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/test.lisp

;;;; Test Cases for Search

(deftest search
  "Test the code for Solving Problems by Searching"
  "Start with a trivial version of the missionaries and cannibals puzzle."
  ((setq p1 (make-cannibal-problem 
	     :initial-state (make-cannibal-state :m1 2 :c1 1))))
  "We search for a solution node:"
  ((setq result (breadth-first-search p1)) *)
  "We can get information out of that solution:"
  ((solution-actions result) *)
  ((solution-nodes result) *)
  "Or we can use SOLVE to print the results nicely. By default, SOLVE
  uses A*-search, but you can give it another algorithm as the second arg."
  ((solve p1) *)
  "For the full 3 missionary and 3 cannibal problem, breadth-first-search"
  "is very inefficient.  Better to use something that handles repeated states,"
  "like A*-search or no-duplicates-breadth-first-search:"
  ((solve (make-cannibal-problem) 'A*-search) *)
  ((solve (make-cannibal-problem) 'no-duplicates-breadth-first-search) *)

  "Here is how to get a problem-solving agent to find the solution,"
  "and then go ahead and execute the actions that comprise the solution."
  ((run-environment (problem->environment p1)))

  "Now we look at the route-finding domain."
  "First, solve the Arad-to-Bucharest problem with A*-search:"
  ((solve (make-romanian-problem :initial-state 'Arad :goal 'Bucharest)) *)
  "Now turn it around:"
  ((solve (make-romanian-problem :goal 'Arad :initial-state 'Bucharest)) *)
  "Let's just see the actions:"
  ((solution-actions (A*-search (make-romanian-problem)))
   '(Sibiu Rimnicu Pitesti Bucharest))
  "The vacuum world as a search problem: clean, go home, switch off."
  ((setq actions (solution-actions (A*-search (vacuum-problem 2 2 (list (@ 1 0)))))) *)
  ((member 'suck actions) *)
  ((car (last actions)) 'shut-off)
  "Now on a random map:"
  ((solve (make-route-finding-problem)))

  "Here's how to compare several algorithms."
  ((setq searchers '(A*-search no-cycles-depth-first-search
			       no-duplicates-breadth-first-search)))
  ((compare-search-algorithms #'make-route-finding-problem searchers))
  ((compare-search-algorithms #'make-romanian-problem searchers :n 1))
  ((compare-search-algorithms #'make-cannibal-problem
			      '(no-returns-breadth-first-search A*-search
				no-duplicates-breadth-first-search)
			      :n 1))
  ((compare-search-algorithms #'make-romanian-problem
			     '(tree-A*-search A*-search tree-IDA*-search)
			     :n 1))
  "We'll look at the iterative improvement algorithms on a harder map problem."
  ((setq searchers '(A*-search hill-climbing-search
			       simulated-annealing-search)))
  ((compare-search-algorithms #'(lambda () (make-romanian-problem :goal 'Iasi))
			      searchers :n 1))
  "Let's take a look at the 8-puzzle:"
  ((solve (make-8-puzzle-problem)) *)
  ((compare-search-algorithms 'make-8-puzzle-problem '(A*-search) :n 2))
  "And the path-planning problem among polygonal obstacles:"
  ((solve (make-path-planning-problem :scene *scene-4.17*)))
  "Now the 8-queens problem"
  ((solve (make-nqueens-problem) 'csp-backtracking-search) *)
  ((compare-search-algorithms
    'make-nqueens-problem
    '(csp-backtracking-search csp-forward-checking-search)
    :n 1))
  "Here is the Travelling Salesperson Problem (TSP)."
  ((solve (make-tsp-problem)))
  ((compare-search-algorithms 'make-tsp-problem
			     '(A*-search greedy-search uniform-cost-search)
			     :n 5))
  "Now we test the environments for 2-player and 3-player games:"
  ((run-game (make-ttt-game)))
  ((run-game (make-cognac-game :players '(X O @))))
  "Now we see that 2x2 tic-tac-toe is a win for the first player, X."
  ((run-game (make-ttt-game :n 2) 
	     :agents '(alpha-beta-ttt-agent alpha-beta-ttt-agent)))
  "In a full 3x3 game, alpha-beta search (playing O) often wins."
  ((run-game (make-ttt-game) :agents '(random-game-agent alpha-beta-ttt-agent)))

  "Constraint satisfaction problems as in the 4th edition, chapter 6."
  "Color the map of Australia [Fig. 6.1] by backtracking search [Fig. 6.5]"
  "with the variable and value heuristics and the inference procedures:"
  ((setq csp (make-australia-csp)))
  ((backtracking-search csp) (csp-solution? csp *))
  ((setq csp (make-australia-csp)))
  ((backtracking-search csp :select-unassigned-variable #'mrv)
   (csp-solution? csp *))
  ((setq csp (make-australia-csp)))
  ((backtracking-search csp :order-domain-values #'lcv) (csp-solution? csp *))
  ((setq csp (make-australia-csp)))
  ((backtracking-search csp :inference #'forward-checking)
   (csp-solution? csp *))
  ((setq csp (make-australia-csp)))
  ((backtracking-search csp :inference #'mac) (csp-solution? csp *))
  ((setq csp (make-usa-csp)))
  ((backtracking-search csp :select-unassigned-variable #'mrv
			:order-domain-values #'lcv :inference #'mac)
   (csp-solution? csp *))
  "A triangle cannot be colored with two colors; MAC sees it at once."
  ((setq csp (make-map-coloring-csp '(R G) "A: B C; B: C")))
  ((progn (csp-support-pruning csp)
	  (mac csp 'A 'R (make-csp-assignment) (csp-suppose csp 'A 'R)))
   nil)
  ((backtracking-search (make-map-coloring-csp '(R G) "A: B C; B: C")
			:inference #'mac)
   nil)
  "The 8-queens problem on the same CSP representation:"
  ((setq csp (make-queens-csp 8)))
  ((backtracking-search csp :select-unassigned-variable #'mrv
			:inference #'forward-checking)
   (csp-solution? csp *))
  "Min-conflicts local search [Fig. 6.9]:"
  ((setq csp (make-australia-csp)))
  ((min-conflicts csp) (csp-solution? csp *))
  ((setq csp (make-queens-csp 8)))
  ((min-conflicts csp) (csp-solution? csp *))
  ((min-conflicts (make-australia-csp '(R G)) :max-steps 1000) nil)

  "Arc consistency: AC-3 [Fig. 6.3], and its variants AC-3b and AC-4."
  "A constraint that no pair of values satisfies is inconsistent:"
  ((setq odd-csp
	 (make-csp :neighbors (parse-neighbors "A: B; B: ") :domain '(0 1 2 3 4)
		   :constraint-fn #'(lambda (x xv y yv)
				      (declare (ignore x y))
				      (and (evenp xv) (= (+ xv yv) 4)
					   (oddp yv))))))
  ((ac-3 odd-csp) nil)
  ((ac-3b (copy-csp-fresh odd-csp)) nil)
  ((ac-4 (copy-csp-fresh odd-csp)) nil)
  "Here AC-3 prunes the odd values of both variables:"
  ((setq even-csp
	 (make-csp :neighbors (parse-neighbors "A: B; B: ") :domain '(0 1 2 3 4)
		   :constraint-fn #'(lambda (x xv y yv)
				      (declare (ignore x y))
				      (and (evenp xv) (= (+ xv yv) 4))))))
  ((setq removals (make-csp-removals)))
  ((ac-3 even-csp :removals removals) t)
  ((coerce removals 'list)
   (member * '(((A . 1) (A . 3) (B . 1) (B . 3))
	       ((B . 1) (B . 3) (A . 1) (A . 3)))
	   :test #'equal))
  ((setq removals (make-csp-removals)))
  ((ac-3b (copy-csp-fresh even-csp) :removals removals) t)
  ((coerce removals 'list)
   (member * '(((A . 1) (A . 3) (B . 1) (B . 3))
	       ((B . 1) (B . 3) (A . 1) (A . 3)))
	   :test #'equal))
  ((setq removals (make-csp-removals)))
  ((ac-4 (copy-csp-fresh even-csp) :removals removals) t)
  ((coerce removals 'list)
   (member * '(((A . 1) (A . 3) (B . 1) (B . 3))
	       ((B . 1) (B . 3) (A . 1) (A . 3)))
	   :test #'equal))
  ((setq csp (make-csp :domains '((A 2 4) (B 3 5))
		       :neighbors (parse-neighbors "A: B; B: ")
		       :constraint-fn #'(lambda (x xv y yv)
					  (or (and (eq x 'A) (eq y 'B))
					      (and (eq x 'B) (eq y 'A)
						   (> xv yv)))))))
  ((ac-3 csp) t)
  ((ac-3b (copy-csp-fresh csp)) t)
  ((ac-4 (copy-csp-fresh csp)) t)
  "REVISE removes nothing when every value has a support:"
  ((setq csp (make-csp :domains '((A 0) (B 4))
		       :neighbors (parse-neighbors "A: B; B: ")
		       :constraint-fn #'(lambda (x xv y yv)
					  (declare (ignore x y))
					  (and (evenp xv) (= (+ xv yv) 4))))))
  ((csp-revise (csp-support-pruning csp) 'A 'B nil) nil)

  "A tree-structured CSP solved in linear time [Fig. 6.11]:"
  ((setq csp (make-map-coloring-csp '(R B) "NT: WA Q; NSW: Q V")))
  ((tree-csp-solver csp)
   (and (csp-solution? csp *)
	(not (eq (cdr (assoc 'NT *)) (cdr (assoc 'WA *))))))

  "N-ary CSPs, solved by generalized arc consistency and domain splitting."
  ((setq ncsp (make-nary-csp
	       :domains '((a 1 2 3) (b 1 2 3))
	       :constraints (list (make-nary-constraint
				   :scope '(a b)
				   :condition #'all-diff-constraint)))))
  ((nary-csp-variables ncsp) '(a b))
  ((nary-csp-consistent? ncsp '((a . 1) (b . 2))) *)
  ((nary-csp-consistent? ncsp '((a . 1) (b . 1))) nil)
  ((length (gethash 'a (nary-csp-var-to-const ncsp))) 1)
  "A crossword with one variable for each word:"
  ((setq ncsp (make-word-crossword-csp)))
  ((ac-solver ncsp)
   (and (= (length *) 5) (nary-csp-consistent? ncsp *)))
  ((ac-solver ncsp :arc-heuristic #'no-heuristic)
   (and (= (length *) 5) (nary-csp-consistent? ncsp *)))
  ((ac-search-solver ncsp)
   (and (= (length *) 5) (nary-csp-consistent? ncsp *)))
  ((gac ncsp) *)
  "A crossword with one variable for each cell:"
  ((setq ncsp (make-crossword-csp '("___" "_*_" "___")
				  '("bat" "tea" "bet" "ant"))))
  ((setq solution (ac-solver ncsp))
   (and (= (length *) 8) (nary-csp-consistent? ncsp *)))
  ((display-crossword '("___" "_*_" "___") solution))
  "Cryptarithmetic: TWO + TWO = FOUR [Fig. 6.2]."
  ((ac-solver (make-two-two-four-csp)) (and * (two-two-four-solution? *)))
  ((ac-search-solver (make-two-two-four-csp))
   (and * (two-two-four-solution? *)))
  "Two Kakuro puzzles:"
  ((setq ncsp (make-kakuro-csp *kakuro1*)))
  ((setq solution (ac-solver ncsp))
   (and (= (length *) 8) (nary-csp-consistent? ncsp *)))
  ((display-kakuro *kakuro1* solution))
  ((setq ncsp (make-kakuro-csp *kakuro2*)))
  ((ac-search-solver ncsp)
   (and (= (length *) 8) (nary-csp-consistent? ncsp *)))

  "The 4th edition search algorithms of chapters 3-5."
  "Depth-limited and iterative deepening search [Fig. 3.12]:"
  ((depth-limited-search (make-romanian-problem) 2) :cut-off)
  ((solution-actions (iterative-deepening-search (make-romanian-problem)))
   '(Sibiu Fagaras Bucharest))
  "Bidirectional best-first search [Fig. 3.14], with f = g (bidirectional"
  "uniform-cost search) and with f = g + h (bidirectional A*):"
  ((solution-actions (bidirectional-search (make-romanian-problem)))
   '(Sibiu Rimnicu Pitesti Bucharest))
  ((node-g-cost (bidirectional-search (make-romanian-problem))) 418)
  ((node-g-cost (bidirectional-a*-search (make-romanian-problem))) 418)
  ((node-g-cost (bibf-search (make-romanian-problem) #'node-f-cost
			     (reverse-problem (make-romanian-problem))
			     #'node-f-cost))
   418)
  "Recursive best-first search [Fig. 3.22] and SMA* [Section 3.5.5]:"
  ((solution-actions (recursive-best-first-search (make-romanian-problem)))
   '(Sibiu Rimnicu Pitesti Bucharest))
  ((solve (make-romanian-problem) 'recursive-best-first-search) *)
  ((solution-actions (sma*-search (make-romanian-problem) 100))
   '(Sibiu Rimnicu Pitesti Bucharest))
  ((node-g-cost (sma*-search (make-romanian-problem) 8)) 418)
  "With memory for 3 nodes no solution fits:"
  ((sma*-search (make-romanian-problem) 3) (null *))
  ((compare-search-algorithms #'make-romanian-problem
			      '(A*-search recursive-best-first-search
				bidirectional-search bidirectional-a*-search
				sma*-search)
			      :n 1))
  "Local beam search [Section 4.1.3] and the genetic algorithm [Fig. 4.7]:"
  ((node-state (nth-value 1 (local-beam-search (make-romanian-problem))))
   'Bucharest)
  ((genetic-algorithm
    (genetic-init-population 8 '(R G) 4)
    #'(lambda (c)
	(count-if #'(lambda (edge)
		      (not (eq (nth (first edge) c) (nth (second edge) c))))
		  '((0 1) (0 3) (1 2) (2 3))))
    :gene-pool '(R G) :f-thres 4)
   (member * '((R G R G) (G R G R)) :test #'equal))
  ((genetic-algorithm (genetic-init-population 100 (iota 8) 8)
		      #'genetic-queens-fitness :gene-pool (iota 8) :f-thres 25)
   (>= (genetic-queens-fitness *) 25))
  "AND-OR search [Fig. 4.10] in the erratic vacuum world [Fig. 4.9]:"
  ((setq plan (and-or-graph-search (make-erratic-vacuum-problem))) *)
  ((first plan) 'suck)
  ((and-or-plan-succeeds? (make-erratic-vacuum-problem) '(A dirty dirty) plan)
   *)
  ((every #'(lambda (s)
	      (let ((p (make-erratic-vacuum-problem :initial-state s)))
		(and-or-plan-succeeds? p s (and-or-graph-search p))))
	  '((A dirty dirty) (B dirty dirty) (A clean dirty) (B clean dirty)
	    (A dirty clean) (B dirty clean) (A clean clean) (B clean clean)))
   *)
  "Online DFS [Fig. 4.20] and LRTA* [Fig. 4.23] agents in the"
  "one-dimensional state space of [Fig. 4.23]:"
  ((setq p (make-one-dim-state-space-problem)))
  ((funcall (make-lrta*-agent-program p) 'state3) 'right)
  ((funcall (make-online-dfs-agent-program p) 'state5) (null *))
  ((run-online-agent (make-online-dfs-agent-program p) p) 'state5)
  ((run-online-agent (make-lrta*-agent-program p) p) 'state5)
  ((setq p (make-one-dim-state-space-problem :initial-state 'state1)))
  ((run-online-agent (make-online-dfs-agent-program p) p) 'state5)
  ((run-online-agent (make-lrta*-agent-program p) p) 'state5)
  "Expectiminimax [Section 5.5] on a small tree with chance nodes:"
  ((expectiminimax-decision (make-small-chance-game-tree)) 'a2)
  ((expectiminimax-value (make-small-chance-game-tree) 'a)
   (< (abs (- * 18.1)) 0.001))
  ((expectiminimax-decision (make-small-chance-game-tree '(2 3 1 4 1 2 4 5)))
   'a1)
  "Iterative deepening alpha-beta [Section 5.3.2] and Monte Carlo tree"
  "search [Fig. 5.11] find the winning move of X in tic-tac-toe:"
  ((setq ttt (make-ttt-game)))
  ((setq s (make-game-state
	    :board (make-array '(3 3) :initial-contents
			       '((X X -) (O O -) (- - -)))
	    :players '(X O))))
  ((iterative-deepening-alpha-beta-decision s ttt #'ttt-eval :max-depth 3)
   '(0 2))
  ((monte-carlo-tree-search s ttt 1000) '(0 2))
  ((run-game (make-ttt-game) :agents '(mcts-game-agent random-game-agent)))
  ((run-game (make-ttt-game)
	     :agents '(id-alpha-beta-ttt-agent random-game-agent)))

  "Problems and problem-solving agents of chapter 3."
  "The map of Romania [Fig. 3.1] and the 8-puzzle [Fig. 3.3]:"
  ((length *romania-map*) 20)
  ((road-distance (find-city 'Arad *romania-map*) 'Sibiu) 140)
  ((8-puzzle-ref *8-puzzle-goal* 4) 0)
  ((node-depth (A*-search (make-8-puzzle-problem
			   :initial-state (move-blank (move-blank *8-puzzle-goal*
								  4 1)
						      1 0))))
   2)
  "A simple problem-solving agent [Fig. 3.1] reaches Bucharest:"
  ((setq ps-env (problem->environment
		 (make-romanian-problem :initial-state 'Arad :goal 'Bucharest))))
  ((typep (first (environment-agents ps-env)) 'problem-solving-agent) t)
  ((progn (setf (environment-stream ps-env) nil)
	  (run-environment ps-env)
	  (environment-state ps-env))
   'Bucharest)
  "Tree search [Fig. 3.7] (here breadth-first), and best-first search"
  "[Fig. 3.7] with f = h (greedy) and f = g + h (A*):"
  ((solution-actions (general-search (make-romanian-problem :goal 'Fagaras)
				     #'enqueue-at-end))
   '(Sibiu Fagaras))
  ((solution-actions (best-first-search (make-romanian-problem)
					#'node-h-cost))
   '(Sibiu Fagaras Bucharest))
  ((node-g-cost (best-first-search (make-romanian-problem) #'node-f-cost))
   418)
  "Depth-first tree search on the finite tree of 4-queens; 3-queens has"
  "no solution:"
  ((depth-first-search (make-nqueens-problem :n 4))
   (and * (CSP-goalp (node-state *)) (= (node-depth *) 4)))
  ((depth-first-search (make-nqueens-problem :n 3)) nil)
  "Graph search [Fig. 3.7] expands each state once, so depth-first graph"
  "search terminates on the map of Romania:"
  ((solution-actions (breadth-first-graph-search (make-romanian-problem)))
   '(Sibiu Fagaras Bucharest))
  ((solution-actions (graph-search (make-romanian-problem :goal 'Fagaras)
				   #'enqueue-at-end))
   '(Sibiu Fagaras))
  ((setq p (make-romanian-problem)))
  ((depth-first-graph-search p) (eq (node-state *) 'Bucharest))
  ((problem-num-expanded p) (<= * 20))
  "The water pouring problem: measure 4 with jugs of 3 and 5."
  ((setq pour (breadth-first-graph-search
	       (make-pour-problem :initial-state '(0 0) :capacities '(3 5)
				  :goals '(4)))))
  ((typep (make-pour-problem :initial-state '(0 0)) 'pour-problem) t)
  ((length (solution-actions pour)) 6)
  ((member 4 (node-state pour)) *)
  ((solution-actions (breadth-first-graph-search
		      (make-pour-problem :initial-state '(2 4 3)
					 :capacities '(8 16 32) :goals '(7))))
   (member * '(((pour 1 2)) ((pour 2 1))) :test #'equal))
  "Shortest paths on a 5x5 grid with a wall that leaves a gap at the top;"
  "a goal walled off on every side is unreachable."
  ((setq grid (make-grid-problem :initial-state '(0 0) :goal '(4 0)
				 :width 5 :height 5
				 :obstacles '((2 0) (2 1) (2 2) (2 3)))))
  ((typep grid 'grid-problem) t)
  ((node-g-cost (A*-search grid)) 12)
  ((node-g-cost (breadth-first-graph-search grid)) 12)
  ((every #'(lambda (node) (grid-passable? grid (node-state node)))
	  (solution-nodes (A*-search grid)))
   t)
  ((A*-search (make-grid-problem :initial-state '(0 0) :goal '(4 4)
				 :width 5 :height 5
				 :obstacles '((3 4) (4 3))))
   nil)
  ((node-g-cost (A*-search (make-grid-problem :initial-state '(0 0)
					      :goal '(3 3) :width 5 :height 5
					      :directions *directions8*)))
   3)

  "Local search [Section 4.1] for the highest peak of a grid: hill climbing"
  "stops on a local maximum; on a grid with a single peak at (2 2) every"
  "variant, and random-restart hill climbing, reaches it."
  ((node-state (nth-value 1 (hill-climbing-search
			     (make-peak-finding-problem
			      :initial-state '(0 0)
			      :grid '((0 5 10 20) (-3 7 11 5))))))
   '(0 3))
  ((node-state (nth-value 1 (hill-climbing-search
			     (make-peak-finding-problem
			      :initial-state '(0 0)
			      :grid '((0 5 10 8) (-3 7 9 999) (1 2 5 11))))))
   '(0 2))
  ((setq peak-grid '((1 2 3) (2 3 4) (3 4 5))))
  ((typep (make-peak-finding-problem :initial-state '(0 0) :grid peak-grid)
	  'peak-finding-problem)
   t)
  ((node-state (nth-value 1 (stochastic-hill-climbing-search
			     (make-peak-finding-problem :initial-state '(0 0)
							:grid peak-grid))))
   '(2 2))
  ((node-state (nth-value 1 (first-choice-hill-climbing-search
			     (make-peak-finding-problem :initial-state '(0 0)
							:grid peak-grid))))
   '(2 2))
  ((node-state (random-restart-search
		#'(lambda ()
		    (make-peak-finding-problem
		     :initial-state (list (random 3) (random 3))
		     :grid peak-grid))
		5))
   '(2 2))
  "The erratic vacuum world [Fig. 4.9]: Suck on a dirty square may clean"
  "the other one too."
  ((typep (make-erratic-vacuum-problem) 'erratic-vacuum-problem) t)
  ((nondeterministic-results (make-erratic-vacuum-problem) '(A dirty dirty)
			     'suck)
   '((A clean dirty) (A clean clean)))
  "The online search problem of [Fig. 4.23], given as an explicit graph:"
  ((setq p (make-one-dim-state-space-problem)))
  ((typep p 'online-graph-problem) t)
  ((online-actions p 'state2) '(right left))
  ((h-cost p 'state1) 8)

  "Minimax [Fig. 5.3], minimax with a depth cutoff and alpha-beta [Fig. 5.7]"
  "find the winning move of X, and the move of O that blocks X."
  ((setq ttt (make-ttt-game)))
  ((typep ttt 'ttt-game) t)
  ((typep (make-cognac-game) 'cognac-game) t)
  ((setq s (make-game-state
	    :board (make-array '(3 3) :initial-contents
			       '((X X -) (O O -) (- - -)))
	    :players '(X O))))
  ((minimax-decision s ttt) '(0 2))
  ((minimax-cutoff-decision s ttt #'ttt-eval 2) '(0 2))
  ((alpha-beta-decision s ttt #'ttt-eval) '(0 2))
  ((alpha-beta-cutoff-decision s ttt #'ttt-eval 3) '(0 2))
  ((setq s (make-game-state
	    :board (make-array '(3 3) :initial-contents
			       '((X X -) (O - -) (- - O)))
	    :players '(O X))))
  ((minimax-decision s ttt) '(0 2))
  ((minimax-cutoff-decision s ttt #'ttt-eval 2) '(0 2))
  ((alpha-beta-decision s ttt #'ttt-eval) '(0 2))
  "A game with chance nodes is given as an explicit tree:"
  ((typep (make-small-chance-game-tree) 'chance-game-tree) t)

  "CSPs of chapter 6.  The arc heuristics pick the next arc to revise:"
  ((setq csp (make-csp :domains '((A 1 2 3) (B 1 2 3) (C 1))
		       :neighbors (parse-neighbors "A: B; B: C"))))
  ((progn (csp-support-pruning csp)
	  (no-arc-heuristic csp '((A . B) (B . C))))
   '(A . B))
  ((dom-j-up csp '((A . B) (B . C))) '(B . C))
  ((ac-3 (copy-csp-fresh even-csp) :arc-heuristic #'no-arc-heuristic) t)
  "Sudoku [Section 6.2.6]: AC-3 alone solves the easy puzzle; the harder"
  "one needs search."
  ((setq csp (make-sudoku-csp *easy-sudoku*)))
  ((ac-3 csp :arc-heuristic #'no-arc-heuristic) t)
  ((sudoku-solution-string (csp-infer-assignment csp))
   "483921657967345821251876493548132976729564138136798245372689514814253769695417382")
  ((setq csp (make-sudoku-csp *harder-sudoku*)))
  ((backtracking-search csp :select-unassigned-variable #'mrv
			:inference #'forward-checking)
   (and (csp-solution? csp *)
	(every #'(lambda (cell)
		   (let ((c (char *harder-sudoku* cell)))
		     (or (not (digit-char-p c))
			 (= (digit-char-p c) (cdr (assoc cell *))))))
	       (iota 81))))
  "The Zebra puzzle: the Japanese owns the zebra, the Norwegian drinks water."
  ((setq csp (make-zebra-csp)))
  ((setq solution (backtracking-search csp :select-unassigned-variable #'mrv
				       :inference #'forward-checking))
   (csp-solution? csp *))
  ((list (cdr (assoc 'Zebra solution)) (cdr (assoc 'Japanese solution))
	 (cdr (assoc 'Water solution)) (cdr (assoc 'Norwegian solution)))
   '(5 5 1 1))
  )
