-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.py
71 lines (57 loc) · 2.16 KB
/
calculate.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
from config import *
"""
m = 4 #num of vehicles
n = 9 #num of customers
"""
def route():
"""generate toy route example for debugging
"""
return np.asarray([[0, 1, 2, 3, 0],
[0, 4, 5, 6, 0],
[0, 7, 8, 9, 0],
[0, 0, 0, 0, 0]])
def distance():
"""generate global distance matrix with shape [n+1, n+1].
"""
routes = route()
# a: number of routes
# b: number of customers in one route
# length of each route may vary, thus 0 is used to fill empty customer up
a,b = routes.shape
distance_matrix = np.zeros((a,b))
for i in range(a):
for j in range(b-1):
distance_matrix[i][j] = config.distance_matrix[routes[i][j], routes[i][j+1]]
return distance_matrix
def CostPerRoute():
"""based on distance matrix and route to calculate distance/cost per route.
return: route_distance * config.cost_vector
"""
route_distance = [np.sum(distance()[a]) for a in range(len(distance()))]
return route_distance * config.cost_vector
def demand():
"""generate demand matrix based on route"""
routes = route()
# a: number of routes
# b: number of customers in one route
# length of each route may vary, thus 0 is used to fill empty customer up
a,b = routes.shape
demand_matrix = np.zeros((a,b))
for i in range(a):
for j in range(b-1):
demand_matrix[i][j] = config.demand_vector[route()[i][j]]
return demand_matrix
def DemandPerRoute():
"""generate demand per route"""
#DemandPerRoute = np.zeros((len(route())))
#DemandPerRoute = [Config.demand_vector[a] for a in range(len(route()))]
DemandPerRoute = [np.sum(demand()[a]) for a in range(len(demand()))]
return DemandPerRoute
## check cost function
# print("cost_vector: \n", Config.cost_vector, "\ncost vector shape: \n", Config.cost_vector.shape)
# print("route(route per row): \n",route(), "\nroute shape: \n",route().shape)
print("cost per route:\n",CostPerRoute())
## check demand function
# print("demand_vector\n",Config.demand_vector)
print("DemandPerRoute\n",DemandPerRoute())
print("demand matrix\n", demand())