-
Notifications
You must be signed in to change notification settings - Fork 25
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 #101 from EdanToledo/feat/add_navix
Feat/add navix
- Loading branch information
Showing
9 changed files
with
184 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 |
---|---|---|
|
@@ -14,6 +14,7 @@ jaxlib | |
jaxmarl | ||
jumanji==1.0.0 | ||
mctx | ||
navix | ||
neptune | ||
numpy | ||
omegaconf | ||
|
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,15 @@ | ||
# ---Environment Configs--- | ||
env_name: navix | ||
scenario: | ||
name: Navix-DoorKey-8x8-v0 | ||
task_name: navix-door_key-8x8-v0 | ||
|
||
kwargs: {} | ||
|
||
# Defines the metric that will be used to evaluate the performance of the agent. | ||
# This metric is returned at the end of an experiment and can be used for hyperparameter tuning. | ||
eval_metric: episode_return | ||
|
||
# Optional wrapper to flatten the observation space. | ||
wrapper: | ||
_target_: stoix.wrappers.transforms.FlattenObservationWrapper |
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,15 @@ | ||
# ---Environment Configs--- | ||
env_name: navix | ||
scenario: | ||
name: Navix-Empty-5x5-v0 | ||
task_name: navix-empty-5x5-v0 | ||
|
||
kwargs: {} | ||
|
||
# Defines the metric that will be used to evaluate the performance of the agent. | ||
# This metric is returned at the end of an experiment and can be used for hyperparameter tuning. | ||
eval_metric: episode_return | ||
|
||
# Optional wrapper to flatten the observation space. | ||
wrapper: | ||
_target_: stoix.wrappers.transforms.FlattenObservationWrapper |
2 changes: 1 addition & 1 deletion
2
...gs/env/minigrid/minigrid_doorkey_5x5.yaml → ...nfigs/env/xland_minigrid/doorkey_5x5.yaml
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,16 @@ | ||
# ---Environment Configs--- | ||
env_name: xland_minigrid | ||
scenario: | ||
name: MiniGrid-Empty-5x5 | ||
task_name: minigrid_empty_5x5 | ||
|
||
kwargs: {} | ||
|
||
# Defines the metric that will be used to evaluate the performance of the agent. | ||
# This metric is returned at the end of an experiment and can be used for hyperparameter tuning. | ||
eval_metric: episode_return | ||
|
||
|
||
# Optional wrapper to flatten the observation space. | ||
wrapper: | ||
_target_: stoix.wrappers.transforms.FlattenObservationWrapper |
2 changes: 1 addition & 1 deletion
2
...figs/env/minigrid/minigrid_empty_6x6.yaml → ...configs/env/xland_minigrid/empty_6x6.yaml
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from typing import TYPE_CHECKING, Tuple | ||
|
||
import chex | ||
import jax | ||
import jax.numpy as jnp | ||
from jumanji import specs | ||
from jumanji.specs import Array, DiscreteArray, Spec | ||
from jumanji.types import StepType, TimeStep, restart | ||
from jumanji.wrappers import Wrapper | ||
from navix.environments import Environment | ||
from navix.environments import Timestep as NavixState | ||
|
||
from stoix.base_types import Observation | ||
|
||
if TYPE_CHECKING: # https://github.com/python/mypy/issues/6239 | ||
from dataclasses import dataclass | ||
else: | ||
from chex import dataclass | ||
|
||
|
||
@dataclass | ||
class NavixEnvState: | ||
key: chex.PRNGKey | ||
navix_state: NavixState | ||
|
||
|
||
class NavixWrapper(Wrapper): | ||
def __init__(self, env: Environment): | ||
self._env = env | ||
self._n_actions = len(self._env.action_set) | ||
|
||
def reset(self, key: chex.PRNGKey) -> Tuple[NavixEnvState, TimeStep]: | ||
key, key_reset = jax.random.split(key) | ||
navix_state = self._env.reset(key_reset) | ||
agent_view = navix_state.observation.astype(float) | ||
legal_action_mask = jnp.ones((self._n_actions,), dtype=float) | ||
step_count = navix_state.t.astype(int) | ||
obs = Observation(agent_view, legal_action_mask, step_count) | ||
timestep = restart(obs, extras={}) | ||
state = NavixEnvState(key=key, navix_state=navix_state) | ||
return state, timestep | ||
|
||
def step(self, state: NavixEnvState, action: chex.Array) -> Tuple[NavixEnvState, TimeStep]: | ||
key, key_step = jax.random.split(state.key) | ||
|
||
navix_state = self._env.step(state.navix_state, action) | ||
|
||
agent_view = navix_state.observation.astype(float) | ||
legal_action_mask = jnp.ones((self._n_actions,), dtype=float) | ||
step_count = navix_state.t.astype(int) | ||
next_obs = Observation(agent_view, legal_action_mask, step_count) | ||
|
||
reward = navix_state.reward.astype(float) | ||
terminal = navix_state.is_termination() | ||
truncated = navix_state.is_truncation() | ||
|
||
discount = jnp.array(1.0 - terminal, dtype=float) | ||
final_step = jnp.logical_or(terminal, truncated) | ||
|
||
timestep = TimeStep( | ||
observation=next_obs, | ||
reward=reward, | ||
discount=discount, | ||
step_type=jax.lax.select(final_step, StepType.LAST, StepType.MID), | ||
extras={}, | ||
) | ||
next_state = NavixEnvState(key=key_step, navix_state=navix_state) | ||
return next_state, timestep | ||
|
||
def reward_spec(self) -> specs.Array: | ||
return specs.Array(shape=(), dtype=float, name="reward") | ||
|
||
def discount_spec(self) -> specs.BoundedArray: | ||
return specs.BoundedArray(shape=(), dtype=float, minimum=0.0, maximum=1.0, name="discount") | ||
|
||
def action_spec(self) -> Spec: | ||
return DiscreteArray(num_values=self._n_actions) | ||
|
||
def observation_spec(self) -> Spec: | ||
agent_view_shape = self._env.observation_space.shape | ||
agent_view_min = self._env.observation_space.minimum | ||
agent_view_max = self._env.observation_space.maximum | ||
agent_view_spec = specs.BoundedArray( | ||
shape=agent_view_shape, | ||
dtype=float, | ||
minimum=agent_view_min, | ||
maximum=agent_view_max, | ||
) | ||
action_mask_spec = Array(shape=(self._n_actions,), dtype=float) | ||
|
||
return specs.Spec( | ||
Observation, | ||
"ObservationSpec", | ||
agent_view=agent_view_spec, | ||
action_mask=action_mask_spec, | ||
step_count=Array(shape=(), dtype=int), | ||
) |