Skip to content

Commit

Permalink
Update examples (#66)
Browse files Browse the repository at this point in the history
* turn on compress-model

* decrease agent num

* auto drive in marl

* update help msg

* format

* remove a comment

* update IO

* marl fix

* fix top down

* esc quit

* format

* update readme

* update catch error

* fix top down

* catch error

* format

Co-authored-by: PENG Zhenghao <pengzh@ie.cuhk.edu.hk>
  • Loading branch information
QuanyiLi and PENG Zhenghao committed Sep 6, 2021
1 parent df78c05 commit 76e3c8b
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 99 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ You can also launch an instance of Multi-Agent scenario as follows
```bash
python -m metadrive.examples.drive_in_multi_agent_env --env roundabout
```

or launch and render in pygame front end

```bash
python -m metadrive.examples.drive_in_multi_agent_env --pygame_render --env roundabout
```

env argument could be:
- roundabout (default)
- intersection
Expand Down
1 change: 1 addition & 0 deletions metadrive/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TerminationState:
" B: Top-down View Camera (control: WASD-=)\n" \
" +: Lift Camera\n" \
" -: Lower Camera\n" \
" Mouse click: move camera (top-down view)\n" \
" Esc: Quit\n"

DEBUG_MESSAGE = " 1: Box Debug Mode\n" \
Expand Down
2 changes: 1 addition & 1 deletion metadrive/engine/core/chase_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, engine, camera_height: float, camera_dist: float):

# TPP rotate
props = WindowProperties()
props.setCursorHidden(True)
# props.setCursorHidden(True)
# props.setMouseMode(WindowProperties.MConfined)
self.engine.win.requestProperties(props)
self.mouse_rotate = 0
Expand Down
9 changes: 5 additions & 4 deletions metadrive/engine/core/engine_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def __init__(self, global_config):
self.accept('1', self.toggleDebug)
self.accept('4', self.toggleAnalyze)

if self.global_config["disable_model_compression"]:
pass
else:
loadPrcFileData("", "compressed-textures 1") # Default to compress

super(EngineCore, self).__init__(windowType=self.mode)

# Change window size at runtime if screen too small
Expand All @@ -124,10 +129,6 @@ def __init__(self, global_config):

# main_window_position = (0, 0)
if self.mode == RENDER_MODE_ONSCREEN:
if self.global_config["disable_model_compression"]:
pass
else:
loadPrcFileData("", "compressed-textures 1") # Default to compress
h = self.pipe.getDisplayHeight()
w = self.pipe.getDisplayWidth()
if self.global_config["window_size"][0] > 0.9 * w or self.global_config["window_size"][1] > 0.9 * h:
Expand Down
4 changes: 2 additions & 2 deletions metadrive/envs/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# ===== Rendering =====
use_render=False, # pop a window to render or not
debug=False,
disable_model_compression=True, # disable compression if you wish to launch the window quicker.
disable_model_compression=False, # disable compression if you wish to launch the window quicker.
cull_scene=True, # only for debug use
use_chase_camera_follow_lane=False, # If true, then vision would be more stable.
camera_height=1.8,
Expand Down Expand Up @@ -464,7 +464,7 @@ def _render_topdown(self, *args, **kwargs):
if self._top_down_renderer is None:
from metadrive.obs.top_down_renderer import TopDownRenderer
self._top_down_renderer = TopDownRenderer(self, self.current_map, *args, **kwargs)
return self._top_down_renderer.render(list(self.vehicles.values()), self.agent_manager)
return self._top_down_renderer.render(self.agent_manager)

@property
def main_camera(self):
Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/marl_envs/marl_bottleneck.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

MABottleneckConfig = dict(
spawn_roads=[Road(FirstPGBlock.NODE_2, FirstPGBlock.NODE_3), -Road(Split.node(2, 0, 0), Split.node(2, 0, 1))],
num_agents=20,
num_agents=12,
map_config=dict(exit_length=60, bottle_lane_num=4, neck_lane_num=1, neck_length=20),
top_down_camera_initial_x=95,
top_down_camera_initial_y=15,
Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/marl_envs/marl_inout_roundabout.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
top_down_camera_initial_x=95,
top_down_camera_initial_y=15,
top_down_camera_initial_z=120,
num_agents=40,
num_agents=12,
)


Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/marl_envs/marl_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-Road(InterSection.node(1, 1, 0), InterSection.node(1, 1, 1)),
-Road(InterSection.node(1, 2, 0), InterSection.node(1, 2, 1)),
],
num_agents=30,
num_agents=12,
map_config=dict(exit_length=60, lane_num=2),
top_down_camera_initial_x=80,
top_down_camera_initial_y=0,
Expand Down
2 changes: 1 addition & 1 deletion metadrive/envs/marl_envs/marl_tollgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

MATollConfig = dict(
spawn_roads=[Road(FirstPGBlock.NODE_2, FirstPGBlock.NODE_3), -Road(Merge.node(3, 0, 0), Merge.node(3, 0, 1))],
num_agents=40,
num_agents=12,
map_config=dict(exit_length=70, lane_num=3, toll_lane_num=8, toll_length=10),
top_down_camera_initial_x=125,
top_down_camera_initial_y=0,
Expand Down
16 changes: 8 additions & 8 deletions metadrive/envs/marl_envs/multi_agent_metadrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
MULTI_AGENT_METADRIVE_DEFAULT_CONFIG = dict(
# ===== Multi-agent =====
is_multi_agent=True,
num_agents=15,
num_agents=12,
# If num_agents is set to None, then endless vehicles will be added only the empty spawn points exist
random_agent_model=False,

Expand Down Expand Up @@ -71,14 +71,14 @@ def _merge_extra_config(self, config) -> "Config":
ret_config = self.default_config().update(
config, allow_add_new_key=False, stop_recursive_update=["target_vehicle_configs"]
)
if not ret_config["crash_done"] and ret_config["crash_vehicle_penalty"] > 2:
logging.warning(
"Are you sure you wish to set crash_vehicle_penalty={} when crash_done=False?".format(
ret_config["crash_vehicle_penalty"]
)
)
# if not ret_config["crash_done"] and ret_config["crash_vehicle_penalty"] > 2:
# logging.warning(
# "Are you sure you wish to set crash_vehicle_penalty={} when crash_done=False?".format(
# ret_config["crash_vehicle_penalty"]
# )
# )
if ret_config["use_render"] and ret_config["disable_model_compression"]:
logging.warning("Turn disable_model_compression=True can accelerate Multi-agent rendering performance!")
logging.warning("Turn disable_model_compression=True can decrease the loading time!")

if "prefer_track_agent" in config and config["prefer_track_agent"]:
ret_config["target_vehicle_configs"][config["prefer_track_agent"]]["use_special_color"] = True
Expand Down
49 changes: 35 additions & 14 deletions metadrive/examples/drive_in_multi_agent_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MultiAgentMetaDrive, MultiAgentTollgateEnv, MultiAgentBottleneckEnv, MultiAgentIntersectionEnv,
MultiAgentRoundaboutEnv, MultiAgentParkingLotEnv
)
from metadrive.constants import HELP_MESSAGE

if __name__ == "__main__":
envs = dict(
Expand All @@ -32,8 +33,10 @@

parser = argparse.ArgumentParser()
parser.add_argument("--env", type=str, default="roundabout", choices=list(envs.keys()))
parser.add_argument("--pygame_render", action="store_true")
args = parser.parse_args()
env_cls_name = args.env
extra_args = dict(mode="top_down", film_size=(800, 800)) if args.pygame_render else {}
assert env_cls_name in envs.keys(), "No environment named {}, argument accepted: \n" \
"(1) roundabout\n" \
"(2) intersection\n" \
Expand All @@ -42,17 +45,35 @@
"(5) parkinglot\n" \
"(6) pgma" \
.format(env_cls_name)
env = envs[env_cls_name]({"use_render": True, "manual_control": True, "crash_done": False})
env.reset()
env.switch_to_third_person_view() # Default is in Top-down view, we switch to Third-person view.
for i in range(1, 10000000000):
o, r, d, info = env.step({agent_id: [0, 0] for agent_id in env.vehicles.keys()})
env.render(
text={
"Number of existing vehicles": len(env.vehicles),
"Tracked agent (Press Q)": env.engine.agent_manager.object_to_agent(env.current_track_vehicle.id)
}
)
if d["__all__"]:
env.reset()
env.close()
env = envs[env_cls_name](
{
"use_render": True if not args.pygame_render else False,
"manual_control": True,
"crash_done": False,
"IDM_agent": True
}
)
try:
env.reset()
if env.current_track_vehicle:
env.current_track_vehicle.expert_takeover = True
print(HELP_MESSAGE)
env.switch_to_third_person_view() # Default is in Top-down view, we switch to Third-person view.
for i in range(1, 10000000000):
o, r, d, info = env.step({agent_id: [0, 0] for agent_id in env.vehicles.keys()})
env.render(
**extra_args,
text={
"Number of existing vehicles": len(env.vehicles),
"Tracked agent (Press Q)": env.engine.agent_manager.object_to_agent(env.current_track_vehicle.id),
"Auto-Drive (Switch mode: T)": "on" if env.current_track_vehicle.expert_takeover else "off",
} if not args.pygame_render else {}
)
if d["__all__"]:
env.reset()
if env.current_track_vehicle:
env.current_track_vehicle.expert_takeover = True
except:
pass
finally:
env.close()
39 changes: 23 additions & 16 deletions metadrive/examples/drive_in_safe_metadrive_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@
import logging

from metadrive.tests.test_functionality.test_object_collision_detection import ComplexEnv
from metadrive.constants import HELP_MESSAGE

if __name__ == "__main__":
env = ComplexEnv(dict(use_render=True, manual_control=True, vehicle_config={"show_navi_mark": False}))
env.reset()
env.vehicle.expert_takeover = True
for i in range(1, 1000000000):
previous_takeover = env.current_track_vehicle.expert_takeover
o, r, d, info = env.step([0, 0])
env.render(
text={
"Auto-Drive (Switch mode: T)": "on" if env.current_track_vehicle.expert_takeover else "off",
"Total episode cost": env.episode_cost
}
)
if not previous_takeover and env.current_track_vehicle.expert_takeover:
logging.warning("Auto-Drive mode may fail to solve some scenarios due to distribution mismatch")
if d and info["arrive_dest"]:
env.reset()
env.close()
try:
env.reset()
print(HELP_MESSAGE)
env.vehicle.expert_takeover = True
for i in range(1, 1000000000):
previous_takeover = env.current_track_vehicle.expert_takeover
o, r, d, info = env.step([0, 0])
env.render(
text={
"Auto-Drive (Switch mode: T)": "on" if env.current_track_vehicle.expert_takeover else "off",
"Total episode cost": env.episode_cost
}
)
if not previous_takeover and env.current_track_vehicle.expert_takeover:
logging.warning("Auto-Drive mode may fail to solve some scenarios due to distribution mismatch")
if d and info["arrive_dest"]:
env.reset()
env.current_track_vehicle.expert_takeover = True
except:
pass
finally:
env.close()
43 changes: 25 additions & 18 deletions metadrive/examples/drive_in_single_agent_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np

from metadrive import MetaDriveEnv
from metadrive.constants import HELP_MESSAGE

if __name__ == "__main__":
config = dict(
Expand All @@ -31,21 +32,27 @@
if args.observation == "rgb_camera":
config.update(dict(offscreen_render=True))
env = MetaDriveEnv(config)
o = env.reset()
env.vehicle.expert_takeover = True
if args.observation == "rgb_camera":
assert isinstance(o, dict)
print("The observation is a dict with numpy arrays as values: ", {k: v.shape for k, v in o.items()})
else:
assert isinstance(o, np.ndarray)
print("The observation is an numpy array with shape: ", o.shape)
for i in range(1, 1000000000):
o, r, d, info = env.step([0, 0])
env.render(
text={
"Auto-Drive (Switch mode: T)": "on" if env.current_track_vehicle.expert_takeover else "off",
}
)
if d and info["arrive_dest"]:
env.reset()
env.close()
try:
o = env.reset()
print(HELP_MESSAGE)
env.vehicle.expert_takeover = True
if args.observation == "rgb_camera":
assert isinstance(o, dict)
print("The observation is a dict with numpy arrays as values: ", {k: v.shape for k, v in o.items()})
else:
assert isinstance(o, np.ndarray)
print("The observation is an numpy array with shape: ", o.shape)
for i in range(1, 1000000000):
o, r, d, info = env.step([0, 0])
env.render(
text={
"Auto-Drive (Switch mode: T)": "on" if env.current_track_vehicle.expert_takeover else "off",
}
)
if d and info["arrive_dest"]:
env.reset()
env.current_track_vehicle.expert_takeover = True
except:
pass
finally:
env.close()
40 changes: 30 additions & 10 deletions metadrive/examples/top_down_metadrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

import random
from metadrive.constants import HELP_MESSAGE

import matplotlib.pyplot as plt

Expand All @@ -27,21 +28,30 @@ def draw_multi_channels_top_down_observation(obs):
"Road and navigation", "Ego now and previous pos", "Neighbor at step t", "Neighbor at step t-1",
"Neighbor at step t-2"
]
fig, axs = plt.subplots(1, num_channels, figsize=(15, 4), dpi=150)
fig, axs = plt.subplots(1, num_channels, figsize=(15, 4), dpi=80)
count = 0

def close_event():
plt.close() # timer calls this function after 3 seconds and closes the window

timer = fig.canvas.new_timer(interval=4500) # creating a timer object and setting an interval of 3000 milliseconds
timer.add_callback(close_event)

for i, name in enumerate(channel_names):
count += 1
ax = axs[i]
ax.imshow(obs[..., i], cmap="bone")
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(name)
print("Drawing {}-th semantic map!".format(count))
# print("Drawing {}-th semantic map!".format(count))
fig.suptitle("Multi-channels Top-down Observation")
timer.start()
plt.show()


if __name__ == "__main__":
print(HELP_MESSAGE)
env = TopDownMetaDrive(
dict(
# We also support using two renderer (Panda3D renderer and Pygame renderer) simultaneously. You can
Expand All @@ -57,11 +67,21 @@ def draw_multi_channels_top_down_observation(obs):
start_seed=random.randint(0, 1000),
)
)
o = env.reset()
for i in range(1, 100000):
o, r, d, info = env.step([0, 1])
env.render(mode="top_down")
if d:
draw_multi_channels_top_down_observation(o)
env.reset()
env.close()
try:
o = env.reset()
for i in range(1, 100000):
o, r, d, info = env.step([0, 1])
env.render(mode="top_down", film_size=(800, 800))
if d:
# print("Close the popup window to continue.")
draw_multi_channels_top_down_observation(o)
env.reset()
# ret = input("Do you wish to quit? Type any ESC to quite, or press enter to continue")
# if len(ret) == 0:
# continue
# else:
# break
except:
pass
finally:
env.close()

0 comments on commit 76e3c8b

Please sign in to comment.