Tick when time changes rather than on event

This commit is contained in:
Ruben1729 2024-03-12 13:32:47 -04:00
parent 2ab91cfaeb
commit 35ec942d94
5 changed files with 117 additions and 106 deletions

Binary file not shown.

10
main.py
View File

@ -1,3 +1,4 @@
import json
from pathlib import Path
from hub.imports.geometry_factory import GeometryFactory
@ -10,7 +11,7 @@ from matsim import Matsim
from matsim_visualizer import MatsimVisualizer
try:
file_path = (Path(__file__).parent / 'input_files' / 'mtlbld_v4.geojson')
file_path = (Path(__file__).parent / 'input_files' / 'summerschool_all_buildings.geojson')
construction_format = 'nrcan'
usage_format = 'nrcan'
energy_systems_format = 'montreal_custom'
@ -18,7 +19,7 @@ try:
out_path = (Path(__file__).parent / 'output_files')
city = GeometryFactory('geojson',
path=file_path,
height_field='ETAGE_HORS',
height_field='citygml_me',
year_of_construction_field='ANNEE_CONS',
function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city
@ -31,10 +32,13 @@ try:
visualizer = MatsimVisualizer(
'output_files/Montreal',
'output_files',
(3854635.608067, 6040255.047194, 3855550.392442, 6039416.667303)
(3854635.608067, 6040255.047194, 3855550.392442, 6039416.667303),
True
)
visualizer.visualize()
except Exception as ex:
print('error: ', ex)
print('[simulation abort]')

View File

@ -55,14 +55,11 @@ class Matsim:
"""
Exports the city's facilities data to XML and shapefile formats.
"""
buildings_shape_data = {
'id': [],
'geometry': []
}
facilities_xml = etree.Element('facilities', name=self._facilities['name'])
for building in self._city.buildings:
try:
facility = {
'id': building.name,
'x': str(building.centroid[0]),
@ -109,6 +106,8 @@ class Matsim:
facility['activity'].append(activity_info)
self._facilities['facility'].append(facility)
except Exception as ex:
print('error: ', ex)
viewport = Polygon([(MONTREAL_LEFT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_BOTTOM), (MONTREAL_LEFT, MONTREAL_BOTTOM)])
@ -142,6 +141,8 @@ class Matsim:
"""
Generates and exports the city's population data to an XML file.
"""
try:
population = etree.Element("population")
id = 0
@ -212,6 +213,8 @@ class Matsim:
current_work += 1
id += 1
except Exception as ex:
print('error: ', ex)
# Write xml content to file
xml_content = etree.tostring(population, pretty_print=True, encoding='UTF-8').decode('utf-8')

View File

@ -92,11 +92,14 @@ class MatsimVisualizer:
with gzip.open(self._events_file_path, 'rb') as file:
events_doc = etree.parse(file)
current_time = None
self._tick = 0
for event in events_doc.xpath('/events/event'):
link_id = event.get('link')
event_type = event.get('type')
time = float(event.get('time'))
if current_time is None:
current_time = float(event.get('time'))
ticked = False
if link_id is not None and event_type is not None and time is not None:
@ -115,8 +118,9 @@ class MatsimVisualizer:
cumulative_traffic[link_id] -= 1
ticked = True
if ticked:
if ticked and current_time != time:
self._tick += 1
current_time = time
def _create_graph(self):
for node_id, coords in self._nodes.items():