|
| 1 | +# MIT License |
| 2 | + |
| 3 | +# Copyright (c) 2018 OpenAI |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +import numpy as np |
| 24 | +import gym |
| 25 | + |
| 26 | + |
| 27 | +class MultiDiscrete(gym.Space): |
| 28 | + """ |
| 29 | + - The multi-discrete action space consists of a series of discrete action spaces with different |
| 30 | + parameters |
| 31 | + - It can be adapted to both a Discrete action space or a continuous (Box) action space |
| 32 | + - It is useful to represent game controllers or keyboards where each key can be represented as |
| 33 | + a discrete action space |
| 34 | + - It is parametrized by passing an array of arrays containing [min, max] for each discrete action |
| 35 | + space where the discrete action space can take any integers from `min` to `max` (both inclusive) |
| 36 | + Note: A value of 0 always need to represent the NOOP action. |
| 37 | + e.g. Nintendo Game Controller |
| 38 | + - Can be conceptualized as 3 discrete action spaces: |
| 39 | + 1) Arrow Keys: Discrete 5 - NOOP[0], UP[1], RIGHT[2], DOWN[3], LEFT[4] - params: min: 0, max: 4 |
| 40 | + 2) Button A: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1 |
| 41 | + 3) Button B: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1 |
| 42 | + - Can be initialized as |
| 43 | + MultiDiscrete([ [0,4], [0,1], [0,1] ]) |
| 44 | + """ |
| 45 | + def __init__(self, array_of_param_array): |
| 46 | + self.low = np.array([x[0] for x in array_of_param_array]) |
| 47 | + self.high = np.array([x[1] for x in array_of_param_array]) |
| 48 | + self.num_discrete_space = self.low.shape[0] |
| 49 | + |
| 50 | + def sample(self): |
| 51 | + """ Returns a array with one sample from each discrete action space """ |
| 52 | + # For each row: round(random .* (max - min) + min, 0) |
| 53 | + # random_array = prng.np_random.rand(self.num_discrete_space) |
| 54 | + random_array = np.random.RandomState().rand(self.num_discrete_space) |
| 55 | + return [int(x) for x in np.floor(np.multiply((self.high - self.low + 1.), random_array) + self.low)] |
| 56 | + |
| 57 | + def contains(self, x): |
| 58 | + return len(x) == self.num_discrete_space \ |
| 59 | + and (np.array(x) >= self.low).all() \ |
| 60 | + and (np.array(x) <= self.high).all() |
| 61 | + |
| 62 | + @property |
| 63 | + def shape(self): |
| 64 | + return self.num_discrete_space |
| 65 | + |
| 66 | + def __repr__(self): |
| 67 | + return "MultiDiscrete" + str(self.num_discrete_space) |
| 68 | + |
| 69 | + def __eq__(self, other): |
| 70 | + return np.array_equal(self.low, other.low) and np.array_equal(self.high, other.high) |
0 commit comments