AI4Finance-Foundation / ElegantRL

Massively Parallel Deep Reinforcement Learning. 🔥
https://ai4finance.org
Other
3.62k stars 832 forks source link

muti_env question #130

Closed wanghui589 closed 2 years ago

wanghui589 commented 2 years ago

when i set 'env_num' more than 1, then the question generate:

Process Process-3:
Traceback (most recent call last):
  File "/home/spaci/anaconda3/envs/RL/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/home/spaci/anaconda3/envs/RL/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/spaci/RL/ElegantRL/elegantrl/train/run.py", line 149, in run
    agent = init_agent(args, gpu_id, env)
  File "/home/spaci/RL/ElegantRL/elegantrl/train/run.py", line 75, in init_agent
    assert isinstance(states, torch.Tensor)
AssertionError

can anyone help me thanks in advance!

hmomin commented 2 years ago

Which environment are you trying to run? If it's an OpenAI gym environment, it's likely not possible to use a vectorized set of multiple environments without changing a lot of the code. For example, see this block in the code:

https://github.com/AI4Finance-Foundation/ElegantRL/blob/7b0f62d9c673c727f07ef3498ff8aa137589bb6b/elegantrl/train/run.py#L69-L76

When env_num is greater than 1, the code is assuming that the environment itself is going to return a Tensor of states (like an Isaac Gym environment would). OpenAI gym environments won't do this on their own without a lot of internal changes to their own code. If you were insistent on using a vectorized environment here, that might require multiple env objects, which could get pretty memory intensive and change a lot of the code base.

It should be possible to use multiple processes when training with OpenAI gym environments though, which may give you the parallelism that you're looking for. Also, it's possible to have env_num > 1 for Isaac Gym environments (see examples/tutorial_Isaac_Gym.py for more info).

If this is a satisfactory answer, please remember to close this issue.

wanghui589 commented 2 years ago

thank u very much. I got it! I try the OpenAI gym environment such as 'Pendulum' and 'LunarLander' and so on, It doesn't work. Does this mean that the vectorized environment is only valid for Isaac Gym environment?

hmomin commented 2 years ago

thank u very much. I got it! I try the OpenAI gym environment such as 'Pendulum' and 'LunarLander' and so on, It doesn't work. Does this mean that the vectorized environment is only valid for Isaac Gym environment?

Yes, Isaac Gym lets you work with vectorized environments and the OpenAI gym only allows you to work with single evironments.

wanghui589 commented 2 years ago

thank u again!!