This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.py
More file actions
198 lines (163 loc) · 6.06 KB
/
Copy pathtesting.py
File metadata and controls
198 lines (163 loc) · 6.06 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Import
from library import *
import matplotlib.pyplot as plt
"""
Run Ant Colony Optimization (ACO) algorithm for a given Symmetric traveling salesman problem (TSP)
@arg
{string} tsp -- The TSP file src name (located in /data folder)
@export
{results} -- Generated files for results
{plots} -- Generated files for plots
"""
def test(tsp):
# Default arguments
'''
iterations {80} -- Number of iterations (Ending condition)
colony {50} -- Number of ants in the colony
alpha {1.0} -- Alpha algorithm parameter, more or less weight to a selected distance
beta {1.0} -- Beta algorithm parameter, more or less weight to a selected distance
del_tau {1.0} -- Delta Tau algorithm parameter, pheromones releasing rate
rho {0.5} -- Rho algorithm parameter, pheromones evaporation rate
'''
# Configuration vars
n = 3 # Repetitions
# Algorithm Parameters
iterations = 80
colony = 50
alpha = 1
beta = 1
del_tau = 1.0
rho = 0.5
results = np.zeros(n) # Store
# Get TSP data
src = getTspData('data/{}.tsp'.format(tsp))
space = None
space = np.array(src['node_coord_section'])
# Inform
msg('Computing {} times for {}'.format(n, tsp))
# Save space plot
saveSpacePlot(tsp, space)
# Repeat and save path plot for each result
for i in range(n):
# Run
min_path, min_distance = runAcoTsp(space, iterations, colony, alpha, beta, del_tau, rho)
# Store result
results[i] = min_distance
# Save path plot
savePathPlot(i, n, tsp, space, min_path, min_distance)
# Save results txt
saveResultsTxt(src, results, iterations, colony, alpha, beta, del_tau, rho)
"""
Save Space plot
@arg
{string} tsp -- The TSP file src name
{numpy.ndarray} space -- The space
@export
{png} -- Generated .png for TSP space plot
"""
def saveSpacePlot(tsp, space):
# Plot nodes
plt.scatter(space[:, 0], space[:, 1], s = 15)
# Plot properties
plt.title('Space for {}'.format(tsp))
plt.xlabel('Latitude')
plt.ylabel('Longitude')
# Save plot
file = 'results/{}-space.png'.format(tsp)
plt.savefig(file)
# Close plot
plt.close()
# Inform
msg('{} generated'.format(file))
"""
Save Path plot for a given result
@arg
{int} i -- The result
{int} n -- The total results
{numpy.ndarray} -- Indexes of the minimun distance path for the result
{float} -- the minimun distance for the result
{string} tsp -- The TSP file src name
{numpy.ndarray} space -- The space
@export
{png} -- Generated .png for ACO-TSP path result plot
"""
def savePathPlot(i, n, tsp, space, min_path, min_distance):
# Plot nodes
plt.scatter(space[:, 0], space[:, 1], marker='o', s = 15)
plt.plot(space[min_path, 0], space[min_path, 1], c='g', linewidth=0.8, linestyle="--")
# Plot properties
plt.suptitle('Mininum Path for {}'.format(tsp))
plt.title('Result #{} of {} for a minimum distance of {}'.format(i + 1, n, min_distance), fontsize = 10)
plt.xlabel('Latitude')
plt.ylabel('Longitude')
# Save plot
file = 'results/{}-path-{}.png'.format(tsp, i + 1)
plt.savefig(file)
# Close plot
plt.close()
# Inform
msg('{} generated'.format(file))
"""
Save results for a given TSP
@arg
{dict} src -- The TSP file src
{numpy.ndarray} results -- The TSP file as dictionary
{int} iterations {80} -- Number of iterations (Ending condition)
{int} colony {50} -- Number of ants in the colony
{float} alpha {1.0} -- Alpha algorithm parameter, more or less weight to a selected distance
{float} beta {1.0} -- Beta algorithm parameter, more or less weight to a selected distance
{float} del_tau {1.0} -- Delta Tau algorithm parameter, pheromones releasing rate
{float} rho {0.5} -- Rho algorithm parameter, pheromones evaporation rate
@export
{txt} -- Generated .txt for ACO-TSP results
"""
def saveResultsTxt(src, results, iterations, colony, alpha, beta, del_tau, rho):
# Open or create
file = 'results/{}-results.txt'.format(src['name'])
txt = open(file, 'w+')
# Write file
txt.write('\n--------------------------')
txt.write('\n 1- TSP INFO')
txt.write('\n--------------------------')
txt.write('\nNAME : {}.tsp (stored in /data)'.format(src['name']))
txt.write('\n# OF NODES : {}\n'.format(src['dimension']))
txt.write('\n--------------------------')
txt.write('\n 2- ALGORITHM PARAMETERS')
txt.write('\n--------------------------')
txt.write('\nITERATIONS : {}'.format(iterations))
txt.write('\nCOLONY : {}'.format(colony))
txt.write('\nALPHA : {}'.format(alpha))
txt.write('\nBETA : {}'.format(beta))
txt.write('\nDEL_TAU : {}'.format(del_tau))
txt.write('\nRHO : {}\n'.format(rho))
txt.write('\n--------------------------')
txt.write('\n 3- RESULTS ')
txt.write('\n--------------------------')
txt.write('\nMIN_DISTANCES : {}'.format(results))
txt.write('\n# OF RESULTS : {}'.format(results.size))
txt.write('\nAVG_MIN_DISTANCE : {}'.format(np.average(results)))
txt.write('\n--------------------------')
# Close file
txt.close()
# Inform
msg('{} generated'.format(file))
"""
Show a console message
@arg
{string} str
"""
def msg(str):
print('[Testing ACO_TSP] {}'.format(str))
"""
Show a console message
@arg
{string} str
"""
def main():
# Test for each stored TSP data
test('kroA100')
test('berlin52')
# Inform
msg('All files generated, see /results for details')
if __name__ == '__main__':
main()