|
| 1 | +# TSP Problem with Genetic Algorithm |
| 2 | +## Data |
| 3 | +The input of this problem files with `tsp` format. I used the `tsplib95` library for weighted test cases (`bayg29`) and a self-written loader for test cases with coordinations. |
| 4 | + |
| 5 | +## Algorithm |
| 6 | +At first, we generate our initial population with random routes(permutations). |
| 7 | + |
| 8 | +The main process is a loop with several sections. At the end of every iteration in this loop, a new generation is born, preferably with better individuals. |
| 9 | + |
| 10 | +Here is each section of this process: |
| 11 | + |
| 12 | +### Selection |
| 13 | +The first thing we do with the previous generation is to select a subset of them as our parents. The primary consideration here is to make a balance between elites and vulgar to prevent overfitting or underfitting. |
| 14 | + |
| 15 | +The algorithm used for this matter is tournament selection. For this, we choose a subset of the population by random, then we sort them based on their fitness and select a number of them as parents. |
| 16 | + |
| 17 | +### Breeding |
| 18 | +With the parents being chosen, it is time for mating them with each other. Two individuals create a child with ordered recombination. |
| 19 | + |
| 20 | +For choosing each pair, we shuffle parents and mate them by random. We repeat this shuffling until the number of our children has reached the population size. |
| 21 | + |
| 22 | +### Mutation |
| 23 | +We have a parameter called the `mutation_rate`, which specifies the probability of mutation. |
| 24 | + |
| 25 | +The mutations in this problem are done by swapping two cities(genes) in a permutation. |
| 26 | + |
| 27 | +### Replacement |
| 28 | +The final step in the loop is to specify the next generation. We have considered a mix of children and parents because we want to keep the best parents for the next generation. |
| 29 | +We replace a fixed number of worst children with the best parents. |
| 30 | + |
| 31 | +## Test |
| 32 | +### bayg29 |
| 33 | +My best distances were between 1600 to 1800 and here is one of them: |
| 34 | +``` |
| 35 | +Epoch 0 : Population total fitness: 0.021402390435083837 Best fitness: 0.000275178866263071 Least Distance: 3634 |
| 36 | +Epoch 1 : Population total fitness: 0.022400741669908814 Best fitness: 0.00028694404591104734 Least Distance: 3485 |
| 37 | +Epoch 2 : Population total fitness: 0.02321754970711249 Best fitness: 0.000299311583358276 Least Distance: 3341 |
| 38 | +Epoch 3 : Population total fitness: 0.024596784139334586 Best fitness: 0.00030321406913280777 Least Distance: 3298 |
| 39 | +Epoch 4 : Population total fitness: 0.025508500567304724 Best fitness: 0.00031655587211142766 Least Distance: 3159 |
| 40 | +Epoch 5 : Population total fitness: 0.02567080676446863 Best fitness: 0.00031655587211142766 Least Distance: 3159 |
| 41 | +Epoch 6 : Population total fitness: 0.026095669980930384 Best fitness: 0.00031826861871419476 Least Distance: 3142 |
| 42 | +Epoch 7 : Population total fitness: 0.026213325051129905 Best fitness: 0.0003176620076238882 Least Distance: 3148 |
| 43 | +... |
| 44 | +Epoch 92 : Population total fitness: 0.046347892289803405 Best fitness: 0.0005633802816901409 Least Distance: 1775 |
| 45 | +Epoch 93 : Population total fitness: 0.04640791685328948 Best fitness: 0.0005633802816901409 Least Distance: 1775 |
| 46 | +Epoch 94 : Population total fitness: 0.0463885029225474 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 47 | +Epoch 95 : Population total fitness: 0.04655714750065836 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 48 | +Epoch 96 : Population total fitness: 0.04607831403892826 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 49 | +Epoch 97 : Population total fitness: 0.04588737374610269 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 50 | +Epoch 98 : Population total fitness: 0.04635046560907124 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 51 | +Epoch 99 : Population total fitness: 0.0466206756566057 Best fitness: 0.000572737686139748 Least Distance: 1746 |
| 52 | +Best Answer: |
| 53 | +[13, 4, 20, 10, 2, 29, 3, 26, 6, 12, 9, 5, 21, 28, 1, 24, 8, 23, 27, 16, 7, 25, 19, 15, 18, 17, 14, 11, 22] 1746 |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +## Considerations |
| 58 | +- Because we want to optimize our answers based on the total distance of the cycle, our fitness function is `1/total_distance` (so we try to maximize fitness). |
| 59 | + |
0 commit comments