Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Getting Started with Provided Simulator

zxsRambo edited this page Mar 17, 2019 · 3 revisions

In this section, we describe how to set up the environment with the simulator and how to run the simulator (CityFlow) with the given traffic signal plan.

1 Sample code download

Download the sample code from https://github.com/tianrang-intelligence/TSCC2019

2 Environment setup

Docker is used by default to set up the competition environment. Beginners to dockers may need to refer to the docker tutorial in https://docker-curriculum.com/.

2.1 Set up a docker environment

There are two ways to set up the docker environment.

Option 1. Use the default docker image. One basic image has been built in advance, which sets up the environment for CityFlow, Tensorflow and Keras.

You can pull this image from DockerHub to your computer by the following command:

docker pull tscc2050/competition
docker tag tscc2050/competition competition

Or, you can download the image from Google Drive (https://drive.google.com/file/d/1A9Q4Mj2GS6vhizKWHS0DKZkiXeBnn5_9/view?usp=sharing) or Baidu Yun(https://pan.baidu.com/s/1DZkO_rtsWm0hvs4y8y55qw) and load this image by:

docker load --input competition.tar

Option 2. Build your own docker image using dockerfile. The original dockerfile is provided and the images can be built from scratch. The guide to build such an image is as follows. You need to run the following command in ./Docker/ . The dockerfile is placed in the folder of ./Docker/

docker build -t competition -f competition.dockerfile --rm context/

2.2 Check the environment

docker run -it --rm -v local_absolute_path:/competition -w /competition competition /bin/bash

The command above will build a docker container, where the local absolute directory of your project is mounted to a contrainner. If you want to run the code, please remember to start a container and run the code inside the container.

When the image is successfully built in the system, you can check whether the environment is set up correctly by the following commands:

python3 -c "import engine" # success if no error

3 Run simulator with the default signal plan

You can first run the simulator using the default signal plan which is defined in the road network file. The command is:

python3 run_default_plan.py --scenario "hangzhou_bc_tyc_1h_7_8_1848"

In run_default_plan.py, the main code segment is shown below, where the simulator (Engine) takes road netwrok and traffic data as input and operates the default signal plan.

args = parse_arguments()
num_step = args.num_step
eng = engine.Engine(sim_setting_default["interval"],
                    sim_setting_default["threadNum"],
                    sim_setting_default["saveReplay"],
                    sim_setting_default["rlTrafficLight"],
                    sim_setting_default["changeLane"])
roadnetFile = "data/{}/roadnet.json".format(args.scenario)
flowFile = "data/{}/flow.json".format(args.scenario)
eng.load_roadnet(roadnetFile)
eng.load_flow(flowFile)

for step in range(num_step):
    eng.next_step()
    eng.get_...()
    ...

4 Run the simulator by reading a signal plan from file

You can run the simulator by reading a traffic signal plan from the file. The command is:

python3 run_signal_plan.py --scenario "hangzhou_bc_tyc_1h_7_8_1848"

"hangzhou_bc_tyc_1h_7_8_1848" is a folder name, which includes three files: road network file, traffic flow file, and a traffic signal plan.

In the signal plan file, the first row is a header indicating the intersection name, seperated by '\t'. Following that, each line is one second, and each column is the selected phase for that corresponding intersection. A special phase number 0 indicating yellow light when switching from one phase to another. a mandatory 5-second yellow phase (represented ass 0) is applied if and only if when the phase is going to change. A sample signal plan can is as follows:

intersection_1_1  intersection_1_2
1  2
1  2
0  2
0  0
0  0
0  0
0  0
2  0
...

The follwing code segment in 'run_signal_plan.py' illustrates how to run a pre-defined signal plan from file. The 'rlTrafficLight' must be set "True" to ignore the default signal plan.

eng = engine.Engine(sim_setting_control["interval"],
                    sim_setting_control["threadNum"],
                    sim_setting_control["saveReplay"],
                    sim_setting_control["rlTrafficLight"],
                    sim_setting_control["changeLane"])
roadnetFile = "data/{}/roadnet.json".format(args.scenario)
flowFile = "data/{}/flow.json".format(args.scenario)
planFile = "data/{}/signal_plan_{}.txt".format(args.scenario, args.scenario)
eng = engine.Engine(sim_setting_control["interval"],
                    sim_setting_control["threadNum"],
                    sim_setting_control["saveReplay"],
                    sim_setting_control["rlTrafficLight"],
                    sim_setting_control["changeLane"])
roadnetFile = "data/{}/roadnet.json".format(args.scenario)
flowFile = "data/{}/flow.json".format(args.scenario)
planFile = "data/{}/signal_plan.txt".format(args.scenario)
eng.load_roadnet(roadnetFile)
eng.load_flow(flowFile)
plan = pd.read_csv(planFile, sep="\t", header=0, dtype=int)
intersection_id = plan.columns[0]
for step in range(num_step):
    phase = plan.loc[step]
    eng.set_tl_phase(intersection_id, phase) 
    eng.next_step()
    eng.get_...()
    ...

5 Evaluation

You can run the following command with a specific scenario to get the evaluation result. The 'num_step', 3600 by default, determines the evaluation time and need be identical to the running time of the signal plan.

python3 evaluate.py --scenario "hangzhou_bc_tyc_1h_7_8_1848" --num_step 3600

evaluate.py takes the signal plan as the input and outputs a score for the proposed method.

def main():
    args = parse_arguments()
    sim_setting = sim_setting_control
    sim_setting["num_step"] = args.num_step
    evaluate_one_traffic(sim_setting, args.scenario)

Next

Learn how to control traffic signal using your own code: Traffic Signal Controller Sample Code