def astar_single(states, start, goal): visited = {} nodes, num_expanded = [], 0 start_node = (start, manhattan(start, goal), 0) nodes.append(start_node) visited[start] = start while nodes: min_node = min(nodes, key=lambda x: x[1]) coord, h, cost = min_node nodes.remove(min_node) num_expanded += 1 if coord == goal: print("Found goal using A* (single goal)") return visited, num_expanded for direction in DIRS: nextCoord = (coord[0] + direction[0], coord[1] + direction[1]) val = visited.get(nextCoord) if nextCoord in states: if val is None: nodes.append((nextCoord, cost + manhattan(nextCoord, goal), cost + 1)) visited[nextCoord] = coord else: new_h = cost + manhattan(nextCoord, goal) if new_h < h: nodes.append( (nextCoord, cost + manhattan(nextCoord, goal), cost + 1)) visited[nextCoord] = coord
def get_distance(result): locs = list( set(result['geohashed_start_loc']) | set(result['geohashed_end_loc'])) if np.nan in locs: locs.remove(np.nan) deloc = [] for loc in locs: deloc.append(geohash.decode_exactly(loc)) loc_dict = dict(zip(locs, deloc)) geohashed_loc = result[['geohashed_start_loc', 'geohashed_end_loc']].values distance = [] manhattan_distance = [] for i in geohashed_loc: if i[0] is not np.nan and i[1] is not np.nan: lat1, lon1, _, _ = loc_dict[i[0]] lat2, lon2, _, _ = loc_dict[i[1]] distance.append( cal_distance(float(lat1), float(lon1), float(lat2), float(lon2))) manhattan_distance.append( manhattan(float(lat1), float(lon1), float(lat2), float(lon2))) else: distance.append(np.nan) manhattan_distance.append(np.nan) result.loc[:, 'distance'] = distance result.loc[:, 'manhattan'] = manhattan_distance return result
def astar_multiple(states, start, goals): """ Implements A* search on a maze with multiple goals Currently using the naive strategy of "next dot = closest dot to current point" Arguments: states {set of tuples} -- represents the "empty" states in the maze start {tuple} -- the starting point goals {list of tuples} -- list of dots that need to be reached Returns: list of tuples, int -- returns the path between the dots and the number of nodes expanded """ goals = copy.deepcopy(goals) num_expanded = 0 coord = start path = deque() reached = deque() path.appendleft(start) while goals: nodes, path_to_dot = [], {} goal = get_closest_dot(goals, coord) # Current dot selection strategy goals.remove(goal) reached.append(goal) start_node = (coord, manhattan(coord, goal), 0) nodes.append(start_node) path_to_dot[coord] = coord while nodes: min_node = min(nodes, key=lambda x: x[1]) coord, _, cost = min_node nodes.remove(min_node) num_expanded += 1 if coord == goal: print("Found goal at {0}".format(goal)) path.extend(visited_to_path_deque(path_to_dot, goal)) break for direction in DIRS: nextCoord = (coord[0] + direction[0], coord[1] + direction[1]) if nextCoord in states and nextCoord not in path_to_dot: nodes.append((nextCoord, cost + manhattan(nextCoord, goal), cost + 1)) path_to_dot[nextCoord] = coord return path, reached, num_expanded
def search_a_star(self, initial_x, initial_y, final_x, final_y): openn = [] closed = [] parents = {} start = (initial_x, initial_y) goal = (final_x, final_y) start_distances = {} goal_distances = {} openn.append(start) parents[start] = None start_distances[start] = 0 while openn: current = min(openn, key=lambda o: start_distances.get( o, 0) + goal_distances.get(o, 0)) if current == goal: path = [] path.append(current) while parents[current]: current = parents[current] path.append(current) return path[::-1] openn.remove(current) closed.append(current) neigbhors = utils.get_neighbors( self.__repository.mapp, current[0], current[1]) for node in neigbhors: if node in closed: continue new_distance = start_distances[current] + 1 if node in openn: if start_distances[node] > new_distance: start_distances[node] = new_distance parents[node] = current else: start_distances[node] = new_distance goal_distances[node] = utils.manhattan( node[0], node[1], goal[0], goal[1]) parents[node] = current openn.append(node) return []
def greedy(states, start, goal): visited = {} nodes, num_expanded = [], 0 start_node = (start, manhattan(start, goal)) nodes.append(start_node) visited[start] = start while nodes: min_node = min(nodes, key=lambda x: x[1]) coord = min_node[0] nodes.remove(min_node) num_expanded += 1 if coord == goal: print("Found goal using Greedy BFS") return visited, num_expanded for direction in DIRS: nextCoord = (coord[0] + direction[0], coord[1] + direction[1]) if nextCoord in states and nextCoord not in visited: nodes.append((nextCoord, manhattan(nextCoord, goal))) visited[nextCoord] = coord
def score(self,game1,game2,method="manhattan",option=2,maxgap=15,coefMat=1,coefGap=1,coefApm=1,coefFreq=1): #print("coefs",coefMat,coefGap,coefApm,coefFreq) man=coefMat*utils.manhattan(game1.matrix,game2.matrix) gap=coefGap*utils.distancedict(game1.frequency_of_gap,game2.frequency_of_gap,maxgap) apm=coefApm*pow((max(game1.APM,game2.APM)/min(game1.APM,game2.APM))-1,option) freq=coefFreq*utils.distancearray(game1.frequency_of_hotkeys,game2.frequency_of_hotkeys) return man+gap+apm+freq if method=="manhattan": return utils.manhattan(game1.matrix,game2.matrix) elif method=="apm": s=utils.manhattan(game1.matrix,game2.matrix) ecartapm=(max(game1.APM,game2.APM)/min(game1.APM,game2.APM))-1 s=s+math.pow(ecartapm,option) return s elif method=="frequency": ecartapm=abs(max(game1.APM,game2.APM))/min(game1.APM,game2.APM)-1 return utils.distancearray(game1.frequency_of_hotkeys,game2.frequency_of_hotkeys)+math.pow(ecartapm,option) elif method=="gap": s=utils.manhattan(game1.matrix,game2.matrix) return utils.distancedict(game1.frequency_of_gap,game2.frequency_of_gap,maxgap)+s
def part1() -> int: # It will be one of the particles with the smallest acceleration # However it appears there are 3 of them data = load_data() min_a = None min_accels = {} for i, particle in enumerate(data): mag = utils.magnitude(*particle.a.as_tuple()) if mag == min_a: min_accels[i] = particle else: old_min = min_a min_a = utils.safe_min(min_a, mag) if old_min != min_a: min_accels = {i: particle} while True: # run simulation until all particles have all position,velocity, and # acceleration components with the same signs (or zero) should_break = True for particle in min_accels.values(): tick(particle) for dim in "xyz": a = getattr(particle.a, dim) v = getattr(particle.v, dim) p = getattr(particle.p, dim) if a != 0 and not ((a > 0 and v > 0 and p > 0) or (a < 0 and v < 0 and p < 0)): should_break = False break if should_break: # do some more steps just in case for _ in range(1000): for particle in min_accels.values(): tick(particle) break min_dist = None min_i = None for i, particle in min_accels.items(): dist = utils.manhattan(*particle.p.as_tuple()) min_dist = utils.safe_min(min_dist, dist) if min_dist == dist: min_i = i return min_i
def score(self, game1, game2, method="manhattan", option=2, maxgap=15, coefMat=1, coefGap=1, coefApm=1, coefFreq=1): #print("coefs",coefMat,coefGap,coefApm,coefFreq) man = coefMat * utils.manhattan(game1.matrix, game2.matrix) gap = coefGap * utils.distancedict(game1.frequency_of_gap, game2.frequency_of_gap, maxgap) apm = coefApm * pow( (max(game1.APM, game2.APM) / min(game1.APM, game2.APM)) - 1, option) freq = coefFreq * utils.distancearray(game1.frequency_of_hotkeys, game2.frequency_of_hotkeys) return man + gap + apm + freq if method == "manhattan": return utils.manhattan(game1.matrix, game2.matrix) elif method == "apm": s = utils.manhattan(game1.matrix, game2.matrix) ecartapm = (max(game1.APM, game2.APM) / min(game1.APM, game2.APM)) - 1 s = s + math.pow(ecartapm, option) return s elif method == "frequency": ecartapm = abs(max(game1.APM, game2.APM)) / min( game1.APM, game2.APM) - 1 return utils.distancearray(game1.frequency_of_hotkeys, game2.frequency_of_hotkeys) + math.pow( ecartapm, option) elif method == "gap": s = utils.manhattan(game1.matrix, game2.matrix) return utils.distancedict(game1.frequency_of_gap, game2.frequency_of_gap, maxgap) + s
def get_distances(states, goals): results = {} for state in states: if state in goals: results[state] = 0 continue smallest = -1 for goal in goals: dist = manhattan(state, goal) smallest = dist if (dist < smallest or smallest == -1) else smallest if smallest == 1: break results[state] = smallest return results
def get_distance(result): locs = list(set(result['geohashed_start_loc']) | set(result['geohashed_end_loc'])) if np.nan in locs: locs.remove(np.nan) deloc = [] for loc in locs: deloc.append(geohash.decode_exactly(loc)) loc_dict = dict(zip(locs, deloc)) geohashed_loc = result[['geohashed_start_loc', 'geohashed_end_loc']].values distance = [] manhattan_distance = [] for i in geohashed_loc: if i[0] is not np.nan and i[1] is not np.nan: lat1, lon1, _, _ = loc_dict[i[0]] lat2, lon2, _, _ = loc_dict[i[1]] distance.append(cal_distance(float(lat1), float(lon1), float(lat2), float(lon2))) manhattan_distance.append(manhattan(float(lat1), float(lon1), float(lat2), float(lon2))) else: distance.append(np.nan) manhattan_distance.append(np.nan) result.loc[:, 'distance'] = distance result.loc[:, 'manhattan'] = manhattan_distance return result