flatland.envs.malfunction_generators module#
Malfunction generators for rail systems
- class flatland.envs.malfunction_generators.FileMalfunctionGen(filename: str | Path = None, load_from_package=None, env_dict=None)[source]#
Bases:
ParamMalfunctionGen
- class flatland.envs.malfunction_generators.Malfunction(num_broken_steps)#
Bases:
NamedTuple- num_broken_steps: int#
Alias for field number 0
- class flatland.envs.malfunction_generators.MalfunctionParameters(malfunction_rate, min_duration, max_duration)#
Bases:
NamedTuple- malfunction_rate: float#
Alias for field number 0
- max_duration: int#
Alias for field number 2
- min_duration: int#
Alias for field number 1
- class flatland.envs.malfunction_generators.MalfunctionProcessData(malfunction_rate, min_duration, max_duration)#
Bases:
NamedTuple- malfunction_rate: float#
Alias for field number 0
- max_duration: int#
Alias for field number 2
- min_duration: int#
Alias for field number 1
- class flatland.envs.malfunction_generators.NoMalfunctionGen[source]#
Bases:
ParamMalfunctionGen
- class flatland.envs.malfunction_generators.ParamMalfunctionGen(parameters: MalfunctionParameters)[source]#
Bases:
objectPreserving old behaviour of using MalfunctionParameters for constructor, but returning MalfunctionProcessData in get_process_data. Data structure and content is the same.
Whether a malfunction starts is decided per agent and step by a block of NBR_CHACHED_RAND random numbers. The block is taken from the np_random passed to the first generate() (for the malfunction effects generators, the env’s np_random), and then read in order, one entry per draw, cyclically: after NBR_CHACHED_RAND draws the entries repeat. The duration of a malfunction is drawn from np_random directly.
generate() advances _rand_idx before it reads, so a new generator’s first draw reads entry 1, and entry 0 is only read after the block wraps around. This is intended behaviour, not a bug: it doesn’t change the probability of a malfunction, and reading entry 0 first would shift every draw: it would change the malfunctions of every recorded episode, and what a snapshot’s persisted malfunction_rand_idx means when the snapshot is replayed within its episode (the next reset() starts the generator over anyway).
reset() starts the generator over at the start of an episode, so an episode draws as in a new env.
- classmethod extract_malfunction_generator(env_dict: dict, key='malfunction') ParamMalfunctionGen[source]#
- generate(np_random: RandomState) Malfunction | None[source]#
- reset()[source]#
Start over as a new generator: the next generate() takes a new block of random numbers from the np_random it is given. Called by the malfunction effects generators at the start of an episode.
Takes no seed: the generator owns no random state of its own. Every generate(np_random) gets the random state of its caller (the env’s np_random for the malfunction effects generators), and the first one after reset() draws the block from it (generate_rand_numbers()). After env.reset(random_seed=s), the env’s np_random is reseeded and is used in the same order as in a new env with seed s, so the block, and the durations drawn after it, are those of that new env. A seed here would be a second source of randomness next to np_random.
- restore(cached_random_state: tuple | None, rand_idx: int)[source]#
Restore the block of random numbers and the position in it, as persisted from _cached_random_state and _rand_idx in a snapshot taken mid-episode. Without a cached_random_state (no draw yet), the block stays to be drawn at the next generate().
- flatland.envs.malfunction_generators.malfunction_from_file(filename: str, load_from_package=None) Tuple[Callable[[RandomState, bool], Malfunction], MalfunctionProcessData][source]#
Utility to load pickle file
Parameters#
input_file : Pickle file generated by env.save() or editor
Returns#
generator, Tuple[float, int, int] with mean_malfunction_rate, min_number_of_steps_broken, max_number_of_steps_broken
- flatland.envs.malfunction_generators.malfunction_from_params(parameters: MalfunctionParameters) Tuple[Callable[[RandomState, bool], Malfunction], MalfunctionProcessData][source]#
Utility to load malfunction from parameters
Parameters#
- parameterscontains all the parameters of the malfunction
malfunction_rate : float rate per timestep at which each agent malfunctions min_duration : int minimal duration of a failure max_number_of_steps_broken : int maximal duration of a failure
Returns#
generator, Tuple[float, int, int] with mean_malfunction_rate, min_number_of_steps_broken, max_number_of_steps_broken
- flatland.envs.malfunction_generators.no_malfunction_generator() Tuple[Callable[[RandomState, bool], Malfunction], MalfunctionProcessData][source]#
Malfunction generator which generates no malfunctions
Parameters#
Nothing
Returns#
generator, Tuple[float, int, int] with mean_malfunction_rate, min_number_of_steps_broken, max_number_of_steps_broken
- flatland.envs.malfunction_generators.single_malfunction_generator(earlierst_malfunction: int, malfunction_duration: int) Tuple[Callable[[RandomState, bool], Malfunction], MalfunctionProcessData][source]#
Malfunction generator which guarantees exactly one malfunction during an episode of an ACTIVE agent.
Parameters#
earlierst_malfunction: Earliest possible malfunction onset malfunction_duration: The duration of the single malfunction
Returns#
generator, Tuple[float, int, int] with mean_malfunction_rate, min_number_of_steps_broken, max_number_of_steps_broken