-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
"""Various wrappers for POPGym environments""" | ||
from popgym.wrappers.antialias import Antialias | ||
from popgym.wrappers.discrete_action import DiscreteAction | ||
from popgym.wrappers.flatten import Flatten | ||
from popgym.wrappers.markovian import Markovian | ||
from popgym.wrappers.previous_action import PreviousAction | ||
|
||
__all__ = ["Antialias", "Markovian", "PreviousAction", "Flatten"] | ||
__all__ = ["Antialias", "Markovian", "PreviousAction", "Flatten", "DiscreteAction"] |
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,50 @@ | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
from gymnasium import spaces | ||
from gymnasium.core import ActType, ObsType | ||
|
||
from popgym.core.env import POPGymEnv | ||
from popgym.core.wrapper import POPGymWrapper | ||
|
||
PREV_ACTION = "prev_action" | ||
|
||
|
||
class DiscreteAction(POPGymWrapper): | ||
"""Wrapper that converts a MultiDiscrete into a single Discrete action. | ||
Args: | ||
env: The environment | ||
Returns: | ||
A gym environment | ||
""" | ||
|
||
def __init__(self, env: POPGymEnv): | ||
super().__init__(env) | ||
self.action_space: spaces.Space | ||
if isinstance(self.action_space, spaces.Discrete): | ||
# Done, do nothing | ||
self.ravel_actions = False | ||
elif isinstance(self.action_space, spaces.MultiDiscrete): | ||
self.ravel_actions = True | ||
self.old_action_space = self.action_space | ||
self.action_space = spaces.Discrete(np.prod(self.action_space.nvec)) | ||
elif isinstance(self.action_space, (spaces.Tuple, spaces.Dict)): | ||
raise NotImplementedError( | ||
"Action space must be Discrete or MultiDiscrete, got a nested space" | ||
f" {self.action_space}.Please use the Flatten wrapper first." | ||
) | ||
else: | ||
raise NotImplementedError( | ||
"Action space must be Discrete or MultiDiscrete, got" | ||
f" {self.action_space}." | ||
) | ||
|
||
def step(self, action: ActType) -> Tuple[ObsType, float, bool, bool, dict]: | ||
if not self.ravel_actions: | ||
return self.env.step(action) | ||
|
||
discrete_action = np.unravel_index(action, self.old_action_space.nvec) | ||
obs, reward, terminated, truncated, info = self.env.step(discrete_action) | ||
return obs, reward, terminated, truncated, info |
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