task_settable_env.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import gym
  2. from typing import List, Any
  3. TaskType = Any # Can be different types depending on env, e.g., int or dict
  4. class TaskSettableEnv(gym.Env):
  5. """
  6. Extension of gym.Env to define a task-settable Env.
  7. Your env must implement this interface in order to be used with MAML.
  8. For curriculum learning, you can add this API to your env such that
  9. the `env_task_fn` can set the next task as needed.
  10. Supports:
  11. - Sampling from a distribution of tasks for meta-learning.
  12. - Setting the env to any task it supports.
  13. - Getting the current task this env has been set to.
  14. Examples:
  15. >>> env = TaskSettableEnv(...)
  16. >>> ...
  17. >>> Trainer.workers.foreach_env(lambda base_env: base_env.my_prop)
  18. """
  19. def sample_tasks(self, n_tasks: int) -> List[TaskType]:
  20. """Samples task of the meta-environment
  21. Args:
  22. n_tasks (int) : number of different meta-tasks needed
  23. Returns:
  24. tasks (list) : an (n_tasks) length list of tasks
  25. """
  26. raise NotImplementedError
  27. def set_task(self, task: TaskType) -> None:
  28. """Sets the specified task to the current environment
  29. Args:
  30. task: task of the meta-learning environment
  31. """
  32. raise NotImplementedError
  33. def get_task(self) -> TaskType:
  34. """Gets the task that the agent is performing in the current environment
  35. Returns:
  36. task: task of the meta-learning environment
  37. """
  38. raise NotImplementedError