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.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 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 Distribution

autopysta-0.0.31.5.tar.gz (718.3 kB view details)

Uploaded Source

Built Distributions

autopysta-0.0.31.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (474.3 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp313-cp313-macosx_14_0_universal2.whl (360.9 kB view details)

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

autopysta-0.0.31.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (474.2 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp312-cp312-macosx_14_0_universal2.whl (360.9 kB view details)

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

autopysta-0.0.31.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (469.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp311-cp311-macosx_14_0_universal2.whl (359.9 kB view details)

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

autopysta-0.0.31.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (469.9 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp310-cp310-macosx_14_0_universal2.whl (360.0 kB view details)

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

autopysta-0.0.31.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (467.6 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp39-cp39-macosx_14_0_universal2.whl (360.0 kB view details)

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

autopysta-0.0.31.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (467.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

autopysta-0.0.31.5-cp38-cp38-macosx_14_0_universal2.whl (359.9 kB view details)

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

File details

Details for the file autopysta-0.0.31.5.tar.gz.

File metadata

  • Download URL: autopysta-0.0.31.5.tar.gz
  • Upload date:
  • Size: 718.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for autopysta-0.0.31.5.tar.gz
Algorithm Hash digest
SHA256 47944f6081d3f8e9fcc76620b0a1ca43d70cdc0dcc49d73c9529be714dab789c
MD5 62550fa2ef4096bd5c622b41ac8d9175
BLAKE2b-256 61cd94da6b88ca163dbdcb594a1be51124581c99d38e24277d6090a321840b45

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 21642f8a485924dbdb25bb791719139a2ba575cb98c60ab4460745f60f7a8af7
MD5 d89cbbf456f5ba6038b62728f4b4a822
BLAKE2b-256 b31aa8aa53f1dccfa1af70d29cf095dba7ec217bd824a37f82cced153f828b96

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp313-cp313-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp313-cp313-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 2d33082313209b6d4f694d8d3bd5b8aa86c21f4b4b61d741780fb5f992e17315
MD5 78262c020f3ea484942fd24b0ad77fd5
BLAKE2b-256 20f071a8c58a4eb7d1e1528a4c41be7407e8169908dd296d8a7c43cc05b2a47f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7e25d0b774e880e1a8f11d9ab0ba4da355d85866c0fdb1fa0a8154b6b2464958
MD5 5d26186691c47828d7e14a2e7c915ebf
BLAKE2b-256 5361f53a1ab89ed1d4b9f75e8786fc6bc33300be929da7019189ffa091268acc

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp312-cp312-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp312-cp312-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 0ce7564e431d70e92861352a719c52beb930ebdac3141ce3bf8d8045c4aa49fc
MD5 3d75bc82bd48093ba8bc13b91ca8a7c6
BLAKE2b-256 7d35e08f38428fdb975718488c16baf736ae2113ac1753bd58fd79840262cfa4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 86af321670e95908efb166095731d017c60ac3b040d045d23ba187f7cd177fb4
MD5 6c4092adc25988a0ca110d5081f136c8
BLAKE2b-256 3227564e3a07e5e46330315b460ef31468a8afaf5e601c3d2b190cd35273cc9c

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp311-cp311-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp311-cp311-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 c8c838d0b7f9270e3bf9db5ca09ca7b1a0e41817798cb3a9a20f13ded80df441
MD5 acf13e02f3dc55fbc45af3116510ea65
BLAKE2b-256 11e573038a6c6eda532d73bc61baaa2345a48abd8a755a7771deb5e5a328a092

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 320a290dd27fa1e5e4af66cc7337798d7cc09df9000f0870dfadc0f00ac83cc0
MD5 9e8acbe0d133b4cad754b906ea090760
BLAKE2b-256 6ee2bfe2d5a6ecd0c2ef2dabbde5f59d200bf69cebdf4fd6a215ef5c5a504f3f

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp310-cp310-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp310-cp310-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 056d8ed73899f7791cf8da4d6b034e2bf31b3ff77404885dced6ca3a806ef72d
MD5 bf2db245da5670a4165b1db0f9d00db2
BLAKE2b-256 75f54f3600014064a41e8129544568268a59e31b5175d06721244b90c9ba7369

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea69c846ad4f7bb2bd55e3fad6a01e2b4c6fe16a53ac9c89aac86fada4ae680a
MD5 7e9019c1dcf04ce3e8529117e27079d9
BLAKE2b-256 dbccf37c8a40bd249d1d0005c9b6f714fc3dfd7f52e819774b5c0ce773c627e3

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp39-cp39-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp39-cp39-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 2b1572d7acb472d6867cb8f857cf57e84fcc876fedb1c049b53a5e301a5d54a3
MD5 c170e59fdaf50b02f05f1beda28ecb6c
BLAKE2b-256 a0bacdb2767ff1025b43c35498ad0d714d55a835bcd064ced5b3cdaae09860f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5af42ab07f345d8fdcfc11665b13b7ef5f7d1b6d51b8b39fefaf0777146d352e
MD5 4e84d910beef1cc437c3f2e3f0d73631
BLAKE2b-256 2979be87f92ba5bec124710207cb7ce24bb9ec616d44c08d50f8063e909d273a

See more details on using hashes here.

File details

Details for the file autopysta-0.0.31.5-cp38-cp38-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for autopysta-0.0.31.5-cp38-cp38-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 9a522a86bbe3410bf5997d7b0178401e014f93c066aa91c1b6a66d37655244a1
MD5 d19a8e275c69979e8563fdcf121837c5
BLAKE2b-256 8b48cc5d2d5707d7c08d66f35872ea8e1010a863a359796a2437d971caa29f6c

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