diff --git a/.gitignore b/.gitignore index dbab12d..3a9801d 100644 --- a/.gitignore +++ b/.gitignore @@ -109,7 +109,7 @@ profile_default/ ipython_config.py # pyenv -# For a library or package, you might want to ignore these files since the code is +# For a library or package, you might want to ignore these files since the aco is # intended to run in multiple environments; otherwise, check them in: # .python-version diff --git a/AI_cup_2020_ClaudioMaggioni.xls b/AI_cup_2020_ClaudioMaggioni.xls index f495d72..4e5e344 100644 Binary files a/AI_cup_2020_ClaudioMaggioni.xls and b/AI_cup_2020_ClaudioMaggioni.xls differ diff --git a/aco/TSP_solver.py b/aco/TSP_solver.py new file mode 100644 index 0000000..96d4a06 --- /dev/null +++ b/aco/TSP_solver.py @@ -0,0 +1,78 @@ +from time import time as t +import numpy as np +import matplotlib.pyplot as plt +from aco.ant_colony import ant_colony_opt + +class TSPSolver: + def __init__(self, problem_instance): + self.duration = np.inf + self.found_length = np.inf + self.solved = False + self.problem_instance = problem_instance + self.solution = None + + def compute_solution(self, verbose=True, return_value=True): + self.solved = False + if verbose: + print(f"### solving with 'C++ ant colony optimization' ####") + start_time = t() + self.solution = ant_colony_opt(self.problem_instance) + if not self.check_if_solution_is_valid(): + print(f"Error the solution of 'C++ ant colony optimization'" + f" for problem {self.problem_instance.name} is not valid") + if return_value: + return False + + end_time = t() + self.duration = np.around(end_time - start_time, 3) + self.solved = True + self.evaluate_solution() + self._gap() + if return_value: + return self.solution + + def plot_solution(self): + assert self.solved, "You can't plot the solution, you need to compute it first!" + plt.figure(figsize=(8, 8)) + self._gap() + plt.title(f"{self.problem_instance.name} solved with 'C++ ant colony optimization'" + f" solver, gap {self.gap}") + ordered_points = self.problem_instance.points[self.solution] + plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-') + plt.show() + + def check_if_solution_is_valid(self): + rights_values = np.sum( + [self.check_validation(i, self.solution[:-1]) for i in + np.arange(self.problem_instance.nPoints)]) + return rights_values == self.problem_instance.nPoints + + def check_validation(self, node, solution): + if np.sum(solution == node) == 1: + return 1 + else: + return 0 + + def evaluate_solution(self, return_value=False): + total_length = 0 + starting_node = self.solution[0] + from_node = starting_node + for node in self.solution[1:]: + total_length += self.problem_instance.dist_matrix[from_node, node] + from_node = node + + self.found_length = total_length + if return_value: + return total_length + + def pass_and_check_if_solution_is_valid(self, solution): + rights_values = np.sum( + [self.check_validation(i, solution[:-1]) for i in + np.arange(self.problem_instance.nPoints)]) + return rights_values == self.problem_instance.nPoints + + def _gap(self): + self.evaluate_solution(return_value=False) + self.gap = np.round( + ((self.found_length - self.problem_instance.best_sol) / + self.problem_instance.best_sol) * 100, 2) diff --git a/src/__init__.py b/aco/__init__.py similarity index 100% rename from src/__init__.py rename to aco/__init__.py diff --git a/src/ant_colony.py b/aco/ant_colony.py similarity index 71% rename from src/ant_colony.py rename to aco/ant_colony.py index 500bd0b..d60d3d4 100644 --- a/src/ant_colony.py +++ b/aco/ant_colony.py @@ -1,6 +1,6 @@ import os -dir = "/Users/maggicl/Git/AI2020BsC/c_prob/" +dir = "c_prob/" def ant_colony_opt(instance): fname = dir + instance.name + ".txt" @@ -12,7 +12,7 @@ def ant_colony_opt(instance): # single core params params = { "eil76": (0.9, 8, 0.4, 1000, 500, 200, 30), # 543 - "d198": (0.9, 8, 0.4, 1000, 500, 60, 30), # 16042 + "d198": (0.9, 8, 0.4, 1000, 250, 125, 5), # 15871 "ch130": (0.9, 8, 0.4, 1000, 1750, 25, 30), # 6212 "kroA100": (0.9, 8, 0.4, 1000, 750, 100, 30), # 21378 "lin318": (0.9, 8, 0.4, 1000, 500, 32, 30), # 43171 @@ -20,22 +20,17 @@ def ant_colony_opt(instance): "pr439": (0.9, 8, 0.4, 1000, 450, 24, 2), # 11734 "rat783": (0.9, 8, 0.4, 1000, 450, 24, 2), # 9232 "u1060": (0.9, 8, 0.4, 1000, 350, 6, 3), # 238025 - "fl1577": (0.9, 8, 0.4, 1000, 50, 9, 3), # 24145 + #"fl1577": (0.9, 8, 0.4, 1000, 50, 9, 3), # 24145 + "fl1577": (0.9, 8, 0.4, 1000, 50, 10, 5), # 24145 } - # params_multicore = { - # "d198": (0.9, 8, 0.4, 1000, 750, 200, 30), # 15970 - # "ch130": (0.9, 8, 0.4, 1000, 1750, 200, 30), # 6173 - # } alpha, beta, evap, weight, ants, loops, optruns = params[instance.name] # Call C++ program cmd = dir + "aco " + str(instance.nPoints) + " " + str(loops) + " " + str(ants) + \ - " " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) + " " + str(optruns) + \ - " < " + fname + " &2>/dev/null" + " " + str(alpha) + " " + str(beta) + " " + str(evap) + " " + str(weight) + \ + " " + str(optruns) + " < " + fname + " &2>/dev/null" print(cmd) solution = eval(os.popen(cmd).read()) solution.append(solution[0]) - print(solution) - print(len(solution)) return solution diff --git a/src/io_tsp.py b/aco/io_tsp.py similarity index 92% rename from src/io_tsp.py rename to aco/io_tsp.py index f3edaea..0ad6596 100644 --- a/src/io_tsp.py +++ b/aco/io_tsp.py @@ -1,11 +1,8 @@ import numpy as np from matplotlib import pyplot as plt - -from src.utils import distance_euc - +from aco.utils import distance_euc class ProblemInstance: - def __init__(self, name_tsp): self.exist_opt = False self.optimal_tour = None @@ -32,7 +29,9 @@ class ProblemInstance: self.points[i, 2] = line_i[2] self.create_dist_matrix() - if self.file_name in ["./problems/eil76.tsp", "./problems/kroA100.tsp", "../problems/eil76.tsp", + if self.file_name in ["./problems/eil76.tsp", + "./problems/kroA100.tsp", + "../problems/eil76.tsp", "../problems/kroA100.tsp"]: self.exist_opt = True file_object = open(self.file_name.replace(".tsp", ".opt.tour")) diff --git a/aco/utils.py b/aco/utils.py new file mode 100644 index 0000000..ef2a9db --- /dev/null +++ b/aco/utils.py @@ -0,0 +1,8 @@ +import numpy as np + +def distance_euc(point_i, point_j): + rounding = 0 + x_i, y_i = point_i[0], point_i[1] + x_j, y_j = point_j[0], point_j[1] + distance = np.sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2) + return round(distance, rounding) diff --git a/aco_gambardella.txt b/aco_gambardella.txt deleted file mode 100644 index 5206464..0000000 --- a/aco_gambardella.txt +++ /dev/null @@ -1,6 +0,0 @@ -ants = 10 -alpha = 0.1 -rho = 0.1 -q0 = 0.95 = 1 - (15/nCities) -beta = b1 = 1 - diff --git a/execute.md b/execute.md new file mode 100644 index 0000000..d8c1603 --- /dev/null +++ b/execute.md @@ -0,0 +1,35 @@ +# Instructions for execution + +## Requirements +In order to reproduce the results reported in the Excel table, +one must have the following requirements: + +- A Python 3 interpreter and all Python dependencies required for the `AI2020BsC` repository installed, namely +`time`, `matplotlib` and `numpy`; +- A working C++11 or greater compiler accessible from the `c++` command (Modern versions of clang and g++ are both +acceptable compilers). + +## Execution +`cd` in the `code` directory and execute `python3 ./run.py` from the terminal. +The script will automatically compile the C++ companion program and start the TSP computation for each problem. + +Results will be saved in `.sol` files where `` is respectively the name +of the problem solved. + +Execution time for each problem will be computed using the same criteria used for the original `AI2020BsC` repository. +Times and lengths found will be saved in a file named `results.csv` which will be located in the same directory as +`run.py`. + +## `.sol` file contents +Solution files are made up of just one line, containing a valid Python expression representing an array. +The array in question contains the order in which cities must be visited, representing each city by its number. +The array starts and ends with the starting city of the tour. + +Here is the Backus Naur grammar for `.sol` files: +``` +<.sol file> ::= '\n' + ::= '[' ", " ']' + ::= ", " | ", " + ::= unsigned integer + ::= unsigned integer +``` diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e9dff3e --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +# 2020 AI Cup submission - Claudio Maggioni +For instructions on how to compile and run my submission in order to reproduce +result please read file `../execute.md`. \ No newline at end of file diff --git a/run.py b/run.py index 800d98a..e6ba8bd 100644 --- a/run.py +++ b/run.py @@ -1,38 +1,16 @@ import glob import pandas as pd -from src.io_tsp import ProblemInstance -from src.TSP_solver import TSPSolver, available_improvers, available_solvers -import numpy as np - - -def use_solver_to_compute_solution(solver, improve, index, results, name, verbose, show_plots): - #solver.bind(improve) - # solver.bind("2-opt") - # solver.bind("2.5-opt") - solver.compute_solution(return_value=False, verbose=verbose) - # solver.pop() - # solver.pop() - - if verbose: - print(f"the total length for the solution found is {solver.found_length}", - f"while the optimal length is {solver.problem_instance.best_sol}", - f"the gap is {solver.gap}%", - f"the solution is found in {solver.duration} seconds", sep="\n") - - index.append((name, solver.name_method)) - results.append([solver.found_length, solver.problem_instance.best_sol, solver.gap, solver.duration]) - - if show_plots: - solver.plot_solution() - +from aco.io_tsp import ProblemInstance +from aco.TSP_solver import TSPSolver +import os def run(show_plots=False, verbose=False): + os.system("rm -f " + " ".join(glob.glob("sol/*") + glob.glob("c_prob/*"))) + os.system("c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc") problems = glob.glob('./problems/*.tsp') - # problems = ["./problems/fl1577.tsp"] + problems = ["./problems/fl1577.tsp"] - solvers_names = available_solvers.keys() - improvers_names = available_improvers.keys() results = [] index = [] for problem_path in problems: @@ -42,35 +20,30 @@ def run(show_plots=False, verbose=False): if show_plots: prob_instance.plot_data() + solver = TSPSolver(prob_instance) + solution = solver.compute_solution(verbose=verbose) + if verbose: + print(f"the total length for the solution found is {solver.found_length}", + f"while the optimal length is {solver.problem_instance.best_sol}", + f"the gap is {solver.gap}%", + f"the solution is found in {solver.duration} seconds", sep="\n") + index.append((problem_path, "'C++ ant colony optimization'")) + results.append([solver.found_length, + solver.problem_instance.best_sol, + solver.gap, + solver.duration]) - for solver_name in solvers_names: - solver = TSPSolver(solver_name, prob_instance) - use_solver_to_compute_solution(solver, None, index, results, problem_path, verbose, show_plots) - # for improve in improvers_names: - # solver = TSPSolver(solver_name, prob_instance) - # use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots) - # for improve2 in [j for j in improvers_names if j not in [improve]]: - # use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots) - # - # for improve3 in [j for j in improvers_names if j not in [improve, improve2]]: - # use_solver_to_compute_solution(solver, improve3, index, results, problem_path, verbose, - # show_plots) - # solver.pop() - # - # solver.pop() + with open("sol/" + prob_instance.name + ".sol", "w") as f: + print(solution, file=f) - if prob_instance.exist_opt and show_plots: - solver = TSPSolver("optimal", prob_instance) - solver.solved = True - solver.solution = np.concatenate([prob_instance.optimal_tour, [prob_instance.optimal_tour[0]]]) + if show_plots: solver.plot_solution() - #index = pd.MultiIndex.from_tuples(index, names=['problem', 'method']) - - #return None - return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"]) + return pd.DataFrame(results, index=index, + columns=["tour length", "optimal solution", + "gap", "time to solve"]) if __name__ == '__main__': - df = run(show_plots=True, verbose=True) + df = run(show_plots=False, verbose=True) df.to_csv("./results.csv") diff --git a/sol/fl1577.sol b/sol/fl1577.sol new file mode 100644 index 0000000..6cca098 --- /dev/null +++ b/sol/fl1577.sol @@ -0,0 +1 @@ +[150, 151, 141, 140, 139, 138, 137, 136, 135, 225, 224, 223, 222, 221, 220, 219, 218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 152, 201, 153, 200, 154, 199, 155, 198, 156, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185, 184, 183, 182, 181, 180, 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 310, 309, 308, 307, 306, 305, 304, 303, 302, 301, 300, 299, 298, 297, 296, 295, 294, 293, 292, 291, 290, 289, 288, 287, 286, 285, 284, 283, 282, 281, 280, 279, 278, 277, 276, 275, 274, 273, 272, 271, 270, 269, 268, 267, 266, 265, 264, 263, 262, 261, 260, 259, 258, 257, 256, 255, 254, 253, 252, 251, 250, 249, 248, 247, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 874, 873, 872, 871, 870, 869, 868, 867, 866, 865, 864, 863, 862, 861, 860, 859, 858, 857, 856, 855, 854, 853, 852, 851, 850, 849, 848, 847, 846, 845, 844, 843, 842, 841, 840, 839, 838, 837, 836, 835, 834, 833, 832, 831, 830, 829, 828, 827, 826, 825, 824, 823, 822, 821, 820, 819, 818, 817, 816, 815, 814, 813, 812, 811, 810, 809, 959, 958, 960, 957, 961, 956, 962, 955, 963, 954, 964, 965, 991, 992, 1014, 1013, 1012, 990, 966, 953, 952, 967, 989, 1011, 1048, 1010, 1063, 1069, 1062, 993, 969, 968, 951, 950, 949, 970, 988, 994, 1009, 1008, 995, 971, 987, 996, 1007, 1006, 985, 986, 972, 948, 973, 947, 974, 946, 975, 997, 1049, 1050, 1051, 1052, 1060, 1077, 1078, 1079, 1075, 1074, 1090, 1073, 1072, 1087, 1086, 1071, 1061, 1070, 1080, 1081, 1082, 1091, 1092, 1093, 1085, 1083, 1068, 1084, 1094, 1095, 1096, 1064, 1065, 1066, 1067, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1201, 1202, 1203, 1204, 1404, 1405, 1452, 1495, 1453, 1406, 1454, 1407, 1408, 1455, 1498, 1456, 1409, 1457, 1410, 1411, 1459, 1412, 1460, 1413, 1414, 1461, 1504, 1462, 1426, 1425, 1471, 1470, 1424, 1423, 1469, 1422, 1468, 1421, 1467, 1420, 1466, 1419, 1465, 1418, 1417, 1416, 1464, 1415, 1463, 1547, 1505, 1548, 1549, 1506, 1550, 1507, 1508, 1551, 1552, 1553, 1554, 1509, 1555, 1510, 1556, 1557, 1511, 1558, 1512, 1559, 1513, 1560, 1561, 1546, 1545, 1503, 1544, 1502, 1543, 1542, 1501, 1458, 1500, 1541, 1499, 1540, 1539, 1497, 1538, 1496, 1537, 1536, 1494, 1535, 1493, 1451, 1403, 1562, 1563, 1514, 1472, 1427, 1473, 1428, 1429, 1430, 1515, 1564, 1565, 1516, 1474, 1517, 1566, 1475, 1431, 1432, 1476, 1567, 1568, 1477, 1433, 1434, 1478, 1435, 1518, 1569, 1570, 1519, 1436, 1571, 1572, 1479, 1480, 1520, 1573, 1521, 1574, 1575, 1522, 1437, 1438, 1576, 1534, 1450, 1402, 1401, 1449, 1492, 1533, 1491, 1448, 1400, 1447, 1490, 1532, 1531, 1489, 1399, 1446, 1530, 1488, 1529, 1487, 1528, 1486, 1527, 1485, 1526, 1525, 1483, 1524, 1482, 1523, 1481, 1391, 1439, 1392, 1440, 1393, 1394, 1441, 1484, 1442, 1395, 1443, 1396, 1444, 1397, 1445, 1398, 1200, 1199, 1198, 1197, 1196, 1195, 1194, 1193, 1192, 1191, 1190, 1189, 1188, 1187, 1186, 1185, 1184, 1183, 1182, 1181, 1180, 1179, 1178, 1177, 1176, 1175, 1174, 1173, 1172, 1171, 1170, 1169, 1168, 1167, 1166, 1165, 1164, 1163, 1162, 1161, 1160, 1159, 1158, 1157, 1156, 1155, 1154, 1153, 1152, 1151, 1150, 1149, 1148, 1147, 1146, 1145, 1144, 1143, 1142, 1141, 1140, 1139, 1099, 1098, 1097, 1100, 1101, 1088, 1089, 1076, 1055, 1004, 1056, 1003, 1057, 1058, 1059, 936, 937, 938, 939, 980, 981, 979, 940, 978, 982, 941, 977, 942, 976, 983, 1002, 1005, 1054, 1053, 1001, 984, 1000, 999, 998, 943, 944, 945, 935, 934, 928, 933, 929, 930, 931, 932, 879, 878, 877, 876, 875, 808, 807, 745, 806, 746, 805, 747, 804, 748, 803, 880, 881, 802, 749, 750, 801, 882, 751, 752, 800, 883, 799, 753, 754, 798, 884, 885, 886, 755, 756, 887, 888, 889, 757, 758, 759, 890, 891, 892, 760, 761, 762, 893, 894, 763, 764, 895, 896, 897, 765, 766, 767, 898, 899, 768, 769, 900, 901, 770, 771, 902, 903, 772, 773, 904, 905, 774, 775, 906, 907, 776, 777, 908, 909, 778, 779, 780, 910, 911, 912, 781, 782, 913, 914, 783, 784, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 797, 796, 795, 794, 793, 792, 791, 790, 789, 788, 787, 786, 785, 727, 687, 726, 688, 725, 689, 724, 690, 723, 691, 722, 692, 721, 693, 720, 694, 719, 695, 718, 696, 717, 697, 716, 698, 715, 714, 699, 615, 608, 614, 613, 700, 713, 701, 712, 702, 711, 710, 703, 612, 611, 610, 609, 709, 708, 707, 706, 705, 704, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 607, 616, 606, 617, 605, 618, 604, 619, 603, 620, 602, 621, 601, 622, 600, 623, 599, 624, 598, 625, 597, 626, 596, 627, 595, 594, 628, 686, 728, 685, 729, 684, 730, 683, 731, 682, 732, 681, 733, 680, 734, 679, 735, 678, 736, 677, 737, 676, 738, 675, 739, 674, 740, 673, 741, 672, 742, 671, 743, 670, 744, 669, 668, 667, 666, 665, 664, 663, 662, 661, 660, 659, 658, 657, 656, 567, 655, 568, 654, 569, 653, 570, 652, 571, 651, 572, 650, 573, 649, 574, 648, 575, 647, 576, 646, 577, 645, 578, 644, 579, 643, 580, 642, 581, 641, 582, 640, 583, 639, 584, 638, 585, 637, 586, 636, 587, 635, 588, 634, 589, 633, 590, 632, 591, 631, 592, 630, 629, 593, 527, 526, 525, 524, 523, 522, 521, 520, 519, 518, 517, 516, 499, 500, 501, 470, 471, 440, 472, 439, 473, 438, 437, 474, 498, 497, 475, 436, 435, 476, 496, 495, 494, 477, 434, 478, 433, 432, 479, 493, 492, 491, 480, 431, 481, 430, 482, 429, 483, 490, 502, 489, 503, 504, 505, 484, 485, 486, 487, 488, 315, 314, 311, 312, 313, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 428, 427, 426, 425, 424, 423, 422, 421, 420, 419, 418, 417, 416, 415, 414, 413, 412, 411, 410, 409, 408, 407, 406, 405, 404, 403, 402, 401, 400, 399, 398, 397, 396, 395, 394, 393, 392, 391, 390, 389, 388, 387, 386, 385, 384, 447, 446, 445, 444, 443, 442, 441, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 566, 565, 564, 563, 562, 561, 560, 559, 558, 557, 556, 383, 382, 381, 380, 379, 378, 377, 376, 375, 374, 373, 372, 371, 370, 369, 368, 367, 366, 365, 364, 363, 362, 361, 360, 359, 358, 357, 356, 355, 354, 353, 352, 351, 350, 349, 348, 347, 346, 345, 344, 343, 342, 341, 340, 339, 338, 337, 336, 157, 158, 159, 160, 161, 148, 149, 147, 146, 145, 144, 143, 142, 134, 1380, 1305, 1381, 1304, 1382, 1303, 1383, 1384, 1302, 1288, 1301, 1385, 1386, 1299, 1387, 1298, 1388, 1297, 1389, 1296, 1390, 1295, 1294, 1205, 1206, 1293, 1207, 1292, 1208, 1291, 1209, 1290, 1300, 1289, 1210, 1211, 1212, 1287, 1213, 1286, 1214, 1285, 1215, 1284, 1216, 1283, 1306, 1379, 1378, 1377, 1217, 1218, 1307, 1376, 1375, 1308, 1282, 1309, 1281, 1374, 1373, 1219, 1310, 1372, 1371, 1311, 1220, 1280, 1221, 1222, 1279, 1370, 1369, 1278, 1223, 1224, 1277, 1368, 1312, 1276, 1313, 1367, 1366, 1314, 1225, 1226, 1227, 1275, 1228, 1274, 1315, 1365, 1364, 9, 8, 19, 20, 57, 56, 55, 18, 17, 7, 15, 16, 40, 41, 42, 39, 38, 43, 74, 78, 77, 75, 76, 95, 96, 97, 109, 110, 108, 111, 107, 112, 106, 113, 105, 114, 133, 132, 131, 130, 129, 128, 115, 127, 116, 126, 117, 125, 118, 119, 120, 121, 61, 50, 60, 51, 46, 59, 58, 84, 83, 69, 82, 70, 71, 52, 72, 53, 54, 73, 80, 92, 91, 81, 100, 99, 98, 93, 94, 79, 44, 37, 45, 36, 10, 6, 5, 4, 21, 22, 23, 3, 2, 1, 0, 24, 25, 14, 13, 47, 48, 35, 49, 34, 12, 11, 33, 26, 32, 27, 31, 28, 29, 30, 64, 65, 66, 67, 68, 86, 85, 122, 123, 124, 101, 102, 87, 88, 89, 103, 104, 90, 63, 62, 1363, 1316, 1273, 1229, 1230, 1272, 1317, 1362, 1318, 1271, 1231, 1232, 1319, 1361, 1360, 1320, 1270, 1233, 1269, 1234, 1235, 1267, 1236, 1266, 1237, 1265, 1238, 1264, 1239, 1263, 1240, 1262, 1241, 1242, 1261, 1260, 1243, 1244, 1259, 1245, 1258, 1246, 1257, 1247, 1256, 1248, 1255, 1249, 1250, 1251, 1254, 1252, 1253, 1336, 1337, 1338, 1339, 1335, 1340, 1334, 1333, 1341, 1342, 1343, 1344, 1332, 1345, 1331, 1346, 1347, 1330, 1348, 1329, 1349, 1328, 1350, 1351, 1352, 1327, 1353, 1326, 1354, 1325, 1355, 1324, 1356, 1323, 1322, 1268, 1321, 1359, 1358, 1357, 150] diff --git a/src/TSP_solver.py b/src/TSP_solver.py deleted file mode 100644 index 0e5b13f..0000000 --- a/src/TSP_solver.py +++ /dev/null @@ -1,126 +0,0 @@ -from time import time as t -import numpy as np -import matplotlib.pyplot as plt - -from src.two_opt import loop2opt -from src.two_dot_five_opt import loop2dot5opt -from src.simulated_annealing import sa -from src.constructive_algorithms import random_method, nearest_neighbor, best_nearest_neighbor, multi_fragment_mf -from src.ant_colony import ant_colony_opt - -# available_solvers = {"random": random_method, -# "nearest_neighbors": nearest_neighbor, -# "best_nn": best_nearest_neighbor, -# "multi_fragment": multi_fragment_mf -# } -available_improvers = {"2-opt": loop2opt, - "2.5-opt": loop2dot5opt, - "simulated_annealing": sa} - -# available_solvers = {} -# for i in range(1, 10): -# for j in range(1, 10): -# available_solvers["aco_" + str(i) + "_" + str(j)] = ant_colony_opt(i/10, j) - -available_solvers = {"aco": ant_colony_opt} - -class TSPSolver: - def __init__(self, algorithm_name, problem_instance, passed_avail_solvers=None, passed_avail_improvers=None): - # assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently." - if passed_avail_improvers is None: - passed_avail_improvers = available_improvers - if passed_avail_solvers is None: - passed_avail_solvers = available_solvers - self.available_improvers = passed_avail_improvers - self.available_solvers = passed_avail_solvers - self.duration = np.inf - self.found_length = np.inf - self.algorithm_name = algorithm_name - self.algorithms = [algorithm_name] - self.name_method = "initialized with " + algorithm_name - self.solved = False - self.problem_instance = problem_instance - self.solution = None - - def bind(self, local_or_meta): - assert local_or_meta in self.available_improvers, f"the {local_or_meta} method is not available currently." - self.algorithms.append(local_or_meta) - self.name_method += ", improved with " + local_or_meta - - def pop(self): - self.algorithms.pop() - self.name_method = self.name_method[::-1][self.name_method[::-1].find("improved"[::-1]) + len("improved") + 2:][ - ::-1] - - def compute_solution(self, verbose=True, return_value=True): - self.solved = False - if verbose: - print(f"### solving with {self.algorithms} ####") - start_time = t() - self.solution = self.available_solvers[self.algorithms[0]](self.problem_instance) - if not self.check_if_solution_is_valid(): - print(f"Error the solution of {self.algorithm_name} for problem {self.problem_instance.name} is not valid") - if return_value: - return False - for i in range(1, len(self.algorithms)): - improver = self.algorithms[i] - self.solution = self.available_improvers[improver](self.solution, self.problem_instance) - if not self.check_if_solution_is_valid(): - print( - f"Error the solution of {self.algorithm_name} with {improver} for problem {self.problem_instance.name} is not valid") - if return_value: - return False - - end_time = t() - self.duration = np.around(end_time - start_time, 3) - self.solved = True - self.evaluate_solution() - self._gap() - if return_value: - return self.solution - - def plot_solution(self): - assert self.solved, "You can't plot the solution, you need to compute it first!" - plt.figure(figsize=(8, 8)) - self._gap() - plt.title(f"{self.problem_instance.name} solved with {self.name_method} solver, gap {self.gap}") - ordered_points = self.problem_instance.points[self.solution] - plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-') - plt.show() - - def check_if_solution_is_valid(self): - rights_values = np.sum( - [self.check_validation(i, self.solution[:-1]) for i in np.arange(self.problem_instance.nPoints)]) - # rights_values = np.sum( - # [1 if np.sum(self.solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)]) - return rights_values == self.problem_instance.nPoints - - def check_validation(self, node, solution): - if np.sum(solution == node) == 1: - return 1 - else: - return 0 - - def evaluate_solution(self, return_value=False): - total_length = 0 - starting_node = self.solution[0] - from_node = starting_node - for node in self.solution[1:]: - total_length += self.problem_instance.dist_matrix[from_node, node] - from_node = node - - self.found_length = total_length - if return_value: - return total_length - - def pass_and_check_if_solution_is_valid(self, solution): - rights_values = np.sum( - [self.check_validation(i, solution[:-1]) for i in np.arange(self.problem_instance.nPoints)]) - # rights_values = np.sum( - # [1 if np.sum(solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)]) - return rights_values == self.problem_instance.nPoints - - def _gap(self): - self.evaluate_solution(return_value=False) - self.gap = np.round( - ((self.found_length - self.problem_instance.best_sol) / self.problem_instance.best_sol) * 100, 2) diff --git a/src/constructive_algorithms.py b/src/constructive_algorithms.py deleted file mode 100644 index 529e39f..0000000 --- a/src/constructive_algorithms.py +++ /dev/null @@ -1,129 +0,0 @@ -import numpy as np - -from src.utils import compute_length - -def random_method(instance_): - n = int(instance_.nPoints) - solution = np.random.choice(np.arange(n), size=n, replace=False) - return np.concatenate([solution, [solution[0]]]) - -def nearest_neighbor(instance_, starting_node=0): - dist_matrix = np.copy(instance_.dist_matrix) - n = int(instance_.nPoints) - node = starting_node - tour = [node] - for _ in range(n - 1): - for new_node in np.argsort(dist_matrix[node]): - if new_node not in tour: - tour.append(new_node) - node = new_node - break - tour.append(starting_node) - return np.array(tour) - - -def best_nearest_neighbor(instance_): - solutions, lengths = [], [] - for start in range(instance_.nPoints): - new_solution = nearest_neighbor(instance_, starting_node=start) - solutions.append(new_solution) - lengths.append(compute_length(new_solution, instance_.dist_matrix)) - - if lengths is []: - return None - else: - solution = solutions[np.argmin(lengths)] - return solution - - -def multi_fragment_check_if_available(n1, n2, sol): - if len(sol[str(n1)]) < 2 and len(sol[str(n2)]) < 2: - return True - else: - return False - - -def multi_fragment_check_if_not_close(edge_to_append, sol): - n1, n2 = edge_to_append - from_city = n2 - if len(sol[str(from_city)]) == 0: - return True - partial_tour = [from_city] - end = False - iteration = 0 - while not end: - if len(sol[str(from_city)]) == 1: - if from_city == n1: - return_value = False - end = True - elif iteration > 1: - # print(f"iterazione {iteration}, elementi dentro partial {len(partial_tour)}", - # f"from city {from_city}") - return_value = True - end = True - else: - from_city = sol[str(from_city)][0] - partial_tour.append(from_city) - iteration += 1 - else: - # print(from_city, partial_tour, sol[str(from_city)]) - for node_connected in sol[str(from_city)]: - # print(node_connected) - if node_connected not in partial_tour: - from_city = node_connected - partial_tour.append(node_connected) - # print(node_connected, sol[str(from_city)]) - iteration += 1 - return return_value - - -def multi_fragment_create_solution(start_sol, sol, n): - assert len(start_sol) == 2, "too many cities with just one link" - end = False - n1, n2 = start_sol - from_city = n2 - sol_list = [n1, n2] - iteration = 0 - while not end: - for node_connected in sol[str(from_city)]: - iteration += 1 - if node_connected not in sol_list: - from_city = node_connected - sol_list.append(node_connected) - # print(f"prossimo {node_connected}", - # f"possibili {sol[str(from_city)]}", - # f"ultim tour {sol_list[-5:]}") - if iteration > 300: - if len(sol_list) == n: - end = True - sol_list.append(n1) - return sol_list - - -def multi_fragment_mf(instance): - mat = np.copy(instance.dist_matrix) - mat = np.triu(mat) - mat[mat == 0] = 100000 - solution = {str(i): [] for i in range(instance.nPoints)} - start_list = [i for i in range(instance.nPoints)] - inside = 0 - for el in np.argsort(mat.flatten()): - node1, node2 = el // instance.nPoints, el % instance.nPoints - possible_edge = [node1, node2] - if multi_fragment_check_if_available(node1, node2, - solution): - if multi_fragment_check_if_not_close(possible_edge, solution): - # print("entrato", inside) - solution[str(node1)].append(node2) - solution[str(node2)].append(node1) - if len(solution[str(node1)]) == 2: - start_list.remove(node1) - if len(solution[str(node2)]) == 2: - start_list.remove(node2) - inside += 1 - # print(node1, node2, inside) - if inside == instance.nPoints - 1: - # print(f"ricostruire la solutione da {start_list}", - # f"vicini di questi due nodi {[solution[str(i)] for i in start_list]}") - solution = multi_fragment_create_solution(start_list, solution, instance.nPoints) - return solution diff --git a/src/iterated_local_search.py b/src/iterated_local_search.py deleted file mode 100644 index 9dbb7d9..0000000 --- a/src/iterated_local_search.py +++ /dev/null @@ -1,4 +0,0 @@ -class IteratedLocalSearch: - - def __call__(self): - pass diff --git a/src/simulated_annealing.py b/src/simulated_annealing.py deleted file mode 100644 index 134ab86..0000000 --- a/src/simulated_annealing.py +++ /dev/null @@ -1,52 +0,0 @@ -import numpy as np - -from src.utils import compute_length - - -def sa(solution, instance, constant_temperature=0.95, iterations_for_each_temp=100): - # initial setup - temperature = instance.best_sol / np.sqrt(instance.nPoints) - current_sol = np.array(solution) - current_len = compute_length(solution, instance.dist_matrix) - best_sol = np.array(solution) - best_len = current_len - - # main loop - while temperature > 0.001: - for it in range(iterations_for_each_temp): - next_sol, delta_E = random_sol_from_neigh(current_sol, instance) - if delta_E < 0: - current_sol = next_sol - current_len += delta_E - if current_len < best_len: - best_sol = current_sol - best_len = current_len - else: - r = np.random.uniform(0, 1) - if r < np.exp(- delta_E / temperature): - current_sol = next_sol - current_len += delta_E - - temperature *= constant_temperature - - return best_sol.tolist() - - -def random_sol_from_neigh(solution, instance): - i, j = np.random.choice(np.arange(1, len(solution) - 1), 2, replace=False) - i, j = np.sort([i, j]) - return sa_swap2opt(solution, i, j), gain(i, j, solution, instance.dist_matrix) - - -def sa_swap2opt(tsp_sequence, i, j): - new_tsp_sequence = np.copy(tsp_sequence) - new_tsp_sequence[i:j + 1] = np.flip(tsp_sequence[i:j + 1], axis=0) # flip or swap ? - return new_tsp_sequence - - -def gain(i, j, tsp_sequence, matrix_dist): - old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[j], tsp_sequence[j + 1]]) - changed_links_len = (matrix_dist[tsp_sequence[j], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[i], tsp_sequence[j + 1]]) - return - old_link_len + changed_links_len diff --git a/src/two_dot_five_opt.py b/src/two_dot_five_opt.py deleted file mode 100644 index c454525..0000000 --- a/src/two_dot_five_opt.py +++ /dev/null @@ -1,80 +0,0 @@ -import numpy as np - -from src.utils import compute_length -from src.two_opt import swap2opt, gain - - -def step2dot5opt(solution, matrix_dist, distance): - seq_length = len(solution) - 2 - tsp_sequence = np.array(solution) - uncrosses = 0 - for i in range(1, seq_length - 1): - for j in range(i + 1, seq_length): - # 2opt swap - two_opt_tsp_sequence = swap2opt(tsp_sequence, i, j) - two_opt_len = distance + gain(i, j, tsp_sequence, matrix_dist) - # node shift 1 - first_shift_tsp_sequence = shift1(tsp_sequence, i, j) - first_shift_len = distance + shift_gain1(i, j, tsp_sequence, matrix_dist) - # node shift 2 - second_shift_tsp_sequence = shift2(tsp_sequence, i, j) - second_shift_len = distance + shift_gain2(i, j, tsp_sequence, matrix_dist) - - best_len, best_method = min([two_opt_len, first_shift_len, second_shift_len]), np.argmin( - [two_opt_len, first_shift_len, second_shift_len]) - sequences = [two_opt_tsp_sequence, first_shift_tsp_sequence, second_shift_tsp_sequence] - if best_len < distance: - uncrosses += 1 - tsp_sequence = sequences[best_method] - distance = best_len - print(i, j, best_len) - #print(distance, best_method, [twoOpt_len, first_shift_len, second_shift_len]) - return tsp_sequence, distance, uncrosses - - -def shift1(tsp_sequence, i, j): - new_tsp_sequence = np.concatenate( - [tsp_sequence[:i], tsp_sequence[i + 1: j + 1], [tsp_sequence[i]], tsp_sequence[j + 1:]]) - return new_tsp_sequence - - -def shift_gain1(i, j, tsp_sequence, matrix_dist): - old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + - matrix_dist[tsp_sequence[i], tsp_sequence[i + 1]] + - matrix_dist[tsp_sequence[j], tsp_sequence[j + 1]]) - changed_links_len = (matrix_dist[tsp_sequence[i - 1], tsp_sequence[i + 1]] + - matrix_dist[tsp_sequence[i], tsp_sequence[j]] - + matrix_dist[tsp_sequence[i], tsp_sequence[j + 1]]) - return - old_link_len + changed_links_len - - -def shift2(tsp_sequence, i, j): - new_tsp_sequence = np.concatenate( - [tsp_sequence[:i], [tsp_sequence[j]], tsp_sequence[i: j], tsp_sequence[j + 1:]]) - return new_tsp_sequence - - -def shift_gain2(i, j, tsp_sequence, matrix_dist): - old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[j], tsp_sequence[j - 1]] + matrix_dist[tsp_sequence[j], tsp_sequence[j + 1]]) - changed_links_len = ( - matrix_dist[tsp_sequence[j], tsp_sequence[i - 1]] + matrix_dist[tsp_sequence[i], tsp_sequence[j]] + - matrix_dist[tsp_sequence[j - 1], tsp_sequence[j + 1]]) - return - old_link_len + changed_links_len - - -def loop2dot5opt(solution, instance, max_num_of_changes=2500): - matrix_dist = instance.dist_matrix - actual_len = compute_length(solution, matrix_dist) - new_tsp_sequence = np.copy(np.array(solution)) - uncross = 0 - while uncross < max_num_of_changes: - new_tsp_sequence, new_len, uncr_ = step2dot5opt(new_tsp_sequence, matrix_dist, actual_len) - uncross += uncr_ - # print(new_len, uncross) - if new_len < actual_len: - actual_len = new_len - else: - return new_tsp_sequence.tolist() - - return new_tsp_sequence.tolist() diff --git a/src/two_opt.py b/src/two_opt.py deleted file mode 100644 index 4e6da3f..0000000 --- a/src/two_opt.py +++ /dev/null @@ -1,48 +0,0 @@ -import numpy as np - -from src.utils import compute_length - -def step2opt(solution, matrix_dist, distance): - seq_length = len(solution) - 1 - tsp_sequence = np.array(solution) - uncrosses = 0 - for i in range(1, seq_length - 1): - for j in range(i + 1, seq_length): - new_tsp_sequence = swap2opt(tsp_sequence, i, j) - new_distance = distance + gain(i, j, tsp_sequence, matrix_dist) - if new_distance < distance: - uncrosses += 1 - tsp_sequence = np.copy(new_tsp_sequence) - distance = new_distance - return tsp_sequence, distance, uncrosses - - -def swap2opt(tsp_sequence, i, j): - new_tsp_sequence = np.copy(tsp_sequence) - new_tsp_sequence[i:j + 1] = np.flip(tsp_sequence[i:j + 1], axis=0) # flip or swap ? - return new_tsp_sequence - - -def gain(i, j, tsp_sequence, matrix_dist): - old_link_len = (matrix_dist[tsp_sequence[i], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[j], tsp_sequence[j + 1]]) - changed_links_len = (matrix_dist[tsp_sequence[j], tsp_sequence[i - 1]] + matrix_dist[ - tsp_sequence[i], tsp_sequence[j + 1]]) - return - old_link_len + changed_links_len - - -def loop2opt(solution, instance, max_num_of_uncrosses=10000): - matrix_dist = instance.dist_matrix - new_len = compute_length(solution, matrix_dist) - new_tsp_sequence = np.copy(np.array(solution)) - uncross = 0 - while uncross < max_num_of_uncrosses: - new_tsp_sequence, new_reward, uncr_ = step2opt(new_tsp_sequence, matrix_dist, new_len) - uncross += uncr_ - if new_reward < new_len: - new_len = new_reward - else: - return new_tsp_sequence.tolist() - - # return new_tsp_sequence.tolist(), new_len, uncross - return new_tsp_sequence.tolist() diff --git a/src/utils.py b/src/utils.py deleted file mode 100644 index a96e8d9..0000000 --- a/src/utils.py +++ /dev/null @@ -1,19 +0,0 @@ -import numpy as np - - -def compute_length(solution, dist_matrix): - total_length = 0 - starting_node = solution[0] - from_node = starting_node - for node in solution[1:]: - total_length += dist_matrix[from_node, node] - from_node = node - return total_length - - -def distance_euc(point_i, point_j): - rounding = 0 - x_i, y_i = point_i[0], point_i[1] - x_j, y_j = point_j[0], point_j[1] - distance = np.sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2) - return round(distance, rounding) diff --git a/sysinfo.md b/sysinfo.md new file mode 100644 index 0000000..03995b0 --- /dev/null +++ b/sysinfo.md @@ -0,0 +1,12 @@ +# System information + +Here is a summary of the system characteristics of the machine used to measure the execution time and +execution results for my AI cup submission: + +| Characteristic | Value | +| :------------- | :----------: | +| OS Version | MacOS Catalina 10.15.7 | +| Kernel | Mach 19.6.0 | +| Compiler used | Apple clang version 12.0.0 (clang-1200.0.32.21) | +| Compiler optimization level | `-O2` | +| CPU | Intel i7-8750H (12) @ 2.20GHz | \ No newline at end of file