Updated matsim example with docs

This commit is contained in:
Ruben1729 2024-02-14 17:26:20 -05:00
parent 5f245f2e7d
commit 2d99352d09
5 changed files with 18 additions and 13 deletions

View File

@ -2,6 +2,8 @@
## Requirements
- [x] [JDK 17](https://www.oracle.com/java/technologies/downloads/#java17)
- [x] Python 3
- [x] Input file holding the network of the city (For example: merged-network.osm.pbf)
- [x] Input GeoJSON file with all buildings (For example: summerschool_all_buildings.geojson)
## Goal
Estimate city pollution coming from people driving in vehicles.
@ -12,18 +14,21 @@ Make sure to have JDK 17 installed and clone this repository.
## Usage
The file `main.py` is essentially a replica from MEB workflow. It constructs a city and then allows the user to apply characteristics to that given city.
The difference is in the implementation of MatSimEngine. This engine should create the following files:
- facilities.xml
- network.xml
- population.xml
- config.xml
The additional steps from the MEB workflow are:
1. Generate Matsim required xml files.
2. Run Matsim analysis.
3. Visualize Matsim results.
Facilities and network are quite simple as the facilities file can be built from our city object and the network file can be built using a matsim utility along with OpenStreetMaps data. The population file must be generated via other ways and the config file can be generated once the first 3 files are created.
### 1- Generate Matsim Required XML Files
Additionally, it may be interesting to include public transport in the simulation. This is a potentially simple task. Montreal offers the GTFS files for its public transport for free and the project [PT2MATSim](https://github.com/matsim-org/pt2matsim) allows you to create the MatSim xml files from the initial GTFS files.
This step is done in `matsim.py`. The class was built to replicate the behavior of other HUB export factory classes. It takes in a city object, an output directory, and then generates all the xml files required by Matsim.
## Visualization
### 2- Run Matsim Analysis
This step is done in `matsim_engine.py`. It simply runs the matsim.jar while providing the config.xml file generated in the previous step.
### 3- Visualize Matsim Results
This step is done in `matsim_visualizer.py`. It takes in a network and events xml file generated by the previous step and then displays it on a graph. It then shows in heatmap style the change in traffic over time.
Given we want to be able to visualize the results similar to the way the Munich scenario did it: https://www.matsim.org/gallery/munich.
The repository also has the code to visualize these results. It's a very basic implementation, but it basically uses matplotlib and nx to plot the network and how much traffic exists on a given link at a given time.

Binary file not shown.

View File

@ -25,10 +25,10 @@ try:
ConstructionFactory(construction_format, city).enrich()
UsageFactory(usage_format, city).enrich()
Matsim(city, 'output_files')._export()
Matsim(city, 'output_files').export()
MatSimEngine('output_files/Montreal_config.xml').run()
visualizer = MatsimVisualizer('output_files/Montreal/output_network.xml.gz', 'output_files/Montreal/output_events.xml.gz')
visualizer = MatsimVisualizer('output_files/Montreal/output_network.xml.gz', 'output_files/Montreal/output_events.xml.gz', 'output_files')
visualizer.visualize()
except Exception as ex:

View File

@ -95,7 +95,7 @@ class MatsimVisualizer():
traffic_data = self._cumulative_traffic[tick]
edge_colors = [self._cmap(self.norm(traffic_data.get(link['id'], 0))) for link in self._links]
edge_widths = [10 + self.norm(traffic_data.get(link['id'], 0)) * 10 for link in self._links]
edge_widths = [1 + self.norm(traffic_data.get(link['id'], 0)) * 10 for link in self._links]
plt.cla()
nx.draw(self._G, self._pos, node_size=0, node_color='blue', width=edge_widths, edge_color=edge_colors,