-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclark_n_wright.py
162 lines (134 loc) · 4.72 KB
/
clark_n_wright.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import random
import math
class Node:
def __init__(self, id, tp, dem, xx, yy):
self.id = id
self.type = tp
self.demand = dem
self.x = xx
self.y = yy
all_nodes = []
service_locations = []
depot = Node(0, 0, 0, 50, 50)
all_nodes.append(depot)
random.seed(1)
for i in range(0, 200):
id = i + 1
tp = random.randint(1,3)
dem = random.randint(1,5) * 100
xx = random.randint(0, 100)
yy = random.randint(0, 100)
serv_node = Node(id, tp, dem, xx, yy)
all_nodes.append(serv_node)
service_locations.append(serv_node)
#print(all_nodes[1].demand)
dist_matrix = [[0.0 for j in range(0, len(all_nodes))] for k in range(0, len(all_nodes))]
for i in range(0, len(all_nodes)):
for j in range(0, len(all_nodes)):
source = all_nodes[i]
target = all_nodes[j]
dx_2 = (source.x - target.x)**2
dy_2 = (source.y - target.y) ** 2
dist = round(math.sqrt(dx_2 + dy_2))
dist_matrix[i][j] = dist
time_matrix = [[0.0 for j in range(0, len(all_nodes))] for k in range(0, len(all_nodes))]
for i in range(0, len(all_nodes)):
for j in range(0, len(all_nodes)):
if(i != j):
time = dist_matrix [i][j] / 35
if(all_nodes[j].type == 1):
time +=(5/60)
elif(all_nodes[j].type == 2):
time +=(15/60)
elif(all_nodes[j].type == 3):
time +=(25/60)
else:
#χρονος μεγαλυτερος του 0
time = 0
time_matrix[i][j] = time
#print(time_matrix[4][2])
total_nodes = []
for i in range(1,201):
total_nodes.append(i)
savings = [[0.0 for j in range(0, len(all_nodes))] for k in range(0, len(all_nodes))]
for i in range(1, len(all_nodes)):
for j in range(1, len(all_nodes)):
if(i!=j):
savings[i][j] = time_matrix[i][0] + time_matrix[0][j] - time_matrix[i][j]
dictionary = {}
for i in range(1, len(all_nodes)):
for j in range(1, len(all_nodes)):
dictionary[(i,j)] = [savings[i][j]]
#print(dictionary)
sort_dict = sorted(dictionary.items(), key=lambda kv:(kv[1], kv[0]), reverse=True)
#sorted savings
nodes=[]
for i in range(0,len(sort_dict)):
nodes.append(list(sort_dict)[i][0])
'''
print(nodes[8][0])
print(nodes[8][1])
'''
roads = []
roads.append(000)
for i in range (1,201):
roads.append([0,i,0])
'''
for i in range(1,201):
if nodes[0][0] in roads[i]:
pos1 = i
print(pos1)
for i in range(1,201):
if nodes[0][1] in roads[i]:
pos2 = i
print(pos2)
if(len(roads[pos1])<len(roads[pos2])):
pos1,pos2 = pos2,pos1
if(pos1!=pos2):
if((roads[pos1][1] == nodes[0][0] or roads[pos1][len(roads[pos1])-2] == nodes[0][0]) and (roads[pos2][1] == nodes[0][1] or roads[pos2][len(roads[pos2])-2] == nodes[0][1])):
sum1 = 0
sum2 = 0
for i in range(1,len(roads[pos1])-1):
sum1 += all_nodes[roads[pos1][i]].demand
for i in range(1,len(roads[pos2])-1):
sum2 += all_nodes[roads[pos2][i]].demand
if((sum1 + sum2) <= 3000):
roads[pos1].insert(len(roads[pos1])-1,nodes[0][1]) #προσθετω στην πρωτη διαδρομη την δευτερη διαδρομη
roads[pos2]=[0] # σβηνω τη διαδρομη που εχω προσθεσει
'''
for k in range(0,len(sort_dict)):
for i in range(1,201):
if nodes[k][0] in roads[i]:
pos1 = i
if nodes[k][1] in roads[i]:
pos2 = i
if(pos1!=pos2):
if((roads[pos1][1] == nodes[k][0] or roads[pos1][len(roads[pos1])-2] == nodes[k][0]) and (roads[pos2][1] == nodes[k][1] or roads[pos2][len(roads[pos2])-2] == nodes[k][1])):
n0 = nodes[k][0]
n1 = nodes[k][1]
if(len(roads[pos1])<len(roads[pos2])):
pos1,pos2 = pos2,pos1
n0,n1=n1,n0
sum1 = 0
sum2 = 0
for i in range(1,len(roads[pos1])-1):
sum1 += all_nodes[roads[pos1][i]].demand
for i in range(1,len(roads[pos2])-1):
sum2 += all_nodes[roads[pos2][i]].demand
if((sum1 + sum2) <= 3000):
roads[pos1].insert(len(roads[pos1])-1,n1) #προσθετω στην πρωτη διαδρομη την δευτερη διαδρομη
roads[pos2]=[0,0,0] # σβηνω τη διαδρομη που εχω προσθεσει
count = 0
mymax=-1
for i in range(1,201):
if(roads[i]!=[0,0,0]):
count+=1
#print(roads[i])
time_sum=0
for j in range(0,len(roads[i])-2):
time_sum+=time_matrix[roads[i][j]][roads[i][j+1]]
print(time_sum)
if(mymax<time_sum):
mymax=time_sum
print(count)
print(mymax)