Giter Site home page Giter Site logo

zhongxiayan / mixed_autonomy_intersections Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 8.0 48.68 MB

[ITSC 2021] Reinforcement Learning for Mixed Autonomy Intersections

Home Page: https://ieeexplore.ieee.org/abstract/document/9565000

Python 22.05% Jupyter Notebook 74.11% Ruby 3.26% Shell 0.57%
reinforcement-learning traffic-simulation

mixed_autonomy_intersections's Introduction

Mixed Autonomy Intersections

This repo contains the code, model checkpoints, and video results for the ITSC 2021 paper Reinforcement Learning for Mixed Autonomy Intersections on arXiv and IEEE.

If you'd like to cite this work, please use

@inproceedings{yan2021reinforcement,
  title={Reinforcement Learning for Mixed Autonomy Intersections},
  author={Yan, Zhongxia and Wu, Cathy},
  booktitle={2021 IEEE International Intelligent Transportation Systems Conference (ITSC)},
  pages={2089--2094},
  year={2021},
  organization={IEEE}
}

Installation

Installation instructions are provided for MacOS and Ubuntu 14.04, 16.04, and 18.04. For microscopic traffic simulations, we use the SUMO simulator with version 1.1.0; the same code may require adjustments on other SUMO versions. We require Python 3.8+.

  1. Run bash setup/setup_sumo_<os_version>.sh corresponding to your OS version to set up SUMO and add ~/sumo_binaries/bin to your SUMO_HOME and PATH environment variables. Try running sumo and sumo-gui (if you'd like to use GUI). Note that GUI probably does not work on servers and may only work on local computers. For Mac installation issues, please refer to setup/setup_issues_osx.md. Update: due to MacOS brew updates, it could be very difficult to install the correct versions of packages for SUMO 1.1.0 on MacOS, so Ubuntu is recommended; for MacOS, you may consider installing gdal with conda install -c conda-forge gdal=2.4.2 and download the ffmpeg=4.4.1 library files (within the tar.bz2) from conda-forge directly instead of trying to use brew.
  2. Note: the previous SUMO installation actually installs a SUMO version which does not support IDM with Gaussian noise. If you'd like to use Gaussian noise (which is what we use in the paper but does not significantly affect results), you can build the forked version of SUMO 1.1.0 at https://github.com/ZhongxiaYan/sumo.
  3. If needed, follow instructions here to install Miniconda, likely wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh followed by bash Miniconda3-latest-Linux-x86_64.sh.
  4. If desired, create and activate a new conda environment following these instructions.
  5. If needed, install PyTorch (1.7+) from pytorch.org.
  6. If needed, install missing Python dependencies pip install -r requirements.txt.

Training

To train a model, create an empty experiment directory EXP_DIR anywhere, then create a file called $EXP_DIR/config.yaml with the desired training hyperparameters. The training experiment directories corresponding to figures in the paper can be found in the results directory (ignore the subdirectories with "baseline" in the name), e.g. results/twoway_2x1_penetration0.333.

The hyperparameter names are mostly self-explanatory. If not, please refer to the code for clarification.

Note that the n_workers argument specifies the number of CPUs to run the code on (we run training on a server with 40 CPUs). Make sure you don't use more CPUs than you have.

# Example training hyperparameters (also see $EXP_DIR/config.yaml)
EXP_DIR=results/twoway_2x1_penetration0.333

python intersection.py $EXP_DIR

Trained Models

All of our trained models used in the paper can be found in results, excluding the subdirectories with "baselines" in the name. The naming convention is intuitive, for example penetration0.5 indicates 50% AV penetration.

Finetuning

To finetune from a previous trained checkpoint (possibly from a different experiment with the same neural network architecture), place the desired model weights in $EXP_DIR before running the training command. For example, if we want to finetune results/fourway_1x1_penetration0.333/model-200.pth on 50% penetration in a new EXP_DIR=results/fourway_1x1_penetration0.5, use the following Python code to preserve the network weights while discarding other training states.

ckpt_path = 'results/fourway_1x1_penetration0.333/models/model-200.pth'
new_ckpt_path = 'results/fourway_1x1_penetration0.5/models/model-0.pth'

import torch
model_dict = torch.load(ckpt_path)
new_model_dict = dict(net=model_dict['net'])
torch.save(new_model_dict, new_ckpt_path)

Afterwards, run the training command python intersection.py $EXP_DIR to start training from these model weights.

We list the finetuned experiments (subdirectories of results/) here with the notation <finetuned> <--- <source> (<ckpt>):

  • twoway_2x1_penetration0.1 <--- twoway_2x1_pretrain_penetration0.5 (200)
  • twoway_2x1_penetration0.15 <--- twoway_2x1_pretrain_penetration0.5 (200)
  • twoway_2x1_penetration0.333 <--- twoway_2x1_pretrain_penetration0.5 (200)
  • twoway_2x1_penetration0.5 <--- twoway_2x1_pretrain_penetration0.5 (200)
  • twoway_2x1_penetration1 <--- twoway_2x1_pretrain_penetration0.5_threechains_finetune_penetration1 (195) <--- twoway_2x1_pretrain_penetration0.5_threechains (170)
  • fourway_1x1_penetration0.5 <--- fourway_1x1_penetration0.333 (200) All intermediate checkpoints are included in results/.

Evaluation

To evaluate a trained model at an integer checkpoint CKPT, run

# Example evaluation hyperparameters
EXP_DIR=results/twoway_2x1_penetration0.333
CKPT=200
FR_H=850 # Horizontal flow rate in vehicles/hour
FR_V=700 # Vertical flow rate
N_ROWS=3
N_COLS=3
RESULT_SAVE_PATH=$EXP_DIR/eval_results/e165_3x3_skip500_flow850x700.csv

python intersection.py $EXP_DIR e=$CKPT n_rows=$N_ROWS n_cols=$N_COLS n_steps=10 n_rollouts_per_step=1 skip_stat_steps=500 flow_rate_h=$FR_H flow_rate_v=$FR_V result_save=$RESULT_SAVE_PATH

Any nonspecified hyperparameter defaults to the value in $EXP_DIR/config.yaml. Other hyperparameters that can be set can be found in the code: any attribute of c (see code) can be set from the command line, i.e. to set c.lr = 0.001 from the command line, run python intersection.py $EXP_DIR ... lr=0.001.

In the paper, for each experiment we evaluated the checkpoint with the best performance during training. These checkpoints are

  • twoway_2x1_penetration0.1: 155
  • twoway_2x1_penetration0.15: 180
  • twoway_2x1_penetration0.333: 165
  • twoway_2x1_penetration0.5: 150
  • twoway_2x1_penetration1: 160
  • fourway_1x1_penetration0.333: 190
  • fourway_1x1_penetration0.5: 170 Note that we evaluated twoway_2x1_penetration0.333 and twoway_2x1_penetration0.5 on both a 2x1 grid of intersections and a 3x3 grid of intersections (see their eval_results/ subdirectories).

Baselines

To run the priority and traffic signal baseline methods which do not require training, create an empty directory BASE_DIR and a $BASE_DIR/config.yaml with hyperparameters pertaining to all the baseline runs in that directory. Refer to results/{twoway_2x1_baselines,twoway_3x3_baselines,fourway_1x1_baselines}/config.yaml as examples. The commands below are similar to previous commands.

# Example baseline hyperparameters
BASE_DIR=results/twoway_2x1_baselines
FR_H=850
FR_V=700

# Priority (Horizontal)
python intersection.py $BASE_DIR e=0 n_steps=3 n_rollouts_per_step=1 skip_stat_steps=500 av_frac=0 speed_mode=SPEED_MODE.all_checks priority=horizontal flow_rate_h=$FR_H flow_rate_v=$FR_V result_save=$BASE_DIR/eval_results/skip500_hpriority_flow${FR_H}x${FR_V}.csv

# Priority (Vertical)
python intersection.py $BASE_DIR e=0 n_steps=3 n_rollouts_per_step=1 skip_stat_steps=500 av_frac=0 speed_mode=SPEED_MODE.all_checks priority=vertical flow_rate_h=$FR_H flow_rate_v=$FR_V result_save=$BASE_DIR/eval_results/skip500_hpriority_flow${FR_H}x${FR_V}.csv

# Traffic Signal with specified phase times
PHASE_H=25 # Horizontal traffic signal phase length in seconds
PHASE_V=25 # Vertical traffic signal phase length in seconds
python intersection.py $BASE_DIR e=0 n_steps=3 n_rollouts_per_step=1 skip_stat_steps=500 av_frac=0 "'tl=($PHASE_H,$PHASE_V)'" yellow=0 flow_rate_h=$FR_H flow_rate_v=$FR_V result_save=$BASE_DIR/eval_results/skip500_signalbest_yellow0_flow${FR_H}x${FR_V}.csv

# Traffic Signal with MaxPressure
MP_T_MIN=12 # Units are in seconds
python intersection.py $BASE_DIR e=0 n_steps=3 n_rollouts_per_step=1 skip_stat_steps=500 av_frac=0 tl=MaxPressure mp_tmin=$MP_T_MIN yellow=0 flow_rate_h=$FR_H flow_rate_v=$FR_V result_save=$BASE_DIR/eval_results/skip500_mpbest_yellow0_flow${FR_H}x${FR_V}.csv

We list the best hyperparameters that we found for traffic lights and MaxPressure baselines below.

Two-way 2x1 Hyperparameters

Oracle traffic signal phase table:

F_H \ F_V 400 550 700 850 1000
1000 (25, 10) (25, 13) (25, 25) (27, 27)
850 (25, 25) (25, 25) (25, 25) (26, 26) (26, 25)
700 (25, 25) (25, 25) (19, 26)
550 (25, 25) (14, 25)
400 (25, 25) (9, 25)

Equal-phase τ_equal: PHASE_H=25 and PHASE_V=25

MaxPressure τ_min: MP_T_MIN=4

Two-way 3x3 Hyperparameters

Oracle traffic signal phase table:

F_H \ F_V 400 550 700 850 1000
1000 (25, 10) (25, 13) (19, 19) (21, 21) nan
850 (25, 24) (25, 25) (25, 25) (26, 25) (21, 21)
700 nan nan (25, 25) (25, 25) (19, 19)
550 nan nan nan (25, 25) (13, 25)
400 nan nan nan (24, 25) (10, 25)

Equal-phase τ_equal: PHASE_H=25 and PHASE_V=25

MaxPressure τ_min: MP_T_MIN=6

Four-way 1x1 Hyperparameters

Oracle traffic signal phase table:

F_H \ F_V 400 550 700 850 1000
1000 (25, 11) (25, 14) (29, 22) (25, 25)
850 (25, 25) (25, 25) (25, 25) (28, 28) (25, 25)
700 (25, 25) (25, 25) (22, 29)
550 (25, 25) (14, 25)
400 (25, 25) (11, 25)

Equal-phase τ_equal: PHASE_H=25 and PHASE_V=25

MaxPressure τ_min: MP_T_MIN=12

Figures

Heatmap Results

Please refer to figures.ipynb for the plotting code.

Time-space Diagram

To save the vehicles states for visualization when evaluating trained models or running baselines, add the vehicle_info_save argument. For example:

# Example evaluation hyperparameters
EXP_DIR=results/twoway_2x1_penetration0.333
CKPT=165
FR_H=1000 # Horizontal flow rate in vehicles/hour
FR_V=700 # Vertical flow rate
N_ROWS=2
N_COLS=1
VEH_INFO_PATH=$EXP_DIR/veh_info/e165_skip500_flow${FR_H}x${FR_V}.csv

python intersection.py $EXP_DIR e=$CKPT n_rows=$N_ROWS n_cols=$N_COLS n_steps=1 n_rollouts_per_step=1 skip_stat_steps=500 flow_rate_h=$FR_H flow_rate_v=$FR_V vehicle_info_save=$VEH_INFO_PATH

Please refer to figures.ipynb for the plotting code.

GUI

To use SUMO GUI to display the traffic scenario when evaluating a trained model or running baselines, add the render argument. For example:

EXP_DIR=results/twoway_2x1_penetration0.333
CKPT=165
FR_H=850 # Horizontal flow rate in vehicles/hour
FR_V=700 # Vertical flow rate
N_ROWS=3
N_COLS=3

python intersection.py $EXP_DIR e=$CKPT n_rows=$N_ROWS n_cols=$N_COLS n_steps=1 n_rollouts_per_step=1 skip_stat_steps=500 flow_rate_h=$FR_H flow_rate_v=$FR_V render

fourway_1x1_penetration0.5

EXP_DIR=results/fourway_1x1_penetration0.5
CKPT=170
FR_H=1000 # Horizontal flow rate in vehicles/hour
FR_V=700 # Vertical flow rate
N_ROWS=1
N_COLS=1

python intersection.py $EXP_DIR e=$CKPT n_rows=$N_ROWS n_cols=$N_COLS n_steps=1 n_rollouts_per_step=1 skip_stat_steps=500 flow_rate_h=$FR_H flow_rate_v=$FR_V render

fourway_1x1_penetration0.5

mixed_autonomy_intersections's People

Contributors

zhongxiayan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mixed_autonomy_intersections's Issues

traci.exceptions.TraCIException: TraCI server already finished

File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 182, in rollouts_single_process
rollout_stats = [c.var(i_rollout=i).rollout() for i in range(c.n_rollouts_per_worker)]
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 182, in
rollout_stats = [c.var(i_rollout=i).rollout() for i in range(c.n_rollouts_per_worker)]
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 201, in rollout
ret = c._env.reset()
File "/home/amax/Users/ljw/mixed_autonomy_intersections/env.py", line 1029, in reset
return self.norm_obs(self.env.reset())
File "/home/amax/Users/ljw/mixed_autonomy_intersections/intersection.py", line 106, in reset
while not self.reset_sumo():
File "/home/amax/Users/ljw/mixed_autonomy_intersections/env.py", line 913, in reset_sumo
self.tc = sumo_def.start_sumo(self.tc)
File "/home/amax/Users/ljw/mixed_autonomy_intersections/env.py", line 345, in start_sumo
tc = traci.connect(self.port, 10, 'localhost', p)
File "/home/amax/.conda/envs/mixed/lib/python3.9/site-packages/traci/main.py", line 105, in connect
raise TraCIException("TraCI server already finished")
traci.exceptions.TraCIException: TraCI server already finished

Errors when trainning models

Hello

Thank you very much for sharing your code

But I had errors when running model at training step with n_workers: 1
I followed your instruction and take

python intersection.py EXP_DIR

Traceback (most recent call last):
File "intersection.py", line 312, in
c.run()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 357, in run
c.train()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 289, in train
rollouts = c.rollouts()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 176, in rollouts
rollout_stats = c.rollouts_single_process()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 182, in rollouts_single_process
rollout_stats = [c.var(i_rollout=i).rollout() for i in range(c.n_rollouts_per_worker)]
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 182, in
rollout_stats = [c.var(i_rollout=i).rollout() for i in range(c.n_rollouts_per_worker)]
File "/home/osboxes/Desktop/mixed_autonomy_intersections/exp.py", line 201, in rollout
ret = c._env.reset()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/env.py", line 1029, in reset
return self.norm_obs(self.env.reset())
File "intersection.py", line 108, in reset
ret = super().init_env()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/env.py", line 932, in init_env
ret = self.step()
File "intersection.py", line 144, in step
super().step()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/env.py", line 894, in step
self.ts.step()
File "/home/osboxes/Desktop/mixed_autonomy_intersections/env.py", line 791, in step
edge = self.edges[veh.road_id]
KeyError: ''

'GridExp' object has no attribute 'wb'

Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
def randint(low, high=None, size=None, dtype=onp.int): # pylint: disable=missing-function-docstring
i 180 | klcoef 0 | lr 0.001
Traceback (most recent call last):
File "/home/amax/Users/ljw/mixed_autonomy_intersections/u.py", line 549, in getattr
return self[key]
KeyError: 'wb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "intersection.py", line 312, in
c.run()
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 359, in run
c.train()
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 289, in train
c.on_step_start()
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 164, in on_step_start
c.log_stats(dict(**stats, **c._alg.on_step_start(), lr=lr))
File "/home/amax/Users/ljw/mixed_autonomy_intersections/exp.py", line 112, in log_stats
if c.wb:
File "/home/amax/Users/ljw/mixed_autonomy_intersections/u.py", line 551, in getattr
self.getattribute(key)
AttributeError: 'GridExp' object has no attribute 'wb'

the training crashes because of the raytask

the config yaml is:

av_frac: 0.5
device: cpu
directions: 4way
n_cols: 1
n_rollouts_per_step: 256
n_rows: 1
n_workers: 16
worker_kwargs:
- flow_rate_h: 700
  flow_rate_v: 700
- flow_rate_h: 850
  flow_rate_v: 850
- flow_rate_h: 850
  flow_rate_v: 700
- flow_rate_h: 850
  flow_rate_v: 550
- flow_rate_h: 850
  flow_rate_v: 400
- flow_rate_h: 700
  flow_rate_v: 850
- flow_rate_h: 550
  flow_rate_v: 850
- flow_rate_h: 400
  flow_rate_v: 850
- flow_rate_h: 1000
  flow_rate_v: 850
- flow_rate_h: 1000
  flow_rate_v: 700
- flow_rate_h: 1000
  flow_rate_v: 550
- flow_rate_h: 1000
  flow_rate_v: 400
- flow_rate_h: 850
  flow_rate_v: 1000
- flow_rate_h: 700
  flow_rate_v: 1000
- flow_rate_h: 550
  flow_rate_v: 1000
- flow_rate_h: 400
  flow_rate_v: 1000

the command is:

python intersection.py results/fourway_1x1_penetration0.5

the log is

i 104 | ii 255 | n_veh_step_mean 14.1 | n_veh_step_sum 2.82e+04 | n_veh_unique 387
2022-10-29 21:17:27,083 WARNING [worker.py:1829](http://worker.py:1829) -- A worker died or was killed while executing a task by an unexpected system error. To troubleshoot the problem, check the logs for the dead worker. RayTask ID: ffffffffffffffff8d9f376e21fe8973fcb0e4d701000000 Worker ID: 3fa7a262580beff71dedab84f2f6ab1a617716292ec00bb73de6dc8c Node ID: 9aedc44a625880aa7985a83bf5aa403f3d9601298409d51e53973e2c Worker IP address: 192.168.96.4 Worker port: 43775 Worker PID: 186329 Worker exit type: SYSTEM_ERROR Worker exit detail: Worker unexpectedly exits with a connection error code 2. End of file. There are some potential root causes. (1) The process is killed by SIGKILL by OOM killer due to high memory usage. (2) ray stop --force is called. (3) The worker is crashed unexpectedly due to SIGSEGV or other unexpected errors.
i 104 | ii  0 | policy_loss -2.01 | kl -5.98e-12 | entropy 0.211
i 104 | gd_time 119 | total_time 7.35e+04

i 105 | klcoef 0 | lr 0.001

Saved model /home/ubuntu/data/Jeffrey/mixed_autonomy_intersections/results_xlchan/fourway_1x1_penetration0.5/models/model-105.pth at step 105

Traceback (most recent call last):

  File "/home/ubuntu/data/Jeffrey/mixed_autonomy_intersections/intersection.py", line 313, in <module>

    c.run()

  File "/home/ubuntu/data/Jeffrey/mixed_autonomy_intersections/exp.py", line 357, in run

    c.train()

  File "/home/ubuntu/data/Jeffrey/mixed_autonomy_intersections/exp.py", line 289, in train

    rollouts = c.rollouts()

  File "/home/ubuntu/data/Jeffrey/mixed_autonomy_intersections/exp.py", line 174, in rollouts

    rollout_stats = flatten(ray.get([w.rollouts_single_process.remote() for w in c._rollout_workers]))

  File "/home/ubuntu/data/anacond3/envs/drl/lib/python3.8/site-packages/ray/_private/client_mode_hook.py", line 105, in wrapper

    return func(*args, **kwargs)

  File "/home/ubuntu/data/anacond3/envs/drl/lib/python3.8/site-packages/ray/_private/worker.py", line 2282, in get

    raise value

ray.exceptions.RayActorError: The actor died unexpectedly before finishing this task.

  class_name: GridExp

  actor_id: 8d9f376e21fe8973fcb0e4d701000000

  pid: 186329

  namespace: 7bd7b1ab-09a7-4e4d-ac05-d44f61be5dbd

  ip: 192.168.96.4

The actor is dead because its worker process has died. Worker exit type: SYSTEM_ERROR Worker exit detail: Worker unexpectedly exits with a connection error code 2. End of file. There are some potential root causes. (1) The process is killed by SIGKILL by OOM killer due to high memory usage. (2) ray stop --force is called. (3) The worker is crashed unexpectedly due to SIGSEGV or other unexpected errors.

My device has 48 CPUs. the version of ray is 2.0.1. Can you provide the detailed versions of python package?(Maybe it is not related to this issue.)
Is there any way to fix this?

Checkpoint and example config.yaml

Hi,

It is great news for me that the code is released! Thanks for your effort!
However, it seems that there is no checkpoint file and config.yaml in your repo?

Dawei

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.