Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE473 Winter 1998 1 01/30/98 State-Space Search Administrative –Reading: Chapter 4 –PS2 on the Web now Last time –problem solving as {graph search, Lisp.

Similar presentations


Presentation on theme: "CSE473 Winter 1998 1 01/30/98 State-Space Search Administrative –Reading: Chapter 4 –PS2 on the Web now Last time –problem solving as {graph search, Lisp."— Presentation transcript:

1 CSE473 Winter 1998 1 01/30/98 State-Space Search Administrative –Reading: Chapter 4 –PS2 on the Web now Last time –problem solving as {graph search, Lisp code} This time –Lisp code for 8-puzzle –search strategies

2 CSE473 Winter 1998 2 8-Puzzle, State Definition (defstruct state tiles actions) (defun copy-state-deeply (state) (make-state :tiles (copy-list (state-tiles state)) :actions (state-actions state))) (defun state-blank-pos (state) (position 0 (state-tiles state) :test '=))

3 CSE473 Winter 1998 3 8-Puzzle: Moving a Tile (defun state-new-pos (state dir) (let ((dimen (truncate (sqrt (length (state-tiles state))))) (blank-pos (state-blank-pos state))) (multiple-value-bind (row col) (truncate blank-pos dimen) (case dir ((:UP) (if (= row 0) NIL (+ (* dimen (- row 1)) col))) ((:DOWN) (if (= row (- dimen 1)) NIL (+ (* dimen (+ row 1)) col))) ((:LEFT) (if (= col 0) NIL (+ (* dimen row) (- col 1)))) ((:RIGHT) (if (= col (- dimen 1)) NIL (+ (* dimen row) (+ col 1))))))))

4 CSE473 Winter 1998 4 8-Puzzle: Utilities for Manipulating States (defun state-swap-pos (state pos1 pos2) (let* ((tiles (state-tiles state)) (temp (nth pos1 tiles))) (setf (nth pos1 tiles) (nth pos2 tiles)) (setf (nth pos2 tiles) temp))) (defun state-add-action (state action) (push action (state-actions state)))

5 CSE473 Winter 1998 5 8-Puzzle: Invoking the Search (defun make-initial-state (tiles) (make-state :tiles tiles :actions '())) (defun goal-state-p (state) (equal (state-tiles state) '(1 2 3 4 5 6 7 8 0))) (defun next-states (state) (remove NIL (list (move-blank state :UP) (move-blank state :DOWN) (move-blank state :LEFT) (move-blank state :RIGHT)))) (defun move-blank (state dir) (let ((blank-pos (state-blank-pos state)) (new-pos (state-new-pos state dir))) (cond ((null new-pos) NIL) (T (let ((new-state (copy-state-deeply state))) (state-swap-pos new-state blank-pos new-pos) (state-add-action new-state dir) new-state)))))


Download ppt "CSE473 Winter 1998 1 01/30/98 State-Space Search Administrative –Reading: Chapter 4 –PS2 on the Web now Last time –problem solving as {graph search, Lisp."

Similar presentations


Ads by Google