def chaser_greedy(env_name, episode_counts, display=True, fps=10): env = gym.make(env_name) chaser = GreedyAgent(actions_n=4) epi = 0 for epi in range(episode_counts): state = env.reset() steps = 0 while True: if display: time.sleep(1 / fps) env.render() action = chaser.act(state) state_, _, done, _ = env.step(action) steps += 1 state = state_ if done: print('Episode: %d\tSteps: %d' % (epi + 1, steps)) break
# Initiate the renderer env_renderer = RenderTool( local_env, gl="PILSVG", agent_render_variant=AgentRenderVariant.ONE_STEP_BEHIND, show_debug=False, screen_height=1200, # Adjust these parameters to fit your resolution screen_width=1800) # Adjust these parameters to fit your resolution ######### Get arguments of the script ######### parser = argparse.ArgumentParser() parser.add_argument("-step", type=int, help="steps") args = parser.parse_args() ######### Custom controller: imports ######### controller = GreedyAgent(218, local_env.action_space[0]) ######### Define custom controller ######### def my_controller(obs, number_of_agents, astar_paths_readable, timestamp): _action = {} grid = obs[0][0] # Chose an action for each agent in the environment for a_id in range(number_of_agents): status, position, direction, initial_position, target, speed = obs[ a_id][1] action = None if status == RailAgentStatus.ACTIVE: next_cell = None if astar_paths_readable[a_id] is not None:
screen_width=1800) # Adjust these parameters to fit your resolution ######### Get arguments of the script ######### parser = argparse.ArgumentParser() parser.add_argument("-a", type=int, help="id of the agent to be displayed") parser.add_argument("-step", type=int, help="steps") args = parser.parse_args() ######### Select agent ######### show_agents = [0] if args.a: show_agents[0] = int(args.a) print(show_agents) ######### Custom controller setup ######### controller = GreedyAgent(218, env.action_space[0]) # Build graph from transition map print("\nCompute transition graph from generated rail grid.") graph_generator = GraphBuilder(env.rail.width, env.rail.height, env.rail.grid) # create whitelist, which cells not to remove from graph cell_whitelist = set() for a_id in show_agents: ag = env.agents[a_id] start = graph_generator.convert_indexes_2_node(ag.initial_position, ) if ag.position is not None: start = graph_generator.convert_indexes_2_node(ag.position) cell_whitelist.update( [start, graph_generator.convert_indexes_2_node(ag.target)])
config = {'players': 2, 'verbose': False} EPISODES = 100 DISCARDS = [.1 * i for i in range(11)] PLAYS = [.5 + .05 * i for i in range(11)] max_rewards = [] mean_rewards = [] median_rewards = [] for play_threshold in PLAYS: max_reward = [] mean_reward = [] median_reward = [] for base_discard in DISCARDS: agents = [GreedyAgent(config) for i in range(2)] for agent in agents: agent.play_threshold = play_threshold agent.discard_thresholds = [ base_discard + i * (play_threshold - base_discard) / 9 for i in range(9) ] runner = GameRunner(agents, EPISODES, config) runner.run() rewards = runner.rewards max_reward.append(np.max(rewards)) mean_reward.append(np.mean(rewards)) median_reward.append(np.median(rewards)) max_rewards.append(max_reward) mean_rewards.append(mean_reward) median_rewards.append(median_reward)
screen_height=1200, # Adjust these parameters to fit your resolution screen_width=1800) # Adjust these parameters to fit your resolution ######### Get arguments of the script ######### parser = argparse.ArgumentParser() parser.add_argument("-agent", type=int, help="id of the agent to be displayed") parser.add_argument("-step", type=int, help="steps") args = parser.parse_args() ######### Select agent ######### show_agents = [2] if args.agent is not None: show_agents[0] = int(args.agent) ######### Custom controller setup ######### controller = GreedyAgent(218, env.action_space[0]) my_grid = [[Node((i, j), env.rail.grid[i, j]) for j in range(env.rail.width)] for i in range(env.rail.height)] astar_planner = AStarAgent(my_grid, env.rail.width, env.rail.height) astar_paths_readable = [None for _ in range(env.number_of_agents)] # run A* for the selected agent for a_id in show_agents: ag = env.agents[a_id] start = ag.initial_position if ag.position is not None: start = ag.position start = Node(start, env.rail.grid[start[0], start[1]], dir=ag.direction) end = Node(ag.target, env.rail.grid[ag.target[0], ag.target[1]]) astar_paths_readable[a_id] = [ node.point for node in astar_planner.aStar(start, end)