code styling: add comments and fix pep8
This commit is contained in:
34
classes.py
34
classes.py
@@ -1,14 +1,24 @@
|
|||||||
|
"""
|
||||||
|
All types used in the project
|
||||||
|
"""
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
class ParsingMode(Enum):
|
class ParsingMode(Enum):
|
||||||
|
"""
|
||||||
|
Used for parsing
|
||||||
|
"""
|
||||||
LOCATIONS = 1
|
LOCATIONS = 1
|
||||||
FLIGHTSCHEDULE = 2
|
FLIGHTSCHEDULE = 2
|
||||||
FINDCONNECTION = 3
|
FINDCONNECTION = 3
|
||||||
|
|
||||||
|
|
||||||
class LocationType(Enum):
|
class LocationType(Enum):
|
||||||
|
"""
|
||||||
|
Differenciate between types of location
|
||||||
|
"""
|
||||||
LOCATION = 1
|
LOCATION = 1
|
||||||
HALTESTELLE = 2
|
HALTESTELLE = 2
|
||||||
FLUGHAFEN = 3
|
FLUGHAFEN = 3
|
||||||
@@ -27,6 +37,10 @@ class TransportData():
|
|||||||
|
|
||||||
|
|
||||||
class TransportMethod(Enum):
|
class TransportMethod(Enum):
|
||||||
|
"""
|
||||||
|
More precise methods of transportation.
|
||||||
|
They are subgroups of TransportKind.
|
||||||
|
"""
|
||||||
WALK = TransportData(0, 4)
|
WALK = TransportData(0, 4)
|
||||||
CITY = TransportData(0.189, 30)
|
CITY = TransportData(0.189, 30)
|
||||||
AUTOBAHN = TransportData(0.189, 100)
|
AUTOBAHN = TransportData(0.189, 100)
|
||||||
@@ -37,7 +51,8 @@ class TransportMethod(Enum):
|
|||||||
|
|
||||||
class TransportKind(Enum):
|
class TransportKind(Enum):
|
||||||
"""
|
"""
|
||||||
distance_penalty
|
General transportation kind.
|
||||||
|
Value: distance_penalty
|
||||||
"""
|
"""
|
||||||
INDIVIDUAL = 0.2
|
INDIVIDUAL = 0.2
|
||||||
TRAIN = 0.1
|
TRAIN = 0.1
|
||||||
@@ -57,14 +72,20 @@ class Coordinate:
|
|||||||
|
|
||||||
|
|
||||||
class Location:
|
class Location:
|
||||||
def __init__(self, coord: Coordinate, continent: int, name: str, type: LocationType):
|
def __init__(
|
||||||
|
self,
|
||||||
|
coord: Coordinate,
|
||||||
|
continent: int,
|
||||||
|
name: str,
|
||||||
|
type: LocationType):
|
||||||
self.coord = coord
|
self.coord = coord
|
||||||
self.continent = continent
|
self.continent = continent
|
||||||
self.name = name
|
self.name = name
|
||||||
self.type = type
|
self.type = type
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{{Location: {self.coord}, continent: {self.continent}, name: {self.name}, type: {self.type}}}"
|
return f"{{Location: {self.coord}, continent: {self.continent}, \
|
||||||
|
name: {self.name}, type: {self.type}}}"
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
@@ -88,13 +109,18 @@ class Location:
|
|||||||
|
|
||||||
|
|
||||||
class DataSet:
|
class DataSet:
|
||||||
|
"""
|
||||||
|
Represent the parsed data
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, locations: dict, flights: dict, connection: tuple):
|
def __init__(self, locations: dict, flights: dict, connection: tuple):
|
||||||
self.locations = locations
|
self.locations = locations
|
||||||
self.flights = flights
|
self.flights = flights
|
||||||
self.connection = connection
|
self.connection = connection
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"locations: {self.locations}, flights: {self.flights}, connection: {self.connection}"
|
return f"locations: {self.locations}, flights: {self.flights}, \
|
||||||
|
connection: {self.connection}"
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ class Dijkstra():
|
|||||||
self.graph = graph
|
self.graph = graph
|
||||||
self.table = {}
|
self.table = {}
|
||||||
for vertex in graph:
|
for vertex in graph:
|
||||||
self.table.update({vertex: {"distance": (9999999, 999999), "prev": None}})
|
self.table.update({vertex: {
|
||||||
|
"distance": (9999999, 999999),
|
||||||
|
"prev": None}})
|
||||||
self.table[connection[0]]["distance"] = (0, 0)
|
self.table[connection[0]]["distance"] = (0, 0)
|
||||||
|
|
||||||
def algorithm(self, switch: str):
|
def algorithm(self, switch: str):
|
||||||
@@ -21,6 +23,7 @@ class Dijkstra():
|
|||||||
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]}")
|
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]
|
||||||
@@ -41,7 +44,7 @@ class Dijkstra():
|
|||||||
self.table[key]["distance"] = (time, co2)
|
self.table[key]["distance"] = (time, co2)
|
||||||
self.table[key]["prev"] = v1
|
self.table[key]["prev"] = v1
|
||||||
|
|
||||||
print("self.table:", self.table)
|
print("Final Dijkstra table:", self.table)
|
||||||
|
|
||||||
def print_result(self, connection: tuple):
|
def print_result(self, connection: tuple):
|
||||||
sequence = []
|
sequence = []
|
||||||
|
|||||||
15
graph.py
15
graph.py
@@ -41,19 +41,23 @@ def calc_time(
|
|||||||
return distance / TransportMethod.WALK.value.speed
|
return distance / TransportMethod.WALK.value.speed
|
||||||
elif distance <= 10 + 1:
|
elif distance <= 10 + 1:
|
||||||
speed = 1 / TransportMethod.WALK.value.speed
|
speed = 1 / TransportMethod.WALK.value.speed
|
||||||
return speed + ((distance - 1) / TransportMethod.CITY.value.speed)
|
new_dist = (distance - 1)
|
||||||
|
speed += (new_dist / TransportMethod.CITY.value.speed)
|
||||||
|
return speed
|
||||||
elif distance < 2000 + 10 + 1:
|
elif distance < 2000 + 10 + 1:
|
||||||
speed = 1 / TransportMethod.WALK.value.speed
|
speed = 1 / TransportMethod.WALK.value.speed
|
||||||
speed += 10 / TransportMethod.CITY.value.speed
|
speed += 10 / TransportMethod.CITY.value.speed
|
||||||
return speed + ((distance - 1 - 10) / TransportMethod.AUTOBAHN.value.speed)
|
new_dist = (distance - 1 - 10)
|
||||||
|
speed += (new_dist / TransportMethod.AUTOBAHN.value.speed)
|
||||||
|
return speed
|
||||||
case TransportKind.TRAIN:
|
case TransportKind.TRAIN:
|
||||||
if distance <= 25:
|
if distance <= 25:
|
||||||
return distance / TransportMethod.OEPNV.value.speed
|
return distance / TransportMethod.OEPNV.value.speed
|
||||||
else:
|
else:
|
||||||
return (distance / TransportMethod.ICE.value.speed)
|
return (distance / TransportMethod.ICE.value.speed)
|
||||||
case TransportKind.FLYING:
|
case TransportKind.FLYING:
|
||||||
assert(flights != None)
|
assert(flights)
|
||||||
assert(frm != None)
|
assert(frm)
|
||||||
stops = flights[frm]["stops"]
|
stops = flights[frm]["stops"]
|
||||||
domestic = flights[frm]["domestic"]
|
domestic = flights[frm]["domestic"]
|
||||||
if domestic:
|
if domestic:
|
||||||
@@ -100,9 +104,10 @@ def create_graph(dataset: DataSet) -> dict:
|
|||||||
|
|
||||||
# Train
|
# Train
|
||||||
is_haltestelle = locations[start].type == LocationType.HALTESTELLE
|
is_haltestelle = locations[start].type == LocationType.HALTESTELLE
|
||||||
|
is_airport = locations[start].type == LocationType.FLUGHAFEN
|
||||||
is_same_continent = locations[start].is_same_continent(locations[dest])
|
is_same_continent = locations[start].is_same_continent(locations[dest])
|
||||||
# trains only drive from haltestelle and same continent
|
# trains only drive from haltestelle and same continent
|
||||||
if is_haltestelle and is_same_continent:
|
if (is_haltestelle or is_airport) and is_same_continent:
|
||||||
dest_type = locations[dest].type
|
dest_type = locations[dest].type
|
||||||
# there are only trains between haltestellen or airports
|
# there are only trains between haltestellen or airports
|
||||||
is_dest_haltestelle = dest_type == LocationType.HALTESTELLE
|
is_dest_haltestelle = dest_type == LocationType.HALTESTELLE
|
||||||
|
|||||||
@@ -72,7 +72,10 @@ def parse(filename: str) -> DataSet:
|
|||||||
domestic = True
|
domestic = True
|
||||||
else:
|
else:
|
||||||
domestic = False
|
domestic = False
|
||||||
flights.update({id1: {"to": id2, "stops": stops, "domestic": domestic}})
|
flights.update({id1: {
|
||||||
|
"to": id2,
|
||||||
|
"stops": stops,
|
||||||
|
"domestic": domestic}})
|
||||||
continue
|
continue
|
||||||
case ParsingMode.FINDCONNECTION:
|
case ParsingMode.FINDCONNECTION:
|
||||||
print("Parsing connection...")
|
print("Parsing connection...")
|
||||||
|
|||||||
Reference in New Issue
Block a user