From f266a6bf354fb78658d03b557fa3cef186b128a8 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Thu, 9 Aug 2018 23:35:07 -0700 Subject: [PATCH 1/4] cleaned up examples folder --- examples/rllab/bottlenecks.py | 138 ---------------------- examples/rllab/cooperative_merge.py | 36 ++++-- examples/rllab/figure_eight.py | 2 +- examples/rllib/cooperative_merge.py | 18 +-- examples/rllib/stabilizing_highway.py | 2 +- examples/rllib/velocity_bottleneck.py | 3 +- examples/sumo/bay_bridge_toll.py | 2 + examples/sumo/two_loops_merge_straight.py | 80 ------------- flow/envs/__init__.py | 4 +- flow/envs/loop/loop_merges.py | 2 +- tests/fast_tests/test_examples.py | 7 +- 11 files changed, 46 insertions(+), 248 deletions(-) delete mode 100644 examples/rllab/bottlenecks.py delete mode 100755 examples/sumo/two_loops_merge_straight.py diff --git a/examples/rllab/bottlenecks.py b/examples/rllab/bottlenecks.py deleted file mode 100644 index e6c0ab91..00000000 --- a/examples/rllab/bottlenecks.py +++ /dev/null @@ -1,138 +0,0 @@ -""" -(description) -""" - -from flow.core.params import SumoParams, EnvParams, NetParams, InitialConfig, \ - InFlows -from flow.core.vehicles import Vehicles -from flow.core.traffic_lights import TrafficLights - -from flow.scenarios.bottleneck.gen import BottleneckGenerator -from flow.scenarios.bottleneck.scenario import BottleneckScenario -from flow.controllers import RLController, SumoLaneChangeController, \ - ContinuousRouter -from flow.core.params import SumoLaneChangeParams - -from rllab.envs.gym_env import GymEnv -from rllab.envs.normalized_env import normalize -from rllab.misc.instrument import run_experiment_lite -from rllab.algos.trpo import TRPO -from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline -from rllab.policies.gaussian_mlp_policy import GaussianMLPPolicy - -SCALING = 1 -DISABLE_TB = True -DISABLE_RAMP_METER = True -FLOW_RATE = 1500 * SCALING # inflow rate - -sumo_params = SumoParams(sim_step=0.5, sumo_binary="sumo") - -vehicles = Vehicles() - -vehicles.add(veh_id="rl", - acceleration_controller=(RLController, {}), - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - speed_mode=0b11111, - lane_change_mode=1621, - num_vehicles=4*SCALING, - sumo_lc_params=SumoLaneChangeParams()) -vehicles.add(veh_id="human", - speed_mode=0b11111, - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - lane_change_mode=512, - num_vehicles=15*SCALING) -vehicles.add(veh_id="rl2", - acceleration_controller=(RLController, {}), - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - speed_mode=0b11111, - lane_change_mode=1621, - num_vehicles=4*SCALING, - sumo_lc_params=SumoLaneChangeParams()) -vehicles.add(veh_id="human2", - speed_mode=0b11111, - lane_change_mode=512, - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - num_vehicles=15*SCALING) - -additional_env_params = {"target_velocity": 50, "num_steps": 150, - "disable_tb": True, "disable_ramp_metering": True, - "add_rl_if_exit": True} -env_params = EnvParams(additional_params=additional_env_params) - -inflow = InFlows() -inflow.add(veh_type="human", edge="1", vehsPerHour=FLOW_RATE, - departLane="random", departSpeed=10) - -traffic_lights = TrafficLights() -if not DISABLE_TB: - traffic_lights.add(node_id="2") -if not DISABLE_RAMP_METER: - traffic_lights.add(node_id="3") - -additional_net_params = {"scaling": SCALING} -net_params = NetParams(in_flows=inflow, - no_internal_links=False, - additional_params=additional_net_params) - -initial_config = InitialConfig(spacing="uniform", min_gap=5, - lanes_distribution=float("inf"), - edges_distribution=["2", "3", "4", "5"]) - -scenario = BottleneckScenario(name="bay_bridge_toll", - generator_class=BottleneckGenerator, - vehicles=vehicles, - net_params=net_params, - initial_config=initial_config, - traffic_lights=traffic_lights) - - -def run_task(*_): - env_name = "BottleneckEnv" - pass_params = (env_name, sumo_params, vehicles, env_params, - net_params, initial_config, scenario) - - env = GymEnv(env_name, record_video=False, register_params=pass_params) - horizon = env.horizon - env = normalize(env) - - policy = GaussianMLPPolicy( - env_spec=env.spec, - hidden_sizes=(100, 50, 25) - ) - - baseline = LinearFeatureBaseline(env_spec=env.spec) - - algo = TRPO( - env=env, - policy=policy, - baseline=baseline, - batch_size=20000, - max_path_length=horizon, - # whole_paths=True, - n_itr=400, - discount=0.995, - # step_size=0.01, - ) - algo.train() - - -exp_tag = "BottleNeckVerySmall" # experiment prefix - -for seed in [1]: # , 1, 5, 10, 73]: - run_experiment_lite( - run_task, - # Number of parallel workers for sampling - n_parallel=4, - # Only keep the snapshot parameters for the last iteration - snapshot_mode="all", - # Specifies the seed for the experiment. If this is not provided, a - # random seed will be used - seed=seed, - mode="local", - exp_prefix=exp_tag, - # plot=True, - ) diff --git a/examples/rllab/cooperative_merge.py b/examples/rllab/cooperative_merge.py index 02f214ad..ec185b30 100644 --- a/examples/rllab/cooperative_merge.py +++ b/examples/rllab/cooperative_merge.py @@ -17,13 +17,14 @@ SumoCarFollowingParams, SumoLaneChangeParams from flow.core.vehicles import Vehicles from flow.scenarios.loop_merge.gen import TwoLoopOneMergingGenerator -from flow.scenarios.loop_merge.scenario import TwoLoopsOneMergingScenario +from flow.scenarios.loop_merge.scenario import TwoLoopsOneMergingScenario, \ + ADDITIONAL_NET_PARAMS HORIZON = 300 def run_task(*_): - sumo_params = SumoParams(sim_step=0.2, sumo_binary="sumo") + sumo_params = SumoParams(sim_step=0.2, sumo_binary="sumo-gui") # note that the vehicles are added sequentially by the generator, # so place the merging vehicles after the vehicles in the ring @@ -65,14 +66,23 @@ def run_task(*_): ), sumo_lc_params=SumoLaneChangeParams()) - additional_env_params = {"target_velocity": 20, "max-deacc": -1.5, - "max-acc": 1} - env_params = EnvParams(horizon=HORIZON, - additional_params=additional_env_params) + env_params = EnvParams( + horizon=HORIZON, + additional_params={ + "max_accel": 3, + "max_decel": 3, + "target_velocity": 10, + "n_preceding": 2, + "n_following": 2, + "n_merging_in": 2, + } + ) - additional_net_params = {"ring_radius": 50, "lanes": 1, - "lane_length": 75, "speed_limit": 30, - "resolution": 40} + additional_net_params = ADDITIONAL_NET_PARAMS.copy() + additional_net_params["ring_radius"] = 50 + additional_net_params["inner_lanes"] = 1 + additional_net_params["outer_lanes"] = 1 + additional_net_params["lane_length"] = 75 net_params = NetParams( no_internal_links=False, additional_params=additional_net_params @@ -80,7 +90,7 @@ def run_task(*_): initial_config = InitialConfig( x0=50, - spacing="custom", + spacing="uniform", additional_params={"merge_bunching": 0} ) @@ -123,17 +133,17 @@ def run_task(*_): exp_tag = "cooperative_merge_example" # experiment prefix -for seed in [1, 5, 10, 56]: # , 1, 5, 10, 73]: +for seed in [1]: # , 5, 10, 56, 73]: run_experiment_lite( run_task, # Number of parallel workers for sampling - n_parallel=8, + n_parallel=1, # Only keep the snapshot parameters for the last iteration snapshot_mode="all", # Specifies the seed for the experiment. If this is not provided, a # random seed will be used seed=seed, - mode="ec2", + mode="local", # "ec2" exp_prefix=exp_tag, # plot=True, ) diff --git a/examples/rllab/figure_eight.py b/examples/rllab/figure_eight.py index 9d4b3124..10c04cbc 100644 --- a/examples/rllab/figure_eight.py +++ b/examples/rllab/figure_eight.py @@ -19,7 +19,7 @@ def run_task(*_): sumo_params = SumoParams(sim_step=0.1, - sumo_binary="sumo") + sumo_binary="sumo-gui") vehicles = Vehicles() vehicles.add(veh_id="rl", diff --git a/examples/rllib/cooperative_merge.py b/examples/rllib/cooperative_merge.py index f88117a8..1935efec 100644 --- a/examples/rllib/cooperative_merge.py +++ b/examples/rllib/cooperative_merge.py @@ -73,7 +73,7 @@ exp_tag="cooperative_merge", # name of the flow environment the experiment is running on - env_name="TwoLoopsMergeEnv", + env_name="TwoLoopsMergePOEnv", # name of the scenario class the experiment is running on scenario="TwoLoopsOneMergingScenario", @@ -91,9 +91,12 @@ env=EnvParams( horizon=HORIZON, additional_params={ - "target_velocity": 20, - "max_accel": 1, - "max_decel": 1.5, + "max_accel": 3, + "max_decel": 3, + "target_velocity": 10, + "n_preceding": 2, + "n_following": 2, + "n_merging_in": 2, }, ), @@ -103,14 +106,15 @@ no_internal_links=False, additional_params={ "ring_radius": 50, - "lanes": 1, "lane_length": 75, + "inner_lanes": 1, + "outer_lanes": 1, "speed_limit": 30, "resolution": 40, }, ), - # vehicles to be placed in the network at the start of a rollout (see +# vehicles to be placed in the network at the start of a rollout (see # flow.core.vehicles.Vehicles) veh=vehicles, @@ -118,7 +122,7 @@ # reset (see flow.core.params.InitialConfig) initial=InitialConfig( x0=50, - spacing="custom", + spacing="uniform", additional_params={ "merge_bunching": 0, }, diff --git a/examples/rllib/stabilizing_highway.py b/examples/rllib/stabilizing_highway.py index cd114fbe..608e78e2 100644 --- a/examples/rllib/stabilizing_highway.py +++ b/examples/rllib/stabilizing_highway.py @@ -83,7 +83,7 @@ # sumo-related parameters (see flow.core.params.SumoParams) sumo=SumoParams( sim_step=0.2, - sumo_binary="sumo-gui", + sumo_binary="sumo", ), # environment related parameters (see flow.core.params.EnvParams) diff --git a/examples/rllib/velocity_bottleneck.py b/examples/rllib/velocity_bottleneck.py index 83167ed1..0e7d265f 100644 --- a/examples/rllib/velocity_bottleneck.py +++ b/examples/rllib/velocity_bottleneck.py @@ -60,7 +60,8 @@ "reset_inflow": False, "lane_change_duration": 5, "max_accel": 3, - "max_decel": 3 + "max_decel": 3, + "inflow_range": [1000, 2000] } # flow rate diff --git a/examples/sumo/bay_bridge_toll.py b/examples/sumo/bay_bridge_toll.py index 006ce005..c632834b 100644 --- a/examples/sumo/bay_bridge_toll.py +++ b/examples/sumo/bay_bridge_toll.py @@ -21,12 +21,14 @@ def bay_bridge_bottleneck_example(sumo_binary=None, Performs a non-RL simulation of the bottleneck portion of the Oakland-San Francisco Bay Bridge. This consists of the toll booth and sections of the road leading up to it. + Parameters ---------- sumo_binary: bool, optional specifies whether to use sumo's gui during execution use_traffic_lights: bool, optional whether to activate the traffic lights in the scenario + Note ---- Unlike the bay_bridge_example, inflows are always activated here. diff --git a/examples/sumo/two_loops_merge_straight.py b/examples/sumo/two_loops_merge_straight.py deleted file mode 100755 index 18946cfd..00000000 --- a/examples/sumo/two_loops_merge_straight.py +++ /dev/null @@ -1,80 +0,0 @@ -""" -Example of ring road with larger merging ring. -""" - -from flow.controllers import IDMController, SumoLaneChangeController, \ - ContinuousRouter -from flow.core.experiment import SumoExperiment -from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \ - SumoCarFollowingParams, SumoLaneChangeParams -from flow.core.vehicles import Vehicles -from flow.envs.loop.loop_accel import AccelEnv, ADDITIONAL_ENV_PARAMS -from flow.scenarios.loop_merge.scenario import \ - TwoLoopsOneMergingScenario, ADDITIONAL_NET_PARAMS -from flow.scenarios.loop_merge.gen import TwoLoopOneMergingGenerator - - -def two_loops_merge_straight_example(sumo_binary=None): - sumo_params = SumoParams(sim_step=0.1, emission_path="./data/", - sumo_binary="sumo-gui") - - if sumo_binary is not None: - sumo_params.sumo_binary = sumo_binary - - # note that the vehicles are added sequentially by the generator, - # so place the merging vehicles after the vehicles in the ring - vehicles = Vehicles() - vehicles.add(veh_id="idm", - acceleration_controller=(IDMController, {}), - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - num_vehicles=7, - sumo_car_following_params=SumoCarFollowingParams( - minGap=0.0, tau=0.5), - sumo_lc_params=SumoLaneChangeParams()) - vehicles.add(veh_id="merge-idm", - acceleration_controller=(IDMController, {}), - lane_change_controller=(SumoLaneChangeController, {}), - routing_controller=(ContinuousRouter, {}), - num_vehicles=10, - sumo_car_following_params=SumoCarFollowingParams( - minGap=0.01, tau=0.5), - sumo_lc_params=SumoLaneChangeParams()) - - env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) - - additional_net_params = ADDITIONAL_NET_PARAMS.copy() - additional_net_params["ring_radius"] = 50 - additional_net_params["inner_lanes"] = 1 - additional_net_params["outer_lanes"] = 1 - additional_net_params["lane_length"] = 75 - net_params = NetParams( - no_internal_links=False, - additional_params=additional_net_params - ) - - initial_config = InitialConfig( - x0=50, - spacing="uniform", - additional_params={"merge_bunching": 0} - ) - - scenario = TwoLoopsOneMergingScenario( - name="two-loop-one-merging", - generator_class=TwoLoopOneMergingGenerator, - vehicles=vehicles, - net_params=net_params, - initial_config=initial_config - ) - - env = AccelEnv(env_params, sumo_params, scenario) - - return SumoExperiment(env, scenario) - - -if __name__ == "__main__": - # import the experiment variable - exp = two_loops_merge_straight_example() - - # run for a set number of rollouts / time steps - exp.run(1, 1500, convert_to_csv=True) diff --git a/flow/envs/__init__.py b/flow/envs/__init__.py index 31e74864..96de0f63 100755 --- a/flow/envs/__init__.py +++ b/flow/envs/__init__.py @@ -7,7 +7,7 @@ from flow.envs.loop.lane_changing import LaneChangeAccelEnv, \ LaneChangeAccelPOEnv from flow.envs.loop.loop_accel import AccelEnv -from flow.envs.loop.loop_merges import TwoLoopsMergeEnv +from flow.envs.loop.loop_merges import TwoLoopsMergePOEnv from flow.envs.loop.wave_attenuation import WaveAttenuationEnv, \ WaveAttenuationPOEnv from flow.envs.merge import WaveAttenuationMergePOEnv @@ -15,7 +15,7 @@ __all__ = ["Env", "AccelEnv", "LaneChangeAccelEnv", "LaneChangeAccelPOEnv", "GreenWaveTestEnv", "GreenWaveTestEnv", "WaveAttenuationMergePOEnv", - "TwoLoopsMergeEnv", "BottleneckEnv", "BottleNeckAccelEnv", + "TwoLoopsMergePOEnv", "BottleneckEnv", "BottleNeckAccelEnv", "WaveAttenuationEnv", "WaveAttenuationPOEnv", "TrafficLightGridEnv", "PO_TrafficLightGridEnv", "DesiredVelocityEnv", "TestEnv", "BayBridgeEnv"] diff --git a/flow/envs/loop/loop_merges.py b/flow/envs/loop/loop_merges.py index f34b4c3f..0821efc9 100755 --- a/flow/envs/loop/loop_merges.py +++ b/flow/envs/loop/loop_merges.py @@ -22,7 +22,7 @@ } -class TwoLoopsMergeEnv(Env): +class TwoLoopsMergePOEnv(Env): """Environment for training cooperative merging behavior in a closed loop merge scenario. diff --git a/tests/fast_tests/test_examples.py b/tests/fast_tests/test_examples.py index c6465c6d..22c6e9eb 100644 --- a/tests/fast_tests/test_examples.py +++ b/tests/fast_tests/test_examples.py @@ -3,8 +3,7 @@ from examples.sumo.figure_eight import figure_eight_example from examples.sumo.highway import highway_example from examples.sumo.sugiyama import sugiyama_example -from examples.sumo.two_loops_merge_straight import \ - two_loops_merge_straight_example +from examples.sumo.loop_merge import loop_merge_example from examples.sumo.grid import grid_example from examples.sumo.bottleneck import bottleneck_example from examples.sumo.merge import merge_example @@ -80,12 +79,12 @@ def test_sugiyama(self): # run the experiment for a few time steps to ensure it doesn't fail exp.run(1, 5) - def test_two_loops_merge_straight(self): + def test_loop_merge(self): """ Verifies that examples/sumo/two_loops_merge_straight.py is working """ # import the experiment variable from the example - exp = two_loops_merge_straight_example(sumo_binary="sumo") + exp = loop_merge_example(sumo_binary="sumo") # run the experiment for a few time steps to ensure it doesn't fail exp.run(1, 5) From 1191fcd190668f9e71f1c52f700e65d493afa4b6 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Thu, 9 Aug 2018 23:52:28 -0700 Subject: [PATCH 2/4] test fixed --- tests/fast_tests/test_two_loops_one_merging.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/fast_tests/test_two_loops_one_merging.py b/tests/fast_tests/test_two_loops_one_merging.py index c96b6e26..10d5a73d 100644 --- a/tests/fast_tests/test_two_loops_one_merging.py +++ b/tests/fast_tests/test_two_loops_one_merging.py @@ -5,7 +5,8 @@ from flow.core.experiment import SumoExperiment from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams from flow.core.vehicles import Vehicles -from flow.envs.loop.loop_merges import TwoLoopsMergeEnv, ADDITIONAL_ENV_PARAMS +from flow.envs.loop.loop_merges import TwoLoopsMergePOEnv, \ + ADDITIONAL_ENV_PARAMS from flow.scenarios.loop_merge.gen import TwoLoopOneMergingGenerator from flow.scenarios.loop_merge.scenario import TwoLoopsOneMergingScenario @@ -53,7 +54,7 @@ def two_loops_one_merging_exp_setup(vehicles=None): "loop-merges", TwoLoopOneMergingGenerator, vehicles, net_params, initial_config=initial_config) - env = TwoLoopsMergeEnv(env_params, sumo_params, scenario) + env = TwoLoopsMergePOEnv(env_params, sumo_params, scenario) return env, scenario From d0fd5cdae8c4f4f6a0c5d2a3d5d9a5b43e1cfd42 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Thu, 9 Aug 2018 23:53:35 -0700 Subject: [PATCH 3/4] added missing example --- examples/sumo/loop_merge.py | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 examples/sumo/loop_merge.py diff --git a/examples/sumo/loop_merge.py b/examples/sumo/loop_merge.py new file mode 100755 index 00000000..4053c958 --- /dev/null +++ b/examples/sumo/loop_merge.py @@ -0,0 +1,83 @@ +""" +Example of ring road with larger merging ring. +""" + +from flow.controllers import IDMController, SumoLaneChangeController, \ + ContinuousRouter +from flow.core.experiment import SumoExperiment +from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \ + SumoCarFollowingParams, SumoLaneChangeParams +from flow.core.vehicles import Vehicles +from flow.envs.loop.loop_accel import AccelEnv, ADDITIONAL_ENV_PARAMS +from flow.scenarios.loop_merge.scenario import \ + TwoLoopsOneMergingScenario, ADDITIONAL_NET_PARAMS +from flow.scenarios.loop_merge.gen import TwoLoopOneMergingGenerator + + +def loop_merge_example(sumo_binary=None): + sumo_params = SumoParams(sim_step=0.1, + emission_path="./data/", + sumo_binary="sumo-gui") + + if sumo_binary is not None: + sumo_params.sumo_binary = sumo_binary + + # note that the vehicles are added sequentially by the generator, + # so place the merging vehicles after the vehicles in the ring + vehicles = Vehicles() + vehicles.add(veh_id="idm", + acceleration_controller=(IDMController, {}), + lane_change_controller=(SumoLaneChangeController, {}), + routing_controller=(ContinuousRouter, {}), + num_vehicles=7, + speed_mode="no_collide", + sumo_car_following_params=SumoCarFollowingParams( + minGap=0.0, tau=0.5), + sumo_lc_params=SumoLaneChangeParams()) + vehicles.add(veh_id="merge-idm", + acceleration_controller=(IDMController, {}), + lane_change_controller=(SumoLaneChangeController, {}), + routing_controller=(ContinuousRouter, {}), + num_vehicles=10, + speed_mode="no_collide", + sumo_car_following_params=SumoCarFollowingParams( + minGap=0.01, tau=0.5), + sumo_lc_params=SumoLaneChangeParams()) + + env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) + + additional_net_params = ADDITIONAL_NET_PARAMS.copy() + additional_net_params["ring_radius"] = 50 + additional_net_params["inner_lanes"] = 1 + additional_net_params["outer_lanes"] = 1 + additional_net_params["lane_length"] = 75 + net_params = NetParams( + no_internal_links=False, + additional_params=additional_net_params + ) + + initial_config = InitialConfig( + x0=50, + spacing="uniform", + additional_params={"merge_bunching": 0} + ) + + scenario = TwoLoopsOneMergingScenario( + name="two-loop-one-merging", + generator_class=TwoLoopOneMergingGenerator, + vehicles=vehicles, + net_params=net_params, + initial_config=initial_config + ) + + env = AccelEnv(env_params, sumo_params, scenario) + + return SumoExperiment(env, scenario) + + +if __name__ == "__main__": + # import the experiment variable + exp = loop_merge_example() + + # run for a set number of rollouts / time steps + exp.run(1, 1500, convert_to_csv=True) From 041fc24719f0a7c3158c87808e12e5d57f464e00 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Wed, 15 Aug 2018 15:34:48 -0700 Subject: [PATCH 4/4] PR fix --- flow/envs/loop/loop_merges.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flow/envs/loop/loop_merges.py b/flow/envs/loop/loop_merges.py index 0821efc9..5a98196a 100755 --- a/flow/envs/loop/loop_merges.py +++ b/flow/envs/loop/loop_merges.py @@ -23,8 +23,8 @@ class TwoLoopsMergePOEnv(Env): - """Environment for training cooperative merging behavior in a closed loop - merge scenario. + """Environment for training cooperative merging behavior in a partially + observable closed loop merge scenario. WARNING: only supports 1 RL vehicle