Advertisement
Guest User

AdaptedAStar

a guest
Oct 14th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. def adaptedAStar(mLength,bList, start, goal, show = False):
  2.     maximumG = mLength * mLength
  3.     currCell = start
  4.     gCell = goal
  5.     magMap = np.zeros((5,mLength, mLength))
  6.     magMap[3] = np.zeros((mLength,mLength), dtype = bool)
  7.     magMap[4] = np.zeros((mLength,mLength), dtype = bool)
  8.     magMap[2][currCell] = 1
  9.     maxG = 0
  10.     totalSteps = 0
  11.     totalExpense = 0
  12.     currTrack = [start]
  13.     update(magMap,mLength,currCell,bList)
  14.     while currCell != gCell:
  15.         totalSteps += 1
  16.         magMap[1][currCell] = 0
  17.         magMap[0][currCell] = totalSteps
  18.         magMap[1][gCell] = np.inf
  19.         magMap[0][gCell] = totalSteps
  20.         oList = []
  21.         oDict = dict()
  22.         fDict = dict()
  23.         magMap[4] = np.zeros((mLength,mLength), dtype = bool)
  24.         bh.insert(oList, oDict, ManhattanDistanes(currCell,gCell), currCell)
  25.         while oList and magMap[1][gCell] > oList[0]:
  26.             coloredCell = bh.pop(oList, oDict)
  27.             magMap[4][coloredCell] = True
  28.             totalExpense += 1
  29.             for newCell in nextNeighbor(coloredCell,mLength):
  30.                 if magMap[2][newCell] != 2 :
  31.                     if magMap[3][newCell]:
  32.                         newHeuris = maxG - magMap[1][newCell]
  33.                     else:
  34.                         newHeuris = ManhattanDistanes(newCell,gCell)
  35.                     if magMap[0][newCell] < totalSteps:
  36.                         magMap[1][newCell] = np.inf
  37.                         magMap[0][newCell] = totalSteps
  38.                     if magMap[1][newCell] > magMap[1][coloredCell] + 1:
  39.                         fDict[newCell] = coloredCell
  40.                         newG = magMap[1][coloredCell] + 1
  41.                         bh.insert(oList, oDict, (maximumG*( newG +  newHeuris) -  newG) , newCell )
  42.                         magMap[1][newCell] = newG
  43.         magMap[3] = magMap[4]
  44.         maxG = magMap[1][gCell]
  45.         if not oList:
  46.             return None, None
  47.         currPath = getForwardPath(fDict,currCell,gCell)
  48.         if show:
  49.             if currCell == start:
  50.                 plt.ion()
  51.                 im, fig,oLine,pLine = showLaunch(mLength, map = magMap, pList = currPath , old = currTrack)
  52.                 plt.show()
  53.             else:
  54.                 showUpdate(im, fig, oLine,pLine, magMap[2],  currTrack ,currPath)
  55.         for cell in currPath:
  56.             if cell == currCell:
  57.                 continue
  58.             else:
  59.                 if magMap[2][cell] != 2 :
  60.                     currTrack.append(cell)
  61.                     currCell = cell
  62.                     update( magMap,mLength,currCell,bList)
  63.                 else:
  64.                     break
  65.     if show:
  66.         showUpdate(im, fig, oLine,pLine, magMap[2],  currTrack ,currPath)
  67.     return currTrack,totalExpense
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement