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

If you're not sure about the file name format, learn more about wheel file names.

autopysta-0.0.40.1-cp313-cp313-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (821.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp313-cp313-macosx_10_13_universal2.whl (1.2 MB view details)

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

autopysta-0.0.40.1-cp312-cp312-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (818.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp312-cp312-macosx_10_13_universal2.whl (1.2 MB view details)

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

autopysta-0.0.40.1-cp311-cp311-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (812.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp311-cp311-macosx_10_9_universal2.whl (1.2 MB view details)

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

autopysta-0.0.40.1-cp310-cp310-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (808.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp310-cp310-macosx_10_9_universal2.whl (1.2 MB view details)

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

autopysta-0.0.40.1-cp39-cp39-musllinux_1_2_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (805.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp39-cp39-macosx_10_9_universal2.whl (1.2 MB view details)

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

autopysta-0.0.40.1-cp38-cp38-musllinux_1_2_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

autopysta-0.0.40.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (804.8 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

autopysta-0.0.40.1-cp38-cp38-macosx_10_9_universal2.whl (1.2 MB view details)

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

File details

Details for the file autopysta-0.0.40.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 5e407fc8005b5e4a127a6439aa08ec9284ef9292710ad7ef3093c125f7399ef9
MD5 000b99245001a1c9ee9618a90e9654c2
BLAKE2b-256 e7aa4d10e6ec89deec47e13fc48ad84fddb979cc70281d27543ac06728bb7db8

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1dcf996786a1fa684571e9dee25f9e974f9366516b768bc5be7590aab53ec408
MD5 c4758c19e4e534dc9623708397957cc0
BLAKE2b-256 ebaccb7733a97b2118b14593b3254d0ff78c1089c8b2cd6c2dff4627c9cea1f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 8f5bd78a5c12b0d7426082757ce28e2a80319dc8775795b66a87a52c2df60f64
MD5 d6b7085aaa5ba50e27d39b57dd5d18b2
BLAKE2b-256 5fe066429bd7e3dc35b84a8b720a95a3a3a880f24a2bf04b169c819354a1219e

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 dcdcc20893c0f0a4d33216e5bc8f898a95c54151681976817aec54f1bd48bd0e
MD5 0397830025fbc6e32d9c797e8f8d91ac
BLAKE2b-256 7f3f57d8471d562a6339b757d713bc8c82d660ccda90f6eb145116600f59111e

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9d3ed9f6534cc834ec2d1d7316fb6735eb87c60f42b1df10edec92ee56614172
MD5 51c2ac17da9a666c60b14da05c95b14f
BLAKE2b-256 984d6001c5deb05707378c7fbb2ff2b2fb1973f1fb4a6df8893aba9e9a6f8424

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 b330752973a4850d4022dafe472278f080e47a2315f6487e4b85c1a27ee9a435
MD5 7d5681f7851edcd4fe1f33a883714f56
BLAKE2b-256 990d77d518657bdf1ae95f75fc99fc5b8f138e238503cbf919eee374f9aa7224

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 664dd9c320247ce62b09c87e12e0f7ffd7fb13c460068561361fb8f73a4bd7f8
MD5 5e1c537050d537c8dc9b3cab0ffeffb6
BLAKE2b-256 76aee507c2c8a5effbe30b21993c6e4ed0e100cb86554f902f52a5ab0b16965b

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 71001c8eec6d1429840cbd384e42cd3979fa47664a53973fe62996839c9a1fea
MD5 f071070f65a19b73326d96bc79dacd32
BLAKE2b-256 9d5d0b8b03971103439d1561fd33dd6f07bb1957902293a574376d4518dba96e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b41be11528c5b51307a113041108dab0c18bdd562804da8a8c23bdfafd3dbfeb
MD5 4a4804a92b6c66730781b4a76be9aca1
BLAKE2b-256 0a7ae29d6cd947942b02d8eb76ed8b82d3c92991001db4f1f525a7dc1af5762e

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e16dcbf816a56860c32c72692e13a23f992a68da80413788d022ad7eb731f3dd
MD5 a1c9494db6e283f4078f6dbb71369b5b
BLAKE2b-256 107626ef1a47170d0270f8a1cee6d07fe60a460684d770fb065ffc2673133c62

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c50ccdf166e21c94590d3238426cfb6e9b360aa4ab53ed24ca155ab48e641b39
MD5 ae1665a77eca7c27f4f593d2235abd9b
BLAKE2b-256 0c01e34c4b73a958a5cb04b4987b01beecad4a58627fb0dd66a965ae24b71f3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9e37cbd813c00875104999beb3288f121eaa6d336768d2d912632f44511f7e67
MD5 9077ddf84dfefb7b887c10c1605f58fa
BLAKE2b-256 f449daad4d2f9e2a7604b41b41c559bc6f293c31e1975bb5929712ff0ed4b720

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8ff9fd0d84ecf280b320bdded7cc0379b9b7686a2167044e66c3fc663f9c19f5
MD5 639146b18b069e55822712f3a89ddc13
BLAKE2b-256 39c5764dd006c4a0568c711655b4022e99977e6ebbc588d77ef5a8a7266cc69a

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ce668bfa68214cdd162ccc0e43a3387f23457abb5234a6a70e4866dcf00496b3
MD5 86d9d14a6002f2355ca4720c8a8cd976
BLAKE2b-256 8136e53c2a2aa793f6fcdc0bbb7d64aae219ed31eb731396c4a378dd7b5ebb8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 54a028014eff9e640cd332ad91a5e51455e02b8e08704f67f704e66587306436
MD5 8b7f59ca682d30ba237bc001e33d8348
BLAKE2b-256 d5b2619fceaeddf9a3fa1a704785efa1f8b2ce3a8af469b459bef76b821ce18b

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp38-cp38-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8fe2f08d4695348e495dd9905bb104211c2c8b6bdb33a360ac3e3cca70fddc74
MD5 64eb9d8f4fd2a85e5fd7cccb6cea674e
BLAKE2b-256 73a90bdc540b407d56a45e1ad21d9b7d6e73e38f1fc8b2576b2b79cb1ee4f3fe

See more details on using hashes here.

File details

Details for the file autopysta-0.0.40.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1d4b704faaf6518fff3a07031bfa034b38fdfae7546267ce5b6629ba0dfe3ab8
MD5 d27f3e792375ae0b0aebe25530208487
BLAKE2b-256 4e4d4ee13818eec1d59f5c8419ee35b555333a35853639ddf67a59980cc2a842

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for autopysta-0.0.40.1-cp38-cp38-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 be0c0dcfa715eb0384ccd8f2024732fefc7b17a80c92c4e8927e7445d899c023
MD5 2ce6f3016180799a8255e147268ebc1d
BLAKE2b-256 52ba8c060710eaf1059264bcdeb41dd3ab5fe2b6dd757fe1d812b15bd4f2643f

See more details on using hashes here.

Supported by

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