Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs @ 3232

Last change on this file since 3232 was 3232, checked in by abeham, 14 years ago

Renamed operators according to the underlying operation #889

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