Skip to content

Commit

Permalink
Add fixed_time option to pettingzoo env
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAlegre committed Apr 23, 2024
1 parent e8ab788 commit 0e741e3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions sumo_rl/environment/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def step(self, action: Union[dict, int]):
If single_agent is True, action is an int, otherwise it expects a dict with keys corresponding to traffic signal ids.
"""
# No action, follow fixed TL defined in self.phases
if action is None or action == {}:
if self.fixed_ts or action is None or action == {}:
for _ in range(self.delta_time):
self._sumo_step()
else:
Expand Down Expand Up @@ -364,15 +364,15 @@ def _compute_info(self):

def _compute_observations(self):
self.observations.update(
{ts: self.traffic_signals[ts].compute_observation() for ts in self.ts_ids if self.traffic_signals[ts].time_to_act}
{ts: self.traffic_signals[ts].compute_observation() for ts in self.ts_ids if self.traffic_signals[ts].time_to_act or self.fixed_ts}
)
return {ts: self.observations[ts].copy() for ts in self.observations.keys() if self.traffic_signals[ts].time_to_act}
return {ts: self.observations[ts].copy() for ts in self.observations.keys() if self.traffic_signals[ts].time_to_act or self.fixed_ts}

def _compute_rewards(self):
self.rewards.update(
{ts: self.traffic_signals[ts].compute_reward() for ts in self.ts_ids if self.traffic_signals[ts].time_to_act}
{ts: self.traffic_signals[ts].compute_reward() for ts in self.ts_ids if self.traffic_signals[ts].time_to_act or self.fixed_ts}
)
return {ts: self.rewards[ts] for ts in self.rewards.keys() if self.traffic_signals[ts].time_to_act}
return {ts: self.rewards[ts] for ts in self.rewards.keys() if self.traffic_signals[ts].time_to_act or self.fixed_ts}

@property
def observation_space(self):
Expand Down Expand Up @@ -580,10 +580,16 @@ def step(self, action):
"It is currently {}".format(agent, self.action_spaces[agent].n, action)
)

self.env._apply_actions({agent: action})
if not self.env.fixed_ts:
self.env._apply_actions({agent: action})

if self._agent_selector.is_last():
self.env._run_steps()
if not self.env.fixed_ts:
self.env._run_steps()
else:
for _ in range(self.env.delta_time):
self.env._sumo_step()

self.env._compute_observations()
self.rewards = self.env._compute_rewards()
self.compute_info()
Expand Down

0 comments on commit 0e741e3

Please sign in to comment.