How to extend single cartpole environment to multiple cartpole environment in skrl and isaacsim tutorial

309 views Asked by At

Hello I want to use isaacgym environment to train my own agent

to complete this, I modify cartpole_task.py to multiple environment.

so I change num_envs=2 in __init__ method like below code

class CartpoleTask(BaseTask):
    def __init__(
        self,
        name,
        headless, 
        offset=None,

    ) -> None:
        self.headless = headless
        # task-specific parameters
        self._cartpole_position = [0.0, 0.0, 2.0]
        self._cartpole_position2 = [2.0, 0.0, 2.0]

        self._reset_dist = 3.0
        self._max_push_effort = 400.0

        # values used for defining RL buffers
        self._num_observations = 4
        self._num_actions = 1
        self._device = "cpu"
        self.num_envs = 2

        # a few class buffers to store RL-related states
        self.obs = torch.zeros((self.num_envs, self._num_observations))
        self.resets = torch.zeros((self.num_envs, 1))

        # set the action and observation space for RL
        self.action_space = spaces.Box(np.ones(self._num_actions) * -1.0, np.ones(self._num_actions) * 1.0)
        self.observation_space = spaces.Box(np.ones(self._num_observations) * -np.Inf, np.ones(self._num_observations) * np.Inf)

        # trigger __init__ of parent class
        BaseTask.__init__(self, name=name, offset=offset)

and add another cartpole like this

    def set_up_scene(self, scene) -> None:
        # retrieve file path for the Cartpole USD file
        # assets_root_path = get_assets_root_path()
        usd_path = "/home/nscl2004/Study/rl/stb/skrl/examples/usd/cartpole.usd"

        
        assets_root_path = get_assets_root_path()
        usd_path = assets_root_path + "/Isaac/Robots/Cartpole/cartpole.usd"
        
        # add the Cartpole USD to our stage
        create_prim(prim_path="/World/Cartpole", prim_type="Xform", position=self._cartpole_position)
        create_prim(prim_path="/World/Cartpole2", prim_type="Xform", position=self._cartpole_position2)
        
        add_reference_to_stage(usd_path, "/World/Cartpole")
        add_reference_to_stage(usd_path, "/World/Cartpole2")

        # create an ArticulationView wrapper for our cartpole - this can be extended towards accessing multiple cartpoles
        self._cartpoles = ArticulationView(prim_paths_expr="/World/Cartpole*", name="cartpole_view")
        # add Cartpole ArticulationView and ground plane to the Scene
        scene.add(self._cartpoles)
        scene.add_default_ground_plane()

        self.sd_helper = None
        self.viewport_window = None
        self._set_camera()

        # set default camera viewport position and target
        # self.set_initial_camera_params()

and execute train code from skrl tutorial but I met this error.

Traceback (most recent call last):
  File "cartpole_example_skrl.py", line 113, in <module>
    trainer.train()
  File "/home/user/.local/share/ov/pkg/isaac_sim-2022.1.0/kit/python/lib/python3.7/site-packages/skrl/trainers/torch/sequential.py", line 54, in train
    self.single_agent_train()
  File "/home/user/.local/share/ov/pkg/isaac_sim-2022.1.0/kit/python/lib/python3.7/site-packages/skrl/trainers/torch/base.py", line 208, in single_agent_train
    next_states, rewards, dones, infos = self.env.step(actions)
  File "/home/user/.local/share/ov/pkg/isaac_sim-2022.1.0/kit/python/lib/python3.7/site-packages/skrl/envs/torch/wrappers.py", line 322, in step
    observation, reward, done, info = self._env.step(self._tensor_to_action(actions))
  File "/home/user/.local/share/ov/pkg/isaac_sim-2022.1.0/kit/python/lib/python3.7/site-packages/skrl/envs/torch/wrappers.py", line 309, in _tensor_to_action
    return np.array(actions.cpu().numpy(), dtype=space.dtype).reshape(space.shape)
ValueError: cannot reshape array of size 2 into shape (1,)
/home/user/.local/share/ov/pkg/isaac_sim-2022.1.0/python.sh: line 46: 60952 Segmentation fault      (core dumped) $python_exe "$@" $args
There was an error running python

It's really hard to understand this error because I think if I set num_envs value as 2, trainer is automatically matching observation shape.

Did anyone solve an error similar to this one?

1

There are 1 answers

0
Catachii On

The problem comes from the definition of action space.

self.action_space = spaces.Box(np.ones(self._num_actions) * -1.0, np.ones(self._num_actions) * 1.0)

The size of the action space is only for one env and you should probably vectorize it or not use action space.