forked from huawei-noah/SMARTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_agent.py
85 lines (66 loc) · 2.48 KB
/
single_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
84
85
import logging
import pathlib
import gym
from examples.argument_parser import default_argument_parser
from smarts.core.agent import Agent
from smarts.core.agent_interface import AgentInterface, AgentType
from smarts.core.sensors import Observation
from smarts.core.utils.episodes import episodes
from smarts.env.wrappers.single_agent import SingleAgent
from smarts.sstudio import build_scenario
from smarts.zoo.agent_spec import AgentSpec
logging.basicConfig(level=logging.INFO)
class ChaseViaPointsAgent(Agent):
def act(self, obs: Observation):
if (
len(obs.via_data.near_via_points) < 1
or obs.ego_vehicle_state.road_id != obs.via_data.near_via_points[0].road_id
):
return (obs.waypoint_paths[0][0].speed_limit, 0)
nearest = obs.via_data.near_via_points[0]
if nearest.lane_index == obs.ego_vehicle_state.lane_index:
return (nearest.required_speed, 0)
return (
nearest.required_speed,
1 if nearest.lane_index > obs.ego_vehicle_state.lane_index else -1,
)
def main(scenarios, headless, num_episodes, max_episode_steps=None):
agent_spec = AgentSpec(
interface=AgentInterface.from_type(
AgentType.LanerWithSpeed, max_episode_steps=max_episode_steps
),
agent_builder=ChaseViaPointsAgent,
)
env = gym.make(
"smarts.env:hiway-v0",
scenarios=scenarios,
agent_specs={"SingleAgent": agent_spec},
headless=headless,
sumo_headless=True,
)
# Convert `env.step()` and `env.reset()` from multi-agent interface to
# single-agent interface.
env = SingleAgent(env=env)
for episode in episodes(n=num_episodes):
agent = agent_spec.build_agent()
observation = env.reset()
episode.record_scenario(env.scenario_log)
done = False
while not done:
agent_action = agent.act(observation)
observation, reward, done, info = env.step(agent_action)
episode.record_step(observation, reward, done, info)
env.close()
if __name__ == "__main__":
parser = default_argument_parser("single-agent-example")
args = parser.parse_args()
if not args.scenarios:
args.scenarios = [
str(pathlib.Path(__file__).absolute().parents[1] / "scenarios" / "loop")
]
build_scenario(args.scenarios)
main(
scenarios=args.scenarios,
headless=args.headless,
num_episodes=args.episodes,
)