From 01994fc80e0ca298b2998943afba65d2088350e7 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Sat, 20 Jun 2020 17:45:43 -0700 Subject: [PATCH 1/8] fixed h-baselines bug --- examples/train.py | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/examples/train.py b/examples/train.py index 1b2f22476..5f8edbb22 100644 --- a/examples/train.py +++ b/examples/train.py @@ -213,13 +213,10 @@ def train_rllib(submodule, flags): run_experiments({flow_params["exp_tag"]: exp_config}) -def train_h_baselines(flow_params, args, multiagent): +def train_h_baselines(env_name, args, multiagent): """Train policies using SAC and TD3 with h-baselines.""" from hbaselines.algorithms import OffPolicyRLAlgorithm from hbaselines.utils.train import parse_options, get_hyperparameters - from hbaselines.envs.mixed_autonomy import FlowEnv - - flow_params = deepcopy(flow_params) # Get the command-line arguments that are relevant here args = parse_options(description="", example_usage="", args=args) @@ -227,31 +224,6 @@ def train_h_baselines(flow_params, args, multiagent): # the base directory that the logged data will be stored in base_dir = "training_data" - # Create the training environment. - env = FlowEnv( - flow_params, - multiagent=multiagent, - shared=args.shared, - maddpg=args.maddpg, - render=args.render, - version=0 - ) - - # Create the evaluation environment. - if args.evaluate: - eval_flow_params = deepcopy(flow_params) - eval_flow_params['env'].evaluate = True - eval_env = FlowEnv( - eval_flow_params, - multiagent=multiagent, - shared=args.shared, - maddpg=args.maddpg, - render=args.render_eval, - version=1 - ) - else: - eval_env = None - for i in range(args.n_training): # value of the next seed seed = args.seed + i @@ -299,8 +271,8 @@ def train_h_baselines(flow_params, args, multiagent): # Create the algorithm object. alg = OffPolicyRLAlgorithm( policy=policy, - env=env, - eval_env=eval_env, + env="flow:{}".format(env_name), + eval_env="flow:{}".format(env_name) if args.evaluate else None, **hp ) @@ -393,8 +365,7 @@ def main(args): elif flags.rl_trainer.lower() == "stable-baselines": train_stable_baselines(submodule, flags) elif flags.rl_trainer.lower() == "h-baselines": - flow_params = submodule.flow_params - train_h_baselines(flow_params, args, multiagent) + train_h_baselines(flags.exp_config, args, multiagent) else: raise ValueError("rl_trainer should be either 'rllib', 'h-baselines', " "or 'stable-baselines'.") From ea308d61353801c686eef3064caff3a3edbfa1a8 Mon Sep 17 00:00:00 2001 From: AboudyKreidieh Date: Sat, 20 Jun 2020 18:13:08 -0700 Subject: [PATCH 2/8] potential bug fix --- tests/fast_tests/test_examples.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/fast_tests/test_examples.py b/tests/fast_tests/test_examples.py index 0b385f28a..b5faf6517 100644 --- a/tests/fast_tests/test_examples.py +++ b/tests/fast_tests/test_examples.py @@ -229,11 +229,11 @@ class TestHBaselineExamples(unittest.TestCase): confirming that it runs. """ @staticmethod - def run_exp(flow_params, multiagent): + def run_exp(env_name, multiagent): train_h_baselines( - flow_params=flow_params, + env_name=env_name, args=[ - flow_params["env_name"].__name__, + env_name, "--initial_exploration_steps", "1", "--total_steps", "10" ], @@ -241,10 +241,10 @@ def run_exp(flow_params, multiagent): ) def test_singleagent_ring(self): - self.run_exp(singleagent_ring.copy(), multiagent=False) + self.run_exp("singleagent_ring", multiagent=False) def test_multiagent_ring(self): - self.run_exp(multiagent_ring.copy(), multiagent=True) + self.run_exp("multiagent_ring", multiagent=True) class TestRllibExamples(unittest.TestCase): From 26d9d29da2ded14892728015cf3e80b9e49d5450 Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Sun, 21 Jun 2020 22:03:42 -0700 Subject: [PATCH 3/8] add 210 edgestarts for backwards compatibility --- flow/visualize/time_space_diagram.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index 3c7ab8b21..c131d6b91 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -360,6 +360,22 @@ def _get_abs_pos(df, params): } elif params['network'] == HighwayNetwork: return df['x'] + elif params['network'] == I210SubNetwork: + edgestarts = { + '119257914': -5.0999999999995795, + '119257908#0': 56.49000000018306, + ':300944379_0': 56.18000000000016, + ':300944436_0': 753.4599999999871, + '119257908#1-AddedOnRampEdge': 756.3299999991157, + ':119257908#1-AddedOnRampNode_0': 853.530000000022, + '119257908#1': 856.7699999997207, + ':119257908#1-AddedOffRampNode_0': 1096.4499999999707, + '119257908#1-AddedOffRampEdge': 1099.6899999995558, + ':1686591010_1': 1198.1899999999541, + '119257908#2': 1203.6499999994803, + ':1842086610_1': 1780.2599999999056, + '119257908#3': 1784.7899999996537, + } else: edgestarts = defaultdict(float) From 625af3a27b7c6b2ee19d13ef0d9f8579ff4cd322 Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Sun, 21 Jun 2020 22:03:42 -0700 Subject: [PATCH 4/8] add 210 edgestarts for backwards compatibility --- flow/visualize/time_space_diagram.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index 3c7ab8b21..c131d6b91 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -360,6 +360,22 @@ def _get_abs_pos(df, params): } elif params['network'] == HighwayNetwork: return df['x'] + elif params['network'] == I210SubNetwork: + edgestarts = { + '119257914': -5.0999999999995795, + '119257908#0': 56.49000000018306, + ':300944379_0': 56.18000000000016, + ':300944436_0': 753.4599999999871, + '119257908#1-AddedOnRampEdge': 756.3299999991157, + ':119257908#1-AddedOnRampNode_0': 853.530000000022, + '119257908#1': 856.7699999997207, + ':119257908#1-AddedOffRampNode_0': 1096.4499999999707, + '119257908#1-AddedOffRampEdge': 1099.6899999995558, + ':1686591010_1': 1198.1899999999541, + '119257908#2': 1203.6499999994803, + ':1842086610_1': 1780.2599999999056, + '119257908#3': 1784.7899999996537, + } else: edgestarts = defaultdict(float) From f422bb11fe79e0f1d55bdd36bf75b8462a6b04a4 Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Sun, 21 Jun 2020 22:03:42 -0700 Subject: [PATCH 5/8] add 210 edgestarts for backwards compatibility --- flow/visualize/time_space_diagram.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index 3c7ab8b21..c131d6b91 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -360,6 +360,22 @@ def _get_abs_pos(df, params): } elif params['network'] == HighwayNetwork: return df['x'] + elif params['network'] == I210SubNetwork: + edgestarts = { + '119257914': -5.0999999999995795, + '119257908#0': 56.49000000018306, + ':300944379_0': 56.18000000000016, + ':300944436_0': 753.4599999999871, + '119257908#1-AddedOnRampEdge': 756.3299999991157, + ':119257908#1-AddedOnRampNode_0': 853.530000000022, + '119257908#1': 856.7699999997207, + ':119257908#1-AddedOffRampNode_0': 1096.4499999999707, + '119257908#1-AddedOffRampEdge': 1099.6899999995558, + ':1686591010_1': 1198.1899999999541, + '119257908#2': 1203.6499999994803, + ':1842086610_1': 1780.2599999999056, + '119257908#3': 1784.7899999996537, + } else: edgestarts = defaultdict(float) From 0008da20937d156041a7124eaad7a722650b3d33 Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Sun, 21 Jun 2020 22:03:42 -0700 Subject: [PATCH 6/8] add 210 edgestarts for backwards compatibility --- flow/visualize/time_space_diagram.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index 3c7ab8b21..c131d6b91 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -360,6 +360,22 @@ def _get_abs_pos(df, params): } elif params['network'] == HighwayNetwork: return df['x'] + elif params['network'] == I210SubNetwork: + edgestarts = { + '119257914': -5.0999999999995795, + '119257908#0': 56.49000000018306, + ':300944379_0': 56.18000000000016, + ':300944436_0': 753.4599999999871, + '119257908#1-AddedOnRampEdge': 756.3299999991157, + ':119257908#1-AddedOnRampNode_0': 853.530000000022, + '119257908#1': 856.7699999997207, + ':119257908#1-AddedOffRampNode_0': 1096.4499999999707, + '119257908#1-AddedOffRampEdge': 1099.6899999995558, + ':1686591010_1': 1198.1899999999541, + '119257908#2': 1203.6499999994803, + ':1842086610_1': 1780.2599999999056, + '119257908#3': 1784.7899999996537, + } else: edgestarts = defaultdict(float) From e66ef8ec87f6a2fecfdfb774e8eb8faec8e2c771 Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Wed, 1 Jul 2020 22:30:41 -0700 Subject: [PATCH 7/8] fastforward PR 989 --- flow/visualize/time_space_diagram.py | 56 +++++++++++++++++++--------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index c131d6b91..1095a597e 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -27,7 +27,8 @@ import matplotlib matplotlib.use('TkAgg') from matplotlib import pyplot as plt -from matplotlib.collections import LineCollection +from matplotlib.collections import LineCollection, PatchCollection +from matplotlib.patches import Rectangle import matplotlib.colors as colors import numpy as np import pandas as pd @@ -141,7 +142,7 @@ def get_time_space_data(data, params): def _merge(data): - r"""Generate position and speed data for the merge. + r"""Generate time and position data for the merge. This only include vehicles on the main highway, and not on the adjacent on-ramp. @@ -189,15 +190,13 @@ def _highway(data): pd.DataFrame modified trajectory dataframe """ - data.loc[:, :] = data[(data['distance'] > 500)] - data.loc[:, :] = data[(data['distance'] < 2300)] segs = data[['time_step', 'distance', 'next_time', 'next_pos']].values.reshape((len(data), 2, 2)) return segs, data def _ring_road(data): - r"""Generate position and speed data for the ring road. + r"""Generate time and position data for the ring road. Vehicles that reach the top of the plot simply return to the bottom and continue. @@ -243,10 +242,6 @@ def _i210_subnetwork(data): pd.DataFrame modified trajectory dataframe """ - # Omit ghost edges - omit_edges = {'ghost0', '119257908#3'} - data.loc[:, :] = data[~data['edge_id'].isin(omit_edges)] - # Reset lane numbers that are offset by ramp lanes offset_edges = set(data[data['lane_id'] == 5]['edge_id'].unique()) data.loc[data['edge_id'].isin(offset_edges), 'lane_id'] -= 1 @@ -259,7 +254,7 @@ def _i210_subnetwork(data): def _figure_eight(data): - r"""Generate position and speed data for the figure eight. + r"""Generate time and position data for the figure eight. The vehicles traveling towards the intersection from one side will be plotted from the top downward, while the vehicles from the other side will @@ -393,7 +388,7 @@ def _get_abs_pos(df, params): return ret -def plot_tsd(ax, df, segs, args, lane=None): +def plot_tsd(ax, df, segs, args, lane=None, ghost_edges=None, ghost_bounds=Non): """Plot the time-space diagram. Take the pre-processed segments and other meta-data, then plot all the line segments. @@ -410,6 +405,10 @@ def plot_tsd(ax, df, segs, args, lane=None): parsed arguments lane : int, optional lane number to be shown in plot title + ghost_edges : list or set of str + ghost edge names to be greyed out, default None + ghost_bounds : tuple + lower and upper bounds of domain, excluding ghost edges, default None Returns ------- @@ -417,8 +416,7 @@ def plot_tsd(ax, df, segs, args, lane=None): """ norm = plt.Normalize(args.min_speed, args.max_speed) - xmin = max(df['time_step'].min(), args.start) - xmax = min(df['time_step'].max(), args.stop) + xmin, xmax = df['time_step'].min(), df['time_step'].max() xbuffer = (xmax - xmin) * 0.025 # 2.5% of range ymin, ymax = df['distance'].min(), df['distance'].max() ybuffer = (ymax - ymin) * 0.025 # 2.5% of range @@ -432,6 +430,25 @@ def plot_tsd(ax, df, segs, args, lane=None): ax.add_collection(lc) ax.autoscale() + rects = [] + if ghost_edges: + y_domain_min = df[~df['edge_id'].isin(ghost_edges)]['distance'].min() + y_domain_max = df[~df['edge_id'].isin(ghost_edges)]['distance'].max() + rects.append(Rectangle((xmin, y_domain_min), args.start - xmin, y_domain_max - y_domain_min)) + rects.append(Rectangle((xmin, ymin), xmax - xmin, y_domain_min - ymin)) + rects.append(Rectangle((xmin, y_domain_max), xmax - xmin, ymax - y_domain_max)) + elif ghost_bounds: + rects.append(Rectangle((xmin, ghost_bounds[0]), args.start - xmin, ghost_bounds[1] - ghost_bounds[0])) + rects.append(Rectangle((xmin, ymin), xmax - xmin, ghost_bounds[0] - ymin)) + rects.append(Rectangle((xmin, ghost_bounds[1]), xmax - xmin, ymax - ghost_bounds[1])) + else: + rects.append(Rectangle((xmin, ymin), args.start - xmin, ymax - ymin)) + + if rects: + pc = PatchCollection(rects, facecolor='grey', alpha=0.5, edgecolor=None) + pc.set_zorder(20) + ax.add_collection(pc) + if lane: ax.set_title('Time-Space Diagram: Lane {}'.format(lane), fontsize=25) else: @@ -471,8 +488,6 @@ def plot_tsd(ax, df, segs, args, lane=None): help='The minimum speed in the color range.') parser.add_argument('--start', type=float, default=0, help='initial time (in sec) in the plot.') - parser.add_argument('--stop', type=float, default=float('inf'), - help='final time (in sec) in the plot.') args = parser.parse_args() @@ -504,13 +519,17 @@ def plot_tsd(ax, df, segs, args, lane=None): for lane, df in traj_df.groupby('lane_id'): ax = plt.subplot(nlanes, 1, lane+1) - plot_tsd(ax, df, segs[lane], args, lane) + plot_tsd(ax, df, segs[lane], args, int(lane+1), ghost_edges={'ghost0', '119257908#3'}) + plt.tight_layout() else: # perform plotting operation fig = plt.figure(figsize=(16, 9)) ax = plt.axes() - plot_tsd(ax, traj_df, segs, args) + if flow_params['network'] == HighwayNetwork: + plot_tsd(ax, traj_df, segs, args, ghost_bounds=(500, 2300)) + else: + plot_tsd(ax, traj_df, segs, args) ########################################################################### # Note: For MergeNetwork only # @@ -521,4 +540,5 @@ def plot_tsd(ax, df, segs, args, lane=None): [-0.1, -0.1], linewidth=3, color="white") # ########################################################################### - plt.show() + outfile = args.trajectory_path.replace('csv', 'png') + plt.savefig(outfile) From d84a58f4cbea4dc3cb21363ebadf2faf205e199f Mon Sep 17 00:00:00 2001 From: liljonnystyle Date: Wed, 1 Jul 2020 22:31:46 -0700 Subject: [PATCH 8/8] fix typo --- flow/visualize/time_space_diagram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow/visualize/time_space_diagram.py b/flow/visualize/time_space_diagram.py index 1095a597e..a5c9f45d2 100644 --- a/flow/visualize/time_space_diagram.py +++ b/flow/visualize/time_space_diagram.py @@ -388,7 +388,7 @@ def _get_abs_pos(df, params): return ret -def plot_tsd(ax, df, segs, args, lane=None, ghost_edges=None, ghost_bounds=Non): +def plot_tsd(ax, df, segs, args, lane=None, ghost_edges=None, ghost_bounds=None): """Plot the time-space diagram. Take the pre-processed segments and other meta-data, then plot all the line segments.