Question: astar_midterm.py ************************************************************************************************************************************************* puzz8_midterm.py **************************************************************************************************************************************************** TH-2: Complete function extract path(,) in file astar-midterm.py. This function takes two arguments, the last state from open (which matches the

 astar_midterm.py ************************************************************************************************************************************************* puzz8_midterm.py **************************************************************************************************************************************************** TH-2: Complete function extract path(,) in file

astar_midterm.py

astar-midterm.py. This function takes two arguments, the last state from open (which

matches the goal state) and list of nodes closed. Its purpose is

to extract the path between the starting state and the goal state

from the nodes stored on closed. # MIDTERM TAKEHOME PROBLEM # define

*************************************************************************************************************************************************

puzz8_midterm.py

extractpath see below nxtpath extract path (nxt, closed) - print GOAL FOUND:

" print "State: print nxtstate print "PathL : %d" % len(nxtpath) print

"Steps: %d " % steps # return 3 items of info: the

last state, solution path, # and number of search steps (iterations); return

[nxtstate, nxtpath, steps] if nxt in closed: # MIDTERM provided adjust parents

(nxt, closed) continue closed.append (nxt) succ SUCCESSOR FCT (nxtstate) random. shuffle (succ)

for x in succ: xcost = EVAL FCT(x, target) # Node nxt

****************************************************************************************************************************************************

TH-2: Complete function extract path(,) in file astar-midterm.py. This function takes two arguments, the last state from open (which matches the goal state) and list of nodes closed. Its purpose is to extract the path between the starting state and the goal state from the nodes stored on closed. # MIDTERM TAKEHOME PROBLEM # define extractpath see below nxtpath extract path (nxt, closed) - print GOAL FOUND: " print "State: print nxtstate print "PathL : %d" % len(nxtpath) print "Steps: %d " % steps # return 3 items of info: the last state, solution path, # and number of search steps (iterations); return [nxtstate, nxtpath, steps] if nxt in closed: # MIDTERM provided adjust parents (nxt, closed) continue closed.append (nxt) succ SUCCESSOR FCT (nxtstate) random. shuffle (succ) for x in succ: xcost = EVAL FCT(x, target) # Node nxt is parent of node with state x newnodeNode (x,nxt, nxtdpth+1,xcosttnxtdpth+1) #check whether news tate [0] (or, x) is already on open or closed with #shorter path; if so, do not bother to put open; keeper = True for c in closed: if newnode.thestate == c.the state and\ newnode.thedepth > c.thedepth: keeper = False break if not keeper: continue for op in open: if newnode.thestateop.thestate and newnode.thedepth > op.thedepth: keeperFalse break if keeper: open.append (newnode) #open. sort (lambda x,y: CostCmp(x,y)) # NOTICE SORTING # below works for Python 2 and 3 open_plus [ (x,x.theeval) for x in open] open plus.sort (key operator.itemgetter (1)) open = [x for (x, ) in open plus] steps 1 return None # MIDTERM provided def adjust_parents (nd, nodes): for x in nodes: if nd =-x: # see whether x needs adjusting if nd.thedepth c.thedepth: keeper = False break if not keeper: continue for op in open: if newnode.thestateop.thestate and newnode.thedepth > op.thedepth: keeperFalse break if keeper: open.append (newnode) #open. sort (lambda x,y: CostCmp(x,y)) # NOTICE SORTING # below works for Python 2 and 3 open_plus [ (x,x.theeval) for x in open] open plus.sort (key operator.itemgetter (1)) open = [x for (x, ) in open plus] steps 1 return None # MIDTERM provided def adjust_parents (nd, nodes): for x in nodes: if nd =-x: # see whether x needs adjusting if nd.thedepth

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!