Changeset 12031 for branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers
- Timestamp:
- 02/18/15 10:54:32 (10 years ago)
- Location:
- branches/HeuristicLab.DatasetRefactor/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DatasetRefactor/sources
- Property svn:mergeinfo changed
-
branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs
r11171 r12031 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 4Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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.