argparser: add argparser and nice print
This commit is contained in:
30
args.py
Normal file
30
args.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
"""
|
||||||
|
Parse commandline arguments and provide global variables for settings.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
class Settings():
|
||||||
|
file = None
|
||||||
|
sort = "time"
|
||||||
|
debug = False
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
parser = argparse.ArgumentParser(description='IHK Dijkstra Projekt.')
|
||||||
|
parser.add_argument('file', help='The input file.')
|
||||||
|
parser.add_argument(
|
||||||
|
'-sort',
|
||||||
|
default='time',
|
||||||
|
help='Whether to sort after "time" or "co2"')
|
||||||
|
parser.add_argument(
|
||||||
|
'-debug',
|
||||||
|
action='store_true',
|
||||||
|
help='Show debug output.')
|
||||||
|
args = parser.parse_args()
|
||||||
|
assert(args.sort == "time" or args.sort == "co2")
|
||||||
|
|
||||||
|
# update default values
|
||||||
|
self.file = args.file
|
||||||
|
self.debug = args.debug
|
||||||
|
self.sort = args.sort
|
||||||
39
dijsktra.py
39
dijsktra.py
@@ -2,6 +2,9 @@
|
|||||||
This module holds the logic for the solving
|
This module holds the logic for the solving
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from args import Settings
|
||||||
|
from parser import DataSet
|
||||||
|
|
||||||
|
|
||||||
class Dijkstra():
|
class Dijkstra():
|
||||||
def __init__(self, graph: dict, connection: tuple):
|
def __init__(self, graph: dict, connection: tuple):
|
||||||
@@ -17,13 +20,13 @@ class Dijkstra():
|
|||||||
"prev": None}})
|
"prev": None}})
|
||||||
self.table[connection[0]]["distance"] = (0, 0)
|
self.table[connection[0]]["distance"] = (0, 0)
|
||||||
|
|
||||||
def algorithm(self, switch: str):
|
def algorithm(self, settings: Settings):
|
||||||
assert(switch == "time" or switch == "co2")
|
if settings.debug:
|
||||||
|
print("Performing Dijkstra on the following graph...")
|
||||||
print("Performing Dijkstra on the following graph...")
|
|
||||||
for vertex in self.graph:
|
for vertex in self.graph:
|
||||||
print(f"{vertex}: {self.graph[vertex]}")
|
if settings.debug:
|
||||||
print("")
|
print(f"{vertex}: {self.graph[vertex]}")
|
||||||
|
print("")
|
||||||
|
|
||||||
for v1 in self.graph.keys():
|
for v1 in self.graph.keys():
|
||||||
neighbors = self.graph[v1]
|
neighbors = self.graph[v1]
|
||||||
@@ -35,18 +38,19 @@ class Dijkstra():
|
|||||||
time = self.table[v1]["distance"][0] + edge[0]
|
time = self.table[v1]["distance"][0] + edge[0]
|
||||||
co2 = self.table[v1]["distance"][1] + edge[1]
|
co2 = self.table[v1]["distance"][1] + edge[1]
|
||||||
# but only eval the one we want
|
# but only eval the one we want
|
||||||
if switch == "time":
|
if settings.sort == "time":
|
||||||
if time < self.table[key]["distance"][0]:
|
if time < self.table[key]["distance"][0]:
|
||||||
self.table[key]["distance"] = (time, co2)
|
self.table[key]["distance"] = (time, co2)
|
||||||
self.table[key]["prev"] = v1
|
self.table[key]["prev"] = v1
|
||||||
elif switch == "co2":
|
elif settings.sort == "co2":
|
||||||
if co2 < self.table[key]["distance"][1]:
|
if co2 < self.table[key]["distance"][1]:
|
||||||
self.table[key]["distance"] = (time, co2)
|
self.table[key]["distance"] = (time, co2)
|
||||||
self.table[key]["prev"] = v1
|
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 = []
|
sequence = []
|
||||||
(start, dest) = connection
|
(start, dest) = connection
|
||||||
# start at destination
|
# start at destination
|
||||||
@@ -57,8 +61,19 @@ class Dijkstra():
|
|||||||
current = self.table[sequence[-1]] # last element of list
|
current = self.table[sequence[-1]] # last element of list
|
||||||
prev = current["prev"]
|
prev = current["prev"]
|
||||||
sequence.append(prev)
|
sequence.append(prev)
|
||||||
|
|
||||||
sequence.reverse()
|
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 Time:", self.table[dest]["distance"][0])
|
||||||
print("Total CO2:", self.table[dest]["distance"][1])
|
print("Total CO2:", self.table[dest]["distance"][1])
|
||||||
|
|||||||
21
graph.py
21
graph.py
@@ -3,6 +3,7 @@ Module, which creates a graph
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from classes import DataSet, LocationType, TransportMethod, TransportKind
|
from classes import DataSet, LocationType, TransportMethod, TransportKind
|
||||||
|
from args import Settings
|
||||||
|
|
||||||
|
|
||||||
def calc_co2(distance: float, kind: TransportKind) -> float:
|
def calc_co2(distance: float, kind: TransportKind) -> float:
|
||||||
@@ -68,14 +69,15 @@ def calc_time(
|
|||||||
return waittime + (distance / TransportMethod.AIRPLANE.value.speed)
|
return waittime + (distance / TransportMethod.AIRPLANE.value.speed)
|
||||||
|
|
||||||
|
|
||||||
def create_graph(dataset: DataSet) -> dict:
|
def create_graph(dataset: DataSet, settings: Settings) -> dict:
|
||||||
"""
|
"""
|
||||||
Creates the initial graph, with all edges
|
Creates the initial graph, with all edges
|
||||||
"""
|
"""
|
||||||
locations = dataset.locations
|
locations = dataset.locations
|
||||||
graph: dict = {}
|
graph: dict = {}
|
||||||
|
|
||||||
print("Creating graph...")
|
if settings.debug:
|
||||||
|
print("Creating graph...")
|
||||||
|
|
||||||
# add nodes with no edges
|
# add nodes with no edges
|
||||||
for start in locations:
|
for start in locations:
|
||||||
@@ -87,7 +89,8 @@ def create_graph(dataset: DataSet) -> dict:
|
|||||||
# skip, if we wouldnt go anywhere
|
# skip, if we wouldnt go anywhere
|
||||||
if start == dest:
|
if start == dest:
|
||||||
continue
|
continue
|
||||||
print(f"Searching for nodes from {start} to {dest}...")
|
if settings.debug:
|
||||||
|
print(f"Searching for nodes from {start} to {dest}...")
|
||||||
distance = locations[start].distance(locations[dest])
|
distance = locations[start].distance(locations[dest])
|
||||||
|
|
||||||
# Individualtransport
|
# Individualtransport
|
||||||
@@ -100,7 +103,8 @@ def create_graph(dataset: DataSet) -> dict:
|
|||||||
"co2": co2,
|
"co2": co2,
|
||||||
"time": time}}
|
"time": time}}
|
||||||
graph[start].append(new_connection)
|
graph[start].append(new_connection)
|
||||||
print(f"Added new INDIVIDUAL connection from {start} to {dest}")
|
if settings.debug:
|
||||||
|
print(f"Added new INDIVIDUAL connection from {start} to {dest}")
|
||||||
|
|
||||||
# Train
|
# Train
|
||||||
is_haltestelle = locations[start].type == LocationType.HALTESTELLE
|
is_haltestelle = locations[start].type == LocationType.HALTESTELLE
|
||||||
@@ -121,10 +125,12 @@ def create_graph(dataset: DataSet) -> dict:
|
|||||||
"co2": co2,
|
"co2": co2,
|
||||||
"time": time}}
|
"time": time}}
|
||||||
graph[start].append(new_connection)
|
graph[start].append(new_connection)
|
||||||
print(f"Added new TRAIN connection from {start} to {dest}")
|
if settings.debug:
|
||||||
|
print(f"Added new TRAIN connection from {start} to {dest}")
|
||||||
|
|
||||||
# Flying
|
# Flying
|
||||||
print("Adding flights...")
|
if settings.debug:
|
||||||
|
print("Adding flights...")
|
||||||
flights = dataset.flights
|
flights = dataset.flights
|
||||||
for flight in flights:
|
for flight in flights:
|
||||||
start = flight
|
start = flight
|
||||||
@@ -138,6 +144,7 @@ def create_graph(dataset: DataSet) -> dict:
|
|||||||
"co2": co2,
|
"co2": co2,
|
||||||
"time": time}}
|
"time": time}}
|
||||||
graph[start].append(new_connection)
|
graph[start].append(new_connection)
|
||||||
print(f"Added new FLYING connection from {start} to {dest}")
|
if settings.debug:
|
||||||
|
print(f"Added new FLYING connection from {start} to {dest}")
|
||||||
|
|
||||||
return graph
|
return graph
|
||||||
|
|||||||
16
main.py
16
main.py
@@ -6,20 +6,20 @@ Requires: python > 3.10.x
|
|||||||
from parser import parse
|
from parser import parse
|
||||||
from graph import create_graph
|
from graph import create_graph
|
||||||
from dijsktra import Dijkstra
|
from dijsktra import Dijkstra
|
||||||
|
from args import Settings
|
||||||
|
|
||||||
INPUTFILE = "file.txt"
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# parse args
|
||||||
|
settings = Settings()
|
||||||
# parse inputfile
|
# parse inputfile
|
||||||
dataset: dict = parse(INPUTFILE)
|
dataset: dict = parse(settings)
|
||||||
# create graph
|
# create graph
|
||||||
graph: dict = create_graph(dataset)
|
graph: dict = create_graph(dataset, settings)
|
||||||
# solve
|
# solve
|
||||||
dijkstra = Dijkstra(graph, dataset.connection)
|
dijkstra: Dijkstra = Dijkstra(graph, dataset.connection)
|
||||||
dijkstra.algorithm("co2")
|
dijkstra.algorithm(settings)
|
||||||
dijkstra.print_result(dataset.connection)
|
dijkstra.print_result(dataset.connection, dataset)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
23
parser.py
23
parser.py
@@ -3,9 +3,10 @@ Parse the input file
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from classes import ParsingMode, Coordinate, LocationType, Location, DataSet
|
from classes import ParsingMode, Coordinate, LocationType, Location, DataSet
|
||||||
|
from args import Settings
|
||||||
|
|
||||||
|
|
||||||
def parse(filename: str) -> DataSet:
|
def parse(settings: Settings) -> DataSet:
|
||||||
"""
|
"""
|
||||||
Parse a given file with given format and return a DataSet containing the
|
Parse a given file with given format and return a DataSet containing the
|
||||||
parsed locations, flightschedules and wanted connection
|
parsed locations, flightschedules and wanted connection
|
||||||
@@ -13,7 +14,7 @@ def parse(filename: str) -> DataSet:
|
|||||||
locations: dict = {}
|
locations: dict = {}
|
||||||
flights: dict = {}
|
flights: dict = {}
|
||||||
connection: tuple = ()
|
connection: tuple = ()
|
||||||
with open(filename, "r") as file:
|
with open(settings.file, "r") as file:
|
||||||
for line in file.readlines():
|
for line in file.readlines():
|
||||||
line = line.replace("\n", "") # strip newline
|
line = line.replace("\n", "") # strip newline
|
||||||
line = line.replace(" ", "") # strip whitespaces
|
line = line.replace(" ", "") # strip whitespaces
|
||||||
@@ -25,21 +26,25 @@ def parse(filename: str) -> DataSet:
|
|||||||
# meta parsing
|
# meta parsing
|
||||||
match line:
|
match line:
|
||||||
case "Locations:":
|
case "Locations:":
|
||||||
print("Parsing `Locations`...")
|
if settings.debug:
|
||||||
|
print("Parsing `Locations`...")
|
||||||
current_parsing = ParsingMode.LOCATIONS
|
current_parsing = ParsingMode.LOCATIONS
|
||||||
continue
|
continue
|
||||||
case "FlightSchedule:":
|
case "FlightSchedule:":
|
||||||
print("Parsing `FlightSchedule`...")
|
if settings.debug:
|
||||||
|
print("Parsing `FlightSchedule`...")
|
||||||
current_parsing = ParsingMode.FLIGHTSCHEDULE
|
current_parsing = ParsingMode.FLIGHTSCHEDULE
|
||||||
continue
|
continue
|
||||||
case "FindBestConnections:":
|
case "FindBestConnections:":
|
||||||
print("Parsing `FindBestConnections`...")
|
if settings.debug:
|
||||||
|
print("Parsing `FindBestConnections`...")
|
||||||
current_parsing = ParsingMode.FINDCONNECTION
|
current_parsing = ParsingMode.FINDCONNECTION
|
||||||
continue
|
continue
|
||||||
|
|
||||||
match current_parsing:
|
match current_parsing:
|
||||||
case ParsingMode.LOCATIONS:
|
case ParsingMode.LOCATIONS:
|
||||||
print("Parsing location...")
|
if settings.debug:
|
||||||
|
print("Parsing location...")
|
||||||
splitted = line.split(";")
|
splitted = line.split(";")
|
||||||
assert(len(splitted) == 6) # make sure we have a location
|
assert(len(splitted) == 6) # make sure we have a location
|
||||||
id = splitted[0]
|
id = splitted[0]
|
||||||
@@ -59,7 +64,8 @@ def parse(filename: str) -> DataSet:
|
|||||||
location = Location(coord, continent, name, type)
|
location = Location(coord, continent, name, type)
|
||||||
locations.update({id: location})
|
locations.update({id: location})
|
||||||
case ParsingMode.FLIGHTSCHEDULE:
|
case ParsingMode.FLIGHTSCHEDULE:
|
||||||
print("Parsing flight schedule...")
|
if settings.debug:
|
||||||
|
print("Parsing flight schedule...")
|
||||||
splitted = line.split(";")
|
splitted = line.split(";")
|
||||||
assert(len(splitted) >= 2)
|
assert(len(splitted) >= 2)
|
||||||
id1 = splitted[0]
|
id1 = splitted[0]
|
||||||
@@ -78,7 +84,8 @@ def parse(filename: str) -> DataSet:
|
|||||||
"domestic": domestic}})
|
"domestic": domestic}})
|
||||||
continue
|
continue
|
||||||
case ParsingMode.FINDCONNECTION:
|
case ParsingMode.FINDCONNECTION:
|
||||||
print("Parsing connection...")
|
if settings.debug:
|
||||||
|
print("Parsing connection...")
|
||||||
splitted = line.split(";")
|
splitted = line.split(";")
|
||||||
assert(len(splitted) == 2)
|
assert(len(splitted) == 2)
|
||||||
connection = (splitted[0], splitted[1])
|
connection = (splitted[0], splitted[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user