forked from huawei-noah/SMARTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrajectory_tracking_agent.py
83 lines (66 loc) · 2.33 KB
/
trajectory_tracking_agent.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
73
74
75
76
77
78
79
80
81
82
83
import logging
import gym
from smarts.core.utils.episodes import episodes
from smarts.core.agent_interface import AgentInterface, AgentType
from smarts.core.agent import AgentSpec, AgentPolicy
from examples import default_argument_parser
logging.basicConfig(level=logging.INFO)
AGENT_ID = "Agent-007"
class Policy(AgentPolicy):
def act(self, obs):
lane_index = 0
num_trajectory_points = min([10, len(obs.waypoint_paths[lane_index])])
# Desired speed is in m/s
desired_speed = 50 / 3.6
trajectory = [
[
obs.waypoint_paths[lane_index][i].pos[0]
for i in range(num_trajectory_points)
],
[
obs.waypoint_paths[lane_index][i].pos[1]
for i in range(num_trajectory_points)
],
[
obs.waypoint_paths[lane_index][i].heading
for i in range(num_trajectory_points)
],
[desired_speed for i in range(num_trajectory_points)],
]
return trajectory
def main(scenarios, headless, num_episodes, seed):
agent_spec = AgentSpec(
interface=AgentInterface.from_type(AgentType.Tracker, max_episode_steps=None),
policy_builder=Policy,
)
env = gym.make(
"smarts.env:hiway-v0",
scenarios=scenarios,
agent_specs={AGENT_ID: agent_spec},
headless=headless,
visdom=False,
timestep_sec=0.1,
sumo_headless=True,
seed=seed,
# envision_record_data_replay_path="./data_replay",
)
for episode in episodes(n=num_episodes):
agent = agent_spec.build_agent()
observations = env.reset()
episode.record_scenario(env.scenario_log)
dones = {"__all__": False}
while not dones["__all__"]:
agent_obs = observations[AGENT_ID]
agent_action = agent.act(agent_obs)
observations, rewards, dones, infos = env.step({AGENT_ID: agent_action})
episode.record_step(observations, rewards, dones, infos)
env.close()
if __name__ == "__main__":
parser = default_argument_parser("trajectory-tracking-agent-example")
args = parser.parse_args()
main(
scenarios=args.scenarios,
headless=args.headless,
num_episodes=args.episodes,
seed=args.seed,
)