Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def adaptedAStar(mLength,bList, start, goal, show = False):
- maximumG = mLength * mLength
- currCell = start
- gCell = goal
- magMap = np.zeros((5,mLength, mLength))
- magMap[3] = np.zeros((mLength,mLength), dtype = bool)
- magMap[4] = np.zeros((mLength,mLength), dtype = bool)
- magMap[2][currCell] = 1
- maxG = 0
- totalSteps = 0
- totalExpense = 0
- currTrack = [start]
- update(magMap,mLength,currCell,bList)
- while currCell != gCell:
- totalSteps += 1
- magMap[1][currCell] = 0
- magMap[0][currCell] = totalSteps
- magMap[1][gCell] = np.inf
- magMap[0][gCell] = totalSteps
- oList = []
- oDict = dict()
- fDict = dict()
- magMap[4] = np.zeros((mLength,mLength), dtype = bool)
- bh.insert(oList, oDict, ManhattanDistanes(currCell,gCell), currCell)
- while oList and magMap[1][gCell] > oList[0]:
- coloredCell = bh.pop(oList, oDict)
- magMap[4][coloredCell] = True
- totalExpense += 1
- for newCell in nextNeighbor(coloredCell,mLength):
- if magMap[2][newCell] != 2 :
- if magMap[3][newCell]:
- newHeuris = maxG - magMap[1][newCell]
- else:
- newHeuris = ManhattanDistanes(newCell,gCell)
- if magMap[0][newCell] < totalSteps:
- magMap[1][newCell] = np.inf
- magMap[0][newCell] = totalSteps
- if magMap[1][newCell] > magMap[1][coloredCell] + 1:
- fDict[newCell] = coloredCell
- newG = magMap[1][coloredCell] + 1
- bh.insert(oList, oDict, (maximumG*( newG + newHeuris) - newG) , newCell )
- magMap[1][newCell] = newG
- magMap[3] = magMap[4]
- maxG = magMap[1][gCell]
- if not oList:
- return None, None
- currPath = getForwardPath(fDict,currCell,gCell)
- if show:
- if currCell == start:
- plt.ion()
- im, fig,oLine,pLine = showLaunch(mLength, map = magMap, pList = currPath , old = currTrack)
- plt.show()
- else:
- showUpdate(im, fig, oLine,pLine, magMap[2], currTrack ,currPath)
- for cell in currPath:
- if cell == currCell:
- continue
- else:
- if magMap[2][cell] != 2 :
- currTrack.append(cell)
- currCell = cell
- update( magMap,mLength,currCell,bList)
- else:
- break
- if show:
- showUpdate(im, fig, oLine,pLine, magMap[2], currTrack ,currPath)
- return currTrack,totalExpense
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement