def get_path_astart(maze, start, end): """ returns a list with the best path from the start to the end :param maze: matrice where 0 represents a walkable cell and 1 is a obstacle cell :param start: typle of form (x,y) :param end: typle of form (x,y) """ opened_list = [] closed_list = [] column_length = len(maze) line_length = len(maze[0]) start_node = Node(start, None) end_node = Node(end, None) opened_list.append(start_node) while opened_list: current_node = opened_list.pop(0) closed_list.append(current_node) if current_node == end_node: print("Found it!") return get_correct_path(current_node) neighbours = current_node.get_neighbours() for neighbour in neighbours: # check is the neighbour is outside the maze if (neighbour.position[0] >= line_length or neighbour.position[0] < 0 or neighbour.position[1] >= column_length or neighbour.position[1] < 0): continue # check is the neighbour is an obstacle if maze[neighbour.position[1]][neighbour.position[0]] == 1: continue if neighbour in closed_list: continue neighbour.compute_f(end_node) # if the neighbour is in the opened list check if the new one is a better fit if neighbour in opened_list: index = opened_list.index(neighbour) if opened_list[index].f > neighbour.f: opened_list[index] = neighbour continue opened_list = sorted_insert(opened_list, neighbour) print("Could not find a solution!")
def huffman(data: str): itemqueue = [Node(a, len(list(b))) for a, b in groupby(sorted(data))] heapq.heapify(itemqueue) while len(itemqueue) > 1: l = heapq.heappop(itemqueue) r = heapq.heappop(itemqueue) n = Node(None, l.weight + r.weight) n.setChild(l, r) heapq.heappush(itemqueue, n) codes = {} def generate_codes(s, node): if node.item: if not s: codes[node.item] = "0" else: codes[node.item] = s else: generate_codes(s + "0", node.left) generate_codes(s + "1", node.right) generate_codes("", itemqueue[0]) print(codes)
def get_start_node(size): print("Start State: ") for x in range(0, size*size): x = int(input()) start_state.append(x) start_node = Node(start_state, goal_state, None, None, 0, 0) return start_node
def make_child_node(node, goal, board): children = [] children.append( Node(change_state(node.state, 'up', board), goal, node, 'up', node.depth + 1, node.path_cost + 1)) children.append( Node(change_state(node.state, 'down', board), goal, node, 'down', node.depth + 1, node.path_cost + 1)) children.append( Node(change_state(node.state, 'left', board), goal, node, 'left', node.depth + 1, node.path_cost + 1)) children.append( Node(change_state(node.state, 'right', board), goal, node, 'right', node.depth + 1, node.path_cost + 1)) new_nodes = [x for x in children if x.state] return new_nodes
# parse arguments parser = argparse.ArgumentParser() parser.add_argument("--data", type=str, default="data/graph.json") parser.add_argument("--start", type=int, default=1) parser.add_argument("--end", type=int, default=10) args = parser.parse_args() # read file data = read_file(args.data) # create a graph graph = Graph() for vertex in data: # create a node t = Node(vertex["id"]) # set edges t.set_edges(vertex["edges"]) # add node to the graph graph.add_node(t) stack = [] done = False def iterate(start, end): start_node = graph.set_start(start) end_node = graph.set_end(end) global done if start_node == end_node: print("Found: {}".format(start_node.value))
def prepare_data(data): for node in data: n = Node(node['id']) n.set_edges(node['edges']) g.add_node(n)
def run_algos(state_dict, goal, board): entry = {} clear_dict(entry) #BFS sys.stdout.write("Generating stats for BFS..") for x in range(1, 21): result = bfs(Node(state_dict[x], goal, None, None, 0, 0), board, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "bfs") clear_dict(entry) #DLS sys.stdout.write("Generating stats for DLS..") for x in range(1, 21): result = dls(Node(state_dict[x], goal, None, None, 0, 0), board, x+1, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "dls") clear_dict(entry) #IDS sys.stdout.write("Generating stats for IDS..") for x in range(1, 21): result = ids(Node(state_dict[x], goal, None, None, 0, 0), board, 0, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "ids") clear_dict(entry) #UCS sys.stdout.write("Generating stats for UCS..") for x in range(1, 21): result = ucs(NodeGCost(state_dict[x], goal, None, None, 0, 0), board, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "ucs") clear_dict(entry) #GBFS sys.stdout.write("Generating stats for GBFS..") for x in range(1, 21): result = gbfs(Node(state_dict[x], goal, None, None, 0, 0), board, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "gbfs") clear_dict(entry) #ASTAR sys.stdout.write("Generating stats for A*..") for x in range(1, 21): result = a_star(NodeFCost(state_dict[x], goal, None, None, 0, 0), board, verbose=False) if result.verdict == "success": entry[x]["time"] = result.elapsed_time entry[x]["nodes"] = result.gen_nodes entry[x]["cost"] = result.node.path_cost sys.stdout.write(f" #{x},") else: entry[x]["time"] = 0.0 entry[x]["nodes"] = 0 entry[x]["cost"] = 0 sys.stdout.write(f" #{x}(F),") print("") write_file(entry, "astar") clear_dict(entry)
def unleash_chaos(goal_state): #goal_state = [1,0,8,2,7,3,4,6,5] board = Board(3) print(f"Generating random states from\nGoal: {goal_state}\nBoard Size: 3x3") node = Node(goal_state, goal_state, None, None, 0, 0) frontier = deque([node]) depth = 0 explored = set() entry = {} for x in range(1, 21, 1): entry[x] = [] while frontier: _node = frontier.popleft() explored.add(_node) children = make_child_node(_node, _node.goal, board) for child in children: if child.depth > 20: break if child.str_state not in explored: explored.add(child.str_state) frontier.append(child) depth = child.depth if child.str_state != child.goal_str: entry[child.depth].append(child.state) #print(f"Depth: {depth}") #print(entry) print("Unique states generated for all depth 1 to 20!") for depth, item in entry.items(): x = random.randrange(0, len(item), 1) new = item[x] entry[depth] = new print("Picking a random state for each depth..") for depth, item in entry.items(): if depth < 10: print(f"Depth #{depth} -> {item}") else: print(f"Depth #{depth} -> {item}") print("Running all the algos for each depth (1 to 20):\n") run_algos(entry, goal_state, board) print("All statistics have been recorded and saved in .json files") while True: print("Choose which graphs to show (1-3). To exit this loop please enter 0.") print("Exiting will take you back initial menu!") print("1. 'Time' Graph") print("2. 'Nodes Generated' Graph") print("3. 'Path Cost' Graph") print("4. Rate of node generated") choice = int(input("Choice: ")) if choice == 0: break elif choice == 1: time_graph() elif choice == 2: nodes_graph() elif choice == 3: cost_graph() elif choice == 4: node_by_time() else: continue