Skip to content

Working with the Python3 model generator

This code comes with a model generator, which generates stochastic events for the simulation. The model generator is written in Python3 and can be found in the backend directory.

To use it, first install the requirements in a Python virtual environment.

For Linux, use the following commands:

sudo apt-get install python-virtualenv
cd backend
virtualenv --python=/usr/bin/python3.7 venv
. venv/bin/activate
pip install -r requirements.txt
pip install torch==1.1.0

If Windows is being used then use the following commands and then use the following link to download PyTorch:

py -m venv .
cd scripts
cd ..
pip install -r requirements.txt
(Insert command from the PyTorch website)

Then run the code, which will listen for a socket from the Unity application:

$ python
waiting for tcpConnection

Using the PyCharm IDE

PyCharm is an IDE for Python can be used to setup the backend easily. It automatically installs the virtual environment.

  1. Open up the backend folder as a project.
  2. In settings Ctrl+Alt+S, project: backend > project interpreter, press the cog and press Add...
  3. Press Ok and Apply
  4. By using the Terminal in the bottom left of the screen, install requirements.txt and torch.
  5. The Terminal can then be used to run python

PyCharm can also be used to debug the script by using breakpoints if needed.

More information about PyCharm can be found here:

Writing a custom model generator

This code comes with a model generator, which generates stochastic events for the simulation. To use your own model generator, you need to extend the ModelGenerator class, which can be found in the /backend/ file:

import model_generator

class Traffic3DProcessor(model_generator.ModelGenerator):

Next, add the constructor and call the constructor in the superclass:

def __init__(self, images_path):

The constructor can have as many or as little parameters as needed but the super().__init__ function needs to have the image path of the screenshots that are generated by Unity (normally ../Traffic3D/Assets/Screenshots). If a different port is needed then use the following:

def __init__(self, images_path, port):
    super().__init__(images_path, port)

enable() is an abstract method which needs to be implemented into the new class. It is called once the socket is setup and a connection has been made between Unity and the model generator.

Within the ModelGenerator class there are multiple methods that interact with the Traffic Simulation

  • receive_image() - used when the script needs to grab a screen shot image from the system. This method blocks the main thread.
  • send_action(action) - used to send the action to the simulation, usually a number to the lights that need changing.
  • receive_rewards() - used to receive the rewards from the simulation. This method blocks the main thread.

At the end of the script, be sure to create an instance of the class:


To see an example of this look at the Traffic3DProcessor in the /backend/ file.