Configuration
A MetaDrive instance accepts a dict as the environmental config. For example, you can build a MetaDrive instance with 200 generated maps via
from metadrive import MetaDriveEnv
config = dict(num_scenarios=200, start_seed=0)
env = MetaDriveEnv(config)
In this page, we describe the details of the config system and configurable options for all environments.
Config system
This section discusses how to configure the an environment in MetaDrive and some features of the config system.
Overwriting
Every environment has a default config, which records the parameters required to launch the environment. It is content is actually a nested dictionary whose keys and values represent the parameter names and corresponding values. This default config dict can be accessed via the class method:
default_config = MetaDriveEnv.default_config()
When creating environments, the external config config will overwritten default values of certain fields in the default_config. The following code exemplifies this.
from metadrive import MetaDriveEnv
default_config = MetaDriveEnv.default_config()
env = MetaDriveEnv(dict(num_scenarios=100, log_level=50))
env_config = env.config
print("default_config['num_scenarios']:", default_config["num_scenarios"])
print("env_config['num_scenarios']:", env_config["num_scenarios"])
default_config['num_scenarios']: 1
env_config['num_scenarios']: 100
Sanity Check
There is a check mechanism which prohibit users to set the value for a key that doesn’t exist in the default_config. This is helpful to make sure that users type the correct parameter name and successfully config the target parameter.
try:
env = MetaDriveEnv(dict(non_exist_key=False))
except KeyError as error:
print(str(error)[:62] + " ...")
"'{'non_exist_key'}' does not exist in existing config. Please ...
The check mechanism will further ensure if the type of the parameter is correct. For example, the num_scenarios should be an int type, and thus a list type parameter will raise an error.
try:
env = MetaDriveEnv(dict(num_scenarios=[0, 1]))
except AssertionError as error:
print(str(error)[:62] + " ...")
TypeError: num_scenarios:[0, 1] ...
Basic Config Sharing
The default configs are different across all environments, but may share some identical fields. Take the MetaDriveEnv and ScenarioEnv as example.
from metadrive.envs import MetaDriveEnv, ScenarioEnv
metadrive_config = set(MetaDriveEnv.default_config().keys())
scenario_config = set(ScenarioEnv.default_config().keys())
print("Number of parameters of MetaDriveEnv: {}".format(len(metadrive_config)))
print("Number of parameters of ScenarioEnv: {}\n".format(len(scenario_config)))
try:
assert metadrive_config == scenario_config
except AssertionError as error:
print("The config between MetaDriveEnv and ScenarioEnv is different.\n")
identical_parameters = scenario_config.intersection(metadrive_config)
print("Number of identical parameters: \
{}".format(len(identical_parameters)))
print("Number of unique parameters in MetaDriveEnv: \
{}".format(len(metadrive_config-identical_parameters)))
print("Number of unique parameters in ScenarioEnv: \
{}".format(len(scenario_config-identical_parameters)))
Number of parameters of MetaDriveEnv: 114
Number of parameters of ScenarioEnv: 125
The config between MetaDriveEnv and ScenarioEnv is different.
Number of identical parameters: 93
Number of unique parameters in MetaDriveEnv: 21
Number of unique parameters in ScenarioEnv: 32
It is worth mentioning the parameter sharing mechanism, which is helpful when we create a new environment, so we don’t need to copy some common configs to the default_config to the new environments again and again. Let’s first check out how the default_config() function is implemented.
from metadrive.utils.doc_utils import print_source
print_source(ScenarioEnv.default_config)
[35m@classmethod[39;49;00m
[34mdef[39;49;00m [35mdefault_config[39;49;00m[35m([39;49;00m[35mcls[39;49;00m[35m)[39;49;00m[35m:[39;49;00m
[30mconfig[39;49;00m = [35msuper[39;49;00m[35m([39;49;00m[30mScenarioEnv[39;49;00m[35m,[39;49;00m [35mcls[39;49;00m[35m)[39;49;00m.[30mdefault_config[39;49;00m[35m([39;49;00m[35m)[39;49;00m
[30mconfig[39;49;00m.[30mupdate[39;49;00m[35m([39;49;00m[30mSCENARIO_ENV_CONFIG[39;49;00m[35m)[39;49;00m
[34mreturn[39;49;00m [30mconfig[39;49;00m
It is quite simple and is implemented by overwriting the super(ScenarioEnv, cls).default_config() with Scenario_ENV_CONFIG. If we check the contents of the two config dict, we will find that the BaseEnv.default_config() = super(ScenarioEnv, cls).default_config() is the subset of ScenarioEnv.default_config() and provides the ScenarioEnv with the basic configs.
from metadrive.envs.base_env import BaseEnv
set(BaseEnv.default_config()).issubset(set(ScenarioEnv.default_config()))
True
It is the same for the MetaDriveEnv as well, whose default config is:
print_source(MetaDriveEnv.default_config)
[35m@classmethod[39;49;00m
[34mdef[39;49;00m [35mdefault_config[39;49;00m[35m([39;49;00m[35mcls[39;49;00m[35m)[39;49;00m -> [30mConfig[39;49;00m[35m:[39;49;00m
[30mconfig[39;49;00m = [35msuper[39;49;00m[35m([39;49;00m[30mMetaDriveEnv[39;49;00m[35m,[39;49;00m [35mcls[39;49;00m[35m)[39;49;00m.[30mdefault_config[39;49;00m[35m([39;49;00m[35m)[39;49;00m
[30mconfig[39;49;00m.[30mupdate[39;49;00m[35m([39;49;00m[30mMETADRIVE_DEFAULT_CONFIG[39;49;00m[35m)[39;49;00m
[30mconfig[39;49;00m.[30mregister_type[39;49;00m[35m([39;49;00m[33m"[39;49;00m[33mmap[39;49;00m[33m"[39;49;00m[35m,[39;49;00m [35mstr[39;49;00m[35m,[39;49;00m [35mint[39;49;00m[35m)[39;49;00m
[30mconfig[39;49;00m[35m[[39;49;00m[33m"[39;49;00m[33mmap_config[39;49;00m[33m"[39;49;00m[35m][39;49;00m.[30mregister_type[39;49;00m[35m([39;49;00m[33m"[39;49;00m[33mconfig[39;49;00m[33m"[39;49;00m[35m,[39;49;00m [34mNone[39;49;00m[35m)[39;49;00m
[34mreturn[39;49;00m [30mconfig[39;49;00m
As there is an overwriting function is called, it is ok to overwrite the values of parameters in BaseEnv.default_config() when making the default_config for a inherited environment. The following code shows that the config show_sidewalk is True in BaseEnv but is overwritten to False in ScenarioEnv because of the SCENARIO_ENV_CONFIG.
from metadrive.envs.scenario_env import SCENARIO_ENV_CONFIG
assert BaseEnv.default_config()["show_sidewalk"]
assert not ScenarioEnv.default_config()["show_sidewalk"]
assert not SCENARIO_ENV_CONFIG["show_sidewalk"]
Programming with Configs
The configs can be accessed everywhere in the program just like the simulation engine instance, so we can use these parameters to adjust the behavior of the simulation. A tutorial of accessing configs when programming new environments is available at config.
Basic Configs
As all environments are subclass of BaseEnv and share the parameters of BaseEnv, we first discuss the parameters in BaseEnv.default_config().
The available items with annotations are listed as follows.
You can check this in the source code as well.
import metadrive.envs.base_env as base_env
from metadrive.utils.doc_utils import print_source, CONFIG
module_source = print_source(base_env, ["BASE_DEFAULT_CONFIG", ")\n\n"], colorscheme=CONFIG)
[31mBASE_DEFAULT_CONFIG[39;49;00m = [35mdict[39;49;00m[35m([39;49;00m
[30m# ===== agent =====[39;49;00m
[30m# Whether randomize the car model for the agent, randomly choosing from 4 types of cars[39;49;00m
[31mrandom_agent_model[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# The ego config is: env_config["vehicle_config"].update(env_config"[agent_configs"]["default_agent"])[39;49;00m
[31magent_configs[39;49;00m=[35m{[39;49;00m[31mDEFAULT_AGENT[39;49;00m[35m:[39;49;00m [35mdict[39;49;00m[35m([39;49;00m[31muse_special_color[39;49;00m=[34mTrue[39;49;00m[35m,[39;49;00m [31mspawn_lane_index[39;49;00m=[34mNone[39;49;00m[35m)[39;49;00m[35m}[39;49;00m[35m,[39;49;00m
[30m# ===== multi-agent =====[39;49;00m
[30m# This should be >1 in MARL envs, or set to -1 for spawning as many vehicles as possible.[39;49;00m
[31mnum_agents[39;49;00m=[34m1[39;49;00m[35m,[39;49;00m
[30m# Turn on this to notify the simulator that it is MARL env[39;49;00m
[31mis_multi_agent[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# The number of agent will be fixed adn determined at the start of the episode, if set to False[39;49;00m
[31mallow_respawn[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# How many substeps for the agent to stay static at the death place after done. (Default for MARL: 25)[39;49;00m
[31mdelay_done[39;49;00m=[34m0[39;49;00m[35m,[39;49;00m
[30m# ===== Action/Control =====[39;49;00m
[30m# Please see Documentation: Action and Policy for more details[39;49;00m
[30m# What policy to use for controlling agents[39;49;00m
[31magent_policy[39;49;00m=[31mEnvInputPolicy[39;49;00m[35m,[39;49;00m
[30m# If set to True, agent_policy will be overriden and change to ManualControlPolicy[39;49;00m
[31mmanual_control[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# What interfaces to use for manual control, options: "steering_wheel" or "keyboard" or "xbos"[39;49;00m
[31mcontroller[39;49;00m=[33m"[39;49;00m[33mkeyboard[39;49;00m[33m"[39;49;00m[35m,[39;49;00m
[30m# Used with EnvInputPolicy. If set to True, the env.action_space will be discrete[39;49;00m
[31mdiscrete_action[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# If True, use MultiDiscrete action space. Otherwise, use Discrete.[39;49;00m
[31muse_multi_discrete[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# How many discrete actions are used for steering dim[39;49;00m
[31mdiscrete_steering_dim[39;49;00m=[34m5[39;49;00m[35m,[39;49;00m
[30m# How many discrete actions are used for throttle/brake dim[39;49;00m
[31mdiscrete_throttle_dim[39;49;00m=[34m5[39;49;00m[35m,[39;49;00m
[30m# Check if the action is contained in gym.space. Usually turned off to speed up simulation[39;49;00m
[31maction_check[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# ===== Observation =====[39;49;00m
[30m# Please see Documentation: Observation for more details[39;49;00m
[30m# Whether to normalize the pixel value from 0-255 to 0-1[39;49;00m
[31mnorm_pixel[39;49;00m=[34mTrue[39;49;00m[35m,[39;49;00m
[30m# The number of timesteps for stacking image observation[39;49;00m
[31mstack_size[39;49;00m=[34m3[39;49;00m[35m,[39;49;00m
[30m# Whether to use image observation or lidar. It takes effect in get_single_observation[39;49;00m
[31mimage_observation[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Like agent_policy, users can use customized observation class through this field[39;49;00m
[31magent_observation[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# ===== Termination =====[39;49;00m
[30m# The maximum length of each agent episode. Set to None to remove this constraint[39;49;00m
[31mhorizon[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# If set to True, the terminated will be True as well when the length of agent episode exceeds horizon[39;49;00m
[31mtruncate_as_terminate[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# ===== Main Camera =====[39;49;00m
[30m# A True value makes the camera follow the reference line instead of the vehicle, making its movement smooth[39;49;00m
[31muse_chase_camera_follow_lane[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Height of the main camera[39;49;00m
[31mcamera_height[39;49;00m=[32m2.2[39;49;00m[35m,[39;49;00m
[30m# Distance between the camera and the vehicle. It is the distance projecting to the x-y plane.[39;49;00m
[31mcamera_dist[39;49;00m=[32m7.5[39;49;00m[35m,[39;49;00m
[30m# Pitch of main camera. If None, this will be automatically calculated[39;49;00m
[31mcamera_pitch[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m [30m# degree[39;49;00m
[30m# Smooth the camera movement[39;49;00m
[31mcamera_smooth[39;49;00m=[34mTrue[39;49;00m[35m,[39;49;00m
[30m# How many frames used to smooth the camera[39;49;00m
[31mcamera_smooth_buffer_size[39;49;00m=[34m20[39;49;00m[35m,[39;49;00m
[30m# FOV of main camera[39;49;00m
[31mcamera_fov[39;49;00m=[34m65[39;49;00m[35m,[39;49;00m
[30m# Only available in MARL setting, choosing which agent to track. Values should be "agent0", "agent1" or so on[39;49;00m
[31mprefer_track_agent[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# Setting the camera position for the Top-down Camera for 3D viewer (pressing key "B" to activate it)[39;49;00m
[31mtop_down_camera_initial_x[39;49;00m=[34m0[39;49;00m[35m,[39;49;00m
[31mtop_down_camera_initial_y[39;49;00m=[34m0[39;49;00m[35m,[39;49;00m
[31mtop_down_camera_initial_z[39;49;00m=[34m200[39;49;00m[35m,[39;49;00m
[30m# ===== Vehicle =====[39;49;00m
[31mvehicle_config[39;49;00m=[35mdict[39;49;00m[35m([39;49;00m
[30m# Vehicle model. Candidates: "s", "m", "l", "xl", "default". random_agent_model makes this config invalid[39;49;00m
[31mvehicle_model[39;49;00m=[33m"[39;49;00m[33mdefault[39;49;00m[33m"[39;49;00m[35m,[39;49;00m
[30m# If set to True, the vehicle can go backwards with throttle/brake < -1[39;49;00m
[31menable_reverse[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Whether to show the box as navigation points[39;49;00m
[31mshow_navi_mark[39;49;00m=[34mTrue[39;49;00m[35m,[39;49;00m
[30m# Whether to show a box mark at the destination[39;49;00m
[31mshow_dest_mark[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Whether to draw a line from current vehicle position to the designation point[39;49;00m
[31mshow_line_to_dest[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Whether to draw a line from current vehicle position to the next navigation point[39;49;00m
[31mshow_line_to_navi_mark[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Whether to draw left / right arrow in the interface to denote the navigation direction[39;49;00m
[31mshow_navigation_arrow[39;49;00m=[34mTrue[39;49;00m[35m,[39;49;00m
[30m# If set to True, the vehicle will be in color green in top-down renderer or MARL setting[39;49;00m
[31muse_special_color[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# Clear wheel friction, so it can not move by setting steering and throttle/brake. Used for ReplayPolicy[39;49;00m
[31mno_wheel_friction[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# ===== image capturing =====[39;49;00m
[30m# Which camera to use for image observation. It should be a sensor registered in sensor config.[39;49;00m
[31mimage_source[39;49;00m=[33m"[39;49;00m[33mrgb_camera[39;49;00m[33m"[39;49;00m[35m,[39;49;00m
[30m# ===== vehicle spawn and navigation =====[39;49;00m
[30m# A BaseNavigation instance. It should match the road network type.[39;49;00m
[31mnavigation_module[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# A lane id specifies which lane to spawn this vehicle[39;49;00m
[31mspawn_lane_index[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# destination lane id. Required only when navigation module is not None.[39;49;00m
[31mdestination[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[30m# the longitudinal and lateral position on the spawn lane[39;49;00m
[31mspawn_longitude[39;49;00m=[32m5.0[39;49;00m[35m,[39;49;00m
[31mspawn_lateral[39;49;00m=[32m0.0[39;49;00m[35m,[39;49;00m
[30m# If the following items is assigned, the vehicle will be spawn at the specified position with certain speed[39;49;00m
[31mspawn_position_heading[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[31mspawn_velocity[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m [30m# m/s[39;49;00m
[31mspawn_velocity_car_frame[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# ==== others ====[39;49;00m
[30m# How many cars the vehicle has overtaken. It is deprecated due to bug.[39;49;00m
[31movertake_stat[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# If set to True, the default texture for the vehicle will be replaced with a pure color one.[39;49;00m
[31mrandom_color[39;49;00m=[34mFalse[39;49;00m[35m,[39;49;00m
[30m# The shape of vehicle are predefined by its class. But in special scenario (WaymoVehicle) we might want to[39;49;00m
[30m# set to arbitrary shape.[39;49;00m
[31mwidth[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[31mlength[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[31mheight[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[31mmass[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m
[31mscale[39;49;00m=[34mNone[39;49;00m[35m,[39;49;00m [30m# triplet (x, y, z)[39;49;00m
Environment Configs
Please see Environments for unique configs for each environment or check the source code of each environment.