Free cookie consent management tool by TermsFeed Policy Generator

source: branches/irace/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs @ 11574

Last change on this file since 11574 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
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>
31  /// An operator to evaluate a translocation or insertion move.
32  /// </summary>
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.")]
34  [StorableClass]
35  public abstract class TSPTranslocationMovePathEvaluator : TSPPathMoveEvaluator, IPermutationTranslocationMoveOperator {
36    public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
37      get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
38    }
39
40    [StorableConstructor]
41    protected TSPTranslocationMovePathEvaluator(bool deserializing) : base(deserializing) { }
42    protected TSPTranslocationMovePathEvaluator(TSPTranslocationMovePathEvaluator original, Cloner cloner) : base(original, cloner) { }
43    public TSPTranslocationMovePathEvaluator()
44      : base() {
45      Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
46    }
47
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
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
67      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
68        coordinates[edge1target, 0], coordinates[edge1target, 1]);
69      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
70        coordinates[edge2target, 0], coordinates[edge2target, 1]);
71      moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
72        coordinates[edge3target, 0], coordinates[edge3target, 1]);
73      // add three edges
74      moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
75        coordinates[edge1target, 0], coordinates[edge1target, 1]);
76      moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
77        coordinates[edge3target, 0], coordinates[edge3target, 1]);
78      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
79        coordinates[edge2target, 0], coordinates[edge2target, 1]);
80      return moveQuality;
81    }
82
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
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    }
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    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.