-
Notifications
You must be signed in to change notification settings - Fork 1
/
constheur.py
105 lines (89 loc) · 3.23 KB
/
constheur.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
import random
import numpy as np
import networkx as nx
def NN(A):
"""Nearest neighbor algorithm.
A is an NxN array indicating distance between N locations
start is the index of the starting location
Returns the path and cost of the found solution
"""
A = np.array(A)
path = [0]
cost = 0
N = A.shape[0]
mask = np.ones(N, dtype=bool) # boolean values indicating which
# locations have not been visited
mask[0] = False
for i in range(N-1):
last = path[-1]
next_ind = np.argmin(A[last][mask]) # find minimum of remaining locations
next_loc = np.arange(N)[mask][next_ind] # convert to original location
path.append(next_loc)
mask[next_loc] = False
cost += A[last, next_loc]
return path+[0]
def InsertionCost(matrix,i,k,j):
return matrix[i][k] + matrix[k][j] - matrix[i][j]
def RandomInsertion(matrix):
n = len(matrix)
nodes = set(range(0,n))
start_node = random.randint(1,n-1)
tour =[0,start_node,0]
while (len(tour) < n+1):
best_cost = np.Inf
tour_set = set(tour)
remaining_nodes = nodes - tour_set
k = random.choice(list(remaining_nodes))
for i in range(0,len(tour)-1):
j=i+1
cost = InsertionCost(matrix,tour[i],k,tour[j])
if cost < best_cost:
best_cost = cost
start = i
end = j
tour = tour[:start+1]+[k]+tour[end:]
return tour
def ClarkeWright(matrix):
n = len(matrix)
# print "size", n
depot = 0
tours = []
for node in range(1,n):
tours.append([node,depot,node])
# print "tours", tours
savings=[]
for i in range(0,n-1):
for j in range(i+1,n):
saving = matrix[0][i] + matrix[0][j] - matrix[i][j]
savings.append((saving,i,j))
savings = sorted(savings,reverse=True)
# print savings
new_tour =[]
while True:
for save,i,j in savings:
if new_tour == []:
tour_1 = tours[i-1]
tour_2 = tours[j-1]
new_tour = tour_1[1:]+tour_2[:-1]
# print new_tour
elif new_tour[-2] == j and i not in new_tour:
new_tour = new_tour[:-1]+tours[i-1][:-1]
# print new_tour
elif new_tour[1] == j and i not in new_tour:
new_tour = tours[i-1][1:]+new_tour[1:]
# print new_tour
elif new_tour[-2] == i and j not in new_tour:
new_tour = new_tour[:-1]+tours[j-1][:-1]
# print new_tour
elif new_tour[1] == i and j not in new_tour:
new_tour = tours[j-1][1:]+new_tour[1:]
# print new_tour
if len(new_tour) >= n+1:break
# print len(new_tour)
# print len(set(new_tour))
return new_tour
def MSTPreOrder(matrix):
numpy_matrix = np.array(matrix)
G = nx.from_numpy_matrix(numpy_matrix)
T = nx.minimum_spanning_tree(G)
return list(nx.dfs_preorder_nodes(T,0))+[0]