Exemplo n.º 1
0
def test_random_rail_generator():
    n_agents = 1
    x_dim = 5
    y_dim = 10

    # Check that a random level at with correct parameters is generated
    env = RailEnv(width=x_dim, height=y_dim, rail_generator=random_rail_generator(), number_of_agents=n_agents)
    env.reset()
    assert env.rail.grid.shape == (y_dim, x_dim)
    assert env.get_num_agents() == n_agents
Exemplo n.º 2
0
def main():
    env = RailEnv(width=7,
                  height=7,
                  rail_generator=random_rail_generator(),
                  number_of_agents=3,
                  obs_builder_object=SimpleObs())
    env.reset()

    # Print the observation vector for each agents
    obs, all_rewards, done, _ = env.step({0: 0})
    for i in range(env.get_num_agents()):
        print("Agent ", i, "'s observation: ", obs[i])
Exemplo n.º 3
0
    def __init__(
            self,
            width,
            height,
            rail_generator: RailGenerator = random_rail_generator(),
            schedule_generator: ScheduleGenerator = random_schedule_generator(
            ),
            number_of_agents=1,
            obs_builder_object: ObservationBuilder = TreeObsForRailEnv(
                max_depth=2),
            max_episode_steps=None,
            stochastic_data=None):
        super().__init__(width, height, rail_generator, schedule_generator,
                         number_of_agents, obs_builder_object)

        self.graph_low_level = nx.DiGraph()
        self.graph_high_level = nx.Graph()
        self.create_graph_from_env(self.obs_builder)
Exemplo n.º 4
0
    def regenerate(self, method=None, nAgents=0, env=None):
        self.log("Regenerate size",
                 self.regen_size_width,
                 self.regen_size_height)

        if method is None or method == "Empty":
            fnMethod = empty_rail_generator()
        elif method == "Random Cell":
            fnMethod = random_rail_generator(cell_type_relative_proportion=[1] * 11)
        else:
            fnMethod = complex_rail_generator(nr_start_goal=nAgents, nr_extra=20, min_dist=12, seed=int(time.time()))

        if env is None:
            self.env = RailEnv(width=self.regen_size_width, height=self.regen_size_height, rail_generator=fnMethod,
                               number_of_agents=nAgents, obs_builder_object=TreeObsForRailEnv(max_depth=2))
        else:
            self.env = env
        self.env.reset(regenerate_rail=True)
        self.fix_env()
        self.selected_agent = None  # clear the selected agent.
        self.set_env(self.env)
        self.view.new_env()
        self.redraw()
Exemplo n.º 5
0
def test_schedule_from_file_random():
    """
    Test to see that all parameters are loaded as expected
    Returns
    -------

    """
    # Different agent types (trains) with different speeds.
    speed_ration_map = {
        1.: 0.25,  # Fast passenger train
        1. / 2.: 0.25,  # Fast freight train
        1. / 3.: 0.25,  # Slow commuter train
        1. / 4.: 0.25
    }  # Slow freight train

    # Generate random test env
    rail_generator = random_rail_generator()
    schedule_generator = random_schedule_generator(speed_ration_map)

    create_and_save_env(file_name="./random_env_test.pkl",
                        rail_generator=rail_generator,
                        schedule_generator=schedule_generator)

    # Random generator
    rail_generator = rail_from_file("./random_env_test.pkl")
    schedule_generator = schedule_from_file("./random_env_test.pkl")
    random_env_from_file = RailEnv(width=1,
                                   height=1,
                                   rail_generator=rail_generator,
                                   schedule_generator=schedule_generator)
    random_env_from_file.reset(True, True)

    # Assert loaded agent number is correct
    assert random_env_from_file.get_num_agents() == 10

    # Assert max steps is correct
    assert random_env_from_file._max_episode_steps == 1350
# Parameters for the environment

x_dim = 5
y_dim = 5
n_agents = 1

# Custom observation builder
tree_depth = 2
tree_obs = TreeObsForRailEnv(max_depth=tree_depth)

# Environment setup
env = RailEnv(
    width=x_dim,
    height=y_dim,
    number_of_agents=n_agents,
    rail_generator=random_rail_generator(),
    obs_builder_object=tree_obs
)

# Render and show the env
env_renderer = RenderTool(env=env)

# ------------------------------------------------------
# 2. Define state & action size
# ------------------------------------------------------

# Calculate the state size (based on number of actions and observations)
features_per_node = env.obs_builder.observation_dim
nr_nodes = 0
for i in range(tree_depth + 1):
    nr_nodes += np.power(4, i)
Exemplo n.º 7
0
# Use a the malfunction generator to break agents from time to time
# stochastic_data = MalfunctionParameters(malfunction_rate=1./10000,  # Rate of malfunction occurence
#                                         min_duration=15,  # Minimal duration of malfunction
#                                         max_duration=50  # Max duration of malfunction
#                                         )

# Different agent types (trains) with different speeds.
speed_ration_map = {
    1.: 1.0,  # Fast passenger train
    1. / 2.: 0.0,  # Fast freight train
    1. / 3.: 0.0,  # Slow commuter train
    1. / 4.: 0.0
}  # Slow freight train

nAgents = 2
fnMethod = random_rail_generator(cell_type_relative_proportion=[1] * 11)
env = RailEnv(width=10,
              height=10,
              max_num_cities=2,
              rail_generator=fnMethod,
              number_of_agents=nAgents,
              obs_builder_object=TreeObsForRailEnv(
                  max_depth=2, predictor=ShortestPathPredictorForRailEnv()))

max_num_cities = 2

# Reset env
env.reset(True, True)
# After training we want to render the results so we also load a renderer
# env_renderer = RenderTool(env, gl="PILSVG", )
#env_renderer = RenderTool(env)
Exemplo n.º 8
0
    def __init__(
            self,
            width,
            height,
            rail_generator: RailGenerator = random_rail_generator(),
            schedule_generator: ScheduleGenerator = random_schedule_generator(
            ),
            number_of_agents=1,
            obs_builder_object: ObservationBuilder = GlobalObsForRailEnv(),
            malfunction_generator_and_process_data=no_malfunction_generator(),
            remove_agents_at_target=True,
            random_seed=1,
            record_steps=False):
        """
        Environment init.

        Parameters
        ----------
        rail_generator : function
            The rail_generator function is a function that takes the width,
            height and agents handles of a  rail environment, along with the number of times
            the env has been reset, and returns a GridTransitionMap object and a list of
            starting positions, targets, and initial orientations for agent handle.
            The rail_generator can pass a distance map in the hints or information for specific schedule_generators.
            Implementations can be found in flatland/envs/rail_generators.py
        schedule_generator : function
            The schedule_generator function is a function that takes the grid, the number of agents and optional hints
            and returns a list of starting positions, targets, initial orientations and speed for all agent handles.
            Implementations can be found in flatland/envs/schedule_generators.py
        width : int
            The width of the rail map. Potentially in the future,
            a range of widths to sample from.
        height : int
            The height of the rail map. Potentially in the future,
            a range of heights to sample from.
        number_of_agents : int
            Number of agents to spawn on the map. Potentially in the future,
            a range of number of agents to sample from.
        obs_builder_object: ObservationBuilder object
            ObservationBuilder-derived object that takes builds observation
            vectors for each agent.
        remove_agents_at_target : bool
            If remove_agents_at_target is set to true then the agents will be removed by placing to
            RailEnv.DEPOT_POSITION when the agent has reach it's target position.
        random_seed : int or None
            if None, then its ignored, else the random generators are seeded with this number to ensure
            that stochastic operations are replicable across multiple operations
        """
        super().__init__()

        self.malfunction_generator, self.malfunction_process_data = malfunction_generator_and_process_data
        self.rail_generator: RailGenerator = rail_generator
        self.schedule_generator: ScheduleGenerator = schedule_generator
        self.rail: Optional[GridTransitionMap] = None
        self.width = width
        self.height = height

        self.remove_agents_at_target = remove_agents_at_target

        self.rewards = [0] * number_of_agents
        self.done = False
        self.obs_builder = obs_builder_object
        self.obs_builder.set_env(self)

        self._max_episode_steps: Optional[int] = None
        self._elapsed_steps = 0

        self.dones = dict.fromkeys(
            list(range(number_of_agents)) + ["__all__"], False)

        self.obs_dict = {}
        self.rewards_dict = {}
        self.dev_obs_dict = {}
        self.dev_pred_dict = {}

        self.agents: List[EnvAgent] = []
        self.number_of_agents = number_of_agents
        self.num_resets = 0
        self.distance_map = DistanceMap(self.agents, self.height, self.width)

        self.action_space = [5]

        self._seed()
        self._seed()
        self.random_seed = random_seed
        if self.random_seed:
            self._seed(seed=random_seed)

        self.valid_positions = None

        # global numpy array of agents position, True means that there is an agent at that cell
        self.agent_positions: np.ndarray = np.full((height, width), False)

        # save episode timesteps ie agent positions, orientations.  (not yet actions / observations)
        self.record_steps = record_steps  # whether to save timesteps
        self.cur_episode = []  # save timesteps in here
# Relative weights of each cell type to be used by the random rail generators.
transition_probability = [
    1.0,  # empty cell - Case 0
    1.0,  # Case 1 - straight
    1.0,  # Case 2 - simple switch
    0.3,  # Case 3 - diamond drossing
    0.5,  # Case 4 - single slip
    0.5,  # Case 5 - double slip
    0.2,  # Case 6 - symmetrical
    0.0,  # Case 7 - dead end
    0.2,  # Case 8 - turn left
    0.2,  # Case 9 - turn right
    1.0
]  # Case 10 - mirrored switch

# Example generate a random rail
env = RailEnv(width=10,
              height=10,
              rail_generator=random_rail_generator(
                  cell_type_relative_proportion=transition_probability),
              number_of_agents=3)

env.reset()

env_renderer = RenderTool(env, gl="PIL")
env_renderer.render_env(show=True)

# uncomment to keep the renderer open
input("Press Enter to continue...")