Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEimer committed Aug 10, 2023
1 parent 65e0046 commit c1f4ebc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion carl/envs/gymnasium/carl_gymnasium_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ def __init__(

def _update_context(self) -> None:
for k, v in self.context.items():
setattr(self.env.unwrapped, k, v)
setattr(self.env.unwrapped, k, v)
31 changes: 27 additions & 4 deletions carl/envs/gymnasium/classic_control/carl_acrobot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

from typing import Optional

import numpy as np

from carl.context.context_space import ContextFeature, UniformFloatContextFeature
from carl.envs.gymnasium.carl_gymnasium_env import CARLGymnasiumEnv

Expand Down Expand Up @@ -64,15 +66,36 @@ def get_context_features() -> dict[str, ContextFeature]:
"INITIAL_VELOCITY_UPPER", lower=-np.inf, upper=np.inf, default_value=0.1
),
}

def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict] = None,
):
super().reset(seed=seed, options=options)
angles = self.env.np_random.uniform(low=self.context["INITIAL_ANGLE_LOWER"], high=self.context["INITIAL_ANGLE_UPPER"], size=(2,))
velocities = self.env.np_random.uniform(low=self.context["INITIAL_VELOCITY_LOWER"], high=self.context["INITIAL_VELOCITY_UPPER"], size=(2,))
angles = self.env.np_random.uniform(
low=self.context["INITIAL_ANGLE_LOWER"],
high=self.context["INITIAL_ANGLE_UPPER"],
size=(2,),
)
velocities = self.env.np_random.uniform(
low=self.context["INITIAL_VELOCITY_LOWER"],
high=self.context["INITIAL_VELOCITY_UPPER"],
size=(2,),
)
self.env.unwrapped.state = np.concatenate([angles, velocities])
return np.array([np.cos(self.env.unwrapped.state[0]), np.sin(self.env.unwrapped.state[0]), np.cos(self.env.unwrapped.state[1]), np.sin(self.env.unwrapped.state[1]), self.env.unwrapped.state[2], self.env.unwrapped.state[3]], dtype=np.float32), {}
return (
np.array(
[
np.cos(self.env.unwrapped.state[0]),
np.sin(self.env.unwrapped.state[0]),
np.cos(self.env.unwrapped.state[1]),
np.sin(self.env.unwrapped.state[1]),
self.env.unwrapped.state[2],
self.env.unwrapped.state[3],
],
dtype=np.float32,
),
{},
)
13 changes: 9 additions & 4 deletions carl/envs/gymnasium/classic_control/carl_cartpole.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from typing import Optional

import numpy as np

from carl.context.context_space import ContextFeature, UniformFloatContextFeature
from carl.envs.gymnasium.carl_gymnasium_env import CARLGymnasiumEnv
from gymnasium.envs.classic_control import utils


class CARLCartPole(CARLGymnasiumEnv):
Expand Down Expand Up @@ -38,13 +39,17 @@ def get_context_features() -> dict[str, ContextFeature]:
"initial_state_upper", lower=-np.inf, upper=np.inf, default_value=0.1
),
}

def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict] = None,
):
super().reset(seed=seed, options=options)
self.env.unwrapped.state = self.env.np_random.uniform(low=self.context["initial_state_lower"], high=self.context["initial_state_upper"], size=(4,))
return np.array(self.env.unwrapped.state, dtype=np.float32), {}
self.env.unwrapped.state = self.env.np_random.uniform(
low=self.context["initial_state_lower"],
high=self.context["initial_state_upper"],
size=(4,),
)
return np.array(self.env.unwrapped.state, dtype=np.float32), {}
16 changes: 12 additions & 4 deletions carl/envs/gymnasium/classic_control/carl_mountaincar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import numpy as np
from typing import Optional

import numpy as np

from carl.context.context_space import ContextFeature, UniformFloatContextFeature
from carl.envs.gymnasium.carl_gymnasium_env import CARLGymnasiumEnv

Expand Down Expand Up @@ -46,15 +48,21 @@ def get_context_features() -> dict[str, ContextFeature]:
"max_velocity_start", lower=-np.inf, upper=np.inf, default_value=0
),
}

def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict] = None,
):
super().reset(seed=seed, options=options)
position = self.env.np_random.uniform(low=self.context["min_position_start"], high=self.context["max_position_start"])
velocity = self.env.np_random.uniform(low=self.context["min_velocity_start"], high=self.context["max_velocity_start"])
position = self.env.np_random.uniform(
low=self.context["min_position_start"],
high=self.context["max_position_start"],
)
velocity = self.env.np_random.uniform(
low=self.context["min_velocity_start"],
high=self.context["max_velocity_start"],
)
self.env.unwrapped.state = np.array([position, velocity])
return np.array(self.env.unwrapped.state, dtype=np.float32), {}
16 changes: 12 additions & 4 deletions carl/envs/gymnasium/classic_control/carl_mountaincarcontinuous.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import numpy as np
from typing import Optional

import numpy as np

from carl.context.context_space import ContextFeature, UniformFloatContextFeature
from carl.envs.gymnasium.carl_gymnasium_env import CARLGymnasiumEnv

Expand Down Expand Up @@ -43,15 +45,21 @@ def get_context_features() -> dict[str, ContextFeature]:
"max_velocity_start", lower=-np.inf, upper=np.inf, default_value=0
),
}

def reset(
self,
*,
seed: Optional[int] = None,
options: Optional[dict] = None,
):
super().reset(seed=seed, options=options)
position = self.env.np_random.uniform(low=self.context["min_position_start"], high=self.context["max_position_start"])
velocity = self.env.np_random.uniform(low=self.context["min_velocity_start"], high=self.context["max_velocity_start"])
position = self.env.np_random.uniform(
low=self.context["min_position_start"],
high=self.context["max_position_start"],
)
velocity = self.env.np_random.uniform(
low=self.context["min_velocity_start"],
high=self.context["max_velocity_start"],
)
self.env.unwrapped.state = np.array([position, velocity])
return np.array(self.env.unwrapped.state, dtype=np.float32), {}
2 changes: 2 additions & 0 deletions carl/envs/gymnasium/classic_control/carl_pendulum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

from typing import Optional

import numpy as np

from carl.context.context_space import ContextFeature, UniformFloatContextFeature
from carl.envs.gymnasium.carl_gymnasium_env import CARLGymnasiumEnv

Expand Down

0 comments on commit c1f4ebc

Please sign in to comment.