forked from huawei-noah/SMARTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaner.py
More file actions
84 lines (67 loc) · 2.24 KB
/
laner.py
File metadata and controls
84 lines (67 loc) · 2.24 KB
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
import random
import sys
from pathlib import Path
import gym
sys.path.insert(0, str(Path(__file__).parents[1]))
from tools.argument_parser import default_argument_parser
from cli.studio import build_scenarios
from smarts.core.agent import Agent
from smarts.core.agent_interface import AgentInterface, AgentType
from smarts.core.utils.episodes import episodes
from smarts.zoo.agent_spec import AgentSpec
N_AGENTS = 4
AGENT_IDS = ["Agent %i" % i for i in range(N_AGENTS)]
class KeepLaneAgent(Agent):
def act(self, obs):
val = ["keep_lane", "slow_down", "change_lane_left", "change_lane_right"]
return random.choice(val)
def main(scenarios, headless, num_episodes, max_episode_steps=None):
agent_specs = {
agent_id: AgentSpec(
interface=AgentInterface.from_type(
AgentType.Laner, max_episode_steps=max_episode_steps
),
agent_builder=KeepLaneAgent,
)
for agent_id in AGENT_IDS
}
env = gym.make(
"smarts.env:hiway-v0",
scenarios=scenarios,
agent_specs=agent_specs,
headless=headless,
sumo_headless=True,
)
for episode in episodes(n=num_episodes):
agents = {
agent_id: agent_spec.build_agent()
for agent_id, agent_spec in agent_specs.items()
}
observations = env.reset()
episode.record_scenario(env.scenario_log)
dones = {"__all__": False}
while not dones["__all__"]:
actions = {
agent_id: agents[agent_id].act(agent_obs)
for agent_id, agent_obs in observations.items()
}
observations, rewards, dones, infos = env.step(actions)
episode.record_step(observations, rewards, dones, infos)
env.close()
if __name__ == "__main__":
parser = default_argument_parser("laner")
args = parser.parse_args()
if not args.scenarios:
args.scenarios = [
str(Path(__file__).absolute().parents[2] / "scenarios" / "sumo" / "loop")
]
build_scenarios(
clean=False,
scenarios=args.scenarios,
seed=42,
)
main(
scenarios=args.scenarios,
headless=args.headless,
num_episodes=args.episodes,
)