cartpole_mass.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. from gymnasium.envs.classic_control.cartpole import CartPoleEnv
  2. from gymnasium.utils import EzPickle
  3. import numpy as np
  4. from ray.rllib.env.apis.task_settable_env import TaskSettableEnv
  5. class CartPoleMassEnv(CartPoleEnv, EzPickle, TaskSettableEnv):
  6. """CartPoleMassEnv varies the weights of the cart and the pole."""
  7. def sample_tasks(self, n_tasks):
  8. # Sample new cart- and pole masses (random floats between 0.5 and 2.0
  9. # (cart) and between 0.05 and 0.2 (pole)).
  10. cart_masses = np.random.uniform(low=0.5, high=2.0, size=(n_tasks, 1))
  11. pole_masses = np.random.uniform(low=0.05, high=0.2, size=(n_tasks, 1))
  12. return np.concatenate([cart_masses, pole_masses], axis=-1)
  13. def set_task(self, task):
  14. """
  15. Args:
  16. task (Tuple[float]): Masses of the cart and the pole.
  17. """
  18. self.masscart = task[0]
  19. self.masspole = task[1]
  20. def get_task(self):
  21. """
  22. Returns:
  23. Tuple[float]: The current mass of the cart- and pole.
  24. """
  25. return np.array([self.masscart, self.masspole])