The problem
There are 50 cities. A valid solution is a permutation containing every city exactly once.
[ 0, 4, 5, 1, 3, 2 ]
The objective is to find the route with the smallest total travel distance.
For 50 cities, the number of possible tours is enormous: even fixing the starting city leaves 49! possible orderings.
Why fitness needs care
A shorter distance is better, but selection usually gives a higher probability to a higher fitness score.
One simple transformation is:
fitness = 1 / distance
This demo uses normalized distance and exponential scaling to make differences between strong routes more noticeable.
Crossover example
P1 = [0, 4, 5, 1, 3, 2]
P2 = [4, 2, 0, 3, 5, 1]
copy 3 genes → [0, 4, 5, _, _, _]
fill from P2 → [0, 4, 5, 2, 3, 1]
This keeps every city unique while mixing genetic material from both parents.
Mutation example
With a 5% mutation rate, a route can randomly choose two positions and swap their cities.
Before: [0, 4, 5, 1, 3, 2]
Swap 1 ↔ 4
After: [0, 4, 5, 3, 1, 2]
Mutation helps maintain diversity and can help the population escape a local minimum.