def draw_menu(stdscr, agents): k = 0 # Clear and refresh the screen for a blank canvas # Start colors in curses curses.start_color() curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE) stdscr.clear() cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) map = print_map.render_map(cur_map, False) stdscr.addstr(0, 0, map, curses.color_pair(1)) stdscr.refresh() # Loop where k is the last character pressed while (k != ord('q')): # Initialization stdscr.clear() # height, width = stdscr.getmaxyx() dir = '' if k == curses.KEY_DOWN: dir = 'S' elif k == curses.KEY_UP: dir = 'N' elif k == curses.KEY_RIGHT: dir = 'E' elif k == curses.KEY_LEFT: dir = 'W' elif k == ord('p'): m1 = map_parser.remove_robot_map(cur_map) m2 = FINAL_MAP stdscr.addstr(0, 0, print_map.render_map(m1, False), curses.color_pair(2)) dir = None else: dir = None if dir: new_agents, _ = next_state(dir, agents) agents = deepcopy(new_agents) cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) map = print_map.render_map(cur_map, False) stdscr.addstr(1, 0, map, curses.color_pair(1)) if map_parser.remove_robot_map(cur_map) == FINAL_MAP: stdscr.addstr(0, 0, "SUCCESS", curses.color_pair(2)) # Rendering some text stdscr.refresh() # Wait for next input k = stdscr.getch()
def step(agents): cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) print(print_map.render_map(cur_map)) scenario = '' while map_parser.remove_robot_map(cur_map) != FINAL_MAP: cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) print('heur', heuristic2(agents, GOALS)) print(print_map.render_map(cur_map)) if len(scenario) < 1: scenario = user_input(DIRS) print("scenario", scenario) dir = scenario[0] scenario = scenario[1:] new_agents, _ = next_state(dir, agents) agents = deepcopy(new_agents) input('continue?\n\n')
def bfs(agents): visited.append(agents) queue.append((agents, '')) while queue: agents, path = queue.pop(0) cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) debug(print_map.render_map(cur_map)) debug(agents) if map_parser.remove_robot_map(cur_map) == FINAL_MAP: SOLUTIONS.append(agents) debug(SOLUTIONS) debug(SHORTEST_PATH_TO_STATE[hash_agent(agents)]) return # input("enter to continue") elif diam_on_corner(cur_map): continue elif diam_on_empty_edge(cur_map): continue # if nb diam on edge > nb goal else: neighbours = [] for d in DIRS: new_agents, has_changed = next_state(d, agents) if has_changed: new_path = path + d neighbours.append((new_agents, new_path)) # sort by heuristic here for neighbour, path in neighbours: if neighbour not in visited: visited.append(neighbour) queue.append((neighbour, path)) SHORTEST_PATH_TO_STATE[hash_agent(neighbour)].append(path) debug("*** FINISHED ***") for agents in SOLUTIONS: debug("---") paths = SHORTEST_PATH_TO_STATE[hash_agent(agents)] sols = sorted(paths, key=lambda x: len(x)) sols.reverse() debug(sols)
def rec_tick(agents, path='', it=0): cur_map = map_parser.add_agent_to_map(agents, STATIC_MAP) print(it, path) print(print_map.render_map(cur_map)) # if len(SOLUTIONS) > 0: # return True if map_parser.remove_robot_map(cur_map) == FINAL_MAP: SOLUTIONS.append(min(SHORTEST_PATH_TO_STATE[hash_agent(agents)])) debug(SHORTEST_PATH_TO_STATE[hash_agent(agents)]) debug(len(SHORTEST_PATH_TO_STATE[hash_agent(agents)])) debug(len(SHORTEST_PATH_TO_STATE[hash_agent(agents)][0])) input("enter to continue") return True elif diam_on_corner(cur_map): pass elif diam_on_empty_edge(cur_map): pass else: it = it + 1 candidats = [] for d in DIRS: new_path = path + d new_agents, _ = next_state(d, agents) SHORTEST_PATH_TO_STATE[hash_agent(new_agents)].append(new_path) if not new_agents in PREVIOUS_STATES: PREVIOUS_STATES.append(new_agents) # input('continue?') candidats.append((new_agents, new_path, it)) sorted_candidats = sorted( candidats, key=lambda x: heuristic2(x[0], GOALS)) for candidat in sorted_candidats: rec_tick(*candidat)
scenario = user_input(DIRS) print("scenario", scenario) dir = scenario[0] scenario = scenario[1:] new_agents, _ = next_state(dir, agents) agents = deepcopy(new_agents) input('continue?\n\n') if __name__ == '__main__': from pprint import pprint import sys MAP = map_parser.parse_from_text_file('./compet.txt') # MAP = MAPS.init print(MAP) print_map.render_map(MAP) FINAL_MAP = map_parser.create_final(MAP) STATIC_MAP, AGENTS, GOALS = map_parser.parse_map(MAP) cur_map = deepcopy(MAP) PREVIOUS_STATES.append(AGENTS) if sys.argv[1] == 'step': step(AGENTS) elif sys.argv[1] == 'bfs': sys.setrecursionlimit(10**6) bfs(AGENTS) elif sys.argv[1] == 'ia': sys.setrecursionlimit(10**6) # from ia import split_map