Can I not inherit gym as env?

Because I want to use my own data set as the observation space as input, rather than the observation space based on gym.

class MultiEnv(gym.Env):
    def __init__(self, env_config):
        # pick actual env based on worker and env indexes
        self.env = gym.make(
            choose_env_for(env_config.worker_index, env_config.vector_index))
        self.action_space = self.env.action_space
        self.observation_space = self.env.observation_space
    def reset(self):
        return self.env.reset()
    def step(self, action):
        return self.env.step(action)

register_env("multienv", lambda config: MultiEnv(config))

Hi @sunmouren,

Welcome to the forum. Do you have a trace back and reproduction script you can share?

I just want to know if it is possible to not inherit gym, the state space of my env is a pure numpy array.

Hi @sunmouren,

I misunderstood. I thought you were saying that environment you posted was having an error.

You will need to wrap that numpy array in the gym api. You do not actually have to inherit from gym.Env but your wrapper will need to have the observation_space and action_space member variables as well as the reset and step methods.

This would be a minimal example :slight_smile:

import gym
import numpy as np
class NumpyEnv(): #(gym.Env):
    """ Minimal numpy env
    """

    def __init__(self, episode_length, config=None):
        self.episode_length = episode_length
        self.config = config
        self.i = np.zeros(1)
        self.observation_space = gym.spaces.Discrete(1)
        self.action_space = gym.spaces.Discrete(2)

    def reset(self):
        self.i = np.zeros(1)
        return self.i
        # return observation

    def step(self, action):
        self.i += 1
        return 0, 1.0, self.i >= self.episode_length, {}
        #return observation, reward, done, info

If you have a fixed dataset of experiences you could prepare an offline dataset to train with instead.

https://docs.ray.io/en/latest/rllib-offline.html


2 Likes