-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (105 loc) · 3.6 KB
/
main.py
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
110
111
112
113
114
115
116
117
import numpy as np
import random
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
D = [
[0,8943,8019,3652,10545],
[8943,0,2619,6317,2078],
[8019,2619,0,5836,4939],
[3652,6317,5836,0,7825],
[10545,2078,4939,7825,0]
]
# https://timeforchange.org/co2-emissions-shipping-goods
# assume 20g per km per metric ton (of pineapples)
co2 = 0.020
# DATA BLOCK ENDS
# these variables are initialised to nonsensical values
# your program should determine the correct values for them
smallest = 1000000
bestroute = [0, 0, 0, 0, 0]
def permutations(route, ports):
#print(f"route so far: {route}, ports: {ports}")
if not ports or len(ports) == 0:
global co2, smallest, bestroute
print(' '.join([portnames[i] for i in route]))
#for i in range(0, len(route) - 1):
# emission += D[route[i]][route[i+1]]
emission = co2 * sum(D[i][j] for i, j in zip(route[:-1], route[1:]))
if emission < smallest:
smallest = emission
bestroute = list(route)
else:
for i in ports:
#print(f"Processing port {i} of {ports}, Route: {route}")
#if i not in route:
newRoute = list(route)
newRoute.append(i)
#print(f"newRoute: {newRoute}")
j = ports.index(i)
#print(f"Remaining ports: {ports[:j]+ports[j+1:]}")
permutations(newRoute, ports[:j]+ports[j+1:])
# Print the port names in route when the recursion terminates
def polynomial():
x = np.array([66, 5, 15, 2, 500])
c = np.array([3000, 200 , -50, 5000, 100])
print(f"x @ c: {x @ c}, np.dot(x, c): {np.dot(x, c)}")
x = np.array([[66, 5, 15, 2, 500],
[21, 3, 50, 1, 100]])
print(f"x @ c: {x @ c}, np.dot(x, c): {np.dot(x, c)}")
def kmin(n): #N minimum (index) values in a numpy array
# https://stackoverflow.com/questions/16817948/i-have-need-the-n-minimum-index-values-in-a-numpy-array
arr = np.array([1, 4, 2, 5, 3])
indices = arr.argsort()[:n] # Sort the array
print(f"arr: {arr}, tmp: {indices}")
def testZip():
a = [1,2,3,4,5]
b = [9,8,3,5,6]
for i, j in zip(a,b):
print(f"[{i}, {j}]")
c = [abs(i- j) for i, j in zip(a, b)]
print(f"Manhattan distance: {c}")
d = sum([abs(i- j) for i, j in zip(a, b)])
print(f"Manhattan distance: {d}")
def primary_diagonal(matrix):
sum = 0
for i in range(len(matrix)):
sum += matrix[i][i]
return sum
def secondary_left_diagonal(matrix):
sum = 0
for i in range(len(matrix)):
sum += matrix[i][len(matrix) - i - 1]
return sum
def matrix_sums():
matrix = [
[1,2,3,4],
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]
]
row_totals = [ sum(x) for x in matrix ]
col_totals = [ sum(x) for x in zip(*matrix) ]
diag1 = primary_diagonal(matrix)
diag2 = secondary_left_diagonal(matrix)
print(f"rows: {row_totals}, cols: {col_totals}, diag1: {diag1}, diag2: {diag2}")
def sort_dict_by_tuple_values():
result = {
"a": (0,10),
"b": (10,5),
"c": (5,5),
"d": (7,8),
"e": (8,7),
"f": (6,6),
"g": (5,6),
}
result = {k: v for k, v in sorted(result.items(), key=lambda item: (item[1][0], -item[1][1]))}
for k,v in result.items():
print(f"{k}: {v}")
def main():
sort_dict_by_tuple_values()
permutations([0], list(range(1, len(portnames)))) # This will start the recursion with 0 ("PAN") as the first stop
print(' '.join([portnames[i] for i in bestroute]) + " %.1f kg" % smallest)
polynomial()
kmin(3)
testZip()
matrix_sums()
main()