Skip to main content

2D traffic modeling.

Project description

Autopysta

Autopysta is a Python library for simulating 2D traffic flow on highways. It is written in C++ for high performance and wrapped using SWIG for Python compatibility. The library allows users to create realistic highway traffic scenarios with customizable vehicle behaviors, lane-changing logic, and road configurations.

Table of Contents

Platform Support

Autopysta supports Windows, Linux, and macOS. However, the specific Python versions supported on each platform are still under testing and validation. Currently:

  • Windows: Tentatively supports Python 3.8, pending further compatibility testing.
  • Linux and macOS: Compatible with Python 3.8 and later, though compatibility with other versions is being evaluated.

Key Features

  1. Flexible Vehicle Behavior
    Autopysta provides a variety of vehicle models that dictate acceleration, deceleration, and lane-changing dynamics. Examples include:

    • Intelligent Driver Model (IDM)
    • Gipps Model
    • Newell Random Acceleration Model
  2. Vehicle Creation Options
    Users can choose different vehicle creation strategies for each lane, defining how vehicles are added to the simulation.

  3. Customizable Highway Geometry
    Configure highways with parameters such as road length, number of lanes, and merge/diverge points for ramps.


Installing Autopysta from PyPI

If you don’t want to compile from source, you can install Autopysta directly from PyPI (precompiled binaries are available for supported platforms):

pip install autopysta

This will install the latest version of Autopysta and its dependencies, allowing you to skip the manual build process.


Compiling Autopysta from Source

To compile Autopysta from source, you will need to build the C++ components, generate the Python wrappers, and copy the necessary binaries to the appropriate directories.

Prerequisites

Before building from source, ensure you have the following installed:

  • Python (version 3.8 on Windows, 3.8 or higher on Linux/macOS)
  • CMake (for building the C++ components)
  • setuptools and wheel (pip install setuptools wheel)
  • SWIG (for generating Python wrappers)

Steps to Compile Autopysta

  1. Clone the Repository
    Run the following commands to clone the repository and navigate to its directory:

    git clone https://bitbucket.org/rdelpiano/autopysta.git
    cd autopysta
    
  2. Build the Extension
    Use the following command to build the C++ extension:

    python setup.py build_ext
    

    This will:

    1. Use CMake to compile the C++ code.
    2. Generate the autopysta.py wrapper and the platform-specific _autopysta.* shared library.
    3. Copy the generated binaries to:
      1. The root autopysta directory.
      2. The examples/autopysta directory.
  3. Verify the Build
    After building, check the autopysta directory (both in the tests/ and inside examples/) for:

    1. autopysta.py: Python wrapper.
    2. _autopysta.*: Shared library file specific to your platform (e.g., _autopysta.so on Linux/macOS or _autopysta.pyd on Windows).

Running an Example

Once the build is complete, you can run any example provided in the examples directory.

Example: Running the Different Vehicle Creators Example

Navigate to the examples/ directory:

cd examples

Run the different_creators.py (or any other example script):

python different_creators.py

This script simulates traffic with different vehicle creation strategies, as shown in the "Quick Start Example" below.


Quick Start Example: Using Autopysta to Simulate Different Vehicle Creators

Autopysta offers various ways to populate lanes with vehicles, each governed by different driving models and behaviors. Here’s an example of setting up a simulation with different vehicle creators to model distinct traffic conditions.

# ------------------------------------------------------------------------------
# Case Study: Using Different Vehicle Creators
#
# This example demonstrates the use of different vehicle creators to populate 
# lanes with vehicles using distinct driving models. The creators used are:
# 1. `FixedStateCreator`: Generates vehicles with specified spacing and speed.
# 2. `FixedDemandCreator`: Generates vehicles based on a fixed traffic flow rate.
# ------------------------------------------------------------------------------

# Import the library
import autopysta as ap

# Display the current version of autopysta
print(ap.version())

# Define the highway geometry for the simulation. The Geometry object takes:
# 1. `length`: The length of the highway (meters).
# 2. `lanes`: Number of lanes.
# 3. `merge_pos`: Position where a merge ramp ends (0 for no merge ramp).
# 4. `diverge_pos`: Position where a diverge ramp starts (highway length for no diverge ramp).
length = 1000
initial_lanes = 4
merge_position = 300
diverge_position = 700
highway_geometry = ap.Geometry(length, initial_lanes, merge_position, diverge_position)

# Define the driving models for acceleration/deceleration.
# Default parameters are used for both models.
idm_model = ap.idm()
gipps_model = ap.gipps()

# Specify the types of vehicle creators for each lane. The creators define the
# conditions under which vehicles are added to the lanes:
# - `FixedStateCreator`: Generates vehicles based on specified spacing and speed.
# - `FixedDemandCreator`: Generates vehicles based on a fixed flow rate.
# If only one creator is given, it will apply to all lanes. The final parameter 
# is optional, specifying a maximum number of vehicles (default is unlimited).
spacing = 15
speed = 10
flow_rate = 0.5
lane_creators = [
    ap.FixedStateCreator(gipps_model, spacing, speed),  # Lane 1
    ap.FixedDemandCreator(idm_model, flow_rate),        # Lane 2
    ap.FixedStateCreator(idm_model, spacing, speed),    # Lane 3
    ap.FixedDemandCreator(gipps_model, flow_rate),      # Lane 4 (if present)
]


# Define the lane-changing model. To disable lane changing, use `no_lch`.
lane_change_model = ap.lcm_gipps()

# Initialize the Simulation object with the defined settings. The simulation is
# set to run for 80 seconds with a timestep of 0.1 seconds.
# No vehicles are predefined, so we use an empty list.
# We set verbose mode to get some extra output and see the shape of the geometry
total_time = 80
vehicles=[]
time_step = 0.1
verbose = True
simulation = ap.Simulation(
    lane_change_model,
    total_time,
    highway_geometry,
    lane_creators,
    vehicles,
    time_step
)


# Run the simulation. The `run()` method returns a `Results` object containing
# trajectory data from the simulation.
simulation_results = simulation.run()

# The `plot_x_vs_t` method generates trajectory plots. It can be used to plot
# all lanes or specific lanes, as shown below.
plotting = True
if plotting:
    simulation_results.plot_x_vs_t()       # Plot all lanes
    simulation_results.plot_x_vs_t(lane=1) # Plot only lane 1
    simulation_results.plot_x_vs_t(lane=2) # Plot only lane 2
    simulation_results.plot_x_vs_t(lane=3) # Plot only lane 3
    simulation_results.plot_x_vs_t(lane=4) # Plot only lane 4 (if applicable)

Explanation of the Simulation Example

  • Geometry Setup: Creates a highway with a length of 1000 meters and 4 lanes. The highway includes a merge ramp at 300 meters and a diverge ramp at 700.
  • Vehicle Models: The IDM, Gipps, and Newell Random Acceleration models are assigned to control vehicle dynamics. Different models can represent various driving styles or traffic conditions.
  • Lane Creators: Vehicles are added based on either a fixed spacing (FixedStateCreator) or a flow rate (FixedDemandCreator). This allows the simulation of both congested and free-flowing traffic conditions.
  • Lane-Changing Logic: The lane-change model (lcm_gipps) handles vehicle lane changes based on the surrounding traffic conditions, with optional parameters for enabling or disabling lane changes.
  • Simulation Execution: The simulation runs for a set period, returning vehicle trajectories for analysis.

Additional Features

  • Verbose Mode: Enables detailed output during the simulation, useful for understanding and troubleshooting the behavior of individual vehicles.
  • Plotting: The plot_x_vs_t() method generates time-space plots for vehicle trajectories, providing a visual representation of the simulation results.

Documentation

For more detailed information on various aspects of Autopysta, please refer to the following documentation files:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

autopysta-0.0.40.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (784.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp313-cp313-macosx_10_13_universal2.whl (1.1 MB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

autopysta-0.0.40.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (784.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp312-cp312-macosx_10_13_universal2.whl (1.1 MB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

autopysta-0.0.40.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (787.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp311-cp311-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

autopysta-0.0.40.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (784.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp310-cp310-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

autopysta-0.0.40.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (788.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp39-cp39-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.9macOS 10.9+ universal2 (ARM64, x86-64)

autopysta-0.0.40.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (785.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

autopysta-0.0.40.0-cp38-cp38-macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.8macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file autopysta-0.0.40.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d080e5c2417dfb5ad804c7b0ee87fefa3e4471838fc2b2c9640e235ef64bb5b7
MD5 aa3ce9025ed9f931a32a56f22fbd091a
BLAKE2b-256 23727a15fc77e50125597434da84da98eb116976425d359389c3c9e98eb70caa

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 ccccc93b883d601862d80c126472f9b1c23223a35a2fca817b3d581c77f5af55
MD5 f82edbbdc7bdae5a1e7a2b3025be2469
BLAKE2b-256 4a98a6c82066491c794b5736b94b36fd5bf0597ce12ef8beb5dd92d47d56a192

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 26f4bab12b911b17065f94c99cc534ef628ef73e470fd0812310c134d9bf8d98
MD5 0b384a318792b029089f223dc7945612
BLAKE2b-256 ee05600b2510debe835e2033d778bcccff5ac16dd348a6f90fc4c124021e7fa2

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 46d45ac8d879a13fe949ea31db442e96e2955c52e160a0a86522298ee8ace157
MD5 2efb5f1e304e443658a70b46b8aa2994
BLAKE2b-256 76796d3cbb7aea192bcf671610c962fd84bce1d10b7e5d8fb70d4b513835730f

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5e6b1115b3566da7fa03e5ce25b7980fe6f963797f411eaef23e3af65b7aeb9
MD5 7c55b9f6188913f124f8cf26f81e3b5a
BLAKE2b-256 45db24d5ad197b6800542c04f12857c5ee8343c7aabe1d3c6a37b224ceccb26d

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 02b7c85b5963058fa7ff9877b7c287889b2da1794f3b12dcd19fc11cca3e2ef0
MD5 d1276c4abbe1d25ad5fdaf1d1b6ed9bd
BLAKE2b-256 05e26745e0088d22fac8bd1bf112da2c1488ae4f62c8be5ddf701dd1a529b44a

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d3bf4a5420e8d5a9cc112c4581f909975e1dcaac0239b49498790848f9259407
MD5 10fc92b1e6a55099cbc4e333c4bdaaaf
BLAKE2b-256 5c7f0a85510aa24d7947d48113692e403bfff1e7f0ee53dc06c403fb7af3e23b

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 371608f94f5a6b3860622a0c1d22e36603a19c2860dee1e98b47445d47639e19
MD5 215f2a43d06f59c67f6705d13cb45204
BLAKE2b-256 53719457b7e71063fd47f9860c12c80e02569195344b1c0fd25992977af79a65

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 146accb4f426a9c5371a029bee3951f7b61e1e746ddfb3e2756acccedfa5d3db
MD5 b107954a0cba602feed738ac97eb0db9
BLAKE2b-256 d8d5126d8c7ea0c853311c23bc984828d67ba090255b4dbe7406ff824b755afb

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 372dcca29d83e1bf5535e79c6a887c95bad50185973a9b1a2975a354a61d64ea
MD5 af4a988f107e2284fa7109b1cbb9ee3a
BLAKE2b-256 4f997facf3695ae185c0e32b68c97c98566f83b92b4d3c1a9d166063e5b4197a

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65e23667570d117343e415061fbad75c45a7ac3e9b3b1cf465c49d26fa2cceda
MD5 574d89537e7edd1c0ea9a3daf8647b88
BLAKE2b-256 0e9f8bb9fd5bfd333d761c8366f98ff79896370a9bc6a838514e1116d50d93af

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.0-cp38-cp38-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.0-cp38-cp38-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 48e53f80af2c8a8e1ef1a4bc69dd27254e83ecd72ff97dcb178166ac7ce17daa
MD5 e3ff17b456dae13309e1e7efee22a0ca
BLAKE2b-256 2f0cf28ca9ff3b648b34770c6f82020aa83b9372db28dc8b10c103804cf4d6ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page