78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
"""
|
|
Module, which holds the logic for the solving algorithms
|
|
"""
|
|
|
|
from classes import DataSet, LocationType, Location, TransportMethod
|
|
|
|
|
|
def calc_co2(distance: float, method: TransportMethod) -> float:
|
|
"""
|
|
Return co2 emission in kg
|
|
"""
|
|
return (distance * method.value.distance_penalty) * method.value.co2
|
|
|
|
|
|
def calc_time(distance: float, method: TransportMethod) -> float:
|
|
"""
|
|
Return time taking in h
|
|
"""
|
|
return (distance * method.value.distance_penalty) / method.value.speed
|
|
|
|
|
|
def add_node(graph: dict, frm: str, to: str, locs: dict, method: TransportMethod):
|
|
"""
|
|
Add a node into the graph
|
|
"""
|
|
distance = locs[frm].distance(locs[to])
|
|
co2 = calc_co2(distance, method)
|
|
time = calc_time(distance, method)
|
|
new_connection: dict = {to: {"type": method, "co2": co2, "time": time}}
|
|
graph[frm].append(new_connection)
|
|
print(f"Added node from {frm} to {to} with type {method}.")
|
|
|
|
|
|
def create_graph(dataset: DataSet) -> dict:
|
|
"""
|
|
Creates the initial graph, with all edges
|
|
"""
|
|
locations = dataset.locations
|
|
graph: dict = {}
|
|
|
|
# add nodes with no edges
|
|
for start in locations:
|
|
graph.update({start: []})
|
|
|
|
# add edges
|
|
for start in locations:
|
|
for dest in locations:
|
|
# skip, if we wouldnt go anywhere
|
|
if start == dest:
|
|
continue
|
|
print(f"Searching for nodes from {start} to {dest}...")
|
|
distance = locations[start].distance(locations[dest])
|
|
|
|
# Individualtransport
|
|
if distance <= 1:
|
|
add_node(graph, start, dest, locations, TransportMethod.WALK)
|
|
if distance <= 10 + 1:
|
|
add_node(graph, start, dest, locations, TransportMethod.CITY)
|
|
if distance <= 2000 + 10 + 1:
|
|
add_node(graph, start, dest, locations, TransportMethod.AUTOBAHN)
|
|
|
|
# Train
|
|
if locations[start].type == LocationType.HALTESTELLE:
|
|
dest_type = locations[dest].type
|
|
# there are only trains between haltestellen or airports
|
|
if dest_type == LocationType.HALTESTELLE or dest_type == LocationType.FLUGHAFEN:
|
|
if distance <= 25:
|
|
add_node(graph, start, dest, locations, TransportMethod.OEPNV)
|
|
else:
|
|
add_node(graph, start, dest, locations, TransportMethod.ICE)
|
|
|
|
# Flying
|
|
flights = dataset.flights
|
|
for flight in flights:
|
|
add_node(graph, start, dest, locations, TransportMethod.AIRPLANE)
|
|
|
|
return graph
|