5

Info: I am using OpenAI Gym to create RL environments but need multiple copies of an environment for something I am doing. I do not want to do anything like [gym.make(...) for i in range(2)] to make a new environment.

Question: Given one gym env what is the best way to make a copy of it so that you have 2 duplicate but disconnected envs?

Here is an example:

import gym

env = gym.make("CartPole-v0")
new_env = # NEED COPY OF ENV HERE

env.reset() # Should not alter new_env

2 Answers2

2

Astariul has an updated answer::

Their answer states:

import copy 
env_2 = copy.deepcopy(env)

For more info about 'copy.deepcopy', and the copy library

Link to copy library documentation

  • Oh, you are right. I just assumed that wouldn't work. Thanks! –  Sep 18 '20 at 23:02
  • 1
    This answer seems deprecated, when I run `env.copy()` I receive the following error : `'CartPoleEnv' object has no attribute 'copy'` – Astariul May 18 '22 at 00:21
  • 1
    Updated! Thanks for notifying me. I don't know too much about this forum, but if there is a way to transfer the 'correct answer' then sending the check to your answer would be good. Also, the first thing in my new answer was a link to yours. Also, would you mind adding a link to the docs for your answer—I know some people (like me, and you) want to take a look at more details regarding the library. [Link to copy docs](https://docs.python.org/3/library/copy.html). Anyways thanks a lot for your comment! – William Banquier May 19 '22 at 01:28
  • Hey thanks for the update :) Yep it's a great idea to add a link to the doc, will do ! Choosing the accepted answer is OP's responsibility ! – Astariul May 19 '22 at 03:45
2

You can use copy.deepcopy() to copy the current environment :

import gym
import copy

env = gym.make("CartPole-v0")
env.reset()

env_2 = copy.deepcopy(env)

env.step() # Stepping through `env` will not alter `env_2`

However note that this solution might not work in case of custom environment, if it contains things that can't be deepcopied (like generators).

Astariul
  • 2,190
  • 4
  • 24
  • 41