Skip to content

Commit

Permalink
Merge main into development
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamc committed Dec 11, 2023
2 parents cfa6be5 + e924075 commit 8c43a58
Show file tree
Hide file tree
Showing 340 changed files with 10,211 additions and 657 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ jobs:
# Miniconda is available in $CONDA env var
$CONDA/bin/conda create -n testenv --yes pip wheel gxx_linux-64 gcc_linux-64 python=${{ matrix.python-version }}
$CONDA/envs/testenv/bin/python3 -m pip install --upgrade pip
$CONDA/envs/testenv/bin/pip3 install -e .[dev,dm_control]
$CONDA/envs/testenv/bin/pip3 install -e ".[dev,box2d,brax,dm_control,mario]"
- name: Source install
if: matrix.kind == 'source'
run: |
python -m pip install --upgrade pip
pip install -e .[dev,dm_control]
pip install -e ".[dev,box2d,brax,dm_control,mario]"
- name: Dist install
if: matrix.kind == 'dist'
run: |
python -m pip install --upgrade pip
python setup.py sdist
last_dist=$(ls -t dist/carl-*.tar.gz | head -n 1)
pip install $last_dist[dev,dm_control]
pip install $last_dist[dev,box2d,brax,dm_control,mario]
- name: Tests
timeout-minutes: 60
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ outputs
testvenv
*.egg-info
runs
*.png
*.pdf
*.csv
*.pickle
*.ipynb_checkpoints
*optgap*
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,8 @@ RNA and Mario at this point, however, will not run on any operation system besid

To install ToadGAN for the Mario environment:
```bash
git submodule update --init --recursive

# if this does not work, clone manually
git clone https://github.com/frederikschubert/Mario-AI-Framework carl/envs/mario/Mario-AI-Framework
git clone https://github.com/Mawiszus/TOAD-GUI carl/envs/mario/TOAD-GUI

# System requirements
sudo apt install libfreetype6-dev xvfb

# Compile java source files
cd carl/envs/mario/Mario-AI-Framework/src
javac *.java
```

If you want to use RNA, please take a look at the associated [ReadME](carl/envs/rna/readme.md).
Expand Down
2 changes: 1 addition & 1 deletion carl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime

name = "CARL"
package_name = "carl"
package_name = "carl-bench"
author = __author__

author_email = "[email protected]"
Expand Down
4 changes: 4 additions & 0 deletions carl/context/context_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import List

import warnings

import gymnasium.spaces as spaces
import numpy as np
from ConfigSpace.hyperparameters import (
Expand All @@ -16,6 +18,8 @@

from carl.utils.types import Context, Contexts

warnings.filterwarnings("ignore", message="invalid value encountered in scalar divide")

ContextFeature: TypeAlias = Hyperparameter
NumericalContextFeature: TypeAlias = NumericalHyperparameter
NormalFloatContextFeature: TypeAlias = NormalFloatHyperparameter
Expand Down
6 changes: 3 additions & 3 deletions carl/envs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ def check_spec(spec_name: str) -> bool:
if found:
from carl.envs.dmc import *

found = check_spec("distance")
if found:
from carl.envs.rna import *
# found = check_spec("distance")
# if found:
# from carl.envs.rna import *
2 changes: 1 addition & 1 deletion carl/envs/brax/carl_brax_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,4 @@ def _update_context(self) -> None:
updated_geoms.append(geom_new)
sys = sys.replace(geoms=updated_geoms)

self.env.sys = sys
self.env.unwrapped.sys = sys
103 changes: 7 additions & 96 deletions carl/envs/gymnasium/box2d/carl_bipedal_walker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import numpy as np
from Box2D.b2 import edgeShape, fixtureDef, polygonShape
from gymnasium.envs.box2d import bipedal_walker
from gymnasium.envs.box2d import bipedal_walker as bpw
Expand Down Expand Up @@ -87,6 +86,9 @@ def get_context_features() -> dict[str, ContextFeature]:

def _update_context(self) -> None:
self.env: bipedal_walker.BipedalWalker
self.context = CARLBipedalWalker.get_context_space().insert_defaults(
self.context
)
bpw.FPS = self.context["FPS"]
bpw.SCALE = self.context["SCALE"]
bpw.FRICTION = self.context["FRICTION"]
Expand All @@ -112,14 +114,14 @@ def _update_context(self) -> None:
gravity_y = self.context["GRAVITY_Y"]

gravity = (gravity_x, gravity_y)
self.env.world.gravity = gravity
self.env.unwrapped.world.gravity = gravity

# Important for building terrain
self.env.fd_polygon = fixtureDef(
self.env.unwrapped.fd_polygon = fixtureDef(
shape=polygonShape(vertices=[(0, 0), (1, 0), (1, -1), (0, -1)]),
friction=bipedal_walker.FRICTION,
)
self.env.fd_edge = fixtureDef(
self.env.unwrapped.fd_edge = fixtureDef(
shape=edgeShape(vertices=[(0, 0), (1, 1)]),
friction=bipedal_walker.FRICTION,
categoryBits=0x0001,
Expand Down Expand Up @@ -152,95 +154,4 @@ def _update_context(self) -> None:
maskBits=0x001,
)

self.env.world.gravity = gravity


def demo_heuristic(env: CARLBipedalWalker | bipedal_walker.BipedalWalker) -> None:
env.reset()
steps = 0
total_reward = 0
a = np.array([0.0, 0.0, 0.0, 0.0])
STAY_ON_ONE_LEG, PUT_OTHER_DOWN, PUSH_OFF = 1, 2, 3
SPEED = 0.29 # Will fall forward on higher speed
state = STAY_ON_ONE_LEG
moving_leg = 0
supporting_leg = 1 - moving_leg
SUPPORT_KNEE_ANGLE = +0.1
supporting_knee_angle = SUPPORT_KNEE_ANGLE
while True:
s, r, terminated, truncated, info = env.step(a)
s = s["state"]
total_reward += r
if steps % 20 == 0 or terminated or truncated:
print("\naction " + str(["{:+0.2f}".format(x) for x in a]))
print("step {} total_reward {:+0.2f}".format(steps, total_reward))
print("hull " + str(["{:+0.2f}".format(x) for x in s[0:4]]))
print("leg0 " + str(["{:+0.2f}".format(x) for x in s[4:9]]))
print("leg1 " + str(["{:+0.2f}".format(x) for x in s[9:14]]))
steps += 1

contact0 = s[8] # noqa: F841
contact1 = s[13] # noqa: F841
moving_s_base = 4 + 5 * moving_leg
supporting_s_base = 4 + 5 * supporting_leg

hip_targ = np.array([None, None]) # -0.8 .. +1.1
knee_targ = np.array([None, None]) # -0.6 .. +0.9
hip_todo = np.array([0.0, 0.0])
knee_todo = np.array([0.0, 0.0])

if state == STAY_ON_ONE_LEG:
hip_targ[moving_leg] = 1.1
knee_targ[moving_leg] = -0.6
supporting_knee_angle += 0.03
if s[2] > SPEED:
supporting_knee_angle += 0.03
supporting_knee_angle = min(supporting_knee_angle, SUPPORT_KNEE_ANGLE)
knee_targ[supporting_leg] = supporting_knee_angle
if s[supporting_s_base + 0] < 0.10: # supporting leg is behind
state = PUT_OTHER_DOWN
if state == PUT_OTHER_DOWN:
hip_targ[moving_leg] = +0.1
knee_targ[moving_leg] = SUPPORT_KNEE_ANGLE
knee_targ[supporting_leg] = supporting_knee_angle
if s[moving_s_base + 4]:
state = PUSH_OFF
supporting_knee_angle = min(s[moving_s_base + 2], SUPPORT_KNEE_ANGLE)
if state == PUSH_OFF:
knee_targ[moving_leg] = supporting_knee_angle
knee_targ[supporting_leg] = +1.0
if s[supporting_s_base + 2] > 0.88 or s[2] > 1.2 * SPEED:
state = STAY_ON_ONE_LEG
moving_leg = 1 - moving_leg
supporting_leg = 1 - moving_leg

if hip_targ[0]:
hip_todo[0] = 0.9 * (hip_targ[0] - s[4]) - 0.25 * s[5]
if hip_targ[1]:
hip_todo[1] = 0.9 * (hip_targ[1] - s[9]) - 0.25 * s[10]
if knee_targ[0]:
knee_todo[0] = 4.0 * (knee_targ[0] - s[6]) - 0.25 * s[7]
if knee_targ[1]:
knee_todo[1] = 4.0 * (knee_targ[1] - s[11]) - 0.25 * s[12]

hip_todo[0] -= 0.9 * (0 - s[0]) - 1.5 * s[1] # PID to keep head strait
hip_todo[1] -= 0.9 * (0 - s[0]) - 1.5 * s[1]
knee_todo[0] -= 15.0 * s[3] # vertical speed, to damp oscillations
knee_todo[1] -= 15.0 * s[3]

a[0] = hip_todo[0]
a[1] = knee_todo[0]
a[2] = hip_todo[1]
a[3] = knee_todo[1]
a = np.clip(0.5 * a, -1.0, 1.0)

env.render()
if terminated or truncated:
break


if __name__ == "__main__":
# Heurisic: suboptimal, have no notion of balance.
env = CARLBipedalWalker()
demo_heuristic(env)
env.close()
self.env.unwrapped.world.gravity = gravity
13 changes: 9 additions & 4 deletions carl/envs/gymnasium/box2d/carl_lunarlander.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from Box2D.b2 import vec2
from gymnasium.envs.box2d import lunar_lander
from gymnasium.envs.box2d.lunar_lander import LunarLander

Expand Down Expand Up @@ -77,8 +78,12 @@ def _update_context(self) -> None:
if hasattr(lunar_lander, key):
setattr(lunar_lander, key, value)

gravity_x = self.context["GRAVITY_X"]
gravity_y = self.context["GRAVITY_Y"]
gravity_x = self.context.get(
"GRAVITY_X", self.get_context_features()["GRAVITY_X"].default_value
)
gravity_y = self.context.get(
"GRAVITY_Y", self.get_context_features()["GRAVITY_Y"].default_value
)

gravity = (gravity_x, gravity_y)
self.env.world.gravity = gravity
gravity = vec2(float(gravity_x), float(gravity_y))
self.env.unwrapped.world.gravity = gravity
Loading

0 comments on commit 8c43a58

Please sign in to comment.