|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | + |
| 4 | +class VectorEnvironment(ABC): |
| 5 | + """ |
| 6 | + A reinforcement learning vector Environment. |
| 7 | +
|
| 8 | + Similar to a regular RL environment except many environments are stacked together |
| 9 | + in the observations, rewards, and dones, and the vector environment expects |
| 10 | + an action to be given for each environment in step. |
| 11 | +
|
| 12 | + Also, since sub-environments are done at different times, you do not need to |
| 13 | + manually reset the environments when they are done, rather the vector environment |
| 14 | + automatically resets environments when they are complete. |
| 15 | + """ |
| 16 | + |
| 17 | + @property |
| 18 | + @abstractmethod |
| 19 | + def name(self): |
| 20 | + """ |
| 21 | + The name of the environment. |
| 22 | + """ |
| 23 | + |
| 24 | + @abstractmethod |
| 25 | + def reset(self): |
| 26 | + """ |
| 27 | + Reset the environment and return a new initial state. |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + State |
| 32 | + The initial state for the next episode. |
| 33 | + """ |
| 34 | + |
| 35 | + @abstractmethod |
| 36 | + def step(self, action): |
| 37 | + """ |
| 38 | + Apply an action and get the next state. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + action : Action |
| 43 | + The action to apply at the current time step. |
| 44 | +
|
| 45 | + Returns |
| 46 | + ------- |
| 47 | + all.environments.State |
| 48 | + The State of the environment after the action is applied. |
| 49 | + This State object includes both the done flag and any additional "info" |
| 50 | + float |
| 51 | + The reward achieved by the previous action |
| 52 | + """ |
| 53 | + |
| 54 | + @abstractmethod |
| 55 | + def close(self): |
| 56 | + """ |
| 57 | + Clean up any extraneous environment objects. |
| 58 | + """ |
| 59 | + |
| 60 | + @property |
| 61 | + @abstractmethod |
| 62 | + def state_array(self): |
| 63 | + """ |
| 64 | + A StateArray of the Environments at the current timestep. |
| 65 | + """ |
| 66 | + |
| 67 | + @property |
| 68 | + @abstractmethod |
| 69 | + def state_space(self): |
| 70 | + """ |
| 71 | + The Space representing the range of observable states for each environment. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + Space |
| 76 | + An object of type Space that represents possible states the agent may observe |
| 77 | + """ |
| 78 | + |
| 79 | + @property |
| 80 | + def observation_space(self): |
| 81 | + """ |
| 82 | + Alias for Environment.state_space. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + Space |
| 87 | + An object of type Space that represents possible states the agent may observe |
| 88 | + """ |
| 89 | + return self.state_space |
| 90 | + |
| 91 | + @property |
| 92 | + @abstractmethod |
| 93 | + def action_space(self): |
| 94 | + """ |
| 95 | + The Space representing the range of possible actions for each environment. |
| 96 | +
|
| 97 | + Returns |
| 98 | + ------- |
| 99 | + Space |
| 100 | + An object of type Space that represents possible actions the agent may take |
| 101 | + """ |
| 102 | + |
| 103 | + @property |
| 104 | + @abstractmethod |
| 105 | + def device(self): |
| 106 | + """ |
| 107 | + The torch device the environment lives on. |
| 108 | + """ |
| 109 | + |
| 110 | + @property |
| 111 | + @abstractmethod |
| 112 | + def num_envs(self): |
| 113 | + """ |
| 114 | + Number of environments in vector. This is the number of actions step() expects as input |
| 115 | + and the number of observations, dones, etc returned by the environment. |
| 116 | + """ |
0 commit comments