Commit 9c81bf2c authored by Erik Nygren's avatar Erik Nygren 🚅
Browse files

Enhancing reproducibility by unifying seeds to generators

parent b8fefaea
......@@ -117,7 +117,7 @@ class GridTransitionMap(TransitionMap):
GridTransitionMap implements utility functions.
"""
def __init__(self, width, height, transitions: Transitions = Grid4Transitions([])):
def __init__(self, width, height, transitions: Transitions = Grid4Transitions([]), random_seed=None):
"""
Builder for GridTransitionMap object.
......@@ -136,7 +136,11 @@ class GridTransitionMap(TransitionMap):
self.width = width
self.height = height
self.transitions = transitions
self.random_generator = np.random.RandomState()
if random_seed is None:
self.random_generator.seed(12)
else:
self.random_generator.seed(random_seed)
self.grid = np.zeros((height, width), dtype=self.transitions.get_type())
def get_full_transitions(self, row, column):
......@@ -511,7 +515,6 @@ class GridTransitionMap(TransitionMap):
gDir2dRC = self.transitions.gDir2dRC # [[-1,0] = N, [0,1]=E, etc]
grcPos = array(rcPos)
grcMax = self.grid.shape
# Transition elements
transitions = RailEnvTransitions()
cells = transitions.transition_list
......@@ -572,15 +575,15 @@ class GridTransitionMap(TransitionMap):
elif switch_type_idx == 2:
transition = simple_switch_east_south
else:
transition = np.random.choice(three_way_transitions, 1)
transition = self.random_generator.choice(three_way_transitions, 1)
else:
transition = np.random.choice(three_way_transitions, 1)
transition = self.random_generator.choice(three_way_transitions, 1)
transition = transitions.rotate_transition(transition, int(hole * 90))
self.set_transitions((rcPos[0], rcPos[1]), transition)
# Make a double slip switch
if number_of_incoming == 4:
rotation = np.random.randint(2)
rotation = self.random_generator.randint(2)
transition = transitions.rotate_transition(double_slip, int(rotation * 90))
self.set_transitions((rcPos[0], rcPos[1]), transition)
return True
......
......@@ -168,6 +168,7 @@ def single_malfunction_generator(earlierst_malfunction: int, malfunction_duratio
Parameters
----------
earlierst_malfunction: Earliest possible malfunction onset
malfunction_duration: The duration of the single malfunction
Returns
......
......@@ -314,7 +314,8 @@ class RailEnv(Environment):
optionals = {}
if regenerate_rail or self.rail is None:
rail, optionals = self.rail_generator(self.width, self.height, self.number_of_agents, self.num_resets)
rail, optionals = self.rail_generator(self.width, self.height, self.number_of_agents, self.num_resets,
self.np_random)
self.rail = rail
self.height, self.width = self.rail.grid.shape
......@@ -332,7 +333,8 @@ class RailEnv(Environment):
if optionals and 'agents_hints' in optionals:
agents_hints = optionals['agents_hints']
schedule = self.schedule_generator(self.rail, self.number_of_agents, agents_hints, self.num_resets)
schedule = self.schedule_generator(self.rail, self.number_of_agents, agents_hints, self.num_resets,
self.np_random)
self.agents = EnvAgent.from_schedule(schedule)
if agents_hints and 'city_orientations' in agents_hints:
......@@ -564,8 +566,8 @@ class RailEnv(Environment):
self.rewards_dict[i_agent] += self.stop_penalty
if not agent.moving and not (
action == RailEnvActions.DO_NOTHING or
action == RailEnvActions.STOP_MOVING):
action == RailEnvActions.DO_NOTHING or
action == RailEnvActions.STOP_MOVING):
# Allow agent to start with any forward or direction action
agent.moving = True
self.rewards_dict[i_agent] += self.start_penalty
......@@ -604,7 +606,7 @@ class RailEnv(Environment):
if agent.moving:
agent.speed_data['position_fraction'] += agent.speed_data['speed']
if agent.speed_data['position_fraction'] > 1.0 or np.isclose(agent.speed_data['position_fraction'], 1.0,
rtol=1e-03):
rtol=1e-03):
# Perform stored action to transition to the next cell as soon as cell is free
# Notice that we've already checked new_cell_valid and transition valid when we stored the action,
# so we only have to check cell_free now!
......
......@@ -5,6 +5,7 @@ from typing import Callable, Tuple, Optional, Dict, List
import msgpack
import numpy as np
from numpy.random.mtrand import RandomState
from flatland.core.grid.grid4 import Grid4TransitionsEnum
from flatland.core.grid.grid4_utils import get_direction, mirror, direction_to_point
......@@ -26,7 +27,8 @@ def empty_rail_generator() -> RailGenerator:
Primarily used by the editor
"""
def generator(width: int, height: int, num_agents: int = 0, num_resets: int = 0) -> RailGeneratorProduct:
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
rail_trans = RailEnvTransitions()
grid_map = GridTransitionMap(width=width, height=height, transitions=rail_trans)
rail_array = grid_map.grid
......@@ -58,7 +60,8 @@ def complex_rail_generator(nr_start_goal=1,
The matrix with the correct 16-bit bitmaps for each cell.
"""
def generator(width, height, num_agents, num_resets=0):
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
if num_agents > nr_start_goal:
num_agents = nr_start_goal
......@@ -67,8 +70,6 @@ def complex_rail_generator(nr_start_goal=1,
rail_array = grid_map.grid
rail_array.fill(0)
np.random.seed(seed + num_resets)
# generate rail array
# step 1:
# - generate a start and goal position
......@@ -96,8 +97,8 @@ def complex_rail_generator(nr_start_goal=1,
while nr_created < nr_start_goal and created_sanity < sanity_max:
all_ok = False
for _ in range(sanity_max):
start = (np.random.randint(0, height), np.random.randint(0, width))
goal = (np.random.randint(0, height), np.random.randint(0, width))
start = (np_random.randint(0, height), np_random.randint(0, width))
goal = (np_random.randint(0, height), np_random.randint(0, width))
# check to make sure start,goal pos is empty?
if rail_array[goal] != 0 or rail_array[start] != 0:
......@@ -150,8 +151,8 @@ def complex_rail_generator(nr_start_goal=1,
while nr_created < nr_extra and created_sanity < sanity_max:
all_ok = False
for _ in range(sanity_max):
start = (np.random.randint(0, height), np.random.randint(0, width))
goal = (np.random.randint(0, height), np.random.randint(0, width))
start = (np_random.randint(0, height), np_random.randint(0, width))
goal = (np_random.randint(0, height), np_random.randint(0, width))
# check to make sure start,goal pos are not empty
if rail_array[goal] == 0 or rail_array[start] == 0:
continue
......@@ -195,7 +196,8 @@ def rail_from_manual_specifications_generator(rail_spec):
the matrix of correct 16-bit bitmaps for each rail_spec_of_cell.
"""
def generator(width, height, num_agents, num_resets=0):
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
rail_env_transitions = RailEnvTransitions()
height = len(rail_spec)
......@@ -234,7 +236,8 @@ def rail_from_file(filename, load_from_package=None) -> RailGenerator:
the matrix of correct 16-bit bitmaps for each rail_spec_of_cell.
"""
def generator(width, height, num_agents, num_resets):
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
rail_env_transitions = RailEnvTransitions()
if load_from_package is not None:
from importlib_resources import read_binary
......@@ -272,7 +275,8 @@ def rail_from_grid_transition_map(rail_map) -> RailGenerator:
Generator function that always returns the given `rail_map` object.
"""
def generator(width: int, height: int, num_agents: int, num_resets: int = 0) -> RailGeneratorProduct:
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
return rail_map, None
return generator
......@@ -310,8 +314,8 @@ def random_rail_generator(cell_type_relative_proportion=[1.0] * 11, seed=1) -> R
The matrix with the correct 16-bit bitmaps for each cell.
"""
def generator(width: int, height: int, num_agents: int, num_resets: int = 0) -> RailGeneratorProduct:
np.random.seed(seed + num_resets)
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
t_utils = RailEnvTransitions()
transition_probability = cell_type_relative_proportion
......@@ -379,7 +383,7 @@ def random_rail_generator(cell_type_relative_proportion=[1.0] * 11, seed=1) -> R
num_insertions = 0
while num_insertions < MAX_INSERTIONS and len(cells_to_fill) > 0:
cell = cells_to_fill[np.random.choice(len(cells_to_fill), 1)[0]]
cell = cells_to_fill[np_random.choice(len(cells_to_fill), 1)[0]]
cells_to_fill.remove(cell)
row = cell[0]
col = cell[1]
......@@ -463,7 +467,7 @@ def random_rail_generator(cell_type_relative_proportion=[1.0] * 11, seed=1) -> R
possible_transitions, possible_probabilities = zip(*besttrans)
possible_probabilities = [p / sum(possible_probabilities) for p in possible_probabilities]
rail[row][col] = np.random.choice(possible_transitions,
rail[row][col] = np_random.choice(possible_transitions,
p=possible_probabilities)
num_insertions += 1
......@@ -477,7 +481,7 @@ def random_rail_generator(cell_type_relative_proportion=[1.0] * 11, seed=1) -> R
possible_transitions, possible_probabilities = zip(*possible_cell_transitions)
possible_probabilities = [p / sum(possible_probabilities) for p in possible_probabilities]
rail[row][col] = np.random.choice(possible_transitions,
rail[row][col] = np_random.choice(possible_transitions,
p=possible_probabilities)
num_insertions += 1
......@@ -560,7 +564,7 @@ def random_rail_generator(cell_type_relative_proportion=[1.0] * 11, seed=1) -> R
def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_rails_between_cities: int = 4,
max_rails_in_city: int = 4, seed: int = 1) -> RailGenerator:
max_rails_in_city: int = 4, seed=0) -> RailGenerator:
"""
Generates railway networks with cities and inner city rails
......@@ -583,7 +587,8 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
Returns the rail generator object to the rail env constructor
"""
def generator(width: int, height: int, num_agents: int, num_resets: int = 0) -> RailGeneratorProduct:
def generator(width: int, height: int, num_agents: int, num_resets: int = 0,
np_random: RandomState = None) -> RailGenerator:
"""
Parameters
......@@ -607,11 +612,8 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
'train_stations': locations of train stations for start and targets
'city_orientations' : orientation of cities
"""
np.random.seed(seed + num_resets)
rail_trans = RailEnvTransitions()
grid_map = GridTransitionMap(width=width, height=height, transitions=rail_trans)
# We compute the city radius by the given max number of rails it can contain.
# The radius is equal to the number of tracks divided by 2
# We add 2 cells to avoid that track lenght is to short
......@@ -637,11 +639,11 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
height)
# Distribute cities randomlz
else:
city_positions = _generate_random_city_positions(max_feasible_cities, city_radius, width, height)
city_positions = _generate_random_city_positions(max_feasible_cities, city_radius, width, height,
np_random=np_random)
# reduce num_cities if less were generated in random mode
num_cities = len(city_positions)
# If random generation failed just put the cities evenly
if num_cities < 2:
warnings.warn("[WARNING] Changing to Grid mode to place at least 2 cities.")
......@@ -653,7 +655,7 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
inner_connection_points, outer_connection_points, city_orientations, city_cells = \
_generate_city_connection_points(
city_positions, city_radius, vector_field, rails_between_cities,
rails_in_city)
rails_in_city, np_random=np_random)
# Connect the cities through the connection points
inter_city_lines = _connect_cities(city_positions, outer_connection_points, city_cells,
......@@ -679,7 +681,8 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
}}
def _generate_random_city_positions(num_cities: int, city_radius: int, width: int,
height: int) -> (IntVector2DArray, IntVector2DArray):
height: int, np_random: RandomState = None) -> (
IntVector2DArray, IntVector2DArray):
"""
Distribute the cities randomly in the environment while respecting city sizes and guaranteeing that they
don't overlap.
......@@ -700,14 +703,15 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
Returns a list of all city positions as coordinates (x,y)
"""
city_positions: IntVector2DArray = []
for city_idx in range(num_cities):
too_close = True
tries = 0
while too_close:
row = city_radius + 1 + np.random.randint(height - 2 * (city_radius + 1))
col = city_radius + 1 + np.random.randint(width - 2 * (city_radius + 1))
row = city_radius + 1 + np_random.randint(height - 2 * (city_radius + 1))
col = city_radius + 1 + np_random.randint(width - 2 * (city_radius + 1))
too_close = False
# Check distance to cities
for city_pos in city_positions:
......@@ -773,10 +777,11 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
def _generate_city_connection_points(city_positions: IntVector2DArray, city_radius: int,
vector_field: IntVector2DArray, rails_between_cities: int,
rails_in_city: int = 2) -> (List[List[List[IntVector2D]]],
List[List[List[IntVector2D]]],
List[np.ndarray],
List[Grid4TransitionsEnum]):
rails_in_city: int = 2, np_random: RandomState = None) -> (
List[List[List[IntVector2D]]],
List[List[List[IntVector2D]]],
List[np.ndarray],
List[Grid4TransitionsEnum]):
"""
Generate the city connection points. Internal connection points are used to generate the parallel paths
within the city.
......@@ -814,6 +819,7 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
outer_connection_points: List[List[List[IntVector2D]]] = []
city_orientations: List[Grid4TransitionsEnum] = []
city_cells: IntVector2DArray = []
for city_position in city_positions:
# Chose the directions where close cities are situated
......@@ -826,7 +832,7 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
connection_sides_idx = []
idx = 1
if grid_mode:
current_closest_direction = np.random.randint(4)
current_closest_direction = np_random.randint(4)
else:
current_closest_direction = direction_to_point(city_position, city_positions[closest_neighb_idx[idx]])
connection_sides_idx.append(current_closest_direction)
......@@ -835,12 +841,12 @@ def sparse_rail_generator(max_num_cities: int = 5, grid_mode: bool = False, max_
city_cells.extend(_get_cells_in_city(city_position, city_radius, city_orientations[-1], vector_field))
# set the number of tracks within a city, at least 2 tracks per city
connections_per_direction = np.zeros(4, dtype=int)
nr_of_connection_points = np.random.randint(2, rails_in_city + 1)
nr_of_connection_points = np_random.randint(2, rails_in_city + 1)
for idx in connection_sides_idx:
connections_per_direction[idx] = nr_of_connection_points
connection_points_coordinates_inner: List[List[IntVector2D]] = [[] for i in range(4)]
connection_points_coordinates_outer: List[List[IntVector2D]] = [[] for i in range(4)]
number_of_out_rails = np.random.randint(1, min(rails_between_cities, nr_of_connection_points) + 1)
number_of_out_rails = np_random.randint(1, min(rails_between_cities, nr_of_connection_points) + 1)
start_idx = int((nr_of_connection_points - number_of_out_rails) / 2)
for direction in range(4):
connection_slots = np.arange(nr_of_connection_points) - start_idx
......
......@@ -4,6 +4,7 @@ from typing import Tuple, List, Callable, Mapping, Optional, Any
import msgpack
import numpy as np
from numpy.random.mtrand import RandomState
from flatland.core.grid.grid4_utils import get_new_position
from flatland.core.transition_map import GridTransitionMap
......@@ -15,7 +16,7 @@ ScheduleGenerator = Callable[[GridTransitionMap, int, Optional[Any], Optional[in
def speed_initialization_helper(nb_agents: int, speed_ratio_map: Mapping[float, float] = None,
seed: int = None) -> List[float]:
seed: int = None, np_random: RandomState = None) -> List[float]:
"""
Parameters
----------
......@@ -29,9 +30,6 @@ def speed_initialization_helper(nb_agents: int, speed_ratio_map: Mapping[float,
List[float]
A list of size nb_agents of speeds with the corresponding probabilistic ratios.
"""
if seed:
np.random.seed(seed)
if speed_ratio_map is None:
return [1.0] * nb_agents
......@@ -39,7 +37,7 @@ def speed_initialization_helper(nb_agents: int, speed_ratio_map: Mapping[float,
speed_ratio_map_as_list: List[Tuple[float, float]] = list(speed_ratio_map.items())
speed_ratios = list(map(lambda t: t[1], speed_ratio_map_as_list))
speeds = list(map(lambda t: t[0], speed_ratio_map_as_list))
return list(map(lambda index: speeds[index], np.random.choice(nb_classes, nb_agents, p=speed_ratios)))
return list(map(lambda index: speeds[index], np_random.choice(nb_classes, nb_agents, p=speed_ratios)))
def complex_schedule_generator(speed_ratio_map: Mapping[float, float] = None, seed: int = 1) -> ScheduleGenerator:
......@@ -54,7 +52,8 @@ def complex_schedule_generator(speed_ratio_map: Mapping[float, float] = None, se
:return:
"""
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0) -> Schedule:
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0,
np_random: RandomState = None) -> Schedule:
"""
The generator that assigns tasks to all the agents
......@@ -64,8 +63,8 @@ def complex_schedule_generator(speed_ratio_map: Mapping[float, float] = None, se
:param num_resets: How often the generator has been reset.
:return: Returns the generator to the rail constructor
"""
#Todo: Remove parameters and variables not used for next version, Issue: <https://gitlab.aicrowd.com/flatland/flatland/issues/305>
_runtime_seed = seed + num_resets
np.random.seed(_runtime_seed)
start_goal = hints['start_goal']
start_dir = hints['start_dir']
......@@ -74,7 +73,7 @@ def complex_schedule_generator(speed_ratio_map: Mapping[float, float] = None, se
agents_direction = start_dir[:num_agents]
if speed_ratio_map:
speeds = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed)
speeds = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed, np_random=np_random)
else:
speeds = [1.0] * len(agents_position)
......@@ -94,7 +93,8 @@ def sparse_schedule_generator(speed_ratio_map: Mapping[float, float] = None, see
:param seed: Initiate random seed generator
"""
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0) -> Schedule:
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0,
np_random: RandomState = None) -> Schedule:
"""
The generator that assigns tasks to all the agents
......@@ -106,7 +106,6 @@ def sparse_schedule_generator(speed_ratio_map: Mapping[float, float] = None, see
"""
_runtime_seed = seed + num_resets
np.random.seed(_runtime_seed)
train_stations = hints['train_stations']
city_positions = hints['city_positions']
......@@ -128,24 +127,24 @@ def sparse_schedule_generator(speed_ratio_map: Mapping[float, float] = None, see
tries += 1
infeasible_agent = False
# Set target for agent
city_idx = np.random.choice(len(city_positions), 2, replace=False)
city_idx = np_random.choice(len(city_positions), 2, replace=False)
start_city = city_idx[0]
target_city = city_idx[1]
start_idx = np.random.choice(np.arange(len(train_stations[start_city])))
target_idx = np.random.choice(np.arange(len(train_stations[target_city])))
start_idx = np_random.choice(np.arange(len(train_stations[start_city])))
target_idx = np_random.choice(np.arange(len(train_stations[target_city])))
start = train_stations[start_city][start_idx]
target = train_stations[target_city][target_idx]
while start[1] % 2 != 0:
start_idx = np.random.choice(np.arange(len(train_stations[start_city])))
start_idx = np_random.choice(np.arange(len(train_stations[start_city])))
start = train_stations[start_city][start_idx]
while target[1] % 2 != 1:
target_idx = np.random.choice(np.arange(len(train_stations[target_city])))
target_idx = np_random.choice(np.arange(len(train_stations[target_city])))
target = train_stations[target_city][target_idx]
possible_orientations = [city_orientation[start_city],
(city_orientation[start_city] + 2) % 4]
agent_orientation = np.random.choice(possible_orientations)
agent_orientation = np_random.choice(possible_orientations)
if not rail.check_path_exists(start[0], agent_orientation, target[0]):
agent_orientation = (agent_orientation + 2) % 4
if not (rail.check_path_exists(start[0], agent_orientation, target[0])):
......@@ -160,7 +159,7 @@ def sparse_schedule_generator(speed_ratio_map: Mapping[float, float] = None, see
# Orient the agent correctly
if speed_ratio_map:
speeds = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed)
speeds = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed, np_random=np_random)
else:
speeds = [1.0] * len(agents_position)
......@@ -186,12 +185,10 @@ def random_schedule_generator(speed_ratio_map: Optional[Mapping[float, float]] =
initial positions, directions, targets speeds
"""
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None,
num_resets: int = 0) -> Schedule:
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0,
np_random: RandomState = None) -> Schedule:
_runtime_seed = seed + num_resets
np.random.seed(_runtime_seed)
valid_positions = []
for r in range(rail.height):
for c in range(rail.width):
......@@ -206,9 +203,9 @@ def random_schedule_generator(speed_ratio_map: Optional[Mapping[float, float]] =
return Schedule(agent_positions=[], agent_directions=[],
agent_targets=[], agent_speeds=[], agent_malfunction_rates=None)
agents_position_idx = [i for i in np.random.choice(len(valid_positions), num_agents, replace=False)]
agents_position_idx = [i for i in np_random.choice(len(valid_positions), num_agents, replace=False)]
agents_position = [valid_positions[agents_position_idx[i]] for i in range(num_agents)]
agents_target_idx = [i for i in np.random.choice(len(valid_positions), num_agents, replace=False)]
agents_target_idx = [i for i in np_random.choice(len(valid_positions), num_agents, replace=False)]
agents_target = [valid_positions[agents_target_idx[i]] for i in range(num_agents)]
update_agents = np.zeros(num_agents)
......@@ -224,10 +221,10 @@ def random_schedule_generator(speed_ratio_map: Optional[Mapping[float, float]] =
for i in range(num_agents):
if update_agents[i] == 1:
x = np.setdiff1d(np.arange(len(valid_positions)), agents_position_idx)
agents_position_idx[i] = np.random.choice(x)
agents_position_idx[i] = np_random.choice(x)
agents_position[i] = valid_positions[agents_position_idx[i]]
x = np.setdiff1d(np.arange(len(valid_positions)), agents_target_idx)
agents_target_idx[i] = np.random.choice(x)
agents_target_idx[i] = np_random.choice(x)
agents_target[i] = valid_positions[agents_target_idx[i]]
update_agents = np.zeros(num_agents)
......@@ -259,9 +256,9 @@ def random_schedule_generator(speed_ratio_map: Optional[Mapping[float, float]] =
break
else:
agents_direction[i] = valid_starting_directions[
np.random.choice(len(valid_starting_directions), 1)[0]]
np_random.choice(len(valid_starting_directions), 1)[0]]
agents_speed = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed)
agents_speed = speed_initialization_helper(num_agents, speed_ratio_map, seed=_runtime_seed, np_random=np_random)
return Schedule(agent_positions=agents_position, agent_directions=agents_direction,
agent_targets=agents_target, agent_speeds=agents_speed, agent_malfunction_rates=None)
......@@ -282,8 +279,8 @@ def schedule_from_file(filename, load_from_package=None) -> ScheduleGenerator:
initial positions, directions, targets speeds
"""
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None,
num_resets: int = 0) -> Schedule:
def generator(rail: GridTransitionMap, num_agents: int, hints: Any = None, num_resets: int = 0,
np_random: RandomState = None) -> Schedule:
if load_from_package is not None:
from importlib_resources import read_binary
load_data = read_binary(load_from_package, filename)
......
......@@ -10,7 +10,6 @@ from flatland.envs.rail_generators import rail_from_grid_transition_map
from flatland.envs.schedule_generators import random_schedule_generator
from flatland.utils.simple_rail import make_simple_rail
np.random.seed(1)
def test_initial_status():
......
......@@ -10,7 +10,6 @@ def test_build_railway_infrastructure():
rail_trans = RailEnvTransitions()
grid_map = GridTransitionMap(width=20, height=20, transitions=rail_trans)
grid_map.grid.fill(0)
np.random.seed(0)
# Make connection with dead-ends on both sides
start_point = (2, 2)
......@@ -84,7 +83,6 @@ def test_fix_inner_nodes():
rail_trans = RailEnvTransitions()
grid_map = GridTransitionMap(width=6, height=10, transitions=rail_trans)
grid_map.grid.fill(0)
np.random.seed(0)
start = (2, 2)
target = (8, 2)
......
......@@ -142,16 +142,16 @@ def test_malfunction_process_statistically():
env.agents[0].target = (0, 0)
# Next line only for test generation
# agent_malfunction_list = [[] for i in range(10)]
agent_malfunction_list = [[0, 5, 4, 3, 2, 1, 0, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1],
[0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 0],
[5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 0, 0, 5, 4, 3, 2, 1, 0],
[0, 5, 4, 3, 2, 1, 0, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1],
[0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2],
[5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 4, 3, 2, 1, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0]]
agent_malfunction_list = [[0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 0, 0, 5, 4],
[0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 0, 5, 4, 3, 2],
[0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1],
[0, 0, 5, 4, 3, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0],
[5, 4, 3, 2, 1, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 5],
[5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2],
[5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0, 5, 4]]