Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMovePathEvaluator.cs @ 9844

Last change on this file since 9844 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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 inversion moves (2-opt).
32  /// </summary>
33  [Item("TSPInversionMovePathEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
34  [StorableClass]
35  public abstract class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
36    public ILookupParameter<InversionMove> InversionMoveParameter {
37      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
38    }
39
40    [StorableConstructor]
41    protected TSPInversionMovePathEvaluator(bool deserializing) : base(deserializing) { }
42    protected TSPInversionMovePathEvaluator(TSPInversionMovePathEvaluator original, Cloner cloner) : base(original, cloner) { }
43    public TSPInversionMovePathEvaluator()
44      : base() {
45      Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
46    }
47
48    public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPInversionMovePathEvaluator evaluator) {
49      int edge1source = permutation.GetCircular(move.Index1 - 1);
50      int edge1target = permutation[move.Index1];
51      int edge2source = permutation[move.Index2];
52      int edge2target = permutation.GetCircular(move.Index2 + 1);
53      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
54      double moveQuality = 0;
55      // remove two edges
56      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
57            coordinates[edge1target, 0], coordinates[edge1target, 1]);
58      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
59        coordinates[edge2target, 0], coordinates[edge2target, 1]);
60      // add two edges
61      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
62        coordinates[edge2source, 0], coordinates[edge2source, 1]);
63      moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
64        coordinates[edge2target, 0], coordinates[edge2target, 1]);
65      return moveQuality;
66    }
67
68    public static double EvaluateByDistanceMatrix(Permutation permutation, InversionMove move, DistanceMatrix distanceMatrix) {
69      int edge1source = permutation.GetCircular(move.Index1 - 1);
70      int edge1target = permutation[move.Index1];
71      int edge2source = permutation[move.Index2];
72      int edge2target = permutation.GetCircular(move.Index2 + 1);
73      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
74      double moveQuality = 0;
75      // remove two edges
76      moveQuality -= distanceMatrix[edge1source, edge1target];
77      moveQuality -= distanceMatrix[edge2source, edge2target];
78      // add two edges
79      moveQuality += distanceMatrix[edge1source, edge2source];
80      moveQuality += distanceMatrix[edge1target, edge2target];
81      return moveQuality;
82    }
83
84    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
85      return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);
86    }
87
88    protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
89      return EvaluateByDistanceMatrix(permutation, InversionMoveParameter.ActualValue, distanceMatrix);
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.