// "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); } }
/// <summary> /// Tato metoda by mela vylepsit reseni ulozene v promenne "current". Kvalitu reseni je mozne ziskat volanim "current.eval()". Je mozne napr. prohodit nektere prvky v permutaci pomoci metody "current.swap(int i, int j)". /// Pokud uz soucasne reseni nelze vylepsit, metoda by mela nastavit promenou "stop" na "true", coz zpusobi, ze prohledavani skonci. /// Pro vylepseni (nebo obecne zmenu) reseni pouzijte nekterou variantu Hill-Climbingu. /// </summary> private void goOneStepII() { 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) { stop = true; } else { current.swap(bestX, bestY); } }