How to copy gym environment?

Viewed 1073

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 Answers

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).

Related