forked from huawei-noah/SMARTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_agent.py
68 lines (54 loc) · 1.84 KB
/
multi_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
import gym
from examples.argument_parser import default_argument_parser
from smarts.core.agent import Agent, AgentSpec
from smarts.core.agent_interface import AgentInterface, AgentType
from smarts.core.utils.episodes import episodes
N_AGENTS = 4
AGENT_IDS = ["Agent %i" % i for i in range(N_AGENTS)]
class KeepLaneAgent(Agent):
def act(self, obs):
return "keep_lane"
def main(scenarios, sim_name, headless, num_episodes, seed, 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,
sim_name=sim_name,
headless=headless,
seed=seed,
)
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("multi-agent-example")
args = parser.parse_args()
main(
scenarios=args.scenarios,
sim_name=args.sim_name,
headless=args.headless,
num_episodes=args.episodes,
seed=args.seed,
)