@fardinabbasi : Keep in mind that the class of your custom environment, inheriting from gym.Env., must also be “ready” to take an env_config dictionary and parse it accordingly to attributes. See below a code snippet developed by me.
def assign_env_config(self, args, kwargs):
"""Configure instance based on args and keyword args."""
# In order to ensure compatbility to multiple RL libraries, this method is supposed
# to flexible treat the input as "args" or as "kwargs"
# First path: Parameters are available as a dictionary, which is delivered under the keyword "env_config".
# This case is occurring while gym.make().
if kwargs is not None:
for key, value in kwargs.items():
setattr(self, key, value)
if hasattr(self, "env_config"):
for key, value in self.env_config.items():
# Check types based on default settings
if hasattr(self, key):
if type(getattr(self, key)) == np.ndarray:
setattr(self, key, value)
else:
setattr(self, key, type(getattr(self, key))(value))
else:
raise AttributeError(f"{self} has no attribute {key}")
# Second path: "env_config" is passed as flattened dictionary, which is part of a tuple.
# This case is occurring in e.g. ray rllib.
# While ray provides EnvContext to capture that properly, we want to avoid dependency on ray.
if args is not None:
for i in range(len(args)):
args_item = args[i]
for key, value in args_item.items():
# Check types based on default settings
if hasattr(self, key):
if type(getattr(self, key)) == np.ndarray:
setattr(self, key, value)
else:
setattr(self, key, type(getattr(self, key))(value))
else:
raise AttributeError(f"{self} has no attribute {key}")