-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path03_scenic_env_usage.py
72 lines (60 loc) · 2.44 KB
/
03_scenic_env_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
import numpy as np
from scenic.domains.driving.roads import ManeuverType
import mats_gym
from mats_gym.envs import renderers
"""
This example shows how to use the Scenic scenario adapter.
"""
NUM_EPISODES = 3
def policy():
"""
A simple policy that drives the agent forward and turns left or right randomly.
"""
return np.array(
[
0.5 + np.random.rand() / 2, # throttle
np.random.rand() - 0.5, # steer
0.0, # brake
]
)
def main():
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(filename)s - [%(levelname)s] - %(message)s",
)
# MATS-Gym also supports Scenic scenarios. Scenic is a probabilistic programming language for generating complex
# scenarios. The Scenic environment adapter can be used to load Scenic scenarios and generate scenes from them.
# You can provide either a single scenario or a list of scenarios. Moreover, you can control how many scenes per
# scenario should be generated.
env = mats_gym.scenic_env(
host="localhost", # The host to connect to
port=2000, # The port to connect to
scenario_specification="scenarios/scenic/carla_challenge_08.scenic", # Path to the scenario specification
scenes_per_scenario=5, # How many scenes should be generated per scenario
resample_scenes=False, # if True, the scenes are resampled after all initial scenes have been used.
agent_name_prefixes=["sut", "adv"], # Each actor whose role-name starts with one of the prefixes is an agent.
render_mode="human", # The render mode. Can be "human", "rgb_array", "rgb_array_list".
render_config=renderers.camera_pov(agent="sut"), # See adex_gym.envs.renderers for more render configs.
params={
"MANEUVER_TYPE": ManeuverType.LEFT_TURN.value,
"NPC_MANEUVER_CONFLICT_ONLY": True,
"NPC_PARAMS": {
"ignore_traffic_lights": False,
"ignore_vehicles": False,
"target_speed": 30,
},
"NUM_NPCS": 1,
},
)
for _ in range(5):
obs, info = env.reset()
done = False
while not done:
actions = {agent: policy() for agent in env.agents}
obs, reward, done, truncated, info = env.step(actions)
done = all(done.values())
env.render()
env.close()
if __name__ == "__main__":
main()