Project 1: Background Informed search : 8- puzzle world BFS, DFS and A* algorithms Heuristics : Manhattan distance, Number of misplaced tiles Lisp programming Defstruct, defparameter, setf, list, zerop mapcar, append, cond, loop..do, aref Funcall, return-from, format and ….
A* search Combines –Uniform cost search g(n): Exact path cost from start state to node n Initial node: 0, later nodes: (parent g-value)+1 –Greedy search h(n): Heuristic path cost from node n to a goal state Initial node:0, later nodes: Manhattan distance/ Misplaced Number of tiles Heuristic function for A* f(n) = g(n) + h(n) choose h(n) so that it never overestimates (admissible) A*: Next node to expand is node with lowest f(n)
Some lisp Funcall(a b c d..) calls function “a” with arguments b,c,d.. Sort(list #’< :key x) sorts the “list” in “<“ order based on the x field of each item in “list” – destructive Append(list1 list2), cond (mapcar #’(lambda (x) (op)) ‘(list)) computes op on each item of list and returns new list (**Children fn**) ‘ quote (common mistake) protects from evaluation (Make-array ‘(2 3) :initial-element 0) 2 by 3 array (Aref array x y) returns x,y the element of array Equalp (cannot be used with arrays)
A* Search Code (defun A* (nodes goalp children-fn fvalue-fn) ( cond ((null nodes) nil) ;; Return the first node if it is a goal node. ((funcall goalp (first nodes)) (first nodes)) ;; Append the children to the set of old nodes (t (A* (sort (append (funcall children-fn (first nodes)) (rest nodes)) #'< :key fvalue-fn ) goalp children-fn fvalue-fn)))) (t (let ((temp (sort (append (funcall children-fn (first nodes) (rest nodes)) #'< :key fvalue-fn ))) (A* temp ‘goalp ‘children-fn ‘fvalue-fn))))) A list of nodes (state, action, parent, g-val, h-val, f-val) Functions : goalp: Compare current state to goal-state: returns true or nil children-fn: takes parent-node and returns a list of children nodes fvalue-fn: takes a node and returns its f-value ( a one line code) A*: Good place to find the maximum length of queue
Node structure & global parameters Node –State : Could be just an array (3*3) –Parent : again a node –Action :left, right, up, down from parent (string) –G-val : parent g-val + 1 –H-val : call manhattan distance or misplaced tiles –F-val: G-val + H-val –Start node(start state, NIL, NIL, 0,0,0) Global parameters (defparameter) Number of nodes generated Number of nodes expanded Maximum size of queue Goal state
goalp Goalp (state) –Compare state with goal (global) –Do NOT use equalp –Loop for each element in state to compare with corresponding element in goal state – generalize and write “equal-state” to compare any two states – return true / NIL (return-from)
Children Fn & puzzle children You have left-child, similarly right, up, down child puzzle children will call each of them on a given state (append left right down up) Good place to keep track of number of nodes generated and expanded
Heuristics Manhattan –for each element a at (x,y) in “state” Find position (another function) of a in goal state Say (x1,y1) Find (abs(x-x1)+abs(y-y1)) Number of misplaced tiles – modification of goalp – whenever 2 corresponding elements are not equal, increment a counter initially set to 0
Possible Order in writing functions Goalp Right, up, down children Children-fn Heuristics A* Then statistics