Skip to content

Commit

Permalink
Merge branch 'main' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
Carolin Benjamins committed Apr 14, 2023
2 parents 711b17f + d2fb5d7 commit ebb9080
Show file tree
Hide file tree
Showing 48 changed files with 1,769 additions and 524 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# If things break on update, raise an issue
repos:

- repo: https://github.com/pycqa/isort
rev: 5.10.1
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
name: isort imports carl
Expand Down Expand Up @@ -50,8 +50,8 @@ repos:
name: mypy carl
files: carl/.*

- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
name: flake8 carl
Expand Down
23 changes: 0 additions & 23 deletions carl/context/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,3 @@ def add_gaussian_noise(
value = default_value + random_generator.normal(loc=mean, scale=std)

return value


if __name__ == "__main__":
import matplotlib.pyplot as plt

seed = 123456
rng = np.random.default_rng(seed=seed)
default_value = list(np.arange(0, 4))
percentage_std = 0.01
n_samples = 1000
values = np.array(
[
add_gaussian_noise(
default_value=default_value,
percentage_std=percentage_std,
random_generator=rng,
)
for i in range(n_samples)
]
)
plt.close("all")
plt.hist(values, bins=100)
plt.show()
21 changes: 21 additions & 0 deletions carl/context/selection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import abstractmethod
from typing import Any, Callable, List, Optional, Tuple

Expand Down Expand Up @@ -73,6 +75,25 @@ def select(self) -> Context:
self.n_calls += 1
return context

@property
def context_key(self) -> Any | None:
"""
Return context key
If no context has been selected yet (context_id=None),
return None.
Returns
-------
Any | None
The key of the current context or None
"""
if self.context_id:
key = self.contexts_keys[self.context_id]
else:
key = None
return key


class RandomSelector(AbstractSelector):
"""
Expand Down
2 changes: 1 addition & 1 deletion carl/envs/box2d/carl_vehicle_racing.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def reset(
else:
return self.step(None)[0], {} # type: ignore [arg-type]

def render_indicators(self, W: int, H: int) -> None:
def _render_indicators(self, W: int, H: int) -> None:
# copied from meta car racing
s = W / 40.0
h = H / 40.0
Expand Down
4 changes: 2 additions & 2 deletions carl/envs/box2d/parking_garage/street_car.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from __future__ import annotations

import Box2D
import numpy as np
Expand Down Expand Up @@ -363,7 +363,7 @@ def __init__(
self.drawlist = self.wheels + [self.hull, self.trailer, self.trailer_axel]
else:
self.drawlist = self.wheels + [self.hull]
self.particles = [] # type: List
self.particles: list = []

def gas(self, gas: float) -> None:
"""control: rear wheel drive
Expand Down
2 changes: 1 addition & 1 deletion carl/envs/brax/carl_halfcheetah.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import brax
import numpy as np
from brax.envs.halfcheetah import _SYSTEM_CONFIG, Halfcheetah
from brax.envs.half_cheetah import _SYSTEM_CONFIG, Halfcheetah
from brax.envs.wrappers import GymWrapper, VectorGymWrapper, VectorWrapper
from google.protobuf import json_format, text_format
from google.protobuf.json_format import MessageToDict
Expand Down
19 changes: 14 additions & 5 deletions carl/envs/carl_env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, Dict, List, Mapping, Optional, Tuple, Type, Union

import importlib
Expand Down Expand Up @@ -138,9 +140,9 @@ def __init__(
)
context_keys: Vector
if state_context_features is not None:
if (
state_context_features == "changing_context_features"
or state_context_features[0] == "changing_context_features"
if state_context_features == "changing_context_features" or (
type(state_context_features) == list
and state_context_features[0] == "changing_context_features"
):
# if we have only one context the context features do not change during training
if len(self.contexts) > 1:
Expand Down Expand Up @@ -193,9 +195,12 @@ def __init__(
# where it is allowed to add gaussian noise

# Set initial context
self.context_index = 0 # type: int
# TODO only set context during reset?
# Don't use the context selector. This way after the first reset we actually
# start with the first context. We just need a default/initial context here
# so all the tests and the rest does not break.
context_keys = list(self.contexts.keys())
self.context = self.contexts[context_keys[self.context_index]]
self.context = self.contexts[context_keys[0]]

# Scale context features
if scale_context_features not in self.available_scale_methods:
Expand Down Expand Up @@ -236,6 +241,10 @@ def context(self) -> Dict:
def context(self, context: Context) -> None:
self._context = self.fill_context_with_default(context=context)

@property
def context_key(self) -> Any | None:
return self.context_selector.context_key

@property
def contexts(self) -> Dict[Any, Dict[Any, Any]]:
return self._contexts
Expand Down
4 changes: 4 additions & 0 deletions carl/envs/dmc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# flake8: noqa: F401
# Contexts and bounds by name
from carl.envs.dmc.carl_dm_finger import CONTEXT_BOUNDS as CARLDmcFingerEnv_bounds
from carl.envs.dmc.carl_dm_finger import CONTEXT_MASK as CARLDmcFingerEnv_mask
from carl.envs.dmc.carl_dm_finger import DEFAULT_CONTEXT as CARLDmcFingerEnv_defaults
from carl.envs.dmc.carl_dm_finger import CARLDmcFingerEnv
from carl.envs.dmc.carl_dm_fish import CONTEXT_BOUNDS as CARLDmcFishEnv_bounds
from carl.envs.dmc.carl_dm_fish import CONTEXT_MASK as CARLDmcFishEnv_mask
from carl.envs.dmc.carl_dm_fish import DEFAULT_CONTEXT as CARLDmcFishEnv_defaults
Expand Down
103 changes: 103 additions & 0 deletions carl/envs/dmc/carl_dm_finger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from typing import Dict, List, Optional, Union

import numpy as np

from carl.context.selection import AbstractSelector
from carl.envs.dmc.carl_dmcontrol import CARLDmcEnv
from carl.envs.dmc.dmc_tasks.fish import STEP_LIMIT # type: ignore
from carl.utils.trial_logger import TrialLogger
from carl.utils.types import Context, Contexts

DEFAULT_CONTEXT = {
"gravity": -9.81, # Gravity is disabled via flag
"friction_tangential": 1, # Scaling factor for tangential friction of all geoms (objects)
"friction_torsional": 1, # Scaling factor for torsional friction of all geoms (objects)
"friction_rolling": 1, # Scaling factor for rolling friction of all geoms (objects)
"timestep": 0.004, # Seconds between updates
"joint_damping": 1.0, # Scaling factor for all joints
"joint_stiffness": 0.0,
"actuator_strength": 1, # Scaling factor for all actuators in the model
"density": 5000.0,
"viscosity": 0.0,
"geom_density": 1.0, # No effect, because no gravity
"wind_x": 0.0,
"wind_y": 0.0,
"wind_z": 0.0,
"limb_length_0": 0.17,
"limb_length_1": 0.16,
"spinner_radius": 0.04,
"spinner_length": 0.18,
}

CONTEXT_BOUNDS = {
"gravity": (-np.inf, -0.1, float),
"friction_tangential": (0, np.inf, float),
"friction_torsional": (0, np.inf, float),
"friction_rolling": (0, np.inf, float),
"timestep": (
0.001,
0.1,
float,
),
"joint_damping": (0, np.inf, float),
"joint_stiffness": (0, np.inf, float),
"actuator_strength": (0, np.inf, float),
"density": (0, np.inf, float),
"viscosity": (0, np.inf, float),
"geom_density": (0, np.inf, float),
"wind_x": (-np.inf, np.inf, float),
"wind_y": (-np.inf, np.inf, float),
"wind_z": (-np.inf, np.inf, float),
"limb_length_0": (0.01, 0.2, float),
"limb_length_1": (0.01, 0.2, float),
"spinner_radius": (0.01, 0.05, float),
"spinner_length": (0.01, 0.4, float),
}

CONTEXT_MASK = [
"gravity",
"geom_density",
"wind_x",
"wind_y",
"wind_z",
]


class CARLDmcFingerEnv(CARLDmcEnv):
def __init__(
self,
domain: str = "finger",
task: str = "spin_context",
contexts: Contexts = {},
context_mask: Optional[List[str]] = [],
hide_context: bool = True,
add_gaussian_noise_to_context: bool = False,
gaussian_noise_std_percentage: float = 0.01,
logger: Optional[TrialLogger] = None,
scale_context_features: str = "no",
default_context: Optional[Context] = DEFAULT_CONTEXT,
max_episode_length: int = STEP_LIMIT,
state_context_features: Optional[List[str]] = None,
dict_observation_space: bool = False,
context_selector: Optional[
Union[AbstractSelector, type[AbstractSelector]]
] = None,
context_selector_kwargs: Optional[Dict] = None,
):
super().__init__(
domain=domain,
task=task,
contexts=contexts,
context_mask=context_mask,
hide_context=hide_context,
add_gaussian_noise_to_context=add_gaussian_noise_to_context,
gaussian_noise_std_percentage=gaussian_noise_std_percentage,
logger=logger,
scale_context_features=scale_context_features,
default_context=default_context,
max_episode_length=max_episode_length,
state_context_features=state_context_features,
dict_observation_space=dict_observation_space,
context_selector=context_selector,
context_selector_kwargs=context_selector_kwargs,
)
2 changes: 1 addition & 1 deletion carl/envs/dmc/carl_dm_fish.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

CONTEXT_BOUNDS = {
"gravity": (-0.1, -np.inf, float),
"gravity": (-np.inf, -0.1, float),
"friction_tangential": (0, np.inf, float),
"friction_torsional": (0, np.inf, float),
"friction_rolling": (0, np.inf, float),
Expand Down
2 changes: 1 addition & 1 deletion carl/envs/dmc/carl_dm_quadruped.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

CONTEXT_BOUNDS = {
"gravity": (-0.1, -np.inf, float),
"gravity": (-np.inf, -0.1, float),
"friction_tangential": (0, np.inf, float),
"friction_torsional": (0, np.inf, float),
"friction_rolling": (0, np.inf, float),
Expand Down
2 changes: 1 addition & 1 deletion carl/envs/dmc/carl_dm_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

CONTEXT_BOUNDS = {
"gravity": (-0.1, -np.inf, float),
"gravity": (-np.inf, -0.1, float),
"friction_tangential": (0, np.inf, float),
"friction_torsional": (0, np.inf, float),
"friction_rolling": (0, np.inf, float),
Expand Down
Loading

0 comments on commit ebb9080

Please sign in to comment.