forked from NglQ/vehicle-routing-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bounds_generator.py
30 lines (24 loc) · 975 Bytes
/
bounds_generator.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
import numpy as np
def generate_lowerbound(n, D):
distances = []
for i in range(n):
distances.append(D[i][n] + D[n][i])
return max(distances)
def generate_upperbound(n, m, D):
depot_to_cities = np.array(D[-1][:len(D) - 1])
cities_to_depot = np.array([D[i][-1] for i in range(len(D) - 1)])
depot_cities_depot = depot_to_cities + cities_to_depot
distances_prov = np.sort(depot_cities_depot)
distances = distances_prov[-m + 1:]
indices_to_remove = np.argsort(depot_cities_depot)[-m + 1:]
range_n = [i for i in range(n) if i not in indices_to_remove]
idx = n
visited = [idx]
dist = 0
while len(range_n) != 0:
idx = np.argmin(np.array([val if i != idx and i in range_n else np.inf for i, val in enumerate(D[idx])]))
dist += D[visited[-1]][idx]
range_n.remove(idx)
visited.append(idx)
dist += D[visited[-1]][n]
return int(max(np.append(distances, dist)))