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.

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.9 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.9 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 root 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.

Platform-Specific Details

  • Linux and macOS: Autopysta uses .so shared libraries and works with Python 3.8+.
  • Windows: Only Python 3.8 is supported due to DLL handling requirements. Autopysta uses .pyd files on Windows.

Autopysta provides an efficient and flexible framework for traffic simulation, making it suitable for research, experimentation, and traffic model development. 🚦

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.9a9-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (860.8 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

autopysta-0.9a9-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (787.6 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARM64

autopysta-0.9a9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (860.8 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

autopysta-0.9a9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (788.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

autopysta-0.9a9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (852.2 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

autopysta-0.9a9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (779.3 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

autopysta-0.9a9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (852.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

autopysta-0.9a9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (779.3 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

autopysta-0.9a9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (847.6 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

autopysta-0.9a9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (776.3 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

autopysta-0.9a9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (847.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

File details

Details for the file autopysta-0.9a9-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 27f50c98392262806a6e02c471a8be34eac1c04fca88e314d456fb6e04eb45e3
MD5 5038addc0afaa1e5508ec6cae2107911
BLAKE2b-256 4b465102b522b5e1d84e560830cb26f47d1f8e6007e8712dc8b98032eb83d67a

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b93a8f11c4da74e4ce0627ec80cd375404acd9e0b3cb44202ebd6353da37bdc1
MD5 88979e9c8b8f848dcb917f2dc1e78408
BLAKE2b-256 be7f6e06751179929825c033c8a356dd8aad810a3c9005ea393db4b581655682

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 43e9de05d4a47e2bc9ae428afce93b4e5f53d66ed729e22c576298599aa3c6a8
MD5 db77d763a1f6842857931f8791298fec
BLAKE2b-256 1d651eb6ede8323bff9a1c44f70b31af6ceab357790110490a62695a18a007f1

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9489f0ffb6d9a451870dc649616302b4f35699626e4355eae6308b45a4962a93
MD5 703677f321b9efad7ffa122e53bc8c47
BLAKE2b-256 3bfc267bb6659782ddf9b6d7b28833291ac23c0d77e2dbf7d0a9dffde627b91a

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f7b5ecf5c7ea2effef2b4f8250b53d3e7b7dff594205ff476266a3d9284eaf19
MD5 9f337433d4e75e504ccc64a940cb9a0a
BLAKE2b-256 d7261973630f2ebe760f781b10fcea72162ae6286c605e81caccbb05632a1296

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fc3b285e3a3907588f7dfa58949994f3788abaec5ff0e73e9e1544faff0a92e5
MD5 133580b61870d1d51076b9492d97e384
BLAKE2b-256 356650965847c2f60f3c6563993c7ab7b873761b6d8f7a2a84dc082413d115c0

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1c3c6efdf4c736f25ef3954d0bf2dd51ddcabaae8a12f1c3fb888c997ca99a79
MD5 88bebfb80bfb66f651dca7c313729fe5
BLAKE2b-256 afbe34833ed2399911e674c1f1efc9289dfcb7be675670c27250d5fbdd82ea25

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 575549a339dce0aa7b44ac0f0193e9d4deb497fcd9e51849e672041515fb3cee
MD5 04c479cb56889fa4288240807c24811d
BLAKE2b-256 c9e2d7c2409a86ce02584bd605848d796cd74d9ac40102bfbbae5482e8283ecf

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9409ebb8edf98c6b759f9ac2376d40cf0026130545fee41df04fb4f7564a02ba
MD5 6b9b6aac8db1c62ec2ee9d65d62aeb3e
BLAKE2b-256 7ef38fcf840a2a48aa1f15650f40f45eb3b1cde231acb8d427abf7b17f952659

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c7667adb9a9a3e3bafc2bc5a01f8819551d210475aa9137470518651cb1558ea
MD5 f5cba4351b93b1008e3761a7a06b5f87
BLAKE2b-256 0d8b1d00ed9a2d149ebfcf451a0201b011414c899b43afd3b9f1ab5557ffd4ea

See more details on using hashes here.

File details

Details for the file autopysta-0.9a9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.9a9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b584b43028157d30c56d79a26f80d3d0bb3359f4e5ce3d33efeb0134e9d3428a
MD5 15c88eb3c166d85f6ade13a9f4f1aada
BLAKE2b-256 b5295e7043ca8469757945d6e286d0dbc63da618ed51a2468d7024c395d53637

See more details on using hashes here.

Supported by

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