-
Notifications
You must be signed in to change notification settings - Fork 0
/
tspv4.py
173 lines (149 loc) · 5.99 KB
/
tspv4.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
161
162
163
164
165
166
167
168
169
170
171
172
173
#NOT WORKING/BROKEN
import numpy as np
import sys
import time
import random as rand
#Declaring variables
infoLoop = False
nodes = []
tempEuclideanDistances = []
tempDistances = []
tempNodes = []
tempCoords = []
route = []
routeDistance = 0
routeCoords = []
realRoute = []
realDistance = 0
alpha = 0.1 #Greedy ಠ_ಠ
timeLimit = 100.0 #seconds
bestDistance = sys.float_info.max
timeLimitExceed = False
#Function to calculate Euclidean Distance
def Eu2D(x1, y1, x2, y2):
return np.sqrt( (x1-x2)**2 + (y1-y2)**2 )
#Open the file we want to use.
tsp = open("TSP/eil76.tsp", "r")
#Save its information.
while infoLoop == False :
actualLine = tsp.readline().split()
actualLineLenght = len(actualLine)
if actualLine[0] == 'NAME' or actualLine[0] == 'NAME:' :
filename = actualLine[actualLineLenght-1]
print( "\n%s file loaded!\n" %filename )
elif actualLine[0] == 'DIMENSION' or actualLine[0] == 'DIMENSION:' :
dimension = int( actualLine[actualLineLenght-1] )
nodeBool = [ True for i in range(0, dimension) ]
elif actualLine[0].isnumeric() == True :
x,y = actualLine[1:]
nodes.append([float(x), float(y)])
if actualLine[0] == 'EOF' :
infoLoop = True
#Algorithm timer execute
startTime = time.time()
#Start point !!!!!!!!!!!!!!
initNumber = int( input("From 1 to %s\n\tWhere do you want to start?: " %dimension) ) - 1
print( "\nCalculating, please wait...\n" )
initCoords = nodes[initNumber]
x1 = nodes[initNumber][0]
y1 = nodes[initNumber][1]
route.append(initNumber+1)
nodeBool[initNumber] = False
routeCoords.append(initCoords)
#Multi-start with alpha
for x in range( 0, 3**dimension ) :
actualTime = time.time() - startTime
if actualTime >= timeLimit :
timeLimitExceed = True
break
x1 = nodes[initNumber][0]
y1 = nodes[initNumber][1]
route.clear()
route.append(initNumber+1)
routeDistance = 0
nodeBool = [ True for i in range(0, dimension) ]
nodeBool[initNumber] = False
realDistance = 0
realRoute.clear()
routeCoords.clear()
routeCoords.append(initCoords)
lsDistance = 0
for i in range( 0, dimension-1 ) :
if actualTime >= timeLimit :
timeLimitExceed = True
break
tempMinEucDistance = sys.float_info.max
tempMaxEucDistance = 0
tempNodes.clear()
tempDistances.clear()
tempCoords.clear()
tempEuclideanDistances.clear()
for j in range( 0, dimension ) : #Obtain all its euclidean distances from actual node
x2 = nodes[j][0]
y2 = nodes[j][1]
euclideanDistance = Eu2D( x1, y1, x2, y2)
tempEuclideanDistances.append(euclideanDistance)
for j in range( 0, dimension ) : #Looking for the smallest and greatest distance from my actual node to neighbors
if (tempEuclideanDistances[j] > tempMaxEucDistance) and (nodeBool[j] == True) :
tempMaxEucDistance = tempEuclideanDistances[j].copy()
if (tempEuclideanDistances[j] < tempMinEucDistance) and (nodeBool[j] == True) :
tempMinEucDistance = tempEuclideanDistances[j].copy()
djE = tempMinEucDistance + ( alpha*(tempMaxEucDistance-tempMinEucDistance) )
sumTrues = sum(nodeBool)
if sumTrues > 1 :
for j in range( 0, dimension ) :
if ( tempEuclideanDistances[j] <= djE ) and ( nodeBool[j] == True ) :
tempDistances.append( tempEuclideanDistances[j] )
tempNodes.append( j )
tempCoords.append(nodes[j])
nRand = rand.randint( 0, len(tempNodes)-1 )
nodeBool[ tempNodes[nRand] ] = False
x1 = tempCoords[nRand][0]
y1 = tempCoords[nRand][1]
routeDistance += tempDistances[nRand]
routeCoords.append(tempCoords[nRand])
route.append( tempNodes[nRand]+1 )
elif sumTrues == 1:
for j in range( 0, dimension ) :
if nodeBool[j] == True :
routeDistance += Eu2D(x1, y1, nodes[j][0], nodes[j][1])
route.append( j+1 )
x1 = nodes[j][0]
y1 = nodes[j][1]
routeCoords.append( nodes[j] )
routeDistance += Eu2D( x1, y1, nodes[initNumber][0], nodes[initNumber][1] )
route.append( initNumber+1 )
routeCoords.append( initCoords )
realRouteCoords = routeCoords.copy()
realRoute = route.copy()
realDistance = routeDistance.copy()
if realDistance < bestDistance :
bestDistance = realDistance.copy()
bestRoute = realRoute.copy()
print("Alpha notifiacion:\n\tCyle: {:,.0f}\tBest distance at the moment: {:,.5f}" .format(x+1, bestDistance))
print( "\tRoute: %s" %bestRoute)
#Local search (2node-switch)
for k in range(1, dimension-1) :
aux = realRouteCoords[k]
realRouteCoords[k] = realRouteCoords[k+1]
realRouteCoords[k+1] = aux.copy()
aux = realRoute[k]
realRoute[k] = realRoute[k+1]
realRoute[k+1] = aux
lsDistance = 0
for f in range(0, dimension) :
lsDistance += Eu2D( realRouteCoords[f][0], realRouteCoords[f][1], realRouteCoords[f+1][0], realRouteCoords[f+1][1] )
if lsDistance < bestDistance :
bestDistance = lsDistance.copy()
bestRoute = realRoute.copy()
print("Local Search notification:\n\tCyle: {:,.0f}\tBest distance at the moment: {:,.5f}" .format(x+1, bestDistance).replace( ',', ' ' ))
print( "\tRoute: %s\n" %realRoute)
actualTime = time.time() - startTime
if actualTime >= timeLimit :
print( "\nTime limit exceeded [{:,.0f} second(s)]. Terminating program." .format(timeLimit).replace( ',', ' ' ) )
break
execTime = time.time() - startTime
print( "\nBest route: %s" %bestRoute)
print( "Best distance: {:,.2f} distance units" .format( bestDistance ).replace( ',', ' ' ) )
print( "Time elapsed: %s seconds\n" %execTime)
print(dimension)