Skip to content

Commit

Permalink
Update to gymnasium (#82)
Browse files Browse the repository at this point in the history
* update install_requires

* §

* fix imports: gym → gymnasium

* fix step function

* fix step

* gym->gymnasium

* §

* fix

* fix

* fix

* fix

* fix

* fix render

* .

* fix all step returns

* Delete build/lib/carl directory

* Delete myenv directory

* Update README.md

Fix documentation link.

* make pre-commit and format

* rename every occurrence of trunched to truncated

* @Arman717

* change trunched to truncated

* Fix gymnasium version with box2d

---------

Co-authored-by: C. Benjamins <75323339+benjamc@users.noreply.github.com>
  • Loading branch information
2 people authored and TheEimer committed Jul 20, 2023
1 parent 3d79a0e commit d8bb5fe
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 15 deletions.
2 changes: 1 addition & 1 deletion carl/envs/rna/carl_rna_definitions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from gym import spaces
from gymnasium import spaces

DEFAULT_CONTEXT = {
"mutation_threshold": 5,
Expand Down
2 changes: 1 addition & 1 deletion carl/envs/rna/rna_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from RNA import fold
import gym
import gymnasium as gym

from typing import Any, List

Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def read_file(filepath: str) -> str:
include_package_data=True,
python_requires=">=3.9",
install_requires=[
"gym",
"gymnasium>=0.27.1",
"pygame==2.1.0",
"scipy>=1.7.0",
"ConfigArgParse>=1.5.1",
"numpy>=1.19.5",
Expand Down
199 changes: 188 additions & 11 deletions test/test_CARLEnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,148 @@


class TestStateConstruction(unittest.TestCase):
def test_observation(self):
env = CARLPendulum()
context = CARLPendulum.get_default_context()
obs, info = env.reset()
def test_hiddenstate(self):
"""
Test if we set hide_context = True that we get the original, normal state.
"""
env = CARLPendulumEnv(
contexts={},
hide_context=True,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=None,
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
self.assertEqual(3, len(state))

def test_visiblestate(self):
"""
Test if we set hide_context = False and state_context_features=None that we get the normal state extended by
all context features.
"""
env = CARLPendulumEnv(
contexts={},
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=None,
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
self.assertEqual(10, len(state))

def test_visiblestate_customnone(self):
"""
Test if we set hide_context = False and state_context_features="changing_context_features" that we get the
normal state, not extended by context features.
"""
env = CARLPendulumEnv(
contexts={},
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=["changing_context_features"],
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
# Because we don't change any context features the state length should be 3
self.assertEqual(3, len(state))

def test_visiblestate_custom(self):
"""
Test if we set hide_context = False and state_context_features=["g", "m"] that we get the
normal state, extended by the context feature values of g and m.
"""
env = CARLPendulumEnv(
contexts={},
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=["g", "m"],
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
# state should be of length 5 because we add two context features
self.assertEqual(5, len(state))

def test_visiblestate_changingcontextfeatures_nochange(self):
"""
Test if we set hide_context = False and state_context_features="changing_context_features" that we get the
normal state, extended by the context features which are changing in the set of contexts. Here: None are
changing.
"""
contexts = {
"0": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 1.0},
"1": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 1.0},
"2": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 1.0},
"3": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 1.0},
}
env = CARLPendulumEnv(
contexts=contexts,
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=["changing_context_features"],
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
# state should be of length 3 because all contexts are the same
self.assertEqual(3, len(state))

def test_visiblestate_changingcontextfeatures_change(self):
"""
Test if we set hide_context = False and state_context_features="changing_context_features" that we get the
normal state, extended by the context features which are changing in the set of contexts.
Here: Two are changing.
"""
contexts = {
"0": {"max_speed": 8.0, "dt": 0.03, "g": 10.0, "m": 1.0, "l": 1.0},
"1": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 0.95},
"2": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 0.3},
"3": {"max_speed": 8.0, "dt": 0.05, "g": 10.0, "m": 1.0, "l": 1.3},
}
env = CARLPendulumEnv(
contexts=contexts,
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=["changing_context_features"],
)
env.reset()
action = [0.01] # torque
state, reward, terminated, truncated, info = env.step(action=action)
env.close()
# state should be of length 5 because two features are changing (dt and l)
self.assertEqual(5, len(state))

def test_dict_observation_space(self):
contexts = {"0": {"max_speed": 8.0, "dt": 0.03, "g": 10.0, "m": 1.0, "l": 1.0}}
env = CARLPendulumEnv(
contexts=contexts,
hide_context=False,
dict_observation_space=True,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=["changing_context_features"],
)
obs = env.reset()
self.assertEqual(type(obs), dict)
self.assertTrue("state" in obs)
self.assertTrue("context" in obs)
self.assertEqual(len(obs["context"]), len(context))
action = [0.01] # torque
next_obs, reward, terminated, truncated, info = env.step(action=action)
env.close()

def test_observation_emptycontext(self):
env = CARLPendulum(obs_context_features=[])
Expand All @@ -29,10 +163,34 @@ def test_observation_reducedcontext(self):
state, info = env.reset()
self.assertEqual(len(state["context"]), n)

def test_get_context_key(self):
contexts = self.generate_contexts()
env = CARLPendulum(contexts=contexts)
self.assertEqual(env.context_id, None)
class TestEpisodeTermination(unittest.TestCase):
def test_episode_termination(self):
"""
Test if we set hide_context = True that we get the original, normal state.
"""
ep_length = 100
env = CARLPendulumEnv(
contexts={},
hide_context=True,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=None,
max_episode_length=ep_length,
)
env.reset()
action = [0.0] # torque
done = False
counter = 0
while not done:
state, reward, terminated, truncated, info = env.step(action=action)
counter += 1
self.assertTrue(counter <= ep_length)
if terminated or truncated:
done = True

if counter > ep_length:
break
env.close()


class TestContextSampler(unittest.TestCase):
Expand All @@ -56,7 +214,7 @@ def test_context_feature_scaling_by_mean(self):
)
env.reset()
action = [0.0]
state, reward, done, info = env.step(action=action)
state, reward, terminated, truncated, info = env.step(action=action)
n_c = len(env.default_context)
scaled_contexts = state[-n_c:]
target = np.array(
Expand All @@ -77,7 +235,26 @@ def test_context_feature_scaling_by_default(self):
"initial_angle_max": np.pi,
"initial_velocity_max": 1,
}
self.assertDictEqual(defaults, DEFAULT_CONTEXT)
contexts = {
"0": {"max_speed": 8.0, "dt": 0.03, "g": 10.0, "m": 1.0, "l": 1.8},
}
env = CARLPendulumEnv(
contexts=contexts,
hide_context=False,
add_gaussian_noise_to_context=False,
gaussian_noise_std_percentage=0.01,
state_context_features=None,
scale_context_features="by_default",
default_context=default_context,
)
env.reset()
action = [0.0]
state, reward, terminated, truncated, info = env.step(action=action)
n_c = len(default_context)
scaled_contexts = state[-n_c:]
self.assertTrue(
np.all(np.array([1.0, 0.6, 1, 1, 1.8, 1, 1]) == scaled_contexts)
)

def test_sample_contexts(self):
from carl.context.sampling import sample_contexts
Expand Down

0 comments on commit d8bb5fe

Please sign in to comment.