Skip to content

Commit

Permalink
Tests envs (#235)
Browse files Browse the repository at this point in the history
* testing lane change env is working

* empty tests for environments

* fixed TestEnv and added tests

* fixed 0 initial vehicles bug

* added tests for keyerrors

* pep8

* bug fixes

* more tests to envs

* pep8

* converted tests into utility methods
  • Loading branch information
AboudyKreidieh authored and eugenevinitsky committed Nov 18, 2018
1 parent 257420b commit dcef2be
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 29 deletions.
5 changes: 3 additions & 2 deletions examples/sumo/highway.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from flow.core.params import SumoParams, EnvParams, \
NetParams, InitialConfig, InFlows
from flow.core.vehicles import Vehicles
from flow.envs.loop.loop_accel import AccelEnv, ADDITIONAL_ENV_PARAMS
from flow.envs.loop.lane_changing import LaneChangeAccelEnv, \
ADDITIONAL_ENV_PARAMS
from flow.scenarios.highway import HighwayScenario, ADDITIONAL_NET_PARAMS


Expand Down Expand Up @@ -67,7 +68,7 @@ def highway_example(render=None):
net_params=net_params,
initial_config=initial_config)

env = AccelEnv(env_params, sumo_params, scenario)
env = LaneChangeAccelEnv(env_params, sumo_params, scenario)

return SumoExperiment(env, scenario)

Expand Down
5 changes: 2 additions & 3 deletions flow/envs/loop/lane_changing.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,13 @@ def get_state(self):
lane_headways[j] /= max_length
vel_in_front[j] = self.vehicles.get_speed(lane_leader) \
/ max_speed
self.visible.extend([lane_leader])
for j, lane_follower in enumerate(lane_followers):
if lane_follower != '':
lane_headways[j] /= max_length
vel_behind[j] = self.vehicles.get_speed(lane_follower) \
/ max_speed

self.visible.extend(lane_leaders)
self.visible.extend(lane_followers)
self.visible.extend([lane_follower])

# add the headways, tailways, and speed for all lane leaders
# and followers
Expand Down
23 changes: 7 additions & 16 deletions flow/envs/loop/loop_accel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from flow.core import rewards

from gym.spaces.box import Box
from gym.spaces.tuple_space import Tuple

import numpy as np

Expand Down Expand Up @@ -68,18 +67,11 @@ def action_space(self):
@property
def observation_space(self):
"""See class definition."""
self.obs_var_labels = ["Velocity", "Absolute_pos"]
speed = Box(
return Box(
low=0,
high=1,
shape=(self.vehicles.num_vehicles, ),
dtype=np.float32)
pos = Box(
low=0.,
high=1,
shape=(self.vehicles.num_vehicles, ),
shape=(2 * self.vehicles.num_vehicles, ),
dtype=np.float32)
return Tuple((speed, pos))

def _apply_rl_actions(self, rl_actions):
"""See class definition."""
Expand All @@ -98,13 +90,12 @@ def compute_reward(self, rl_actions, **kwargs):

def get_state(self, **kwargs):
"""See class definition."""
# speed normalizer
max_speed = self.scenario.max_speed
speed = [self.vehicles.get_speed(veh_id) / self.scenario.max_speed
for veh_id in self.sorted_ids]
pos = [self.get_x_by_id(veh_id) / self.scenario.length
for veh_id in self.sorted_ids]

return np.array([[
self.vehicles.get_speed(veh_id) / max_speed,
self.get_x_by_id(veh_id) / self.scenario.length
] for veh_id in self.sorted_ids])
return np.array(speed + pos)

def additional_command(self):
"""Define which vehicles are observed for visualization purposes."""
Expand Down
15 changes: 7 additions & 8 deletions flow/envs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ class and returns a real number.

@property
def action_space(self):
"""See class definition."""
return Box(low=0, high=0, shape=(1,), dtype=np.float32)
return Box(low=0, high=0, shape=(0,), dtype=np.float32)

@property
def observation_space(self):
"""See class definition."""
return Box(low=0, high=0, shape=(1,), dtype=np.float32)
return Box(low=0, high=0, shape=(0,), dtype=np.float32)

def _apply_rl_actions(self, rl_actions):
"""See class definition."""
return

def compute_reward(self, rl_actions, **kwargs):
"""See class definition."""
return 0
if "reward_fn" in self.env_params.additional_params:
return self.env_params.additional_params["reward_fn"](self)
else:
return 0

def get_state(self, **kwargs):
"""See class definition."""
return np.array([0])
return np.array([])
5 changes: 5 additions & 0 deletions flow/scenarios/base_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ def gen_even_start_pos(self, initial_config, num_vehicles, **kwargs):
available_edges, initial_config) = \
self._get_start_pos_util(initial_config, num_vehicles, **kwargs)

# return an empty list of starting positions and lanes if there are no
# vehicles to be placed
if num_vehicles == 0:
return [], []

increment = available_length / num_vehicles

# if not all lanes are equal, then we must ensure that vehicles are in
Expand Down
Loading

0 comments on commit dcef2be

Please sign in to comment.