;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;Joey Baranski ;;ECE 478 HW 2 ;;Breadth First State Space Search ;;Code Adapted from Breadth-First State Space Search Algorithm ;;as described in AI Luger 5th Edition ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun build-record (state parent) (list state parent)) (defun get-state (state-tuple) (nth 0 state-tuple)) (defun get-row (state-tuple) (nth 0 (get-state state-tuple))) (defun get-col (state-tuple) (nth 1 (get-state state-tuple))) (defun get-parent (state-tuple) (nth 1 state-tuple)) (defun retrieve-by-state (state list) (cond ((null list) nil) ((equal state (get-state (car list))) (car list)) (t (retrieve-by-state state (cdr list))))) (defun build-solution (state) (cond ((null state) nil) (t (cons state (build-solution (get-parent (retrieve-by-state state *closed*))))))) (defun run-breadth (start) (setq *rowbound* 5) (setq *colbound* 5) (setq *startrow* (car start)) (setq *startcol* (car (cdr start))) (setq *currow* *startrow*) (setq *curcol* *startcol*) (setq *lab* (generate-labyrinth)) (setq *open* (list (build-record start nil))) (setq *closed* nil) (setq *moves* '(move-north move-south move-east move-west)) (breadth-first)) (defun generate-descendants (state moves) (cond ((null moves) nil) (t (let ((child (funcall (car moves) *currow* *curcol*)) (rest (generate-descendants state (cdr moves)))) (cond ((null child) rest) ((retrieve-by-state child rest) rest) ((retrieve-by-state child *open*) rest) ((retrieve-by-state child *closed*) rest) (t (cons (build-record child state) rest))))))) (defun breadth-first () (cond ((null *open*) nil) (t (let ((state (car *open*))) (setq *closed* (cons state *closed*)) (setq *currow* (get-row state)) (setq *curcol* (get-col state)) (cond ((or (and (equal *curcol* (- *colbound* 1)) (equal (aref *lab* *currow* *curcol*) 0)) (and (equal *currow* 0) (equal (aref *lab* *currow* *curcol*) 0)) (and (equal *curcol* 0) (equal (aref *lab* *currow* *curcol*) 0)) (and (equal *currow* (- *rowbound* 1)) (equal (aref *lab* *currow* *curcol*) 0))) (format t "labyrinth:~%~%") (print *lab*) (format t "~%~%Final Solution: ") (build-solution (car state))) (t (setq *open* (append (cdr *open*) (generate-descendants (get-state state) *moves*))) (breadth-first))))))) (defun move-north (cr cc) (cond ((or (null (numberp cr)) (equal (aref *lab* cr cc) 1)) nil) (t (list (+ cr 1) cc)))) (defun move-south(cr cc) (cond ((or (null (numberp cr)) (equal (aref *lab* cr cc) 1)) nil) (t (list (- cr 1) cc)))) (defun move-east(cr cc) (cond ((or (null (numberp cc)) (equal (aref *lab* cr cc) 1)) nil) (t (list cr (+ cc 1))))) (defun move-west(cr cc) (cond ((or (null (numberp cc)) (equal (aref *lab* cr cc) 1)) nil) (t (list cr (- cc 1))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;generate-labryinth and then ;;customize it with path(s) out ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun generate-labyrinth () (setf lab (make-array '(5 5) :initial-element 1)) (setf (aref lab 3 1) 0) (setf (aref lab 2 1) 0) (setf (aref lab 2 2) 0) (setf (aref lab 1 2) 0) (setf (aref lab 1 3) 0) (setf (aref lab 0 3) 0) ;(setf (aref lab 3 2) 0) (cond (t lab))) (defun print-labyrinth () (concatenate 'string (aref *lab* 0 0) " test" ;" " (aref *lab* 0 0) ;(aref *lab* 0 0) (aref *lab* 0 0) "~%" ;(aref *lab* 0 0) (aref *lab* 0 0) (aref *lab* 0 0) (aref *lab* 0 0) "~%" ;(aref *lab* 0 0) (aref *lab* 0 0) (aref *lab* 0 0) (aref *lab* 0 0) "~%") ))