Factory

class maze.core.utils.factory.Factory(*args, **kwds)

Supports the creation of instances from configuration, that can be plugged into the environments (like demand generators or reward schemes).

Parameters

base_type – A common interface (parent class) of the registered types (e.g. DemandGenerator)

instantiate(config: Union[BaseType, None, Mapping[str, Any], Any], **kwargs: Any) → BaseType

Instantiate an object from configuration.

This is implemented as a thin layer on top of Hydra’s instantiate() function, with the following additions

  • Provides type hinting

  • Asserts the returned type matches the base_type

  • Throws an error if there the field ‘_target_’ is missing (Hydra’s instantiate() returns the config object in this case)

  • In case config is already an instantiated object, immediately return this existing instance (useful in the frequent case of a constructor that accepts either a config object or an already instantiated object)

Parameters
  • config – The configuration as dictionary

  • kwargs – Additional arguments that are merged with the configuration dictionary (useful to sideload objects that can not conveniently be specified in the config, e.g. a shared RandomState)

:return The instantiated object, of type base_type

instantiate_collection(config: Union[List[Union[None, Mapping[str, Any], Any]], Mapping[Union[str, Type], Union[None, Mapping[str, Any], Any]]], **kwargs: Any) → Dict[Union[str, int], BaseType]

Instantiates objects specified in a list or dictionary.

Parameters
  • config – A list or dictionary of individual configs, passed to instantiate()

  • kwargs – Additional arguments that are merged with the configuration dictionary (useful to sideload objects that can not conveniently be specified in the config, e.g. a shared RandomState)

:return A dictionary with either integers (in case config is given as list) or strings as keys and the newly

created instances as values.

type_from_name(name: Union[str, Type[BaseType]]) → Type[BaseType]

Import the given module and lookup the callable or class from the module with the correct base type.

Parameters

name – Fully qualified name including the module path (e.g. maze_envs.logistics.property_based_replenishment.env.maze_env.MazeEnv)

Returns

The one and only callable or class with the given name that derives from base_type.