Skip to content

Conversation

@PRIYANSHU2026
Copy link

GIS-Based Traffic Simulation with Mesa

Overview

bangalore_traffic_congestion

This project simulates urban traffic congestion using the Mesa agent-based modeling framework and OSMnx for extracting road network data from OpenStreetMap. It visualizes traffic patterns and congestion in a critical urban area in Bangalore, India. The simulation includes different vehicle types and their movement on the road network, and the results are animated into a GIF to showcase the traffic dynamics over time.

Why We Need This

25BGHEBBALJUNCTION
181025130410-01-bangalore-traffic-file-1025

Problem Definition

High vehicle density and complex road networks are significant contributors to traffic congestion in Indian cities, especially in densely populated urban areas. This issue affects millions of travelers, leading to:

  • Increased travel duration
  • Higher fuel consumption
  • Elevated pollution levels

In cities like Bangalore, also known as the Tech Hub or Silicon Valley of India, these problems are exacerbated by rapid urbanization and technological growth.

Objective

By developing and refining this traffic simulation project, we aim to deliver more effective and adaptable solutions for managing traffic congestion in Indian cities. The project has the potential to:

  • Improve traffic flow and reduce congestion
  • Decrease travel time and fuel consumption
  • Lower pollution levels
  • Enhance the overall quality of life for urban residents

Features

  • Urban Traffic Simulation: Models traffic flow using various vehicle types (cars, trucks, bikes).
  • OSMnx Integration: Utilizes OSMnx to extract and visualize road networks from OpenStreetMap.
  • Animated Visualization: Generates an animated GIF showing traffic patterns and vehicle movements.
  • Dynamic Visualization: Updates the visualization in real-time to reflect vehicle positions on the map.

Key Contribution of Mesa

Mesa provides a straightforward and efficient framework to create, manage, and simulate the movement of vehicles on a road network. It allows for the simple creation of agents (vehicles) that follow predefined paths, interact within a given space, and produce simulation results without unnecessary complexity.

Mesa is used because it enables you to build a functional and effective traffic simulation without overcomplicating the model. Its integration with other libraries like OSMnx and NetworkX ensures that the model remains easy to understand and maintain, focusing on delivering practical insights rather than adding complexity.

Requirements

  • Python 3.11
  • OSMnx: pip install osmnx
  • NetworkX: pip install networkx
  • Folium: pip install folium
  • Mesa: pip install mesa
  • PIL: pip install pillow

Setup

  1. Clone the Repository

    git clone <repository-url>
    cd <repository-directory>
  2. Install Dependencies

    pip install -r requirements.txt

Usage

  1. Update Bounding Box Coordinates

    Modify the north, south, east, and west variables in the script to reflect the area you want to simulate.

  2. Run the Simulation

    Execute the script to run the traffic simulation and generate the animated GIF:

    python traffic_simulation.py
  3. View the Results

    The resulting animated GIF will be saved in the Data directory. You can view it directly from your file explorer or display it within a Jupyter Notebook using:

    from IPython.display import Image as IPImage, display
    display(IPImage(filename='Data/bangalore_traffic_congestion.gif'))

Code Overview

  • TrafficGeoSpace: Defines the space in which the agents move, based on the road network graph.
  • VehicleAgent: Represents individual vehicles, including their routes and movement.
  • TrafficModel: Manages the simulation, creating vehicles and stepping through the simulation.
  • Visualization: Uses Folium to create maps and convert HTML maps to PNG images, which are then compiled into an animated GIF.

Future Work

The project can be extended to address the following aspects:

  • Advanced Traffic Management: Incorporate real-time traffic data and predictive analytics for dynamic traffic management.
  • Integration with Smart City Solutions: Connect with IoT sensors and smart traffic lights for a more integrated approach.
  • Scenario Testing: Simulate various scenarios such as road closures, construction, or major events to evaluate their impact on traffic.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • OSMnx: For providing easy access to OpenStreetMap data.
  • Mesa: For the agent-based modeling framework.
  • Folium: For interactive map visualizations.

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Good start, looks like it's scoped a lot better!

A few points:

  • Could you put the Model and Agent class code in separate files, like other examples also do? This would make the model also testable and runnable on itself.
  • Could you update the notebook text and readme to actually be descriptive about what's. Please write it yourself, the quality, style and verbosity of this GPT text is really not good enough.

@PRIYANSHU2026
Copy link
Author

Good start, looks like it's scoped a lot better!

A few points:

  • Could you put the Model and Agent class code in separate files, like other examples also do? This would make the model also testable and runnable on itself.
  • Could you update the notebook text and readme to actually be descriptive about what's. Please write it yourself, the quality, style and verbosity of this GPT text is really not good enough.

sir I can make but it required visuvalization server

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Sorry I don't understand what you mean.

@PRIYANSHU2026
Copy link
Author

sir in model and class it will show direct output no visuvalization like step , start and stop

Could you update the notebook text and readme to actually be descriptive about what's. Please write it yourself, the quality, style and verbosity of this GPT text is really not good enough.

Sir, the notebook I wrote by myself is small and easy.

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Yes, that's both good.

Just put the model code and agent code in separate files (model.py and agent.py), and then import the TrafficModel into your notebook.

@PRIYANSHU2026
Copy link
Author

ok sure sir

@EwoutH EwoutH requested a review from wang-boyu August 17, 2024 15:52
@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

@wang-boyu could I get your perspective of the use of the gis/space in this example?

@PRIYANSHU2026
Copy link
Author

Sir, I wanted to know if I should remove the large code sections from the notebook after using model.py and agent.py.

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Yes, that's indeed the idea.

@PRIYANSHU2026
Copy link
Author

image

sir have it is correct

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Structure looks good!

What's in test.py?

@PRIYANSHU2026
Copy link
Author

PRIYANSHU2026 commented Aug 17, 2024

What's in test.py?

image here i initialized my model , run and save as gif file

@PRIYANSHU2026
Copy link
Author

sir should I add in this pr as differnt folder because when i am running on notebook same code it showing me
image

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

It’s not a structuring problem, but within the model code itself.

Without sharing the latest version of the code I can’t help you.

@PRIYANSHU2026
Copy link
Author

sir i added the code

@PRIYANSHU2026
Copy link
Author

@EwoutH thank you sir its working in different notebook

self.G = G # Assign the OSMnx graph to the space
self.crs = crs # Coordinate Reference System

class VehicleAgent(mesa.Agent):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this class.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@PRIYANSHU2026
Copy link
Author

done sir please check it

@PRIYANSHU2026
Copy link
Author

sir If you don't mind can talk at google meet sesion please

@EwoutH
Copy link
Member

EwoutH commented Aug 17, 2024

Do you mind me fixing a few things for you?

@PRIYANSHU2026
Copy link
Author

no sir it want be any problem

@PRIYANSHU2026
Copy link
Author

Add some useful markdown cells in the Jupyter notebook

@EwoutH sir like what i have add in jupyter notebook

@EwoutH
Copy link
Member

EwoutH commented Aug 18, 2024

I can only see the updated Readme.

@PRIYANSHU2026
Copy link
Author

Sorry sir i m asking what i have to add in notebook

@EwoutH
Copy link
Member

EwoutH commented Aug 18, 2024

  • A quick introduction
  • What Mesa elements will be showcased in the example
  • How Mesa and UXsim can work together

I think those are the most important.

@PRIYANSHU2026
Copy link
Author

PRIYANSHU2026 commented Aug 18, 2024

sir already i have added introduction and about mesa in this model
image

@PRIYANSHU2026
Copy link
Author

PRIYANSHU2026 commented Aug 18, 2024

How Mesa and UXsim can work together

Sir, should I create code and a simulation? If I do, it might become messy.

@EwoutH
Copy link
Member

EwoutH commented Aug 18, 2024

Let me take a better look once we have gboeing/osmnx#1209 figured out.

@PRIYANSHU2026
Copy link
Author

Sir is that sufficient

@PRIYANSHU2026
Copy link
Author

sir i wanted to know is there any Discord server for communication

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

sir i wanted to know is there any Discord server for communication

We have a matrix chat: https://matrix.to/#/#project-mesa:matrix.org

I want to let you know we figured out the issue in GeoPandas: It was an incompatibility between Shapely and the new NumPy 2.1.0 release. So I got the model working on my end again, and will try to further review this PR this evening (CEST).

We're getting close!

@PRIYANSHU2026
Copy link
Author

@EwoutH ok sir, I hope this time I'm going in the right direction.

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

Yes, it will take a little bit more cleanup, but it's a huge improvement from before!

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

@wang-boyu @tpike3 this PR uses a custom visualisation using folium and selenium. It's quite heavy, taking around 20 seconds to save each image for each step (200 sec for 10 steps)! The results is a gif like this:

bangalore_traffic_congestion

Is that acceptable for the GIS examples?

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

@PRIYANSHU2026 what I fail to grasp is that the Readme describes the model looking into traffic congestion, but nowhere in the model congestion is actually implemented.

@PRIYANSHU2026
Copy link
Author

PRIYANSHU2026 commented Aug 19, 2024

@EwoutH sir, It describes how vehicles move in a particular lane. If we include more than 200 vehicles, the actual implementation will show how vehicles replace each other at specific adjust in traffic points and how they move forward, especially in urban areas, when observed step by step.

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

the actual implementation will show how vehicles replace each other at specific adjust in traffic points and how they move forward

I see nothing in the code that indicates that vehicles would be moving slower once there more on a link / route. Could you point me to that part in the code?

@PRIYANSHU2026
Copy link
Author

image here sir by initializing Agent and traffic space.

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

I went thought it line by line. Please point me to the specific part.

@PRIYANSHU2026
Copy link
Author

image

in vehicle agent where we describe the steps and its showing flow of movement and replacing in that i didnt describe fast and slow .

@EwoutH
Copy link
Member

EwoutH commented Aug 19, 2024

PRIYANSHU, you need to learn to say "I don't know". It's incredibly important to be able to admit that you don't know something in science, because otherwise you have to start making things up, and then facts and guesses get mixed up.

I'm closing this PR, because you clearly don't understand your code base.

I would recommend taking a break from open-source contributing. If you want to return to it, try to:

  • Improve you communication skills, including asking questions, writing proposals and being honest in what you know and don't know.
  • Improve your core developers skills: OOP and git
  • Focus on doing small things really well, and not huge things mediocre (or bad).

Unfortunately, my time budget for guiding and reviewing this kind of PRs is empty for the next months, so I won't be able to further review PRs.

No hard feelings, but this just doesn't work this way.

If any other maintainer wants to pick this up feel free to reopen the PR.

@EwoutH EwoutH closed this Aug 19, 2024
@PRIYANSHU2026
Copy link
Author

@EwoutH ok sir being honest i am working and improving my self but definitely you pointed my deficiecny and i will work on that thank you for your guidance i very happy to getting mentored by @EwoutH

may be it blind me for Gsoc 25 but from this PR i have learn how to improve my self and how you have help me to correct the code and you also got Bug in OSMnx.

Thank you again now i will learn my core skill and communication .

for the next months, so I won't be able to further review PRs.

??

@PRIYANSHU2026
Copy link
Author

And I m requesting to All mentor of Mesa @jackiekazil @rht @wang-boyu @tpike3 please look on this PR as model working fine but i didn't described Read me and code properly

And again special thanks to @EwoutH for guidance and help at every stage.

@PRIYANSHU2026
Copy link
Author

@jackiekazil @rht @tpike3 @wang-boyu are you there i am waiting your response

@mesa mesa locked as too heated and limited conversation to collaborators Aug 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants