-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFunctions.py
More file actions
109 lines (85 loc) · 4.26 KB
/
HelperFunctions.py
File metadata and controls
109 lines (85 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from datetime import timedelta
from WGUPSDistances import *
from Settings import *
class HelperFunctions:
@staticmethod
def calculate_time_to_arrive(distance_miles, speed_mph):
# time = distance/speed
return distance_miles/speed_mph
@staticmethod
def get_address_id(address_list, address_to_find):
# Fix inconsistency between columns,
# One has "Sta" and another has "Station"
if "Station" in address_to_find:
address_to_find = address_to_find.replace("Station", "Sta")
# Find the correct address.
# Check if package address is a substring of an address in table, and get corresponding row ID.
for address in address_list:
if address_to_find in address:
return address_list.index(address)
# Should not reach this, but throw an exception to easily track the issue.
raise Exception(f"Could not find the address for {address_to_find}")
@staticmethod
def find_closest_next_package(dijkstra_result, truck, packages):
# Convert the Dijkstra result to a map, keeping only the package ids in our truck.
current_data = {}
addresses = Distances.get_addresses()
for package_id in truck.packages:
package_address = packages.retrieve(package_id).address
package_address_id = HelperFunctions.get_address_id(addresses, package_address)
current_data[package_id] = dijkstra_result[package_address_id]
# current_data = {}
# for key, value in enumerate(dijkstra_result):
# if key in truck.packages:
# current_data[key] = value
#
# print(truck.packages)
if Settings.debug:
print("Dijkstra Result: " + str(dijkstra_result))
print("Data: " + str(current_data))
# Find key with the minimum value. Represents the address with minimum distance.
shortest_path_product_id = min(current_data, key=current_data.get)
if Settings.debug:
print(f"The next package is {shortest_path_product_id} with distance {current_data[shortest_path_product_id]}")
package_address = packages.retrieve(shortest_path_product_id).address
return shortest_path_product_id, current_data[shortest_path_product_id], package_address
# Cheap method to have the table look nice without any imports.
# Adds enough spacing to make a pretty table.
@staticmethod
def special_print(str, isAddress):
base_length = 8
if isAddress == True:
base_length = 40
if len(str) < base_length:
diff = base_length - len(str)
str = str + (" " * diff)
return str
@staticmethod
def print_package_with_status(package_ids, packagesHashTable, time):
for package_id in package_ids:
# Get the package from the hash table.
package = packagesHashTable.retrieve(package_id)
# Determine status based on truck time and package delivery time.
status = ""
if (time < package.corresponding_truck_departure_time) or package.corresponding_truck_departure_time == timedelta():
status = "At Hub"
elif package.corresponding_truck_departure_time < time < package.time_delivered:
status = "En route"
elif time > package.time_delivered:
status = "Delivered"
else:
# Note: we shouldn't ever reach this.
status = "Unknown"
# If delivered, provide the delivery time.
deliveryTime = ""
if status != "Delivered":
deliveryTime = "N/A"
else:
deliveryTime = package.time_delivered
print(
"Package ID: " + HelperFunctions.special_print(str(package.id), False) + "\t| " +
"Package Address: " + HelperFunctions.special_print(str(package.address), True) + "\t| " +
"Package Deadline: " + HelperFunctions.special_print(str(package.delivery_deadline), False) + "\t| " +
"Package Status: " + HelperFunctions.special_print(status, False) + "\t| " +
"Time Delivered: " + HelperFunctions.special_print(str(deliveryTime), False) + "\t| "
)