From c72734befe3dfc621721ece9c538000e98d71c57 Mon Sep 17 00:00:00 2001 From: benjamc Date: Thu, 10 Aug 2023 21:24:39 +0200 Subject: [PATCH] Update --- .../gymnasium/box2d/carl_bipedal_walker.py | 1 - carl/envs/gymnasium/box2d/carl_lunarlander.py | 1 - .../gymnasium/box2d/carl_vehicle_racing.py | 1 - examples/demo_carracing.py | 9 ++- examples/demo_heuristic_bipedalwalker.py | 2 +- examples/play_car_racing.py | 77 ------------------- 6 files changed, 6 insertions(+), 85 deletions(-) delete mode 100644 examples/play_car_racing.py diff --git a/carl/envs/gymnasium/box2d/carl_bipedal_walker.py b/carl/envs/gymnasium/box2d/carl_bipedal_walker.py index 6518ce7a..9fc34661 100644 --- a/carl/envs/gymnasium/box2d/carl_bipedal_walker.py +++ b/carl/envs/gymnasium/box2d/carl_bipedal_walker.py @@ -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 diff --git a/carl/envs/gymnasium/box2d/carl_lunarlander.py b/carl/envs/gymnasium/box2d/carl_lunarlander.py index c0bc698c..46ccb6d5 100644 --- a/carl/envs/gymnasium/box2d/carl_lunarlander.py +++ b/carl/envs/gymnasium/box2d/carl_lunarlander.py @@ -1,6 +1,5 @@ from __future__ import annotations -import Box2D from Box2D.b2 import vec2 from gymnasium.envs.box2d import lunar_lander from gymnasium.envs.box2d.lunar_lander import LunarLander diff --git a/carl/envs/gymnasium/box2d/carl_vehicle_racing.py b/carl/envs/gymnasium/box2d/carl_vehicle_racing.py index 50c0ce7e..2b8cb6a6 100644 --- a/carl/envs/gymnasium/box2d/carl_vehicle_racing.py +++ b/carl/envs/gymnasium/box2d/carl_vehicle_racing.py @@ -222,4 +222,3 @@ def _update_context(self) -> None: self.env: CustomCarRacing vehicle_class_index = self.context["VEHICLE_ID"] self.env.vehicle_class = PARKING_GARAGE[vehicle_class_index] - print(self.env.vehicle_class) diff --git a/examples/demo_carracing.py b/examples/demo_carracing.py index d412b413..ac4bb67f 100644 --- a/examples/demo_carracing.py +++ b/examples/demo_carracing.py @@ -43,6 +43,7 @@ def register_input(): a[2] = 0 contexts = {i: {"VEHICLE_ID": i} for i in range(len(VEHICLE_NAMES))} + CARLVehicleRacing.render_mode = "human" env = CARLVehicleRacing(contexts=contexts) record_video = False @@ -62,14 +63,14 @@ def register_input(): restart = False while True: register_input() - s, r, done, info = env.step(a) + s, r, truncated, terminated, info = env.step(a) time.sleep(0.025) total_reward += r - if steps % 200 == 0 or done: + if steps % 200 == 0 or truncated or terminated: print("\naction " + str(["{:+0.2f}".format(x) for x in a])) print("step {} total_reward {:+0.2f}".format(steps, total_reward)) steps += 1 - isopen = env.render() - if done or restart or not isopen: + env.render() + if truncated or terminated or restart or not isopen: break env.close() diff --git a/examples/demo_heuristic_bipedalwalker.py b/examples/demo_heuristic_bipedalwalker.py index 64ef401f..19d6500f 100644 --- a/examples/demo_heuristic_bipedalwalker.py +++ b/examples/demo_heuristic_bipedalwalker.py @@ -92,7 +92,7 @@ def demo_heuristic(env: CARLBipedalWalker | bipedal_walker.BipedalWalker) -> Non CARLBipedalWalker.render_mode = "human" env = CARLBipedalWalker( # Play with leg height :) - contexts = {0: {"LEG_H": 4}} + contexts={0: {"LEG_H": 4}} ) demo_heuristic(env) env.close() diff --git a/examples/play_car_racing.py b/examples/play_car_racing.py deleted file mode 100644 index d36b868a..00000000 --- a/examples/play_car_racing.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -Code adapted from gym.envs.box2d.car_racing.py - -Play Car Racing with the new CARL vehicles and test out our contexts yourself! -""" - -import numpy as np -import time -import pygame -from carl.envs.gymnasium.box2d.carl_vehicle_racing import ( - CARLVehicleRacing, - VEHICLE_NAMES, -) - -if __name__ == "__main__": - a = np.array([0.0, 0.0, 0.0]) - - def register_input(): - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - a[0] = -1.0 - if event.key == pygame.K_RIGHT: - a[0] = +1.0 - if event.key == pygame.K_UP: - a[1] = +1.0 - if event.key == pygame.K_DOWN: - a[2] = +0.8 # set 1.0 for wheels to block to zero rotation - if event.key == pygame.K_RETURN: - global restart - restart = True - if event.key == pygame.K_ESCAPE: - global isopen - isopen = False - - if event.type == pygame.KEYUP: - if event.key == pygame.K_LEFT: - a[0] = 0 - if event.key == pygame.K_RIGHT: - a[0] = 0 - if event.key == pygame.K_UP: - a[1] = 0 - if event.key == pygame.K_DOWN: - a[2] = 0 - - contexts = {i: {"VEHICLE_ID": i} for i in range(len(VEHICLE_NAMES))} - env = CARLVehicleRacing(contexts=contexts) - - record_video = False - if record_video: - from gymnasium.wrappers.record_video import RecordVideo - - env = RecordVideo( - env=env, video_folder="/tmp/video-test", name_prefix="CARLVehicleRacing" - ) - - isopen = True - while isopen: - env.reset() - env.render() - total_reward = 0.0 - steps = 0 - restart = False - while True: - register_input() - s, r, terminated, truncated, info = env.step(a) - done = terminated | truncated - time.sleep(0.025) - total_reward += r - if steps % 200 == 0 or done: - print("\naction " + str(["{:+0.2f}".format(x) for x in a])) - print("step {} total_reward {:+0.2f}".format(steps, total_reward)) - steps += 1 - env.render() - if done or restart or not isopen: - break - env.close()