Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs @ 16462

Last change on this file since 16462 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 6.2 KB
RevLine 
[3209]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3209]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
[4722]22using HeuristicLab.Common;
[3209]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.TravelingSalesman {
30  /// <summary>
[3232]31  /// An operator to evaluate a translocation or insertion move.
[3209]32  /// </summary>
[3232]33  [Item("TSPTranslocationMovePathEvaluator", "Evaluates a translocation or insertion move (3-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
[3209]34  [StorableClass]
[3232]35  public abstract class TSPTranslocationMovePathEvaluator : TSPPathMoveEvaluator, IPermutationTranslocationMoveOperator {
36    public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
37      get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
[3209]38    }
39
[4722]40    [StorableConstructor]
41    protected TSPTranslocationMovePathEvaluator(bool deserializing) : base(deserializing) { }
42    protected TSPTranslocationMovePathEvaluator(TSPTranslocationMovePathEvaluator original, Cloner cloner) : base(original, cloner) { }
[3232]43    public TSPTranslocationMovePathEvaluator()
[3209]44      : base() {
[3232]45      Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
[3209]46    }
47
[7743]48    public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPTranslocationMovePathEvaluator evaluator) {
49      if (move.Index1 == move.Index3
50        || move.Index2 == permutation.Length - 1 && move.Index3 == 0
51        || move.Index1 == 0 && move.Index3 == permutation.Length - 1 - move.Index2) return 0;
52
[3209]53      int edge1source = permutation.GetCircular(move.Index1 - 1);
54      int edge1target = permutation[move.Index1];
55      int edge2source = permutation[move.Index2];
56      int edge2target = permutation.GetCircular(move.Index2 + 1);
57      int edge3source, edge3target;
58      if (move.Index3 > move.Index1) {
59        edge3source = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
60        edge3target = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
61      } else {
62        edge3source = permutation.GetCircular(move.Index3 - 1);
63        edge3target = permutation[move.Index3];
64      }
65      double moveQuality = 0;
66      // remove three edges
[7743]67      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
[3209]68        coordinates[edge1target, 0], coordinates[edge1target, 1]);
[7743]69      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
[3209]70        coordinates[edge2target, 0], coordinates[edge2target, 1]);
[7743]71      moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
[3209]72        coordinates[edge3target, 0], coordinates[edge3target, 1]);
73      // add three edges
[7743]74      moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
[3209]75        coordinates[edge1target, 0], coordinates[edge1target, 1]);
[7743]76      moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
[3209]77        coordinates[edge3target, 0], coordinates[edge3target, 1]);
[7743]78      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
[3209]79        coordinates[edge2target, 0], coordinates[edge2target, 1]);
80      return moveQuality;
81    }
82
[7743]83    public static double EvaluateByDistanceMatrix(Permutation permutation, TranslocationMove move, DistanceMatrix distanceMatrix) {
84      if (move.Index1 == move.Index3
85        || move.Index2 == permutation.Length - 1 && move.Index3 == 0
86        || move.Index1 == 0 && move.Index3 == permutation.Length - 1 - move.Index2) return 0;
87
[3209]88      int edge1source = permutation.GetCircular(move.Index1 - 1);
89      int edge1target = permutation[move.Index1];
90      int edge2source = permutation[move.Index2];
91      int edge2target = permutation.GetCircular(move.Index2 + 1);
92      int edge3source, edge3target;
93      if (move.Index3 > move.Index1) {
94        edge3source = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
95        edge3target = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
96      } else {
97        edge3source = permutation.GetCircular(move.Index3 - 1);
98        edge3target = permutation[move.Index3];
99      }
100      double moveQuality = 0;
101      // remove three edges
102      moveQuality -= distanceMatrix[edge1source, edge1target];
103      moveQuality -= distanceMatrix[edge2source, edge2target];
104      moveQuality -= distanceMatrix[edge3source, edge3target];
105      // add three edges
106      moveQuality += distanceMatrix[edge3source, edge1target];
107      moveQuality += distanceMatrix[edge2source, edge3target];
108      moveQuality += distanceMatrix[edge1source, edge2target];
109      return moveQuality;
110    }
[7743]111
112    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
113      return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, this);
114    }
115
116    protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
117      return EvaluateByDistanceMatrix(permutation, TranslocationMoveParameter.ActualValue, distanceMatrix);
118    }
[3209]119  }
120}
Note: See TracBrowser for help on using the repository browser.