Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/15 09:21:46 (10 years ago)
Author:
jkarder
Message:

#2251: merged r11840 into stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs

    r11170 r11897  
    9595
    9696    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)
    9999        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
    100       if (currSol.PermutationType != PermutationTypes.RelativeUndirected)
     100      if (solution.PermutationType != PermutationTypes.RelativeUndirected)
    101101        throw new ArgumentException("Cannot improve solution because the permutation type is not supported.");
    102102
    103103      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);
    112108      }
    113109
     
    116112      return base.Apply();
    117113    }
    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 swap
    124             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     }
    129114  }
    130115}
Note: See TracChangeset for help on using the changeset viewer.