-
Notifications
You must be signed in to change notification settings - Fork 4
Change _apply_rl_actions in green wave and added tests #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,10 @@ | |
| from flow.envs.base_env import Env | ||
|
|
||
| ADDITIONAL_ENV_PARAMS = { | ||
| # minimum switch time for each traffic light (in seconds) | ||
| "switch_time": 2.0, | ||
| # minimum switch time for a traffic light in the yellow phase (in seconds) | ||
| "min_yellow_time": 2.0, | ||
| # minimum switch time for a traffic light in the green phase (in seconds) | ||
| "min_green_time": 8.0, | ||
| # whether the traffic lights should be actuated by sumo or RL | ||
| # options are "controlled" and "actuated" | ||
| "tl_type": "controlled", | ||
|
|
@@ -76,11 +78,13 @@ def __init__(self, env_params, sumo_params, scenario): | |
| # keeps track of the last time the light was allowed to change. | ||
| self.last_change = np.zeros((self.rows * self.cols, 3)) | ||
|
|
||
| # when this hits min_switch_time we change from yellow to red | ||
| # When this hits min_yellow_time we can change from yellow to red. | ||
| # When this hits min_green_time, we can change from green to yellow. | ||
| # the second column indicates the direction that is currently being | ||
| # allowed to flow. 0 is flowing top to bottom, 1 is left to right | ||
| # For third column, 0 signifies yellow and 1 green or red | ||
| self.min_switch_time = env_params.additional_params["switch_time"] | ||
| self.min_yellow_time = env_params.additional_params["min_yellow_time"] | ||
| self.min_green_time = env_params.additional_params["min_green_time"] | ||
|
|
||
| if self.tl_type != "actuated": | ||
| for i in range(self.rows * self.cols): | ||
|
|
@@ -156,7 +160,7 @@ def _apply_rl_actions(self, rl_actions): | |
| # should switch to red | ||
| if self.last_change[i, 2] == 0: # currently yellow | ||
| self.last_change[i, 0] += self.sim_step | ||
| if self.last_change[i, 0] >= self.min_switch_time: | ||
| if self.last_change[i, 0] >= self.min_yellow_time: | ||
| if self.last_change[i, 1] == 0: | ||
| self.traffic_lights.set_state( | ||
| node_id='center{}'.format(i), | ||
|
|
@@ -165,9 +169,11 @@ def _apply_rl_actions(self, rl_actions): | |
| self.traffic_lights.set_state( | ||
| node_id='center{}'.format(i), | ||
| state='rrrGGGrrrGGG', env=self) | ||
| self.last_change[i, 0] = 0.0 | ||
| self.last_change[i, 2] = 1 | ||
| else: | ||
| if action: | ||
| else: # currently green | ||
| self.last_change[i, 0] += self.sim_step | ||
| if self.last_change[i, 0] >= self.min_green_time and rl_mask[i]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. self.last_change used to only keep track of last change since a yellow phase began. Now it also keeps track of time since a green phase began. So now, last_change[i, 0] resets when the green phase begins (it didn't use to) and increments during the green phase as well. |
||
| if self.last_change[i, 1] == 0: | ||
| self.traffic_lights.set_state( | ||
| node_id='center{}'.format(i), | ||
|
|
@@ -179,6 +185,7 @@ def _apply_rl_actions(self, rl_actions): | |
| self.last_change[i, 0] = 0.0 | ||
| self.last_change[i, 1] = not self.last_change[i, 1] | ||
| self.last_change[i, 2] = 0 | ||
|
|
||
|
|
||
| def compute_reward(self, state, rl_actions, **kwargs): | ||
| return rewards.penalize_tl_changes(rl_actions >= 0.5, gain=1.0) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| from flow.core.experiment import SumoExperiment | ||
| from flow.controllers.routing_controllers import GridRouter | ||
| from flow.controllers.car_following_models import IDMController | ||
| from flow.envs.green_wave_env import PO_TrafficLightGridEnv | ||
|
|
||
| os.environ["TEST_FLAG"] = "True" | ||
|
|
||
|
|
@@ -194,6 +195,45 @@ def test_k_closest(self): | |
| self.assertTrue(self.env.vehicles.get_edge(veh_id) in c0_edges) | ||
|
|
||
|
|
||
| def test_min_switch(self): | ||
| # FOR THE PURPOSES OF THIS TEST, never set min switch to be < 2 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this test enforce the constraint of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setup script it uses has min_switch >=2, is that what you mean? |
||
|
|
||
| # reset the environment | ||
| self.env.reset() | ||
|
|
||
| # Set up RL environment | ||
| self.env = PO_TrafficLightGridEnv(self.env.env_params, | ||
| self.env.sumo_params, | ||
| self.env.scenario) | ||
|
|
||
| for i in range(len(self.env.last_change)): | ||
| self.assertEqual(self.env.last_change[i, 2], 1) | ||
|
|
||
| # Run one step and make sure the count is incrementing | ||
| self.env.step([1] * self.env.num_traffic_lights) | ||
| for i in range(len(self.env.last_change)): | ||
| self.assertEqual(self.env.last_change[i, 0], 1) | ||
| self.assertEqual(self.env.last_change[i, 1], 0) | ||
| self.assertEqual(self.env.last_change[i, 2], 1) | ||
|
|
||
| # Run until green switches to yellow | ||
| for i in range(int(self.env.min_green_time - 1.)): | ||
| self.env.step([1] * self.env.num_traffic_lights) | ||
| for i in range(len(self.env.last_change)): | ||
| self.assertEqual(self.env.last_change[i, 0], 0) | ||
| self.assertEqual(self.env.last_change[i, 1], 1) | ||
| self.assertEqual(self.env.last_change[i, 2], 0) | ||
|
|
||
| # Run until yellow switches to green | ||
| for i in range(int(self.env.min_yellow_time)): | ||
| self.env.step([1] * self.env.num_traffic_lights) | ||
|
|
||
| for i in range(len(self.env.last_change)): | ||
| self.assertEqual(self.env.last_change[i, 0], 0) | ||
| self.assertEqual(self.env.last_change[i, 1], 1) | ||
| self.assertEqual(self.env.last_change[i, 2], 1) | ||
|
|
||
|
|
||
| class TestItRuns(unittest.TestCase): | ||
| """ | ||
| Tests the set_state function | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've got merge conflicts in this file.