Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

astar_midterm.py

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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

puzz8_midterm.py

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions