// "Dumb mutation" - just swap some elements private void mutateDumb(TSPSolution initial) { PermutationStandard current = new PermutationStandard(initial); current.swap(rnd.Next(current.size), rnd.Next(current.size)); current.applyToTSPSolution(initial); }
// "Smart mutation" - try to swap and improve the distance private void mutateSmart(TSPSolution initial) { PermutationStandard current = new PermutationStandard(initial); var start = current.eval(); var best = start; var bestX = 0; var bestY = 0; for (int i = 0; i < current.perm.Length; ++i) { for (int j = 0; j < current.perm.Length; ++j) { current.swap(i, j); var temp = current.eval(); current.swap(j, i); if (temp < best) { best = temp; bestX = i; bestY = j; } } } if (best < start) { current.swap(bestX, bestY); current.applyToTSPSolution(initial); } }