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
-
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
-
Vehicle Creation Options
Users can choose different vehicle creation strategies for each lane, defining how vehicles are added to the simulation.
-
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
-
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
-
Build the Extension
Use the following command to build the C++ extension:
python setup.py build_ext
This will:
- Use
CMake
to compile the C++ code. - Generate the
autopysta.py
wrapper and the platform-specific _autopysta.*
shared library. - Copy the generated binaries to:
- The root
autopysta
directory. - The
examples/autopysta
directory.
-
Verify the Build
After building, check the autopysta
directory (both in the root and inside examples/
) for:
autopysta.py
: Python wrapper._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.
import autopysta as ap
print(ap.version())
length = 1000
initial_lanes = 4
merge_position = 300
diverge_position = 700
highway_geometry = ap.Geometry(length, initial_lanes, merge_position, diverge_position)
idm_model = ap.idm()
gipps_model = ap.gipps()
spacing = 15
speed = 10
flow_rate = 0.5
lane_creators = [
ap.FixedStateCreator(gipps_model, spacing, speed),
ap.FixedDemandCreator(idm_model, flow_rate),
ap.FixedStateCreator(idm_model, spacing, speed),
ap.FixedDemandCreator(gipps_model, flow_rate),
]
lane_change_model = ap.lcm_gipps()
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
)
simulation_results = simulation.run()
plotting = True
if plotting:
simulation_results.plot_x_vs_t()
simulation_results.plot_x_vs_t(lane=1)
simulation_results.plot_x_vs_t(lane=2)
simulation_results.plot_x_vs_t(lane=3)
simulation_results.plot_x_vs_t(lane=4)
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. 🚦