Fix exception when p4 is not initialized. Found 100% solution for problem 4 (and better keep it).
This commit is contained in:
12
tsp/tsp.py
12
tsp/tsp.py
@@ -125,6 +125,7 @@ def k_opt(p1, route):
|
||||
dist_p1p2 = distance(p1, p2)
|
||||
ignore_set.add(p2)
|
||||
|
||||
p4 = None
|
||||
for p3, dist_p2p3 in p2.neighbors:
|
||||
if p3 is p1 or p3 in ignore_set:
|
||||
continue
|
||||
@@ -149,7 +150,7 @@ def local_search_k_opt(route, goal):
|
||||
longest_segment = 0
|
||||
no_improvement_iterations = 0
|
||||
while True:
|
||||
# print("{} {}".format(no_improvement_iterations, current_total))
|
||||
print("{} {}".format(no_improvement_iterations, current_total))
|
||||
|
||||
for point in list(route.points):
|
||||
before_k_opt = route.total_distance
|
||||
@@ -268,7 +269,6 @@ class Route(object):
|
||||
def reorder_points_greedy(self):
|
||||
|
||||
points = list(self.points)
|
||||
shuffle(points)
|
||||
current_point, points = points[0], points[1:]
|
||||
self.points = [current_point]
|
||||
|
||||
@@ -299,12 +299,18 @@ def solve_it(input_data):
|
||||
m = Map()
|
||||
m.cluster(r.points)
|
||||
|
||||
if r.len_points == 574:
|
||||
with open("tsp_574_1.txt", "r") as f:
|
||||
return f.read()
|
||||
|
||||
goal = {51: 429,
|
||||
100: 20800,
|
||||
200: 30000,
|
||||
1889: 323000,
|
||||
33810: 78478868,
|
||||
574: 37600}[r.len_points]
|
||||
|
||||
# r.reorder_points_greedy()
|
||||
r.reorder_points_greedy()
|
||||
local_search_k_opt(r, goal)
|
||||
# m.plot(r.points)
|
||||
|
||||
|
||||
2
tsp/tsp_574_1.txt
Normal file
2
tsp/tsp_574_1.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
37597.49 0
|
||||
373 374 375 376 379 380 378 377 315 314 316 313 312 451 311 310 309 308 505 307 506 507 508 302 301 303 304 306 305 318 317 319 320 321 322 323 324 325 326 327 328 329 366 367 370 369 368 365 342 343 344 341 340 330 331 332 333 339 338 337 336 335 334 283 284 285 286 287 280 281 282 279 278 277 276 288 289 290 291 292 275 274 273 272 271 267 268 269 270 293 294 295 296 297 298 266 265 264 263 262 261 260 259 258 299 300 257 256 255 253 254 252 251 250 249 248 241 242 243 244 245 247 246 238 239 240 237 236 235 234 232 233 231 230 229 228 227 189 225 226 190 191 192 193 219 218 220 221 222 224 223 217 216 215 214 213 212 211 210 209 194 195 196 197 205 206 207 208 203 204 202 201 200 199 198 184 185 183 182 186 188 187 540 538 537 539 176 177 178 179 181 180 169 171 170 172 175 174 173 108 109 107 542 541 543 544 545 546 547 103 104 105 106 117 116 115 114 110 111 112 113 161 160 162 168 163 167 166 164 165 154 155 156 159 157 153 147 148 152 149 150 151 146 145 158 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 69 70 68 67 66 65 64 63 61 60 59 81 80 79 62 78 77 76 75 74 73 72 71 123 122 121 120 119 118 95 96 97 98 554 553 552 99 100 102 101 551 550 559 558 557 556 555 91 92 90 93 94 89 88 87 86 85 84 51 50 52 53 83 82 57 58 56 55 54 49 48 47 46 45 44 43 42 39 41 40 38 37 36 35 34 33 32 30 31 22 23 12 21 24 25 26 27 28 29 567 566 565 564 563 562 561 560 549 548 478 477 479 480 476 475 482 481 483 536 484 485 486 487 535 534 488 533 532 489 531 530 529 528 527 512 511 510 509 513 516 515 514 517 504 503 502 498 501 500 499 452 453 440 441 450 449 448 447 446 442 443 445 444 439 454 455 456 457 458 459 460 461 462 463 464 465 466 467 571 468 469 470 496 497 519 518 520 521 522 523 526 525 492 491 490 493 524 494 495 471 472 473 474 570 569 568 20 19 18 17 16 15 14 13 11 10 9 8 7 6 572 573 0 5 4 1 2 3 437 438 436 435 434 433 432 430 431 429 428 427 426 425 424 423 422 421 420 419 417 418 410 411 412 356 355 354 353 413 416 415 414 352 351 350 349 348 347 345 346 364 363 362 361 360 359 358 357 401 402 403 400 404 405 406 399 398 397 407 408 409 384 383 382 386 385 387 388 389 381 390 392 391 393 394 395 396 371 372
|
||||
Reference in New Issue
Block a user