How to use the gym.spaces.Discrete function in gym

To help you get started, we’ve selected a few gym examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fabiopardo / qmap / qmap / envs / custom_mario.py View on Github external
self.frame_skip = frame_skip
        n_frames = stack * (3 * use_color + 1 * (not use_color) + use_rc_frame)
        self.frames = deque([], maxlen=(self.frame_skip * (self.stack - 1) + 1))
        self.observation_space = spaces.Box(low=0, high=255, shape=(self.screen_height, self.screen_width, n_frames))
        # coordinates
        self.coords_ratio = coords_ratio
        assert coords_ratio % screen_ratio == 0, (coords_ratio, screen_ratio)
        self.coords_screen_ratio = coords_ratio // screen_ratio
        self.coords_height = self.original_height // coords_ratio
        self.coords_width = self.original_width // coords_ratio
        self.coords_shape = (self.coords_height, self.coords_width)
        # actions
        self.action_names = ['JUMP+LEFT', 'JUMP', 'JUMP+RIGHT', 'LEFT', 'NOOP', 'RIGHT']
        self.action_list = [actions[n] for n in self.action_names]
        n_actions = len(self.action_list)
        self.action_space = spaces.Discrete(n_actions)
        self.action_repeat = action_repeat
        # miscellaneous
        frame_name = 'RGB' if use_color else 'G'
        if use_rc_frame: frame_name += 'C'
        self.name = 'CustomSuperMarioAllStars_{}_obs{}x{}x{}x{}_qframes{}x{}x{}_skip{}_repeat{}-v0'.format(
            level, *self.screen_shape, frame_name, stack, *self.coords_shape, n_actions, frame_skip, action_repeat)
github xinleipan / gym-gridworld / tmp / gridworld.py View on Github external
def __init__(self, grid_size, grid_map_path, verbose=False, restart=False, show_partial=False):
        self.actions = (0,1,2,3,4) # stay, move up, down, left, right
        self.action_pos = {0:[0,0], 1:[-1, 0], 2:[1, 0], 3:[0,-1], 4:[0,1]}
        self.action_space = spaces.Discrete(len(self.actions))
        self.grid_size = grid_size
        self.states = np.random.randn(grid_size, grid_size, 3) * 0.0
        self.verbose = verbose
        self.show_part = show_partial
        self.grid_map_path = grid_map_path
        grid_map = open(grid_map_path, 'r').readlines()
        grid_map_array = []
        for k1 in grid_map:
            k1s = k1.split(' ')
            tmp_arr = []
            for k2 in k1s:
                 try:
                    tmp_arr.append(int(k2))
                 except:
                    pass
            grid_map_array.append(tmp_arr)
github google-research / recsim / recsim / environments / long_term_satisfaction.py View on Github external
def response_space(cls):
    # `engagement` feature range is [0, MAX_ENGAGEMENT_MAGNITUDE]
    return spaces.Dict({
        'click':
            spaces.Discrete(2),
        'engagement':
            spaces.Box(
                low=0.0,
                high=LTSResponse.MAX_ENGAGEMENT_MAGNITUDE,
                shape=tuple(),
                dtype=np.float32)
    })
github danijar / mindpark / mindpark / env / simple_doom.py View on Github external
def action_space(self):
        return Discrete(len(self.AVAILABLE_ACTIONS))
github camellyx / 10707-deep-learning-project / multiagent-particle-envs / multiagent / environment.py View on Github external
# if true, even the action is continuous, action will be performed
        # discretely
        self.force_discrete_action = world.discrete_action if hasattr(
            world, 'discrete_action') else False
        # if true, every agent has the same reward
        self.shared_reward = False
        self.time = 0

        # configure spaces
        self.action_space = []
        self.observation_space = []
        for agent in self.agents:
            total_action_space = []
            # physical action space
            if self.discrete_action_space:
                u_action_space = spaces.Discrete(world.dim_p * 2 + 1)
            else:
                u_action_space = spaces.Box(
                    low=-agent.u_range, high=+agent.u_range, shape=(world.dim_p,))
            if agent.movable:
                total_action_space.append(u_action_space)
            # communication action space
            if self.discrete_action_space:
                c_action_space = spaces.Discrete(world.dim_c)
            else:
                c_action_space = spaces.Box(
                    low=0.0, high=1.0, shape=(world.dim_c,))
            if not agent.silent:
                total_action_space.append(c_action_space)
            # total action space
            if len(total_action_space) > 1:
                # all action spaces are discrete, so simplify to MultiDiscrete
github KristianHolsheimer / keras-gym / scripts / chase / dqn.py View on Github external
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.action_space = gym.spaces.Discrete(9)
        self.observation_space = gym.spaces.MultiDiscrete(
            nvec=np.full(self.SHAPE, 2, dtype='int'))
        self._seed = 0
github bstriner / gym-traffic / gym_traffic / envs / traffic_env.py View on Github external
binary = "sumo"
            args += ["--no-step-log"]

        with open(routefile) as f:
            self.route = f.read()
        self.tmpfile = tmpfile
        self.pngfile = pngfile
        self.sumo_cmd = [binary] + args
        self.sumo_step = 0
        self.lights = lights
        self.action_space = spaces.DiscreteToMultiDiscrete(
            spaces.MultiDiscrete([[0, len(light.actions) - 1] for light in self.lights]), 'all')

        trafficspace = spaces.Box(low=float('-inf'), high=float('inf'),
                                  shape=(len(self.loops) * len(self.loop_variables),))
        lightspaces = [spaces.Discrete(len(light.actions)) for light in self.lights]
        self.observation_space = spaces.Tuple([trafficspace] + lightspaces)

        self.sumo_running = False
        self.viewer = None
github Geonhee-LEE / ur-reinforcement-learning / ur_openai_ros / ur_training / script / ur_env.py View on Github external
self.ur_state_object.set_desired_world_point(self.desired_pose.position.x,
                                                          self.desired_pose.position.y,
                                                          self.desired_pose.position.z)

        self.ur_joint_pubisher_object = JointPub()
        


        """
        For this version, we consider 5 actions
        1-2) Increment/Decrement haa_joint
        3-4) Increment/Decrement hfe_joint
        5) Dont Move
        6) Perform One Jump
        """
        self.action_space = spaces.Discrete(6)
        self.reward_range = (-np.inf, np.inf)

        self._seed()
github danielegrattarola / deep-q-atari / FeatureSelection / grid_world / grid_world / envs / gridworld_env.py View on Github external
def __init__(self):
        self.width = 16
        self.height = 9
        self._cell_size = 10

        self.action_space = spaces.Discrete(4)
        self.observation_space = spaces.Box(self.height * self._cell_size, self.width * self._cell_size, 1)

        self.viewer = Viewer(width=self.width, height=self.height, cell_size=self._cell_size)

        self._seed()
        self.reset()
github cjm715 / mgym / mgym / envs / snake_env.py View on Github external
def reset(self, N):
        self.done = False
        self.N = N
        self.nA = 4
        self.observation_space = spaces.Box(low=0, high=self.N + 2, shape=(
            GRID_HEIGHT, GRID_WIDTH), dtype=np.uint8)
        self.action_space = spaces.Tuple(
            [spaces.Discrete(self.nA) for _ in range(self.N)])
        self.grid = np.zeros((GRID_HEIGHT, GRID_WIDTH))
        self.apples = []
        self.snakes = []
        self.walls = []
        self.create_boarder()

        for i in range(N):
            snake_id = 3 + i
            location = self._get_empty_location()
            self.snakes.append(Snake(*location, snake_id))

        for i in range(NUM_APPLES):
            location = self._get_empty_location()
            self.apples.append(Apple(*location))

        self.update_grid()