matsim-proto/matsim_engine.py
2024-01-26 12:36:53 -05:00

66 lines
1.9 KiB
Python

import subprocess
import xmltodict
from matsim_activity_to_matsim_schedule import MatsimActivityToMatsimSchedule
from hub_function_to_matsim_activity import HubFunctionToMatsimActivity
class MatSimEngine:
def __init__(self, city, output_file_path):
self._city = city
self._output_file_path = output_file_path
facilities_dict = {
'facilities': {
'@name': 'Montreal Facilities',
'facility': []
}
}
hub_function_to_matsim = HubFunctionToMatsimActivity()
matsim_schedule = MatsimActivityToMatsimSchedule()
# 1- Facilities
# TODO: Add the ability for a building to have multiple activities
for building in city.buildings:
activity = hub_function_to_matsim.dictionary[building.function]
schedule = matsim_schedule.dictionary[activity]
start_time, end_time = schedule.split('-')
facility = {
'@id': building.name,
'@x': str(building.centroid[0]),
'@y': str(building.centroid[1]),
'activity': {
'@type': activity,
'capacity': {
'@value': '4.0' # TODO: Replace with a proper value taken from function
},
'opentime': {
'@day': 'wkday',
'@start_time': start_time,
'@end_time': end_time
}
}
}
facilities_dict['facilities']['facility'].append(facility)
# Convert the Python dictionary to an XML string
xml_content = xmltodict.unparse(facilities_dict, pretty=True)
# Write the XML to the file
with open(f"{output_file_path}/montreal_facilities.xml", 'w') as file:
file.write(xml_content)
# 2- Network
# 3- Population
# 4- Config Generation
def run(self):
java_path = "java"
jar_path = "matsim.jar"
command = [java_path, "-jar", jar_path]
# Must generate this config file first.
# command.append(config_file_path)
subprocess.run(command)