A wrapper around the OpenAI ``gym`` environments.
| 55 | |
| 56 | |
| 57 | class GymEnvironment(Environment): |
| 58 | # language=rst |
| 59 | """ |
| 60 | A wrapper around the OpenAI ``gym`` environments. |
| 61 | """ |
| 62 | |
| 63 | def __init__( |
| 64 | self, |
| 65 | name: str, |
| 66 | render_mode: str = "rgb_array", |
| 67 | encoder: Encoder = NullEncoder(), |
| 68 | **kwargs, |
| 69 | ) -> None: |
| 70 | # language=rst |
| 71 | """ |
| 72 | Initializes the environment wrapper. This class makes the |
| 73 | assumption that the OpenAI ``gym`` environment will provide an image |
| 74 | of format HxW or CxHxW as an observation (we will add the C |
| 75 | dimension to HxW tensors) or a 1D observation in which case no |
| 76 | dimensions will be added. |
| 77 | |
| 78 | :param name: The name of an OpenAI ``gym`` environment. |
| 79 | :param encoder: Function to encode observations into spike trains. |
| 80 | |
| 81 | Keyword arguments: |
| 82 | |
| 83 | :param float max_prob: Maximum spiking probability. |
| 84 | :param bool clip_rewards: Whether or not to use ``np.sign`` of rewards. |
| 85 | |
| 86 | :param int history: Number of observations to keep track of. |
| 87 | :param int delta: Step size to save observations in history. |
| 88 | :param bool add_channel_dim: Allows for the adding of the channel dimension in |
| 89 | 2D inputs. |
| 90 | """ |
| 91 | self.name = name |
| 92 | self.env = gym.make(id=name, render_mode=render_mode) |
| 93 | self.action_space = self.env.action_space |
| 94 | |
| 95 | self.encoder = encoder |
| 96 | |
| 97 | # Keyword arguments. |
| 98 | self.max_prob = kwargs.get("max_prob", 1.0) |
| 99 | self.clip_rewards = kwargs.get("clip_rewards", True) |
| 100 | |
| 101 | self.history_length = kwargs.get("history_length", None) |
| 102 | self.delta = kwargs.get("delta", 1) |
| 103 | self.add_channel_dim = kwargs.get("add_channel_dim", True) |
| 104 | self.seed = kwargs.get("seed", None) |
| 105 | |
| 106 | if self.history_length is not None and self.delta is not None: |
| 107 | self.history = { |
| 108 | i: torch.Tensor() |
| 109 | for i in range(1, self.history_length * self.delta + 1, self.delta) |
| 110 | } |
| 111 | else: |
| 112 | self.history = {} |
| 113 | |
| 114 | self.episode_step_count = 0 |
no outgoing calls
no test coverage detected