def call_algorithm(method, table): solved_table = build_standard_solved_table(table.rows, table.columns) time_before = time.time() if method == BFS: order = build_order(args.order) final_node = bfs.search(begin_table=table, solved_table=solved_table, order=order, max_depth=args.max_depth, random_orders=order is None) elif method == DFS: order = build_order(args.order) final_node = dfs.search(begin_table=table, solved_table=solved_table, order=order, max_depth=args.max_depth, random_orders=order is None) elif method == IDFS: order = build_order(args.order) final_node = idfs.search(begin_table=table, solved_table=solved_table, order=order, min_limit=args.min_limit, max_depth=args.max_depth) elif method == BEST_FIRST_SEARCH: final_node = best_first_search.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth) elif method == A_STAR: final_node = a_star.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth) elif method == SMA_STAR: final_node = sma_star.search(begin_table=table, solved_table=solved_table, heuristic=args.heuristic, max_depth=args.max_depth, max_memory=args.max_memory) if final_node is None: print("Could not find a solution!") return final_node.table.print() print("Solution found in " + str(time.time()-time_before) + 's') moves = utils.create_list_of_moves(final_node) print("Moves to solve the puzzle: " + str(len(moves))) print(utils.convert_moves(moves))
def test_1(): # Grid format: # 0 = Navigable space # 1 = Occupied space grid = [[0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0]] heuristic = [[9, 8, 7, 6, 5, 4], [8, 7, 6, 5, 4, 3], [7, 6, 5, 4, 3, 2], [6, 5, 4, 3, 2, 1], [5, 4, 3, 2, 1, 0]] init = [0, 0] goal = [len(grid) - 1, len(grid[0]) - 1] cost = 1 expand = search(grid, init, goal, cost, heuristic) print('--------------------------------------------') for row in expand: print(row) print('--------------------------------------------') assert expand == [[0, -1, -1, -1, -1, -1], [1, -1, -1, -1, -1, -1], [2, -1, -1, -1, -1, -1], [3, -1, 8, 9, 10, 11], [4, 5, 6, 7, -1, 12]]
def __init__(self, width, height): super().__init__(width, height) arcade.set_background_color(arcade.color.BLACK) self.set_update_rate(1/10) self.shape_list = None *_, self.final_state = search(grid, heuristic, init, goal, cost) self.path = get_extended_path(get_grid_path(self.final_state['path'])) self.smooth_path = deepcopy(self.path) self.change = 1 self.weight_data = 0.02 self.weight_smooth = 0.4 self.tolerance = 0.000001
def __init__(self, grid, width, height): super().__init__(width, height) arcade.set_background_color(arcade.color.BLACK) self.set_update_rate(1/2) self.shape_list = None self.grid = grid self.turtle_center = [ MARGIN + WIDTH // 2, (MARGIN + HEIGHT) * (ROW_COUNT - 1) + MARGIN + HEIGHT // 2 ] self.turtle = self.create_turtle(self.turtle_center[0], self.turtle_center[1]) self.path = search(grid, heuristic, init, goal, cost) self.visited = [] self.recreate_grid()
def connect_gates(self, gates): ''' Find the shortest path for a pair of gates. input: gates: tuple of gates to be connected returns: best: the shortest available path ''' best = [] n1 = a_star.node_neighbors(self, self.gates[gates[0]]) n2 = a_star.node_neighbors(self, self.gates[gates[1]]) for neighbor_1 in n1: for neighbor_2 in n2: path = a_star.search(neighbor_1, neighbor_2, self) if path != False and best == []: best = path continue if path != False and len(path) < len(best): best = path return best
def main(): raw_start = input( "Enter your starting coordinate: Example: 0,0 for X = 0 and Y = 0\n" ).split(',') start = (int(raw_start[0]), int(raw_start[1])) raw_goal = input( "Enter your goal coordinate: Example: 9,8 for X = 9 and Y = 8\n" ).split(',') goal = (int(raw_goal[0]), int(raw_goal[1])) grid = input_loader.load_map(input("Enter map name: Example: mapa1\n")) # grid = input_loader.load_map('mapa1') # start = (0,0) # goal = (9, 8) cost = 1 path = a_star.search(grid, start, goal, cost) print('\nTraveled path: \n', path[1]) print('\nTraveled path (drawn):') [print(i) for i in path[0]]
import collections import sys import a_star rules = collections.defaultdict(lambda: []) for line in sys.stdin: if line == "\n": break left, right = line.strip().split(" => ") rules[right].append(left) molecule = sys.stdin.readline().strip() def neighbors(node): for i in range(len(node)): for k, vs in rules.items(): if node[i:i+len(k)] == k: for v in vs: yield node[:i] + v + node[i+len(k):] def estimate_cost(node): return len(node) # Perform A* search from the final molecule to "e" using the length of string # as the heuristic. path = a_star.search(molecule, "e", neighbors, estimate_cost, progress_callback=lambda x: print(x)) print(len(path) - 1)
for p in producao: print(p) print() grafo_producao = gerar_grafo(producao) for name, node in grafo_producao.get_nodes().items(): for e in node.get_edges(): print(e) print() phrase = 'baba, nao existe: [iXXo], existe: [iXYXYo XaA I@e@@e@O], ba@e@ba ba@e@ba@e@ba@e@ba ba@e@baSba@e@ba ba@e@baSba@e@ba ba@e@baSba@e@ba ba@e@baSba@e@ba' node_start = data_structure.Node(phrase, 'start') first_node = a_star.search(grafo_producao, node_start) node = first_node while node.get_parent() != None: print(node) node = node.get_parent() if node.get_parent() == None: print(node) print() # import sys # if __name__ == '__main__':
# Library imports import sys import numpy as np # User defined library imports from file_read import read_shp, get_values from grid import create_grid from a_star import search from prompt_read import read_search_prompt, read_grid_prompt # Locate file pats path = "shape/crime_dt" # Get data's into data frame df, bbox = read_shp(path) # Convert it into numpy array to work with values = get_values(df, np.float32) # Get grid configuration from user input grid_size, threshold = read_grid_prompt() # Create grid with crime points grid, fig, ax = create_grid(values, bbox, threshold=threshold, grid_size=grid_size, plot=True) # Get search start end points from user input start, end = read_search_prompt(grid) # Define costs cost = [1, 1.3, 1.5] # Search for optimal path path = search(grid, cost, start, end, [fig, ax])
def move(): data = bottle.request.json direction = 'up' # TODO: Do things with data directions = ['up', 'down', 'left', 'right'] food_list = data.get('food') snakes = data.get('snakes') # TODO: Change this so it actually checks if the snake is ours for snake in snakes: if data.get('you') == snake.get('id'): snake_coords = snake.get('coords') snake_head = snake_coords[0] board_width = data.get('width') board_height = data.get('height') sorted_list = get_food_list(snake_head, data) first_food = sorted_list[0].loc primary_path = search(snake_head, data, first_food) sec_path = None if len(sorted_list) > 1: sec_food = sorted_list[1] new_list = get_food_list(first_food, data) sec_path = search(first_food, data, new_list[0].loc) new_path = search(snake_head, data, new_list[0].loc) if len(sorted_list) > 1: if not primary_path and sec_path: primary_path = new_path if len(sorted_list) > 1: if not sec_path: primary_path = search(snake_head, data, sec_food.loc) if not primary_path: #Desperation Move if (if_safe(up(snake_head,1), data)): return { 'move': 'up', 'taunt': random.choice(taunts), } if (if_safe(down(snake_head,1), data)): return { 'move': 'down', 'taunt': random.choice(taunts), } if (if_safe(left(snake_head,1), data)): return { 'move': 'left', 'taunt': random.choice(taunts), } if (if_safe(right(snake_head,1),data)): return { 'move': 'right', 'taunt': random.choice(taunts), } else: first_move = primary_path[-1] if (up(snake_head, 1) == first_move): direction = 'up' if (down(snake_head, 1) == first_move): direction = 'down' if (left(snake_head, 1) == first_move): direction = 'left' if (right(snake_head, 1) == first_move): direction = 'right' return { 'move': direction, 'taunt': random.choice(taunts), }