Changeset 7775 for branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanImprovementOperator.cs
- Timestamp:
- 05/02/12 19:09:48 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanImprovementOperator.cs
r7756 r7775 138 138 139 139 for (int i = 0; i < ImprovementAttempts.Value; i++) { 140 int a = Random.Next(currSol.Length - 1);141 int b = Random.Next(currSol.Length - 1);140 int a = Random.Next(currSol.Length); 141 int b = Random.Next(currSol.Length); 142 142 Invert(currSol, a, b); 143 143 currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol); 144 144 if (currLength < bestLength) { 145 145 bestLength = currLength; 146 bestSol = (Permutation)currSol.Clone();146 bestSol = currSol.Clone() as Permutation; 147 147 } 148 148 Invert(currSol, a, b); … … 155 155 156 156 private void Invert(Permutation sol, int i, int j) { 157 if (i != j) { 158 for (int a = 0; a < Math.Abs(i - j) / 2; a++) { 159 int tmp = sol[(i + a) % sol.Length]; 160 sol[(i + a) % sol.Length] = sol[(j - a + sol.Length) % sol.Length]; 161 sol[(j - a + sol.Length) % sol.Length] = tmp; 162 } 163 } 157 if (i != j) 158 for (int a = 0; a < Math.Abs(i - j) / 2; a++) 159 if (sol[(i + a) % sol.Length] != sol[(j - a + sol.Length) % sol.Length]) { 160 // XOR swap 161 sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; 162 sol[(j - a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length]; 163 sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; 164 } 164 165 } 165 166 }
Note: See TracChangeset
for help on using the changeset viewer.