[workshop] Add notebook
This commit is contained in:
1
template/.dockerignore
Normal file
1
template/.dockerignore
Normal file
@@ -0,0 +1 @@
|
||||
.vscode/
|
||||
2
template/.gitignore
vendored
Normal file
2
template/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.vscode/
|
||||
__pycache__/
|
||||
7
template/Dockerfile
Normal file
7
template/Dockerfile
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM python:3.8
|
||||
|
||||
ADD ./ /tsp
|
||||
|
||||
WORKDIR /tsp
|
||||
|
||||
CMD ["/bin/bash", "-c", "pip install -r requirements.txt && python main.py; mkdir -p /output && cp -r output/* /output"]
|
||||
11
template/README.md
Normal file
11
template/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
## Build Image
|
||||
|
||||
docker build -t com2014-tsp .
|
||||
|
||||
## Windows
|
||||
|
||||
docker container run -v $PWD/requirements.txt:/tsp/requirements.txt -v $PWD/output/:/output/ com2014-tsp
|
||||
|
||||
## Linux
|
||||
|
||||
docker container run -v $PWD/requirements.txt:/tsp/requirements.txt -v $PWD/output/:/output/ com2014-tsp
|
||||
1007
template/data/hard/dsj1000.tsp
Normal file
1007
template/data/hard/dsj1000.tsp
Normal file
File diff suppressed because it is too large
Load Diff
287
template/data/medium/a280.tsp
Normal file
287
template/data/medium/a280.tsp
Normal file
@@ -0,0 +1,287 @@
|
||||
NAME : a280
|
||||
COMMENT : drilling problem (Ludwig)
|
||||
TYPE : TSP
|
||||
DIMENSION: 280
|
||||
EDGE_WEIGHT_TYPE : EUC_2D
|
||||
NODE_COORD_SECTION
|
||||
1 288 149
|
||||
2 288 129
|
||||
3 270 133
|
||||
4 256 141
|
||||
5 256 157
|
||||
6 246 157
|
||||
7 236 169
|
||||
8 228 169
|
||||
9 228 161
|
||||
10 220 169
|
||||
11 212 169
|
||||
12 204 169
|
||||
13 196 169
|
||||
14 188 169
|
||||
15 196 161
|
||||
16 188 145
|
||||
17 172 145
|
||||
18 164 145
|
||||
19 156 145
|
||||
20 148 145
|
||||
21 140 145
|
||||
22 148 169
|
||||
23 164 169
|
||||
24 172 169
|
||||
25 156 169
|
||||
26 140 169
|
||||
27 132 169
|
||||
28 124 169
|
||||
29 116 161
|
||||
30 104 153
|
||||
31 104 161
|
||||
32 104 169
|
||||
33 90 165
|
||||
34 80 157
|
||||
35 64 157
|
||||
36 64 165
|
||||
37 56 169
|
||||
38 56 161
|
||||
39 56 153
|
||||
40 56 145
|
||||
41 56 137
|
||||
42 56 129
|
||||
43 56 121
|
||||
44 40 121
|
||||
45 40 129
|
||||
46 40 137
|
||||
47 40 145
|
||||
48 40 153
|
||||
49 40 161
|
||||
50 40 169
|
||||
51 32 169
|
||||
52 32 161
|
||||
53 32 153
|
||||
54 32 145
|
||||
55 32 137
|
||||
56 32 129
|
||||
57 32 121
|
||||
58 32 113
|
||||
59 40 113
|
||||
60 56 113
|
||||
61 56 105
|
||||
62 48 99
|
||||
63 40 99
|
||||
64 32 97
|
||||
65 32 89
|
||||
66 24 89
|
||||
67 16 97
|
||||
68 16 109
|
||||
69 8 109
|
||||
70 8 97
|
||||
71 8 89
|
||||
72 8 81
|
||||
73 8 73
|
||||
74 8 65
|
||||
75 8 57
|
||||
76 16 57
|
||||
77 8 49
|
||||
78 8 41
|
||||
79 24 45
|
||||
80 32 41
|
||||
81 32 49
|
||||
82 32 57
|
||||
83 32 65
|
||||
84 32 73
|
||||
85 32 81
|
||||
86 40 83
|
||||
87 40 73
|
||||
88 40 63
|
||||
89 40 51
|
||||
90 44 43
|
||||
91 44 35
|
||||
92 44 27
|
||||
93 32 25
|
||||
94 24 25
|
||||
95 16 25
|
||||
96 16 17
|
||||
97 24 17
|
||||
98 32 17
|
||||
99 44 11
|
||||
100 56 9
|
||||
101 56 17
|
||||
102 56 25
|
||||
103 56 33
|
||||
104 56 41
|
||||
105 64 41
|
||||
106 72 41
|
||||
107 72 49
|
||||
108 56 49
|
||||
109 48 51
|
||||
110 56 57
|
||||
111 56 65
|
||||
112 48 63
|
||||
113 48 73
|
||||
114 56 73
|
||||
115 56 81
|
||||
116 48 83
|
||||
117 56 89
|
||||
118 56 97
|
||||
119 104 97
|
||||
120 104 105
|
||||
121 104 113
|
||||
122 104 121
|
||||
123 104 129
|
||||
124 104 137
|
||||
125 104 145
|
||||
126 116 145
|
||||
127 124 145
|
||||
128 132 145
|
||||
129 132 137
|
||||
130 140 137
|
||||
131 148 137
|
||||
132 156 137
|
||||
133 164 137
|
||||
134 172 125
|
||||
135 172 117
|
||||
136 172 109
|
||||
137 172 101
|
||||
138 172 93
|
||||
139 172 85
|
||||
140 180 85
|
||||
141 180 77
|
||||
142 180 69
|
||||
143 180 61
|
||||
144 180 53
|
||||
145 172 53
|
||||
146 172 61
|
||||
147 172 69
|
||||
148 172 77
|
||||
149 164 81
|
||||
150 148 85
|
||||
151 124 85
|
||||
152 124 93
|
||||
153 124 109
|
||||
154 124 125
|
||||
155 124 117
|
||||
156 124 101
|
||||
157 104 89
|
||||
158 104 81
|
||||
159 104 73
|
||||
160 104 65
|
||||
161 104 49
|
||||
162 104 41
|
||||
163 104 33
|
||||
164 104 25
|
||||
165 104 17
|
||||
166 92 9
|
||||
167 80 9
|
||||
168 72 9
|
||||
169 64 21
|
||||
170 72 25
|
||||
171 80 25
|
||||
172 80 25
|
||||
173 80 41
|
||||
174 88 49
|
||||
175 104 57
|
||||
176 124 69
|
||||
177 124 77
|
||||
178 132 81
|
||||
179 140 65
|
||||
180 132 61
|
||||
181 124 61
|
||||
182 124 53
|
||||
183 124 45
|
||||
184 124 37
|
||||
185 124 29
|
||||
186 132 21
|
||||
187 124 21
|
||||
188 120 9
|
||||
189 128 9
|
||||
190 136 9
|
||||
191 148 9
|
||||
192 162 9
|
||||
193 156 25
|
||||
194 172 21
|
||||
195 180 21
|
||||
196 180 29
|
||||
197 172 29
|
||||
198 172 37
|
||||
199 172 45
|
||||
200 180 45
|
||||
201 180 37
|
||||
202 188 41
|
||||
203 196 49
|
||||
204 204 57
|
||||
205 212 65
|
||||
206 220 73
|
||||
207 228 69
|
||||
208 228 77
|
||||
209 236 77
|
||||
210 236 69
|
||||
211 236 61
|
||||
212 228 61
|
||||
213 228 53
|
||||
214 236 53
|
||||
215 236 45
|
||||
216 228 45
|
||||
217 228 37
|
||||
218 236 37
|
||||
219 236 29
|
||||
220 228 29
|
||||
221 228 21
|
||||
222 236 21
|
||||
223 252 21
|
||||
224 260 29
|
||||
225 260 37
|
||||
226 260 45
|
||||
227 260 53
|
||||
228 260 61
|
||||
229 260 69
|
||||
230 260 77
|
||||
231 276 77
|
||||
232 276 69
|
||||
233 276 61
|
||||
234 276 53
|
||||
235 284 53
|
||||
236 284 61
|
||||
237 284 69
|
||||
238 284 77
|
||||
239 284 85
|
||||
240 284 93
|
||||
241 284 101
|
||||
242 288 109
|
||||
243 280 109
|
||||
244 276 101
|
||||
245 276 93
|
||||
246 276 85
|
||||
247 268 97
|
||||
248 260 109
|
||||
249 252 101
|
||||
250 260 93
|
||||
251 260 85
|
||||
252 236 85
|
||||
253 228 85
|
||||
254 228 93
|
||||
255 236 93
|
||||
256 236 101
|
||||
257 228 101
|
||||
258 228 109
|
||||
259 228 117
|
||||
260 228 125
|
||||
261 220 125
|
||||
262 212 117
|
||||
263 204 109
|
||||
264 196 101
|
||||
265 188 93
|
||||
266 180 93
|
||||
267 180 101
|
||||
268 180 109
|
||||
269 180 117
|
||||
270 180 125
|
||||
271 196 145
|
||||
272 204 145
|
||||
273 212 145
|
||||
274 220 145
|
||||
275 228 145
|
||||
276 236 145
|
||||
277 246 141
|
||||
278 252 125
|
||||
279 260 129
|
||||
280 280 133
|
||||
EOF
|
||||
449
template/data/medium/pcb442.tsp
Normal file
449
template/data/medium/pcb442.tsp
Normal file
@@ -0,0 +1,449 @@
|
||||
NAME : pcb442
|
||||
COMMENT : Drilling problem (Groetschel/Juenger/Reinelt)
|
||||
TYPE : TSP
|
||||
DIMENSION : 442
|
||||
EDGE_WEIGHT_TYPE : EUC_2D
|
||||
NODE_COORD_SECTION
|
||||
1 2.00000e+02 4.00000e+02
|
||||
2 2.00000e+02 5.00000e+02
|
||||
3 2.00000e+02 6.00000e+02
|
||||
4 2.00000e+02 7.00000e+02
|
||||
5 2.00000e+02 8.00000e+02
|
||||
6 2.00000e+02 9.00000e+02
|
||||
7 2.00000e+02 1.00000e+03
|
||||
8 2.00000e+02 1.10000e+03
|
||||
9 2.00000e+02 1.20000e+03
|
||||
10 2.00000e+02 1.30000e+03
|
||||
11 2.00000e+02 1.40000e+03
|
||||
12 2.00000e+02 1.50000e+03
|
||||
13 2.00000e+02 1.60000e+03
|
||||
14 2.00000e+02 1.70000e+03
|
||||
15 2.00000e+02 1.80000e+03
|
||||
16 2.00000e+02 1.90000e+03
|
||||
17 2.00000e+02 2.00000e+03
|
||||
18 2.00000e+02 2.10000e+03
|
||||
19 2.00000e+02 2.20000e+03
|
||||
20 2.00000e+02 2.30000e+03
|
||||
21 2.00000e+02 2.40000e+03
|
||||
22 2.00000e+02 2.50000e+03
|
||||
23 2.00000e+02 2.60000e+03
|
||||
24 2.00000e+02 2.70000e+03
|
||||
25 2.00000e+02 2.80000e+03
|
||||
26 2.00000e+02 2.90000e+03
|
||||
27 2.00000e+02 3.00000e+03
|
||||
28 2.00000e+02 3.10000e+03
|
||||
29 2.00000e+02 3.20000e+03
|
||||
30 2.00000e+02 3.30000e+03
|
||||
31 2.00000e+02 3.40000e+03
|
||||
32 2.00000e+02 3.50000e+03
|
||||
33 2.00000e+02 3.60000e+03
|
||||
34 3.00000e+02 4.00000e+02
|
||||
35 3.00000e+02 5.00000e+02
|
||||
36 3.00000e+02 6.00000e+02
|
||||
37 3.00000e+02 7.00000e+02
|
||||
38 3.00000e+02 8.00000e+02
|
||||
39 3.00000e+02 9.00000e+02
|
||||
40 3.00000e+02 1.00000e+03
|
||||
41 3.00000e+02 1.10000e+03
|
||||
42 3.00000e+02 1.20000e+03
|
||||
43 3.00000e+02 1.30000e+03
|
||||
44 3.00000e+02 1.40000e+03
|
||||
45 3.00000e+02 1.50000e+03
|
||||
46 3.00000e+02 1.60000e+03
|
||||
47 3.00000e+02 1.70000e+03
|
||||
48 3.00000e+02 1.80000e+03
|
||||
49 3.00000e+02 1.90000e+03
|
||||
50 3.00000e+02 2.00000e+03
|
||||
51 3.00000e+02 2.10000e+03
|
||||
52 3.00000e+02 2.20000e+03
|
||||
53 3.00000e+02 2.30000e+03
|
||||
54 3.00000e+02 2.40000e+03
|
||||
55 3.00000e+02 2.50000e+03
|
||||
56 3.00000e+02 2.60000e+03
|
||||
57 3.00000e+02 2.70000e+03
|
||||
58 3.00000e+02 2.80000e+03
|
||||
59 3.00000e+02 2.90000e+03
|
||||
60 3.00000e+02 3.00000e+03
|
||||
61 3.00000e+02 3.10000e+03
|
||||
62 3.00000e+02 3.20000e+03
|
||||
63 3.00000e+02 3.30000e+03
|
||||
64 3.00000e+02 3.40000e+03
|
||||
65 3.00000e+02 3.50000e+03
|
||||
66 4.00000e+02 4.00000e+02
|
||||
67 4.00000e+02 5.00000e+02
|
||||
68 4.00000e+02 6.00000e+02
|
||||
69 4.00000e+02 7.00000e+02
|
||||
70 4.00000e+02 8.00000e+02
|
||||
71 4.00000e+02 9.00000e+02
|
||||
72 4.00000e+02 1.00000e+03
|
||||
73 4.00000e+02 1.10000e+03
|
||||
74 4.00000e+02 1.20000e+03
|
||||
75 4.00000e+02 1.30000e+03
|
||||
76 4.00000e+02 1.40000e+03
|
||||
77 4.00000e+02 1.50000e+03
|
||||
78 4.00000e+02 1.60000e+03
|
||||
79 4.00000e+02 1.70000e+03
|
||||
80 4.00000e+02 1.80000e+03
|
||||
81 4.00000e+02 1.90000e+03
|
||||
82 4.00000e+02 2.00000e+03
|
||||
83 4.00000e+02 2.10000e+03
|
||||
84 4.00000e+02 2.20000e+03
|
||||
85 4.00000e+02 2.30000e+03
|
||||
86 4.00000e+02 2.40000e+03
|
||||
87 4.00000e+02 2.50000e+03
|
||||
88 4.00000e+02 2.60000e+03
|
||||
89 4.00000e+02 2.70000e+03
|
||||
90 4.00000e+02 2.80000e+03
|
||||
91 4.00000e+02 2.90000e+03
|
||||
92 4.00000e+02 3.00000e+03
|
||||
93 4.00000e+02 3.10000e+03
|
||||
94 4.00000e+02 3.20000e+03
|
||||
95 4.00000e+02 3.30000e+03
|
||||
96 4.00000e+02 3.40000e+03
|
||||
97 4.00000e+02 3.50000e+03
|
||||
98 4.00000e+02 3.60000e+03
|
||||
99 5.00000e+02 1.50000e+03
|
||||
100 5.00000e+02 1.82900e+03
|
||||
101 5.00000e+02 3.10000e+03
|
||||
102 6.00000e+02 4.00000e+02
|
||||
103 7.00000e+02 3.00000e+02
|
||||
104 7.00000e+02 6.00000e+02
|
||||
105 7.00000e+02 1.50000e+03
|
||||
106 7.00000e+02 1.60000e+03
|
||||
107 7.00000e+02 1.80000e+03
|
||||
108 7.00000e+02 2.10000e+03
|
||||
109 7.00000e+02 2.40000e+03
|
||||
110 7.00000e+02 2.70000e+03
|
||||
111 7.00000e+02 3.00000e+03
|
||||
112 7.00000e+02 3.30000e+03
|
||||
113 7.00000e+02 3.60000e+03
|
||||
114 8.00000e+02 3.00000e+02
|
||||
115 8.00000e+02 6.00000e+02
|
||||
116 8.00000e+02 1.03000e+03
|
||||
117 8.00000e+02 1.50000e+03
|
||||
118 8.00000e+02 1.80000e+03
|
||||
119 8.00000e+02 2.10000e+03
|
||||
120 8.00000e+02 2.40000e+03
|
||||
121 8.00000e+02 2.60000e+03
|
||||
122 8.00000e+02 2.70000e+03
|
||||
123 8.00000e+02 3.00000e+03
|
||||
124 8.00000e+02 3.30000e+03
|
||||
125 8.00000e+02 3.60000e+03
|
||||
126 9.00000e+02 3.00000e+02
|
||||
127 9.00000e+02 6.00000e+02
|
||||
128 9.00000e+02 1.50000e+03
|
||||
129 9.00000e+02 1.80000e+03
|
||||
130 9.00000e+02 2.10000e+03
|
||||
131 9.00000e+02 2.40000e+03
|
||||
132 9.00000e+02 2.70000e+03
|
||||
133 9.00000e+02 3.00000e+03
|
||||
134 9.00000e+02 3.30000e+03
|
||||
135 9.00000e+02 3.60000e+03
|
||||
136 1.00000e+03 3.00000e+02
|
||||
137 1.00000e+03 6.00000e+02
|
||||
138 1.00000e+03 1.10000e+03
|
||||
139 1.00000e+03 1.50000e+03
|
||||
140 1.00000e+03 1.62900e+03
|
||||
141 1.00000e+03 1.80000e+03
|
||||
142 1.00000e+03 2.10000e+03
|
||||
143 1.00000e+03 2.40000e+03
|
||||
144 1.00000e+03 2.60000e+03
|
||||
145 1.00000e+03 2.70000e+03
|
||||
146 1.00000e+03 3.00000e+03
|
||||
147 1.00000e+03 3.30000e+03
|
||||
148 1.00000e+03 3.60000e+03
|
||||
149 1.10000e+03 3.00000e+02
|
||||
150 1.10000e+03 6.00000e+02
|
||||
151 1.10000e+03 7.00000e+02
|
||||
152 1.10000e+03 9.00000e+02
|
||||
153 1.10000e+03 1.50000e+03
|
||||
154 1.10000e+03 1.80000e+03
|
||||
155 1.10000e+03 2.10000e+03
|
||||
156 1.10000e+03 2.40000e+03
|
||||
157 1.10000e+03 2.70000e+03
|
||||
158 1.10000e+03 3.00000e+03
|
||||
159 1.10000e+03 3.30000e+03
|
||||
160 1.10000e+03 3.60000e+03
|
||||
161 1.20000e+03 3.00000e+02
|
||||
162 1.20000e+03 6.00000e+02
|
||||
163 1.20000e+03 1.50000e+03
|
||||
164 1.20000e+03 1.70000e+03
|
||||
165 1.20000e+03 1.80000e+03
|
||||
166 1.20000e+03 2.10000e+03
|
||||
167 1.20000e+03 2.40000e+03
|
||||
168 1.20000e+03 2.70000e+03
|
||||
169 1.20000e+03 3.00000e+03
|
||||
170 1.20000e+03 3.30000e+03
|
||||
171 1.20000e+03 3.60000e+03
|
||||
172 1.30000e+03 3.00000e+02
|
||||
173 1.30000e+03 6.00000e+02
|
||||
174 1.30000e+03 7.00000e+02
|
||||
175 1.30000e+03 1.13000e+03
|
||||
176 1.30000e+03 1.50000e+03
|
||||
177 1.30000e+03 1.80000e+03
|
||||
178 1.30000e+03 2.10000e+03
|
||||
179 1.30000e+03 2.20000e+03
|
||||
180 1.30000e+03 2.40000e+03
|
||||
181 1.30000e+03 2.70000e+03
|
||||
182 1.30000e+03 3.00000e+03
|
||||
183 1.30000e+03 3.30000e+03
|
||||
184 1.30000e+03 3.60000e+03
|
||||
185 1.40000e+03 3.00000e+02
|
||||
186 1.40000e+03 6.00000e+02
|
||||
187 1.40000e+03 9.30000e+02
|
||||
188 1.40000e+03 1.50000e+03
|
||||
189 1.40000e+03 1.80000e+03
|
||||
190 1.40000e+03 2.00000e+03
|
||||
191 1.40000e+03 2.10000e+03
|
||||
192 1.40000e+03 2.40000e+03
|
||||
193 1.40000e+03 2.50000e+03
|
||||
194 1.40000e+03 2.70000e+03
|
||||
195 1.40000e+03 2.82000e+03
|
||||
196 1.40000e+03 2.90000e+03
|
||||
197 1.40000e+03 3.00000e+03
|
||||
198 1.40000e+03 3.30000e+03
|
||||
199 1.40000e+03 3.60000e+03
|
||||
200 1.50000e+03 1.50000e+03
|
||||
201 1.50000e+03 1.80000e+03
|
||||
202 1.50000e+03 1.90000e+03
|
||||
203 1.50000e+03 2.10000e+03
|
||||
204 1.50000e+03 2.40000e+03
|
||||
205 1.50000e+03 2.70000e+03
|
||||
206 1.50000e+03 2.80000e+03
|
||||
207 1.50000e+03 2.86000e+03
|
||||
208 1.50000e+03 3.00000e+03
|
||||
209 1.50000e+03 3.30000e+03
|
||||
210 1.50000e+03 3.60000e+03
|
||||
211 1.60000e+03 1.10000e+03
|
||||
212 1.60000e+03 1.30000e+03
|
||||
213 1.60000e+03 1.50000e+03
|
||||
214 1.60000e+03 1.80000e+03
|
||||
215 1.60000e+03 2.10000e+03
|
||||
216 1.60000e+03 2.40000e+03
|
||||
217 1.60000e+03 2.70000e+03
|
||||
218 1.60000e+03 3.00000e+03
|
||||
219 1.60000e+03 3.30000e+03
|
||||
220 1.60000e+03 3.60000e+03
|
||||
221 1.70000e+03 1.20000e+03
|
||||
222 1.70000e+03 1.50000e+03
|
||||
223 1.70000e+03 1.80000e+03
|
||||
224 1.70000e+03 2.10000e+03
|
||||
225 1.70000e+03 2.40000e+03
|
||||
226 1.70000e+03 3.60000e+03
|
||||
227 1.80000e+03 3.00000e+02
|
||||
228 1.80000e+03 6.00000e+02
|
||||
229 1.80000e+03 1.23000e+03
|
||||
230 1.80000e+03 1.50000e+03
|
||||
231 1.80000e+03 1.80000e+03
|
||||
232 1.80000e+03 2.10000e+03
|
||||
233 1.80000e+03 2.40000e+03
|
||||
234 1.90000e+03 3.00000e+02
|
||||
235 1.90000e+03 6.00000e+02
|
||||
236 1.90000e+03 3.00000e+03
|
||||
237 1.90000e+03 3.52000e+03
|
||||
238 2.00000e+03 3.00000e+02
|
||||
239 2.00000e+03 3.70000e+02
|
||||
240 2.00000e+03 6.00000e+02
|
||||
241 2.00000e+03 8.00000e+02
|
||||
242 2.00000e+03 9.00000e+02
|
||||
243 2.00000e+03 1.00000e+03
|
||||
244 2.00000e+03 1.10000e+03
|
||||
245 2.00000e+03 1.20000e+03
|
||||
246 2.00000e+03 1.30000e+03
|
||||
247 2.00000e+03 1.40000e+03
|
||||
248 2.00000e+03 1.50000e+03
|
||||
249 2.00000e+03 1.60000e+03
|
||||
250 2.00000e+03 1.70000e+03
|
||||
251 2.00000e+03 1.80000e+03
|
||||
252 2.00000e+03 1.90000e+03
|
||||
253 2.00000e+03 2.00000e+03
|
||||
254 2.00000e+03 2.10000e+03
|
||||
255 2.00000e+03 2.20000e+03
|
||||
256 2.00000e+03 2.30000e+03
|
||||
257 2.00000e+03 2.40000e+03
|
||||
258 2.00000e+03 2.50000e+03
|
||||
259 2.00000e+03 2.60000e+03
|
||||
260 2.00000e+03 2.70000e+03
|
||||
261 2.00000e+03 2.80000e+03
|
||||
262 2.00000e+03 2.90000e+03
|
||||
263 2.00000e+03 3.00000e+03
|
||||
264 2.00000e+03 3.10000e+03
|
||||
265 2.00000e+03 3.50000e+03
|
||||
266 2.10000e+03 3.00000e+02
|
||||
267 2.10000e+03 6.00000e+02
|
||||
268 2.10000e+03 3.20000e+03
|
||||
269 2.20000e+03 3.00000e+02
|
||||
270 2.20000e+03 4.69000e+02
|
||||
271 2.20000e+03 6.00000e+02
|
||||
272 2.20000e+03 3.20000e+03
|
||||
273 2.30000e+03 3.00000e+02
|
||||
274 2.30000e+03 6.00000e+02
|
||||
275 2.30000e+03 3.40000e+03
|
||||
276 2.40000e+03 3.00000e+02
|
||||
277 2.40000e+03 6.00000e+02
|
||||
278 2.40000e+03 2.10000e+03
|
||||
279 2.50000e+03 3.00000e+02
|
||||
280 2.50000e+03 8.00000e+02
|
||||
281 2.60000e+03 4.00000e+02
|
||||
282 2.60000e+03 5.00000e+02
|
||||
283 2.60000e+03 8.00000e+02
|
||||
284 2.60000e+03 9.00000e+02
|
||||
285 2.60000e+03 1.00000e+03
|
||||
286 2.60000e+03 1.10000e+03
|
||||
287 2.60000e+03 1.20000e+03
|
||||
288 2.60000e+03 1.30000e+03
|
||||
289 2.60000e+03 1.40000e+03
|
||||
290 2.60000e+03 1.50000e+03
|
||||
291 2.60000e+03 1.60000e+03
|
||||
292 2.60000e+03 1.70000e+03
|
||||
293 2.60000e+03 1.80000e+03
|
||||
294 2.60000e+03 1.90000e+03
|
||||
295 2.60000e+03 2.00000e+03
|
||||
296 2.60000e+03 2.10000e+03
|
||||
297 2.60000e+03 2.20000e+03
|
||||
298 2.60000e+03 2.30000e+03
|
||||
299 2.60000e+03 2.40000e+03
|
||||
300 2.60000e+03 2.50000e+03
|
||||
301 2.60000e+03 2.60000e+03
|
||||
302 2.60000e+03 2.70000e+03
|
||||
303 2.60000e+03 2.80000e+03
|
||||
304 2.60000e+03 2.90000e+03
|
||||
305 2.60000e+03 3.00000e+03
|
||||
306 2.60000e+03 3.10000e+03
|
||||
307 2.60000e+03 3.40000e+03
|
||||
308 2.70000e+03 7.00000e+02
|
||||
309 2.70000e+03 8.00000e+02
|
||||
310 2.70000e+03 9.00000e+02
|
||||
311 2.70000e+03 1.00000e+03
|
||||
312 2.70000e+03 1.10000e+03
|
||||
313 2.70000e+03 1.20000e+03
|
||||
314 2.70000e+03 1.30000e+03
|
||||
315 2.70000e+03 1.40000e+03
|
||||
316 2.70000e+03 1.50000e+03
|
||||
317 2.70000e+03 1.60000e+03
|
||||
318 2.70000e+03 1.70000e+03
|
||||
319 2.70000e+03 1.80000e+03
|
||||
320 2.70000e+03 1.90000e+03
|
||||
321 2.70000e+03 2.00000e+03
|
||||
322 2.70000e+03 2.10000e+03
|
||||
323 2.70000e+03 2.20000e+03
|
||||
324 2.70000e+03 2.30000e+03
|
||||
325 2.70000e+03 2.50000e+03
|
||||
326 2.70000e+03 2.60000e+03
|
||||
327 2.70000e+03 2.70000e+03
|
||||
328 2.70000e+03 2.80000e+03
|
||||
329 2.70000e+03 2.90000e+03
|
||||
330 2.70000e+03 3.00000e+03
|
||||
331 2.70000e+03 3.10000e+03
|
||||
332 2.70000e+03 3.20000e+03
|
||||
333 2.70000e+03 3.30000e+03
|
||||
334 2.70000e+03 3.40000e+03
|
||||
335 2.70000e+03 3.50000e+03
|
||||
336 2.70000e+03 3.60000e+03
|
||||
337 2.70000e+03 3.70000e+03
|
||||
338 2.70000e+03 3.80000e+03
|
||||
339 2.80000e+03 9.00000e+02
|
||||
340 2.80000e+03 1.13000e+03
|
||||
341 2.90000e+03 4.00000e+02
|
||||
342 2.90000e+03 5.00000e+02
|
||||
343 2.90000e+03 1.40000e+03
|
||||
344 2.90000e+03 2.40000e+03
|
||||
345 2.90000e+03 3.00000e+03
|
||||
346 3.00000e+03 7.00000e+02
|
||||
347 3.00000e+03 8.00000e+02
|
||||
348 3.00000e+03 9.00000e+02
|
||||
349 3.00000e+03 1.00000e+03
|
||||
350 3.00000e+03 1.10000e+03
|
||||
351 3.00000e+03 1.20000e+03
|
||||
352 3.00000e+03 1.30000e+03
|
||||
353 3.00000e+03 1.50000e+03
|
||||
354 3.00000e+03 1.60000e+03
|
||||
355 3.00000e+03 1.70000e+03
|
||||
356 3.00000e+03 1.80000e+03
|
||||
357 3.00000e+03 1.90000e+03
|
||||
358 3.00000e+03 2.00000e+03
|
||||
359 3.00000e+03 2.10000e+03
|
||||
360 3.00000e+03 2.20000e+03
|
||||
361 3.00000e+03 2.30000e+03
|
||||
362 3.00000e+03 2.50000e+03
|
||||
363 3.00000e+03 2.60000e+03
|
||||
364 3.00000e+03 2.70000e+03
|
||||
365 3.00000e+03 2.80000e+03
|
||||
366 3.00000e+03 2.90000e+03
|
||||
367 3.00000e+03 3.00000e+03
|
||||
368 3.00000e+03 3.10000e+03
|
||||
369 3.00000e+03 3.20000e+03
|
||||
370 3.00000e+03 3.30000e+03
|
||||
371 3.00000e+03 3.40000e+03
|
||||
372 3.00000e+03 3.50000e+03
|
||||
373 3.00000e+03 3.60000e+03
|
||||
374 3.00000e+03 3.70000e+03
|
||||
375 3.00000e+03 3.80000e+03
|
||||
376 1.50000e+02 3.50000e+03
|
||||
377 1.50000e+02 3.55000e+03
|
||||
378 4.69000e+02 2.55000e+03
|
||||
379 4.69000e+02 3.35000e+03
|
||||
380 4.69000e+02 3.45000e+03
|
||||
381 5.40000e+02 2.33000e+03
|
||||
382 5.40000e+02 2.43000e+03
|
||||
383 6.20000e+02 3.65000e+03
|
||||
384 6.20000e+02 3.70900e+03
|
||||
385 7.50000e+02 2.55000e+03
|
||||
386 8.50000e+02 5.20000e+02
|
||||
387 8.50000e+02 7.00000e+02
|
||||
388 8.50000e+02 2.28000e+03
|
||||
389 9.39000e+02 7.40000e+02
|
||||
390 9.50000e+02 2.22000e+03
|
||||
391 9.10000e+02 2.60000e+03
|
||||
392 1.05000e+03 1.05000e+03
|
||||
393 1.15000e+03 1.35000e+03
|
||||
394 1.17000e+03 2.28000e+03
|
||||
395 1.22000e+03 2.21000e+03
|
||||
396 1.35000e+03 7.50000e+02
|
||||
397 1.35000e+03 1.70000e+03
|
||||
398 1.35000e+03 2.14000e+03
|
||||
399 1.45000e+03 7.70000e+02
|
||||
400 1.55000e+03 3.00000e+02
|
||||
401 1.55000e+03 5.00000e+02
|
||||
402 1.55000e+03 1.85000e+03
|
||||
403 1.65000e+03 1.05000e+03
|
||||
404 1.69000e+03 2.68000e+03
|
||||
405 1.71000e+03 3.10000e+02
|
||||
406 1.71000e+03 5.10000e+02
|
||||
407 1.75000e+03 7.50000e+02
|
||||
408 1.79000e+03 2.58000e+03
|
||||
409 1.72000e+03 2.61000e+03
|
||||
410 1.79000e+03 3.33000e+03
|
||||
411 1.72000e+03 3.40900e+03
|
||||
412 1.82900e+03 2.70000e+03
|
||||
413 1.82900e+03 2.80000e+03
|
||||
414 1.82900e+03 3.45000e+03
|
||||
415 2.06000e+03 1.65000e+03
|
||||
416 2.05000e+03 3.15000e+03
|
||||
417 2.17000e+03 1.90000e+03
|
||||
418 2.11000e+03 2.00000e+03
|
||||
419 2.12000e+03 2.75000e+03
|
||||
420 2.15000e+03 3.25000e+03
|
||||
421 2.29000e+03 1.40000e+03
|
||||
422 2.22000e+03 2.82000e+03
|
||||
423 2.28000e+03 3.25000e+03
|
||||
424 2.39000e+03 1.30000e+03
|
||||
425 2.32000e+03 1.50000e+03
|
||||
426 2.45000e+03 7.10000e+02
|
||||
427 2.62000e+03 3.65000e+03
|
||||
428 2.75000e+03 5.20000e+02
|
||||
429 2.76000e+03 2.36000e+03
|
||||
430 2.85000e+03 2.20000e+03
|
||||
431 2.85000e+03 2.70000e+03
|
||||
432 2.85000e+03 3.35000e+03
|
||||
433 2.93000e+03 9.50000e+02
|
||||
434 2.95000e+03 1.75000e+03
|
||||
435 2.95000e+03 2.05000e+03
|
||||
436 5.20000e+02 3.20000e+03
|
||||
437 2.30000e+03 3.50000e+03
|
||||
438 2.32000e+03 3.15000e+03
|
||||
439 5.30000e+02 2.10000e+03
|
||||
440 2.55000e+03 7.10000e+02
|
||||
441 7.50000e+02 4.90000e+02
|
||||
442 0.00000e+00 0.00000e+00
|
||||
EOF
|
||||
55
template/data/simple/att48.tsp
Normal file
55
template/data/simple/att48.tsp
Normal file
@@ -0,0 +1,55 @@
|
||||
NAME : att48
|
||||
COMMENT : 48 capitals of the US (Padberg/Rinaldi)
|
||||
TYPE : TSP
|
||||
DIMENSION : 48
|
||||
EDGE_WEIGHT_TYPE : ATT
|
||||
NODE_COORD_SECTION
|
||||
1 6734 1453
|
||||
2 2233 10
|
||||
3 5530 1424
|
||||
4 401 841
|
||||
5 3082 1644
|
||||
6 7608 4458
|
||||
7 7573 3716
|
||||
8 7265 1268
|
||||
9 6898 1885
|
||||
10 1112 2049
|
||||
11 5468 2606
|
||||
12 5989 2873
|
||||
13 4706 2674
|
||||
14 4612 2035
|
||||
15 6347 2683
|
||||
16 6107 669
|
||||
17 7611 5184
|
||||
18 7462 3590
|
||||
19 7732 4723
|
||||
20 5900 3561
|
||||
21 4483 3369
|
||||
22 6101 1110
|
||||
23 5199 2182
|
||||
24 1633 2809
|
||||
25 4307 2322
|
||||
26 675 1006
|
||||
27 7555 4819
|
||||
28 7541 3981
|
||||
29 3177 756
|
||||
30 7352 4506
|
||||
31 7545 2801
|
||||
32 3245 3305
|
||||
33 6426 3173
|
||||
34 4608 1198
|
||||
35 23 2216
|
||||
36 7248 3779
|
||||
37 7762 4595
|
||||
38 7392 2244
|
||||
39 3484 2829
|
||||
40 6271 2135
|
||||
41 4985 140
|
||||
42 1916 1569
|
||||
43 7280 4899
|
||||
44 7509 3239
|
||||
45 10 2676
|
||||
46 6807 2993
|
||||
47 5185 3258
|
||||
48 3023 1942
|
||||
EOF
|
||||
77
template/data/simple/st70.tsp
Normal file
77
template/data/simple/st70.tsp
Normal file
@@ -0,0 +1,77 @@
|
||||
NAME: st70
|
||||
TYPE: TSP
|
||||
COMMENT: 70-city problem (Smith/Thompson)
|
||||
DIMENSION: 70
|
||||
EDGE_WEIGHT_TYPE : EUC_2D
|
||||
NODE_COORD_SECTION
|
||||
1 64 96
|
||||
2 80 39
|
||||
3 69 23
|
||||
4 72 42
|
||||
5 48 67
|
||||
6 58 43
|
||||
7 81 34
|
||||
8 79 17
|
||||
9 30 23
|
||||
10 42 67
|
||||
11 7 76
|
||||
12 29 51
|
||||
13 78 92
|
||||
14 64 8
|
||||
15 95 57
|
||||
16 57 91
|
||||
17 40 35
|
||||
18 68 40
|
||||
19 92 34
|
||||
20 62 1
|
||||
21 28 43
|
||||
22 76 73
|
||||
23 67 88
|
||||
24 93 54
|
||||
25 6 8
|
||||
26 87 18
|
||||
27 30 9
|
||||
28 77 13
|
||||
29 78 94
|
||||
30 55 3
|
||||
31 82 88
|
||||
32 73 28
|
||||
33 20 55
|
||||
34 27 43
|
||||
35 95 86
|
||||
36 67 99
|
||||
37 48 83
|
||||
38 75 81
|
||||
39 8 19
|
||||
40 20 18
|
||||
41 54 38
|
||||
42 63 36
|
||||
43 44 33
|
||||
44 52 18
|
||||
45 12 13
|
||||
46 25 5
|
||||
47 58 85
|
||||
48 5 67
|
||||
49 90 9
|
||||
50 41 76
|
||||
51 25 76
|
||||
52 37 64
|
||||
53 56 63
|
||||
54 10 55
|
||||
55 98 7
|
||||
56 16 74
|
||||
57 89 60
|
||||
58 48 82
|
||||
59 81 76
|
||||
60 29 60
|
||||
61 17 22
|
||||
62 5 45
|
||||
63 79 70
|
||||
64 9 100
|
||||
65 17 82
|
||||
66 74 67
|
||||
67 10 68
|
||||
68 48 19
|
||||
69 83 86
|
||||
70 84 94
|
||||
EOF
|
||||
25
template/data/simple/ulysses16.tsp
Normal file
25
template/data/simple/ulysses16.tsp
Normal file
@@ -0,0 +1,25 @@
|
||||
NAME: ulysses16.tsp
|
||||
TYPE: TSP
|
||||
COMMENT: Odyssey of Ulysses (Groetschel/Padberg)
|
||||
DIMENSION: 16
|
||||
EDGE_WEIGHT_TYPE: GEO
|
||||
DISPLAY_DATA_TYPE: COORD_DISPLAY
|
||||
NODE_COORD_SECTION
|
||||
1 38.24 20.42
|
||||
2 39.57 26.15
|
||||
3 40.56 25.32
|
||||
4 36.26 23.12
|
||||
5 33.48 10.54
|
||||
6 37.56 12.19
|
||||
7 38.42 13.11
|
||||
8 37.52 20.44
|
||||
9 41.23 9.10
|
||||
10 41.17 13.05
|
||||
11 36.08 -5.21
|
||||
12 38.47 15.13
|
||||
13 38.15 15.35
|
||||
14 37.51 15.17
|
||||
15 35.49 14.32
|
||||
16 39.36 19.56
|
||||
EOF
|
||||
|
||||
121
template/main.py
Normal file
121
template/main.py
Normal file
@@ -0,0 +1,121 @@
|
||||
import os
|
||||
import signal
|
||||
import json
|
||||
|
||||
from model.anneal_model import SimAnneal
|
||||
from model.my_model import MyModel
|
||||
|
||||
def log(msg):
|
||||
print('[*] {msg}'.format(msg=msg))
|
||||
|
||||
def load_data(file):
|
||||
coords = []
|
||||
with open(file, "r") as infile:
|
||||
line = infile.readline()
|
||||
# Skip instance header
|
||||
while "NODE_COORD_SECTION" not in line:
|
||||
line = infile.readline()
|
||||
|
||||
for line in infile.readlines():
|
||||
line = line.replace("\n", "")
|
||||
if line and 'EOF' not in line:
|
||||
line = [float(x) for x in line.lstrip().split(" ", 1)[1].split(" ") if x]
|
||||
coords.append(line)
|
||||
return coords
|
||||
|
||||
def timeout_handler(signum, frame):
|
||||
print("Timeout")
|
||||
raise Exception("Timeout")
|
||||
|
||||
def TSP(tsp_file, model):
|
||||
|
||||
best_solution = []
|
||||
fitness_list = []
|
||||
|
||||
coords = load_data(tsp_file)
|
||||
|
||||
# Set timeout
|
||||
# signal.signal(signal.SIGALRM, timeout_handler)
|
||||
# signal.alarm(60)
|
||||
|
||||
# Try your algorithm
|
||||
try:
|
||||
model.init(coords)
|
||||
best_solution, fitness_list = model.fit(max_it=10000)
|
||||
except Exception as exc:
|
||||
if(str(exc) == "Timeout"):
|
||||
log("Timeout -3")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-3")
|
||||
else:
|
||||
print(exc)
|
||||
log("Unkown -4")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-4")
|
||||
|
||||
# Collect results
|
||||
if(len(fitness_list) > 0):
|
||||
log("Best: " + str(fitness_list[-1]))
|
||||
|
||||
if (len(best_solution) == 0):
|
||||
log("No Answer -1")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-1")
|
||||
elif (len(best_solution) != len(coords)):
|
||||
log("Invalid -2")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-2")
|
||||
else:
|
||||
log("Writing the best solution to file" )
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write(str(model.fitness(best_solution)))
|
||||
outfile.write("\n")
|
||||
outfile.write(", ".join(str(item) for item in best_solution))
|
||||
|
||||
# Write to JSON
|
||||
data = {}
|
||||
|
||||
data['nodes'] = []
|
||||
for i in range(len(coords)):
|
||||
data['nodes'].append({
|
||||
'title': str(i),
|
||||
'id': i,
|
||||
'x': int(coords[i][0]),
|
||||
'y': int(coords[i][1])
|
||||
})
|
||||
|
||||
data['edges'] = []
|
||||
for i in range(len(best_solution)):
|
||||
if i == len(best_solution)-1:
|
||||
data['edges'].append({
|
||||
'source': best_solution[i],
|
||||
'target': best_solution[0]
|
||||
})
|
||||
else:
|
||||
data['edges'].append({
|
||||
'source': best_solution[i],
|
||||
'target': best_solution[i+1]
|
||||
})
|
||||
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.json', 'w') as outfile:
|
||||
json.dump(data, outfile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
for root, _, files in os.walk('./data'):
|
||||
if(files):
|
||||
for f in files:
|
||||
# Get input file name
|
||||
tsp_file = str(root) + '/' + str(f)
|
||||
log(tsp_file)
|
||||
|
||||
# Your Model
|
||||
# model = MyModel()
|
||||
|
||||
# Simulated Annealing
|
||||
model = SimAnneal()
|
||||
|
||||
TSP(tsp_file, model)
|
||||
|
||||
print()
|
||||
88
template/model/anneal_model.py
Normal file
88
template/model/anneal_model.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import math
|
||||
import random
|
||||
from model.base_model import Model
|
||||
|
||||
class SimAnneal(Model):
|
||||
def __init__(self, T=-1, alpha=-1, stopping_T=-1):
|
||||
super().__init__()
|
||||
|
||||
self.iteration = 0
|
||||
|
||||
self.T = T
|
||||
self.alpha = 0.995 if alpha == -1 else alpha
|
||||
self.stopping_temperature = 1e-8 if stopping_T == -1 else stopping_T
|
||||
|
||||
def init(self, coords):
|
||||
super().init(coords)
|
||||
|
||||
if (self.T == -1):
|
||||
self.T = math.sqrt(self.N)
|
||||
self.T_save = self.T # save inital T to reset if batch annealing is used
|
||||
|
||||
def initial_solution(self):
|
||||
"""
|
||||
Greedy algorithm to get an initial solution (closest-neighbour).
|
||||
"""
|
||||
cur_node = random.choice(self.nodes) # start from a random node
|
||||
solution = [cur_node]
|
||||
|
||||
free_nodes = set(self.nodes)
|
||||
free_nodes.remove(cur_node)
|
||||
while free_nodes:
|
||||
next_node = min(free_nodes, key=lambda x: self.dist(cur_node, x)) # nearest neighbour
|
||||
free_nodes.remove(next_node)
|
||||
solution.append(next_node)
|
||||
cur_node = next_node
|
||||
|
||||
cur_fit = self.fitness(solution)
|
||||
if cur_fit < self.best_fitness: # If best found so far, update best fitness
|
||||
self.best_fitness = cur_fit
|
||||
self.best_solution = solution
|
||||
self.fitness_list.append(cur_fit)
|
||||
return solution, cur_fit
|
||||
|
||||
def p_accept(self, candidate_fitness):
|
||||
"""
|
||||
Probability of accepting if the candidate is worse than current.
|
||||
Depends on the current temperature and difference between candidate and current.
|
||||
"""
|
||||
return math.exp(-abs(candidate_fitness - self.cur_fitness) / self.T)
|
||||
|
||||
def accept(self, candidate):
|
||||
"""
|
||||
Accept with probability 1 if candidate is better than current.
|
||||
Accept with probabilty p_accept(..) if candidate is worse.
|
||||
"""
|
||||
candidate_fitness = self.fitness(candidate)
|
||||
if candidate_fitness < self.cur_fitness:
|
||||
self.cur_fitness, self.cur_solution = candidate_fitness, candidate
|
||||
if candidate_fitness < self.best_fitness:
|
||||
self.best_fitness, self.best_solution = candidate_fitness, candidate
|
||||
else:
|
||||
if random.random() < self.p_accept(candidate_fitness):
|
||||
self.cur_fitness, self.cur_solution = candidate_fitness, candidate
|
||||
|
||||
def fit(self, max_it=1000):
|
||||
"""
|
||||
Execute simulated annealing algorithm.
|
||||
"""
|
||||
# Initialize with the greedy solution.
|
||||
self.cur_solution, self.cur_fitness = self.initial_solution()
|
||||
|
||||
self.log("Starting annealing.")
|
||||
while self.T >= self.stopping_temperature and self.iteration < max_it:
|
||||
candidate = list(self.cur_solution)
|
||||
l = random.randint(1, self.N - 1)
|
||||
i = random.randint(0, self.N - l)
|
||||
candidate[i : (i + l)] = reversed(candidate[i : (i + l)])
|
||||
self.accept(candidate)
|
||||
self.T *= self.alpha
|
||||
self.iteration += 1
|
||||
|
||||
self.fitness_list.append(self.cur_fitness)
|
||||
|
||||
self.log(f"Best fitness obtained: {self.best_fitness}")
|
||||
improvement = 100 * (self.fitness_list[0] - self.best_fitness) / (self.fitness_list[0])
|
||||
self.log(f"Improvement over greedy heuristic: {improvement : .2f}%")
|
||||
|
||||
return self.best_solution, self.fitness_list
|
||||
35
template/model/base_model.py
Normal file
35
template/model/base_model.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import math
|
||||
|
||||
class Model:
|
||||
|
||||
def __init__(self):
|
||||
self.best_solution = []
|
||||
self.best_fitness = float("Inf")
|
||||
self.fitness_list = []
|
||||
|
||||
def init(self, coords):
|
||||
self.coords = coords
|
||||
self.N = len(coords)
|
||||
self.nodes = [i for i in range(self.N)]
|
||||
|
||||
def dist(self, node_0, node_1):
|
||||
"""
|
||||
Euclidean distance between two nodes.
|
||||
"""
|
||||
coord_0, coord_1 = self.coords[node_0], self.coords[node_1]
|
||||
return math.sqrt((coord_0[0] - coord_1[0]) ** 2 + (coord_0[1] - coord_1[1]) ** 2)
|
||||
|
||||
def fitness(self, solution):
|
||||
"""
|
||||
Total distance of the current solution path.
|
||||
"""
|
||||
cur_fit = 0
|
||||
for i in range(self.N):
|
||||
cur_fit += self.dist(solution[i % self.N], solution[(i + 1) % self.N])
|
||||
return cur_fit
|
||||
|
||||
def fit(self):
|
||||
raise NotImplementedError("Your fitting method not implemented yet")
|
||||
|
||||
def log(self, message):
|
||||
print('[{name}] {msg}'.format(name=self.__class__.__name__, msg=message))
|
||||
22
template/model/my_model.py
Normal file
22
template/model/my_model.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import math
|
||||
import random
|
||||
from model.base_model import Model
|
||||
|
||||
class MyModel(Model):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def init(self, coords):
|
||||
"""
|
||||
Put your initialization here.
|
||||
"""
|
||||
super().init(coords)
|
||||
self.log("Nothing to initialize in your model now")
|
||||
|
||||
def fit(self, max_it=1000, visualize=False):
|
||||
"""
|
||||
Put your iteration process here.
|
||||
"""
|
||||
self.log("Nothing happens in your model now")
|
||||
|
||||
return self.best_solution, self.fitness_list
|
||||
8
template/output/.gitignore
vendored
Normal file
8
template/output/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# .gitignore sample
|
||||
###################
|
||||
|
||||
# Ignore all files in this dir...
|
||||
*
|
||||
|
||||
# ... except for this one.
|
||||
!.gitignore
|
||||
0
template/requirements.txt
Normal file
0
template/requirements.txt
Normal file
21
template/utils/load_data.py
Normal file
21
template/utils/load_data.py
Normal file
@@ -0,0 +1,21 @@
|
||||
def log(msg):
|
||||
print('[*] {msg}'.format(msg=msg))
|
||||
|
||||
def load_data(file):
|
||||
coords = []
|
||||
with open(file, "r") as infile:
|
||||
line = infile.readline()
|
||||
# Skip instance header
|
||||
while "NODE_COORD_SECTION" not in line:
|
||||
line = infile.readline()
|
||||
|
||||
for line in infile.readlines():
|
||||
line = line.replace("\n", "")
|
||||
if line and 'EOF' not in line:
|
||||
line = [float(x) for x in line.lstrip().split(" ", 1)[1].split(" ") if x]
|
||||
coords.append(line)
|
||||
return coords
|
||||
|
||||
def timeout_handler(signum, frame):
|
||||
print("Timeout")
|
||||
raise Exception("Timeout")
|
||||
139
template/utils/tsp.py
Normal file
139
template/utils/tsp.py
Normal file
@@ -0,0 +1,139 @@
|
||||
import os
|
||||
import signal
|
||||
import json
|
||||
import math
|
||||
import timeit
|
||||
|
||||
from utils.load_data import load_data
|
||||
from utils.load_data import log
|
||||
|
||||
def timeout_handler(signum, frame):
|
||||
raise Exception("Timeout")
|
||||
|
||||
def dist(node_0, node_1, coords):
|
||||
"""
|
||||
Euclidean distance between two nodes.
|
||||
"""
|
||||
coord_0, coord_1 = coords[node_0], coords[node_1]
|
||||
return math.sqrt((coord_0[0] - coord_1[0]) ** 2 + (coord_0[1] - coord_1[1]) ** 2)
|
||||
|
||||
def fitness(solution, coords):
|
||||
N = len(coords)
|
||||
cur_fit = 0
|
||||
for i in range(len(solution)):
|
||||
cur_fit += dist(solution[i % N], solution[(i + 1) % N], coords)
|
||||
return cur_fit
|
||||
|
||||
def TSP_Bench(tsp_file, model, *args, max_it=1000, timeout=60):
|
||||
|
||||
start = timeit.default_timer()
|
||||
|
||||
# Model Running
|
||||
best_solution, fitness_list = TSP(tsp_file, model, *args, max_it=max_it,timeout=timeout)
|
||||
# Model End
|
||||
|
||||
stop = timeit.default_timer()
|
||||
print('[*] Running for: {time:.2f} seconds'.format(time=(stop - start)))
|
||||
|
||||
print()
|
||||
return best_solution, fitness_list, (stop - start)
|
||||
|
||||
def TSP_Bench_ALL(tsp_file_path, model, *args, max_it=1000, timeout=60):
|
||||
best_solutions = []
|
||||
fitness_lists = []
|
||||
times = []
|
||||
for root, _, files in os.walk(tsp_file_path):
|
||||
if(files):
|
||||
for f in files:
|
||||
# Get input file name
|
||||
tsp_file = str(root) + '/' + str(f)
|
||||
log(tsp_file)
|
||||
|
||||
# Run TSP
|
||||
best_solution, fitness_list, time = TSP_Bench(tsp_file, model, *args, max_it=max_it,timeout=timeout)
|
||||
best_solutions.append(best_solution)
|
||||
fitness_lists.append(fitness_lists)
|
||||
times.append(time)
|
||||
|
||||
return best_solutions, fitness_lists, times
|
||||
|
||||
def TSP(tsp_file, model, *args, max_it=1000, timeout=60):
|
||||
|
||||
best_solution = []
|
||||
fitness_list = []
|
||||
|
||||
nodes = load_data(tsp_file)
|
||||
|
||||
# Set timeout
|
||||
signal.signal(signal.SIGALRM, timeout_handler)
|
||||
signal.alarm(timeout)
|
||||
|
||||
if not os.path.exists('output'):
|
||||
os.makedirs('output')
|
||||
|
||||
# Try your algorithm
|
||||
try:
|
||||
model.init(nodes, *args)
|
||||
best_solution, fitness_list = model.fit(max_it)
|
||||
except Exception as exc:
|
||||
if(str(exc) == "Timeout"):
|
||||
log("Timeout -3")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-3")
|
||||
best_solution = [-1] * len(nodes)
|
||||
else:
|
||||
print(exc)
|
||||
log("Unkown -4")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-4")
|
||||
|
||||
signal.alarm(0)
|
||||
|
||||
# Collect results
|
||||
if(len(fitness_list) > 0):
|
||||
log("[Node] " + str(len(best_solution)) + ", [Best] " + str(fitness_list[fitness_list.index(min(fitness_list))]))
|
||||
|
||||
if (len(best_solution) == 0):
|
||||
log("No Answer -1")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-1")
|
||||
elif (len(best_solution) != len(nodes)):
|
||||
log("Invalid -2")
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write("-2")
|
||||
else:
|
||||
# log("Writing the best solution to file" )
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.txt', "w") as outfile:
|
||||
outfile.write(str(model.fitness(best_solution)))
|
||||
outfile.write("\n")
|
||||
outfile.write(", ".join(str(item) for item in best_solution))
|
||||
|
||||
# Write to JSON
|
||||
data = {}
|
||||
|
||||
data['nodes'] = []
|
||||
for i in range(len(nodes)):
|
||||
data['nodes'].append({
|
||||
'title': str(i),
|
||||
'id': i,
|
||||
'x': int(nodes[i][0]),
|
||||
'y': int(nodes[i][1])
|
||||
})
|
||||
|
||||
data['edges'] = []
|
||||
for i in range(len(best_solution)):
|
||||
if i == len(best_solution)-1:
|
||||
data['edges'].append({
|
||||
'source': best_solution[i],
|
||||
'target': best_solution[0]
|
||||
})
|
||||
else:
|
||||
data['edges'].append({
|
||||
'source': best_solution[i],
|
||||
'target': best_solution[i+1]
|
||||
})
|
||||
|
||||
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.json', 'w') as outfile:
|
||||
json.dump(data, outfile)
|
||||
|
||||
return best_solution, fitness_list
|
||||
64
template/utils/visualize_tsp.py
Normal file
64
template/utils/visualize_tsp.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def plot_learning(fitness_list):
|
||||
"""
|
||||
Plot the fitness through iterations.
|
||||
"""
|
||||
plt.plot([i for i in range(len(fitness_list))], fitness_list)
|
||||
plt.ylabel("Fitness")
|
||||
plt.xlabel("Iteration")
|
||||
plt.show()
|
||||
|
||||
|
||||
def plotTSP(paths, points, num_iters=1):
|
||||
|
||||
"""
|
||||
path: List of lists with the different orders in which the nodes are visited
|
||||
points: coordinates for the different nodes
|
||||
num_iters: number of paths that are in the path list
|
||||
|
||||
"""
|
||||
|
||||
# Unpack the primary TSP path and transform it into a list of ordered
|
||||
# coordinates
|
||||
|
||||
x = []; y = []
|
||||
for i in paths[0]:
|
||||
x.append(points[i][0])
|
||||
y.append(points[i][1])
|
||||
|
||||
plt.plot(x, y, 'co')
|
||||
|
||||
# Set a scale for the arrow heads (there should be a reasonable default for this, WTF?)
|
||||
# a_scale = float(max(x))/float(100)
|
||||
|
||||
# Draw the older paths, if provided
|
||||
if num_iters > 1:
|
||||
|
||||
for i in range(1, num_iters):
|
||||
|
||||
# Transform the old paths into a list of coordinates
|
||||
xi = []; yi = [];
|
||||
for j in paths[i]:
|
||||
xi.append(points[j][0])
|
||||
yi.append(points[j][1])
|
||||
|
||||
plt.arrow(xi[-1], yi[-1], (xi[0] - xi[-1]), (yi[0] - yi[-1]),
|
||||
color = 'r', length_includes_head = True, ls = 'dashed',
|
||||
width = 0.001/float(num_iters))
|
||||
for i in range(0, len(x) - 1):
|
||||
plt.arrow(xi[i], yi[i], (xi[i+1] - xi[i]), (yi[i+1] - yi[i]),
|
||||
color = 'r', length_includes_head = True,
|
||||
ls = 'dashed', width = 0.001/float(num_iters))
|
||||
|
||||
# Draw the primary path for the TSP problem
|
||||
plt.arrow(x[-1], y[-1], (x[0] - x[-1]), (y[0] - y[-1]),
|
||||
color ='g', length_includes_head=True)
|
||||
for i in range(0,len(x)-1):
|
||||
plt.arrow(x[i], y[i], (x[i+1] - x[i]), (y[i+1] - y[i]),
|
||||
color = 'g', length_includes_head = True)
|
||||
|
||||
#Set axis too slitghtly larger than the set of x and y
|
||||
# plt.xlim(min(x)*1.1, max(x)*1.1)
|
||||
# plt.ylim(min(y)*1.1, max(y)*1.1)
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user