Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs @ 7683

Last change on this file since 7683 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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