Module pacai.core.distanceCalculator
Functions
def computeDistances(layout)
-
Runs UCS to all other positions from each position.
def getDistanceOnGrid(distances, pos1, pos2)
def getGrids1D(x)
def getGrids2D(pos)
def isInt(pos)
Classes
class DistanceCalculator (layout, distancer)
-
Expand source code
class DistanceCalculator: def __init__(self, layout, distancer): self.layout = layout self.distancer = distancer self.cache = {} def run(self): if self.layout.walls not in self.cache: self.cache[self.layout.walls] = computeDistances(self.layout) self.distancer._distances = self.cache[self.layout.walls]
Methods
def run(self)
class Distancer (layout)
-
A class for computing and caching the shortest path between any two points in a given maze.
Example:
distancer = Distancer(gameState.getInitialLayout()) distancer.getDistance((1, 1), (10, 10))
Expand source code
class Distancer(object): """ A class for computing and caching the shortest path between any two points in a given maze. Example: ``` distancer = Distancer(gameState.getInitialLayout()) distancer.getDistance((1, 1), (10, 10)) ``` """ def __init__(self, layout): self._distances = None self.dc = DistanceCalculator(layout, self) def getMazeDistances(self): self.dc.run() def getDistance(self, pos1, pos2): """ The only function you will need after you create the object. """ if (self._distances is None): return manhattan(pos1, pos2) if isInt(pos1) and isInt(pos2): return self.getDistanceOnGrid(pos1, pos2) pos1Grids = getGrids2D(pos1) pos2Grids = getGrids2D(pos2) bestDistance = DEFAULT_DISTANCE for pos1Snap, snap1Distance in pos1Grids: for pos2Snap, snap2Distance in pos2Grids: gridDistance = self.getDistanceOnGrid(pos1Snap, pos2Snap) distance = gridDistance + snap1Distance + snap2Distance if bestDistance > distance: bestDistance = distance return bestDistance def getDistanceOnGrid(self, pos1, pos2): key = (pos1, pos2) if key in self._distances: return self._distances[key] raise Exception("Position not in grid: " + str(key)) def isReadyForMazeDistance(self): return (self._distances is not None)
Methods
def getDistance(self, pos1, pos2)
-
The only function you will need after you create the object.
def getDistanceOnGrid(self, pos1, pos2)
def getMazeDistances(self)
def isReadyForMazeDistance(self)