Skip to content

Commit

Permalink
Update gymnasium registration
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisyeh96 committed Dec 4, 2023
1 parent b17c358 commit a87fada
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 29 deletions.
25 changes: 24 additions & 1 deletion docs/buildingenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,27 @@ More instructions coming soon!

### Custom RL Loop

Coming soon!
```python
from sustaingym.envs.building import BuildingEnv, ParameterGenerator
params = ParameterGenerator(
building='OfficeSmall', weather='Hot_Dry', location='Tucson')
env = BuildingEnv(params)
num_hours = 24
obs, _ = env.reset(seed=123)
for _ in range(num_hours):
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
```
If you prefer using [`gym.make()`](https://gymnasium.farama.org/api/registry/), you may instead create the environment as follows:
```python
import gymnasium as gym
from sustaingym.envs.building import ParameterGenerator
params = ParameterGenerator(
building='OfficeSmall', weather='Hot_Dry', location='Tucson')
env = gym.make('sustaingym/Building-v0', parameters=params)
```
10 changes: 10 additions & 0 deletions docs/cogenenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ while not terminated:
obs, reward, terminated, truncated, info = env.step(action)
```

If you prefer using [`gym.make()`](https://gymnasium.farama.org/api/registry/), you may instead create the environment as follows:

```python
import gymnasium as gym
import sustaingym

rm = 300 # 300 MW renewables penetration
env = gym.make('sustaingym/Cogen-v0', renewables_magnitude=rm)
```


### Using our training script

Expand Down
10 changes: 10 additions & 0 deletions docs/evchargingenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ while not terminated:
obs, reward, terminated, truncated, info = env.step(action)
```

If you prefer using [`gym.make()`](https://gymnasium.farama.org/api/registry/), you may instead create the environment as follows:

```python
import gymnasium as gym
from sustaingym.envs.evcharging import GMMsTraceGenerator

gmmg = GMMsTraceGenerator('caltech', 'Summer 2019')
env = gym.make('sustaingym/EVCharging-v0', data_generator=gmmg)
```

### Using our training script

1. Install [miniconda3](https://docs.conda.io/en/latest/miniconda-other-installer-links.html).
Expand Down
23 changes: 18 additions & 5 deletions sustaingym/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@

register(
id='sustaingym/EVCharging-v0',
entry_point='sustaingym.envs:EVChargingEnv'
entry_point='sustaingym.envs.evcharging:EVChargingEnv',
nondeterministic=False
)

# register(
# id='sustaingym/ElectricityMarket-v0',
# entry_point='sustaingym.envs.electricitymarket:ElectricityMarketEnv'
# )

# register(
# id='sustaingym/DataCenter-v0',
# entry_point='sustaingym.envs:DataCenterEnv'
# )

register(
id='sustaingym/ElectricityMarket-v0',
entry_point='sustaingym.envs:ElectricityMarketEnv'
id='sustaingym/Building-v0',
entry_point='sustaingym.envs.building:BuildingEnv',
nondeterministic=False
)

register(
id='sustaingym/DataCenter-v0',
entry_point='sustaingym.envs:DataCenterEnv'
id='sustaingym/Cogen-v0',
entry_point='sustaingym.envs.cogen:CogenEnv',
nondeterministic=False
)
8 changes: 0 additions & 8 deletions sustaingym/envs/building/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

import gymnasium as gym
from gymnasium.envs.registration import EnvSpec
import numpy as np
from numpy import linalg as LA
from scipy.linalg import expm
Expand Down Expand Up @@ -194,13 +193,6 @@ def __init__(self, parameters: dict[str, Any]):
self.A_d = expm(A * self.timestep)
self.BD_d = LA.inv(A) @ (self.A_d - np.eye(self.A_d.shape[0])) @ BD

# Define environment spec
self.spec = EnvSpec(
id='sustaingym/BuildingEnv-v0',
entry_point='sustaingym.envs:BuildingEnv',
nondeterministic=False,
max_episode_steps=288)

def step(self, action: np.ndarray
) -> tuple[np.ndarray, float, bool, bool, dict[str, Any]]:
"""Steps the environment.
Expand Down
6 changes: 0 additions & 6 deletions sustaingym/envs/cogen/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ def __init__(self,
self.timesteps_per_day = len(self.ambients_dfs[0])
assert (0 <= self.forecast_horizon < self.timesteps_per_day - 1), 'forecast_horizon must be in [0, timesteps_per_day - 1)'

self.spec = gym.envs.registration.EnvSpec(
id='sustaingym/CogenEnv-v0',
entry_point='sustaingym.envs:CogenEnv',
nondeterministic=False,
max_episode_steps=self.timesteps_per_day)

# actual ONNX model is loaded in reset()
self._model: rt.InferenceSession | None = None

Expand Down
10 changes: 1 addition & 9 deletions sustaingym/envs/evcharging/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import acnportal.acnsim as acns
import cvxpy as cp
from gymnasium import Env, spaces
from gymnasium.envs.registration import EnvSpec
import numpy as np

from .event_generation import AbstractTraceGenerator
Expand Down Expand Up @@ -75,7 +74,7 @@ class EVChargingEnv(Env):
action_space: spaces.Box, structure of actions expected by env
observation_space: spaces.Dict, structure of observations
reward_range: tuple[float, float], min and max rewards
spec: EnvSpec, info used to initialize env from gymnasium.make()
spec: EnvSpec, info about env if initialized from gymnasium.make()
metadata: dict[str, Any], unused
np_random: np.random.Generator, random number generator for the env
Expand Down Expand Up @@ -175,13 +174,6 @@ def __init__(self, data_generator: AbstractTraceGenerator,
# Define reward range
self.reward_range = (-np.inf, self.PROFIT_FACTOR * 32 * self.num_stations)

# Define environment spec
self.spec = EnvSpec(
id='sustaingym/EVCharging-v0',
entry_point='sustaingym.envs:EVChargingEnv',
nondeterministic=False,
max_episode_steps=288)

# Set up action projection
if self.project_action_in_env:
self._init_action_projection()
Expand Down

0 comments on commit a87fada

Please sign in to comment.