[CT421]: Assignment 1 over the line

This commit is contained in:
2025-02-20 03:24:49 +00:00
parent 8d726e73c0
commit 5ab6c4775a
4 changed files with 5005 additions and 505 deletions

View File

@ -26,6 +26,6 @@ def plot_tour(graph, tour):
plt.show()
# Example usage
graph = graph_from_file("../materials/berlin52.tsp")
tour = [16, 50, 44, 34, 46, 48, 24, 38, 40, 37, 5, 15, 4, 6, 25, 51, 12, 28, 27, 26, 14, 13, 52, 11, 33, 43, 45, 19, 10, 9, 8, 41, 17, 31, 3, 18, 32, 36, 39, 35, 49, 1, 22, 23, 20, 30, 42, 2, 7, 21, 29, 47]
graph = graph_from_file("../materials/pr1002.tsp")
tour = [678, 679, 675, 673, 672, 665, 674, 676, 667, 692, 710, 711, 726, 611, 608, 610, 997, 587, 798, 823, 859, 860, 854, 862, 858, 830, 839, 867, 849, 852, 866, 916, 908, 895, 871, 870, 847, 840, 828, 825, 805, 787, 797, 600, 586, 584, 449, 468, 467, 414, 452, 446, 431, 442, 441, 606, 740, 741, 691, 734, 747, 373, 409, 380, 461, 593, 572, 580, 582, 454, 629, 603, 499, 242, 511, 523, 999, 565, 545, 451, 480, 416, 425, 411, 992, 599, 590, 573, 472, 271, 264, 265, 260, 469, 581, 554, 553, 841, 846, 827, 592, 550, 577, 567, 516, 518, 564, 546, 598, 589, 746, 732, 649, 653, 357, 358, 362, 639, 790, 761, 784, 961, 704, 671, 951, 966, 967, 973, 988, 773, 775, 774, 779, 887, 902, 897, 873, 885, 891, 893, 865, 856, 838, 540, 521, 529, 526, 525, 530, 533, 531, 547, 549, 570, 470, 429, 424, 422, 456, 371, 366, 378, 659, 352, 342, 395, 382, 385, 351, 364, 643, 654, 635, 742, 765, 702, 1000, 754, 786, 762, 769, 963, 964, 965, 957, 708, 707, 645, 655, 374, 613, 650, 652, 388, 360, 353, 662, 668, 989, 993, 948, 977, 947, 945, 981, 701, 696, 709, 745, 955, 760, 884, 880, 883, 881, 922, 924, 928, 915, 930, 931, 927, 925, 905, 914, 904, 789, 968, 971, 972, 984, 682, 669, 722, 725, 661, 642, 656, 657, 739, 624, 623, 622, 636, 427, 412, 379, 601, 814, 544, 543, 541, 539, 807, 822, 552, 538, 534, 556, 557, 602, 619, 620, 462, 465, 423, 405, 500, 996, 154, 167, 178, 179, 204, 200, 188, 210, 192, 199, 197, 240, 241, 194, 201, 243, 246, 187, 189, 239, 504, 512, 510, 517, 235, 489, 524, 505, 206, 207, 495, 494, 479, 588, 596, 792, 788, 793, 743, 703, 697, 771, 911, 776, 933, 764, 772, 758, 766, 958, 941, 932, 929, 882, 861, 889, 926, 903, 819, 835, 609, 736, 749, 751, 737, 597, 809, 836, 834, 855, 874, 813, 896, 842, 850, 918, 920, 910, 890, 868, 845, 919, 923, 888, 869, 863, 875, 892, 844, 816, 898, 900, 886, 878, 879, 877, 876, 848, 894, 799, 731, 755, 962, 942, 770, 698, 956, 953, 943, 938, 944, 934, 936, 752, 730, 614, 440, 438, 439, 443, 436, 417, 305, 325, 332, 334, 13, 16, 18, 54, 311, 308, 307, 110, 119, 111, 312, 136, 74, 42, 72, 30, 333, 401, 324, 62, 50, 14, 29, 26, 22, 19, 56, 6, 1, 80, 95, 130, 101, 100, 133, 2, 4, 11, 41, 303, 283, 287, 408, 406, 426, 585, 796, 481, 513, 485, 484, 493, 474, 477, 502, 503, 205, 193, 219, 220, 238, 166, 158, 171, 222, 223, 247, 233, 226, 492, 488, 795, 445, 444, 631, 637, 419, 466, 463, 447, 604, 794, 733, 607, 753, 626, 625, 628, 375, 377, 345, 389, 381, 289, 286, 318, 297, 294, 337, 327, 55, 90, 108, 102, 225, 266, 496, 473, 605, 575, 528, 832, 815, 812, 837, 555, 558, 563, 551, 826, 817, 514, 568, 559, 594, 821, 808, 820, 522, 569, 833, 532, 562, 507, 214, 212, 182, 202, 195, 165, 126, 137, 231, 198, 203, 181, 157, 162, 196, 170, 175, 173, 176, 177, 161, 163, 237, 254, 132, 146, 301, 309, 59, 52, 75, 113, 112, 275, 276, 274, 402, 296, 295, 293, 322, 31, 310, 48, 116, 302, 407, 450, 634, 464, 421, 430, 612, 617, 621, 648, 646, 391, 335, 368, 384, 615, 729, 728, 693, 677, 949, 767, 959, 935, 781, 912, 906, 853, 843, 857, 872, 899, 901, 810, 801, 757, 804, 806, 803, 954, 982, 685, 983, 986, 990, 688, 946, 759, 735, 633, 627, 641, 638, 663, 664, 723, 721, 683, 695, 670, 666, 970, 950, 699, 727, 690, 978, 987, 979, 684, 952, 744, 750, 542, 537, 535, 536, 515, 527, 487, 498, 211, 209, 216, 232, 180, 183, 252, 118, 120, 279, 58, 65, 43, 284, 270, 501, 482, 583, 432, 433, 630, 457, 459, 376, 644, 647, 616, 660, 387, 361, 363, 367, 372, 640, 651, 356, 355, 348, 397, 47, 331, 321, 314, 340, 383, 292, 396, 400, 370, 420, 476, 475, 428, 277, 282, 272, 267, 172, 174, 184, 160, 159, 131, 127, 76, 51, 39, 53, 49, 84, 93, 134, 98, 94, 109, 78, 81, 38, 66, 32, 329, 60, 68, 88, 73, 115, 123, 256, 273, 269, 995, 234, 244, 259, 245, 261, 186, 103, 124, 994, 121, 478, 998, 455, 453, 437, 369, 394, 359, 618, 763, 1001, 917, 921, 777, 782, 785, 937, 960, 939, 940, 985, 718, 715, 719, 681, 969, 712, 720, 716, 694, 705, 700, 975, 687, 689, 976, 980, 974, 706, 717, 738, 713, 714, 780, 768, 913, 907, 756, 791, 748, 778, 783, 909, 800, 802, 811, 824, 864, 851, 1002, 829, 831, 818, 574, 578, 579, 448, 434, 632, 435, 460, 268, 122, 142, 140, 169, 153, 144, 152, 151, 148, 251, 150, 145, 129, 141, 143, 69, 61, 27, 21, 35, 15, 36, 25, 17, 63, 40, 45, 319, 300, 299, 288, 338, 330, 403, 415, 410, 413, 418, 458, 519, 561, 595, 576, 571, 520, 560, 490, 506, 486, 566, 548, 591, 471, 278, 285, 117, 280, 104, 105, 128, 255, 147, 218, 168, 185, 125, 249, 263, 230, 262, 215, 221, 508, 509, 224, 228, 227, 248, 149, 253, 139, 164, 138, 97, 77, 83, 57, 64, 7, 12, 8, 9, 70, 24, 20, 316, 34, 37, 107, 106, 250, 258, 155, 156, 135, 79, 10, 5, 3, 71, 67, 991, 398, 336, 343, 315, 33, 23, 341, 347, 349, 365, 390, 393, 386, 339, 344, 392, 354, 346, 350, 328, 320, 323, 497, 213, 491, 217, 236, 208, 191, 483, 190, 229, 257, 304, 306, 281, 290, 291, 46, 89, 82, 114, 99, 96, 85, 92, 91, 87, 44, 86, 317, 313, 326, 28, 399, 298, 404, 658, 724, 680, 686]
plot_tour(graph, tour)

File diff suppressed because it is too large Load Diff

View File

@ -7,12 +7,12 @@ import argparse
# Load TSV file
def plot_fitness_trends(file_path):
df = pd.read_csv(file_path, sep='\t')
plt.figure(figsize=(10, 5))
plt.plot(df['generation'], df['avg_fitness'], label='Avg Fitness', marker=None)
plt.plot(df['generation'], df['generation_best'], label='Generation Best', marker=None)
plt.plot(df['generation'], df['current_best'], label='Current Best', marker=None)
plt.xlabel('Generation')
plt.ylabel('Fitness')
plt.title('Fitness Trends Across Generations')

View File

@ -134,7 +134,7 @@ def crossover(population, crossover_rate, number_to_replace):
if random.random() < 0.5:
child = pmx(parent1, parent2)
else:
child = ox(parent1, parent2)
child = pmx(parent1, parent2)
offspring.append(child)