Task Interface#
Each environment will define tasks in a different way. A “task” might be the environment seed, an initialization argument, or an entirely different subclass. The TaskEnv and TaskWrapper provides a simple interface to change the task of an environment.
A task wrapper defines allows an existing environment to change its task when reset()
is called. It can be used to map tasks from the task space to actual environment
configurations, or to add entirely new tasks to an environment.
To write a custom task wrapper for an environment, simply subclass the TaskWrapper
for gym environments or PettingZooTaskWrapper
for pettingzoo environments.
Syllabus includes a task wrappers for common use cases, as well as examples of task wrappers for specific environments.
Required Methods#
change_task(task)
- Updates the environment configuration to play the provided task on the next episode.This gets called before the wrapper’s environment is reset.
If changing the task only requires you to edit properties of the environment, you can do so in the change_task()
method.
This is called before the internal environment’s reset()
function when you pass a new_task
to the wrapped environment’s reset()
.
If you need to perform more complex operations, you can also override the entire reset()
method or other environment methods.
Optional features#
syllabus.core.task_interface.task_env module#
- class syllabus.core.task_interface.task_env.PettingZooTaskEnv(*args, **kwargs)[source]#
Bases:
ParallelEnv
- change_task(new_task)[source]#
Changes the task of the existing environment to the new_task.
Each environment will implement tasks differently. The easiest system would be to call a function or set an instance variable to change the task.
Some environments may need to be reset or even reinitialized to change the task. If you need to reset or re-init the environment here, make sure to check that it is not in the middle of an episode to avoid unexpected behavior.
- observation(observation)[source]#
Adds the goal encoding to the observation. Override to add additional task-specific observations. Returns a modified observation. TODO: Complete this implementation and find way to support centralized encodings
- class syllabus.core.task_interface.task_env.TaskEnv(*args, **kwargs)[source]#
Bases:
Env
- change_task(new_task)[source]#
Changes the task of the existing environment to the new_task.
Each environment will implement tasks differently. The easiest system would be to call a function or set an instance variable to change the task.
Some environments may need to be reset or even reinitialized to change the task. If you need to reset or re-init the environment here, make sure to check that it is not in the middle of an episode to avoid unexpected behavior.
- observation(observation)[source]#
Adds the goal encoding to the observation. Override to add additional task-specific observations. Returns a modified observation. TODO: Complete this implementation and find way to support centralized encodings
- reset(*args, **kwargs)[source]#
Resets the environment to an initial internal state, returning an initial observation and info.
This method generates a new starting state often with some randomness to ensure that the agent explores the state space and learns a generalised policy about the environment. This randomness can be controlled with the
seed
parameter otherwise if the environment already has a random number generator andreset()
is called withseed=None
, the RNG is not reset.Therefore,
reset()
should (in the typical use case) be called with a seed right after initialization and then never again.For Custom environments, the first line of
reset()
should besuper().reset(seed=seed)
which implements the seeding correctly.Changed in version v0.25: The
return_info
parameter was removed and now info is expected to be returned.- Parameters:
seed (optional int) – The seed that is used to initialize the environment’s PRNG (np_random) and the read-only attribute np_random_seed. If the environment does not already have a PRNG and
seed=None
(the default option) is passed, a seed will be chosen from some source of entropy (e.g. timestamp or /dev/urandom). However, if the environment already has a PRNG andseed=None
is passed, the PRNG will not be reset and the env’snp_random_seed
will not be altered. If you pass an integer, the PRNG will be reset even if it already exists. Usually, you want to pass an integer right after the environment has been initialized and then never again. Please refer to the minimal example above to see this paradigm in action.options (optional dict) – Additional information to specify how the environment is reset (optional, depending on the specific environment)
- Returns:
- Observation of the initial state. This will be an element of
observation_space
(typically a numpy array) and is analogous to the observation returned by
step()
.- info (dictionary): This dictionary contains auxiliary information complementing
observation
. It should be analogous to the
info
returned bystep()
.
- Observation of the initial state. This will be an element of
- Return type:
observation (ObsType)
syllabus.core.task_interface.reinit_task_wrapper module#
Task wrapper for NLE that can change tasks at reset using the NLE’s task definition format.
- class syllabus.core.task_interface.reinit_task_wrapper.PettingZooReinitTaskWrapper(env: Env, env_fn: Callable, task_space: Space | None = None)[source]#
Bases:
PettingZooTaskWrapper
This is a general wrapper for tasks defined as subclasses of a base environment.
This wrapper reinitializes the environment with the provided env function at the start of each episode. This is a simple, general solution to using Syllabus with tasks that need to be reinitialized, but it is inefficient. It’s likely that you can achieve better performance by using a more specialized wrapper.
- change_task(new_task: Any)[source]#
Change task by directly editing environment class.
This ensures that all instance variables are reset, not just the ones for the current task. We do this efficiently by keeping track of which reset functions have already been called, since very few tasks override reset. If new_task is provided, we change the task before calling the final reset.
- class syllabus.core.task_interface.reinit_task_wrapper.ReinitTaskWrapper(env: Env, env_fn: Callable, task_space: Space | None = None)[source]#
Bases:
TaskWrapper
This is a general wrapper for tasks defined as subclasses of a base environment.
This wrapper reinitializes the environment with the provided env function at the start of each episode. This is a simple, general solution to using Syllabus with tasks that need to be reinitialized, but it is inefficient. It’s likely that you can achieve better performance by using a more specialized wrapper.
- change_task(new_task: Tuple | int | float)[source]#
Change task by directly editing environment class.
This ensures that all instance variables are reset, not just the ones for the current task. We do this efficiently by keeping track of which reset functions have already been called, since very few tasks override reset. If new_task is provided, we change the task before calling the final reset.
syllabus.core.task_interface.subclass_task_wrapper module#
Task wrapper for NLE that can change tasks at reset using the NLE’s task definition format.
- class syllabus.core.task_interface.subclass_task_wrapper.SubclassTaskWrapper(env: Env, task_subclasses: List[Env] | None = None, **env_init_kwargs)[source]#
Bases:
TaskWrapper
This is a general wrapper for tasks defined as subclasses of a base environment.
This wrapper reinitializes the environment with the provided env function at the start of each episode. This is a simple, general solution to using Syllabus with tasks that need to be reinitialized, but it is inefficient. It’s likely that you can achieve better performance by using a more specialized wrapper.
- change_task(new_task: int)[source]#
Change task by directly editing environment class.
This ensures that all instance variables are reset, not just the ones for the current task. We do this efficiently by keeping track of which reset functions have already been called, since very few tasks override reset. If new_task is provided, we change the task before calling the final reset.
- property current_task#
syllabus.core.task_interface.task_wrapper module#
- class syllabus.core.task_interface.task_wrapper.PettingZooTaskWrapper(env: ParallelEnv)[source]#
Bases:
BaseParallelWrapper
- property agents#
- change_task(new_task)[source]#
Changes the task of the existing environment to the new_task.
Each environment will implement tasks differently. The easiest system would be to call a function or set an instance variable to change the task.
Some environments may need to be reset or even reinitialized to change the task. If you need to reset or re-init the environment here, make sure to check that it is not in the middle of an episode to avoid unexpected behavior.
- observation(observation)[source]#
Adds the goal encoding to the observation. Override to add additional task-specific observations. Returns a modified observation. TODO: Complete this implementation and find way to support centralized encodings TODO: Support PettingZoo environments TODO: Use TaskSpace for encodings?
- class syllabus.core.task_interface.task_wrapper.TaskWrapper(*args, **kwargs)[source]#
Bases:
Wrapper
- change_task(new_task)[source]#
Changes the task of the existing environment to the new_task.
Each environment will implement tasks differently. The easiest system would be to call a function or set an instance variable to change the task.
Some environments may need to be reset or even reinitialized to change the task. If you need to reset or re-init the environment here, make sure to check that it is not in the middle of an episode to avoid unexpected behavior.
- observation(observation)[source]#
Adds the goal encoding to the observation. Override to add additional task-specific observations. Returns a modified observation. TODO: Complete this implementation and find way to support centralized encodings