Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the MultiDiscrete action space definition #65

Open
vwxyzjn opened this issue Mar 1, 2022 · 0 comments
Open

Modify the MultiDiscrete action space definition #65

vwxyzjn opened this issue Mar 1, 2022 · 0 comments

Comments

@vwxyzjn
Copy link
Collaborator

vwxyzjn commented Mar 1, 2022

I just realized we can use a multi-dimensional array as the nvec for gym's MultiDiscrete action space. We should re-define the action space to have shape (h, w, 7) as follows:

import gym
import numpy as np

shapes = np.ones((2, 2, 7), dtype=np.int32)
shapes[:] = [6, 4, 4, 4, 4, 7, 7]
space = gym.spaces.MultiDiscrete(shapes)

>>> shapes
array([[[6, 4, 4, 4, 4, 7, 7],
        [6, 4, 4, 4, 4, 7, 7]],

       [[6, 4, 4, 4, 4, 7, 7],
        [6, 4, 4, 4, 4, 7, 7]]], dtype=int32)
>>> shapes.shape
(2, 2, 7)
>>> space
MultiDiscrete([[[6 4 4 4 4 7 7]
  [6 4 4 4 4 7 7]]

 [[6 4 4 4 4 7 7]
  [6 4 4 4 4 7 7]]])
>>> space.sample()
array([[[2, 3, 2, 1, 0, 4, 6],
        [0, 1, 2, 2, 2, 1, 3]],

       [[5, 0, 2, 1, 0, 3, 3],
        [4, 1, 3, 3, 3, 5, 2]]])

This will simplify the various flatten and reshape logic such as

https://github.com/vwxyzjn/gym-microrts/blob/3d7a42f46efbd39a0b806388b8a445fbee48d00f/experiments/ppo_gridnet.py#L428
https://github.com/vwxyzjn/gym-microrts/blob/3d7a42f46efbd39a0b806388b8a445fbee48d00f/gym_microrts/envs/vec_env.py#L168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant