Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMoveEvaluator.cs @ 17241

Last change on this file since 17241 was 17241, checked in by abeham, 5 years ago

#2521: worked on refactoring TSP

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Parameters;
27
28namespace HeuristicLab.Problems.TravelingSalesman {
29  /// <summary>
30  /// An operator to evaluate a translocation or insertion move.
31  /// </summary>
32  [Item("TSP Translocation Move Evaluator", "Evaluates a translocation or insertion move (3-opt) by an efficient delta computation.")]
33  [StorableType("3f13bc93-778e-4b9b-8285-1b807886a653")]
34  public class TSPTranslocationMoveEvaluator : TSPMoveEvaluator, IPermutationTranslocationMoveOperator {
35    [Storable] public ILookupParameter<TranslocationMove> MoveParameter { get; private set; }
36
37    ILookupParameter<TranslocationMove> IPermutationTranslocationMoveOperator.TranslocationMoveParameter => MoveParameter;
38    ILookupParameter<Permutation> IPermutationMoveOperator.PermutationParameter => TSPTourParameter;
39
40    [StorableConstructor]
41    protected TSPTranslocationMoveEvaluator(StorableConstructorFlag _) : base(_) { }
42    protected TSPTranslocationMoveEvaluator(TSPTranslocationMoveEvaluator original, Cloner cloner)
43      : base(original, cloner) {
44      MoveParameter = cloner.Clone(original.MoveParameter);
45    }
46    public TSPTranslocationMoveEvaluator()
47      : base() {
48      Parameters.Add(MoveParameter = new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new TSPTranslocationMoveEvaluator(this, cloner);
53    }
54
55    protected override double CalculateTourLengthWithMove(ITSPData tspData, Permutation tspTour, double tourLength) {
56      var move = MoveParameter.ActualValue;
57      return CalculateTourLengthDelta(tspData, tspTour, move);
58    }
59
60    public static double CalculateTourLengthDelta(ITSPData tspData, Permutation tspTour, TranslocationMove move) {
61      if (move.Index1 == move.Index3
62        || move.Index2 == tspTour.Length - 1 && move.Index3 == 0
63        || move.Index1 == 0 && move.Index3 == tspTour.Length - 1 - move.Index2) return 0;
64
65      int edge1source = tspTour.GetCircular(move.Index1 - 1);
66      int edge1target = tspTour[move.Index1];
67      int edge2source = tspTour[move.Index2];
68      int edge2target = tspTour.GetCircular(move.Index2 + 1);
69      int edge3source, edge3target;
70      if (move.Index3 > move.Index1) {
71        edge3source = tspTour.GetCircular(move.Index3 + move.Index2 - move.Index1);
72        edge3target = tspTour.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
73      } else {
74        edge3source = tspTour.GetCircular(move.Index3 - 1);
75        edge3target = tspTour[move.Index3];
76      }
77      double moveQuality = 0;
78      // remove three edges
79      moveQuality -= tspData.GetDistance(edge1source, edge1target);
80      moveQuality -= tspData.GetDistance(edge2source, edge2target);
81      moveQuality -= tspData.GetDistance(edge3source, edge3target);
82      // add three edges
83      moveQuality += tspData.GetDistance(edge3source, edge1target);
84      moveQuality += tspData.GetDistance(edge2source, edge3target);
85      moveQuality += tspData.GetDistance(edge1source, edge2target);
86      return moveQuality;
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.