def get_next_direction(self, start, goal): self.start = start self.goal = goal graph = self.create_graph(self.game_map) path = astar_path(graph, start, goal) direction = self.convert_node_to_direction(path) return direction
def path_between(self, pointA, pointB, avoid_bots=True): start = self.pathfinder.start = pointA goal = self.pathfinder.goal = pointB game_map = self.pathfinder.parse_game_state(self.game_state) graph = self.pathfinder.create_graph(game_map, avoid_bots=avoid_bots) path = astar_path(graph, start, goal, self.manhattan_distance) return path
def optimalPath(source, target, passcode, part2=False): sourceState = (source, '') targetState = (target, '') manhattan = lambda c1, c2: abs(c1[0][0] - c2[0][0]) + abs(c1[0][1] - c2[0][ 1]) if part2: # Infinite targetWeight repels longest path from target node inf = sys.maxsize SV = SecureVault(passcode, targetState, defaultWeight=-1, targetWeight=inf) path = astar_path(SV, sourceState, targetState, heuristic=manhattan) else: SV = SecureVault(passcode, targetState) path = astar_path(SV, sourceState, targetState, heuristic=None) route = path[-2][1] return len(route) if part2 else route
def get_next_direction(self, start, goal): self.pathfinder.start = start self.pathfinder.goal = goal graph = self.pathfinder.create_graph(self.pathfinder.game_map) direction = None try: path = astar_path(graph, start, goal, weight='cost') direction = self.pathfinder.convert_node_to_direction(path) except Exception: pass return direction
def shortestPathLength(source, target, seed, part2=False): PT = PathTimer() CM = CubicleMaze(seed) cutoff = sum(target) if part2: paths = single_source_shortest_path(CM, source, cutoff=cutoff) path = paths.keys() else: manhattan = lambda c1, c2: abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) path = astar_path(CM, source, target, heuristic=manhattan, weight="weight") CM.plotPath(cutoff, cutoff, path) return len(path) if part2 else len(path) - 1
def get_nearest_material_deposit(self, prefer_unvisited=False): possible_goals = [] for material in self.materials: if not self.materials[material]['visited'] or not prefer_unvisited: possible_goals.append(material) if not possible_goals and prefer_unvisited: return self.get_nearest_material_deposit() nearest = None for possible in possible_goals: start = self.pathfinder.start = self.character_state['location'] goal = self.pathfinder.goal = possible game_map = self.pathfinder.parse_game_state(self.game_state) graph = self.pathfinder.create_graph(game_map) path = astar_path(graph, start, goal, self.manhattan_distance) if nearest is None or len(path) < nearest[0]: nearest = (len(path), possible) return nearest[1]
def shortestPath(instructions, part2=False, bidirectional=False): timer = PathTimer() RTF = RadioisotopeTestingFacility() source = getSourceState(instructions) if part2: # New items on floor 1: # An elerium generator, an elerium-compatible microchip, # a dilithium generator, a dilithium-compatible microchip. source.chips[0].update({'E', 'D'}) source.gens[0].update({'E', 'D'}) target = getTargetState(source) if bidirectional: # Ye olde bidirectional search (slow) path = bidirectional_shortest_path(RTF, source, target) else: path = astar_path(RTF, source, target, heuristic=levelRise, weight="weight") for index, state in enumerate(path): print(index, state) return path
def find_path(self, source, target, graph='land'): from networkx.algorithms.shortest_paths import astar_path return astar_path( self.graphs[graph], source, target, self.a_star_heuristic)