argparser: add argparser and nice print
This commit is contained in:
39
dijsktra.py
39
dijsktra.py
@@ -2,6 +2,9 @@
|
||||
This module holds the logic for the solving
|
||||
"""
|
||||
|
||||
from args import Settings
|
||||
from parser import DataSet
|
||||
|
||||
|
||||
class Dijkstra():
|
||||
def __init__(self, graph: dict, connection: tuple):
|
||||
@@ -17,13 +20,13 @@ class Dijkstra():
|
||||
"prev": None}})
|
||||
self.table[connection[0]]["distance"] = (0, 0)
|
||||
|
||||
def algorithm(self, switch: str):
|
||||
assert(switch == "time" or switch == "co2")
|
||||
|
||||
print("Performing Dijkstra on the following graph...")
|
||||
def algorithm(self, settings: Settings):
|
||||
if settings.debug:
|
||||
print("Performing Dijkstra on the following graph...")
|
||||
for vertex in self.graph:
|
||||
print(f"{vertex}: {self.graph[vertex]}")
|
||||
print("")
|
||||
if settings.debug:
|
||||
print(f"{vertex}: {self.graph[vertex]}")
|
||||
print("")
|
||||
|
||||
for v1 in self.graph.keys():
|
||||
neighbors = self.graph[v1]
|
||||
@@ -35,18 +38,19 @@ class Dijkstra():
|
||||
time = self.table[v1]["distance"][0] + edge[0]
|
||||
co2 = self.table[v1]["distance"][1] + edge[1]
|
||||
# but only eval the one we want
|
||||
if switch == "time":
|
||||
if settings.sort == "time":
|
||||
if time < self.table[key]["distance"][0]:
|
||||
self.table[key]["distance"] = (time, co2)
|
||||
self.table[key]["prev"] = v1
|
||||
elif switch == "co2":
|
||||
elif settings.sort == "co2":
|
||||
if co2 < self.table[key]["distance"][1]:
|
||||
self.table[key]["distance"] = (time, co2)
|
||||
self.table[key]["prev"] = v1
|
||||
|
||||
print("Final Dijkstra table:", self.table)
|
||||
if settings.debug:
|
||||
print("Final Dijkstra table:", self.table)
|
||||
|
||||
def print_result(self, connection: tuple):
|
||||
def print_result(self, connection: tuple, dataset: DataSet):
|
||||
sequence = []
|
||||
(start, dest) = connection
|
||||
# start at destination
|
||||
@@ -57,8 +61,19 @@ class Dijkstra():
|
||||
current = self.table[sequence[-1]] # last element of list
|
||||
prev = current["prev"]
|
||||
sequence.append(prev)
|
||||
|
||||
sequence.reverse()
|
||||
print(sequence)
|
||||
|
||||
connection = dataset.connection
|
||||
frm = dataset.locations[connection[0]]
|
||||
to = dataset.locations[connection[1]]
|
||||
print(f"{frm.name}-->{to.name}")
|
||||
print("----")
|
||||
print(f"Lufstrecke: {frm.distance(to)}")
|
||||
total = ""
|
||||
for loc in sequence:
|
||||
total += f"{dataset.locations[loc].name}-->"
|
||||
# remove last arrow again
|
||||
total = total[0:-3]
|
||||
print(total)
|
||||
print("Total Time:", self.table[dest]["distance"][0])
|
||||
print("Total CO2:", self.table[dest]["distance"][1])
|
||||
|
||||
Reference in New Issue
Block a user