Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPThreeOptPathMoveEvaluator.cs @ 3209

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

Added 3-opt move and evaluators for the TSP
Added 2-opt move evaluators for TSP Geo and Euclidean
#889

File size: 5.1 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 3-opt moves.
33  /// </summary>
34  [Item("TSPThreeOptPathMoveEvaluator", "Evaluates a 3-opt move by summing up the length of all added edges and subtracting the length of all deleted edges.")]
35  [StorableClass]
36  public abstract class TSPThreeOptPathMoveEvaluator : TSPPathMoveEvaluator, IThreeOptPermutationMoveOperator {
37    public ILookupParameter<ThreeOptMove> ThreeOptMoveParameter {
38      get { return (ILookupParameter<ThreeOptMove>)Parameters["ThreeOptMove"]; }
39    }
40
41    public TSPThreeOptPathMoveEvaluator()
42      : base() {
43      Parameters.Add(new LookupParameter<ThreeOptMove>("ThreeOptMove", "The move to evaluate."));
44    }
45
46    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
47      ThreeOptMove move = ThreeOptMoveParameter.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      double moveQuality = 0;
61      // remove three edges
62      moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
63        coordinates[edge1target, 0], coordinates[edge1target, 1]);
64      moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
65        coordinates[edge2target, 0], coordinates[edge2target, 1]);
66      moveQuality -= CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
67        coordinates[edge3target, 0], coordinates[edge3target, 1]);
68      // add three edges
69      moveQuality += CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
70        coordinates[edge1target, 0], coordinates[edge1target, 1]);
71      moveQuality += CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
72        coordinates[edge3target, 0], coordinates[edge3target, 1]);
73      moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
74        coordinates[edge2target, 0], coordinates[edge2target, 1]);
75      return moveQuality;
76    }
77
78    protected override double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix) {
79      ThreeOptMove move = ThreeOptMoveParameter.ActualValue;
80      int edge1source = permutation.GetCircular(move.Index1 - 1);
81      int edge1target = permutation[move.Index1];
82      int edge2source = permutation[move.Index2];
83      int edge2target = permutation.GetCircular(move.Index2 + 1);
84      int edge3source, edge3target;
85      if (move.Index3 > move.Index1) {
86        edge3source = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1);
87        edge3target = permutation.GetCircular(move.Index3 + move.Index2 - move.Index1 + 1);
88      } else {
89        edge3source = permutation.GetCircular(move.Index3 - 1);
90        edge3target = permutation[move.Index3];
91      }
92      if (move.Index1 == move.Index3
93        || move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
94      double moveQuality = 0;
95      // remove three edges
96      moveQuality -= distanceMatrix[edge1source, edge1target];
97      moveQuality -= distanceMatrix[edge2source, edge2target];
98      moveQuality -= distanceMatrix[edge3source, edge3target];
99      // add three edges
100      moveQuality += distanceMatrix[edge3source, edge1target];
101      moveQuality += distanceMatrix[edge2source, edge3target];
102      moveQuality += distanceMatrix[edge1source, edge2target];
103      return moveQuality;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.