Changeset 11840
 Timestamp:
 01/29/15 15:35:27 (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs
r11171 r11840 95 95 96 96 public override IOperation Apply() { 97 Permutation currSol= CurrentScope.Variables[SolutionParameter.ActualName].Value as Permutation;98 if ( currSol== null)97 var solution = CurrentScope.Variables[SolutionParameter.ActualName].Value as Permutation; 98 if (solution == null) 99 99 throw new ArgumentException("Cannot improve solution because it has the wrong type."); 100 if ( currSol.PermutationType != PermutationTypes.RelativeUndirected)100 if (solution.PermutationType != PermutationTypes.RelativeUndirected) 101 101 throw new ArgumentException("Cannot improve solution because the permutation type is not supported."); 102 102 103 103 for (int i = 0; i < ImprovementAttempts.Value; i++) { 104 int a = Random.Next(currSol.Length); 105 int b = Random.Next(currSol.Length); 106 double oldFirstEdgeLength = DistanceMatrix[currSol[a], currSol[(a  1 + currSol.Length) % currSol.Length]]; 107 double oldSecondEdgeLength = DistanceMatrix[currSol[b], currSol[(b + 1) % currSol.Length]]; 108 double newFirstEdgeLength = DistanceMatrix[currSol[b], currSol[(a  1 + currSol.Length) % currSol.Length]]; 109 double newSecondEdgeLength = DistanceMatrix[currSol[a], currSol[(b + 1 + currSol.Length) % currSol.Length]]; 110 if (newFirstEdgeLength + newSecondEdgeLength < oldFirstEdgeLength + oldSecondEdgeLength) 111 Invert(currSol, a, b); 104 var move = StochasticInversionSingleMoveGenerator.Apply(solution, Random); 105 double moveQualtiy = TSPInversionMovePathEvaluator.EvaluateByDistanceMatrix(solution, move, DistanceMatrix); 106 if (moveQualtiy < 0) 107 InversionManipulator.Apply(solution, move.Index1, move.Index2); 112 108 } 113 109 … … 116 112 return base.Apply(); 117 113 } 118 119 private void Invert(Permutation sol, int i, int j) {120 if (i != j)121 for (int a = 0; a < Math.Abs(i  j) / 2; a++)122 if (sol[(i + a) % sol.Length] != sol[(j  a + sol.Length) % sol.Length]) {123 // XOR swap124 sol[(i + a) % sol.Length] ^= sol[(j  a + sol.Length) % sol.Length];125 sol[(j  a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length];126 sol[(i + a) % sol.Length] ^= sol[(j  a + sol.Length) % sol.Length];127 }128 }129 114 } 130 115 }
Note: See TracChangeset
for help on using the changeset viewer.