-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from foreverska/feature/multibuffalo
multibuffalo
- Loading branch information
Showing
8 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from buffalo_gym.envs.buffalo_gym import BuffaloEnv | ||
from buffalo_gym.envs.buffalo_gym import BuffaloEnv | ||
from buffalo_gym.envs.multibuffalo_gym import MultiBuffaloEnv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from typing import Any, TypeVar, SupportsFloat | ||
import random | ||
|
||
import gymnasium as gym | ||
import numpy as np | ||
|
||
ObsType = TypeVar("ObsType") | ||
ActType = TypeVar("ActType") | ||
|
||
|
||
class MultiBuffaloEnv(gym.Env): | ||
""" | ||
Multi-armed bandit environment with multiple states that have (probably) distinct rewards | ||
""" | ||
metadata = {'render_modes': []} | ||
|
||
def __init__(self, arms: int = 10, states: int = 2, pace: int | None = None): | ||
""" | ||
Multi-armed bandit environment with k arms and n states | ||
:param arms: number of arms | ||
:param states: number of states | ||
:param pace: number of steps between state changes, None for every step | ||
""" | ||
self.action_space = gym.spaces.Discrete(arms) | ||
self.observation_space = gym.spaces.Box(low=0, high=states, shape=(1,), dtype=np.float32) | ||
|
||
self.offsets = np.random.normal(0, arms, (arms, states)) | ||
self.pace = pace | ||
self.states = states | ||
self.state = 0 | ||
self.ssr = 0 | ||
|
||
def reset(self, | ||
*, | ||
seed: int | None = None, | ||
options: dict[str, Any] | None = None) -> tuple[ObsType, dict[str, Any]]: | ||
""" | ||
Resets the environment | ||
:param seed: WARN unused, defaults to None | ||
:param options: WARN unused, defaults to None | ||
:return: observation, info | ||
""" | ||
|
||
self.state = 0 | ||
self.ssr = 0 | ||
|
||
return np.zeros((1,), dtype=np.float32), {} | ||
|
||
def step(self, action: int) -> tuple[ObsType, SupportsFloat, bool, bool, dict[str, Any]]: | ||
""" | ||
Steps the environment | ||
:param action: arm to pull | ||
:return: observation, reward, done, term, info | ||
""" | ||
reward = np.random.normal(0, 1, 1)[0] + self.offsets[action, self.state] | ||
self.ssr += 1 | ||
if self.pace is None or self.ssr % self.pace == 0: | ||
self.state = random.randint(0, self.states - 1) | ||
|
||
return np.ones((1,), dtype=np.float32)*self.state, reward, False, False, {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
|
||
import numpy as np | ||
import gymnasium as gym | ||
import buffalo_gym.envs.buffalo_gym | ||
|
||
def test_buffalo(): | ||
env = gym.make('Buffalo-v0') | ||
|
||
env.reset() | ||
env.step(env.action_space.sample()) | ||
obs, info = env.reset() | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] == 0 | ||
|
||
obs, reward, done, term, info = env.step(env.action_space.sample()) | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] == 0 | ||
assert done is False | ||
assert term is False | ||
|
||
assert 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import numpy as np | ||
import gymnasium as gym | ||
import buffalo_gym | ||
|
||
def test_multibuffalo(): | ||
env = gym.make('MultiBuffalo-v0') | ||
|
||
obs, info = env.reset() | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] in (0, 1) | ||
|
||
states = [] | ||
for _ in range(10): | ||
obs, reward, done, term, info = env.step(env.action_space.sample()) | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] in (0, 1) | ||
states.append(obs[0]) | ||
assert done is False | ||
assert term is False | ||
|
||
assert set(states) == {0, 1} | ||
|
||
def test_multibuffalo_threestates(): | ||
env = gym.make('MultiBuffalo-v0', states=3) | ||
|
||
obs, info = env.reset() | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] in (0, 1, 2) | ||
|
||
states = [] | ||
for _ in range(10): | ||
obs, reward, done, term, info = env.step(env.action_space.sample()) | ||
|
||
assert obs.shape == (1,) | ||
assert obs.dtype == np.float32 | ||
assert obs[0] in (0, 1, 2) | ||
states.append(obs[0]) | ||
assert done is False | ||
assert term is False | ||
|
||
assert set(states) == {0, 1, 2} |