Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPTwoOptPathMoveEvaluator.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: 3.7 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 2-opt moves.
33  /// </summary>
34  [Item("TSPTwoOptPathMoveEvaluator", "Evaluates a 2-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 TSPTwoOptPathMoveEvaluator : TSPPathMoveEvaluator, ITwoOptPermutationMoveOperator {
37    public ILookupParameter<TwoOptMove> TwoOptMoveParameter {
38      get { return (ILookupParameter<TwoOptMove>)Parameters["TwoOptMove"]; }
39    }
40
41    public TSPTwoOptPathMoveEvaluator()
42      : base() {
43      Parameters.Add(new LookupParameter<TwoOptMove>("TwoOptMove", "The move to evaluate."));
44    }
45
46    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
47      TwoOptMove m = TwoOptMoveParameter.ActualValue;
48      int edge1source = permutation.GetCircular(m.Index1 - 1);
49      int edge1target = permutation[m.Index1];
50      int edge2source = permutation[m.Index2];
51      int edge2target = permutation.GetCircular(m.Index2 + 1);
52      double moveQuality = 0;
53      // remove two edges
54      moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
55            coordinates[edge1target, 0], coordinates[edge1target, 1]);
56      moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
57        coordinates[edge2target, 0], coordinates[edge2target, 1]);
58      // add two edges
59      moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
60        coordinates[edge2source, 0], coordinates[edge2source, 1]);
61      moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
62        coordinates[edge2target, 0], coordinates[edge2target, 1]);
63      return moveQuality;
64    }
65
66    protected override double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix) {
67      TwoOptMove m = TwoOptMoveParameter.ActualValue;
68      int edge1source = permutation.GetCircular(m.Index1 - 1);
69      int edge1target = permutation[m.Index1];
70      int edge2source = permutation[m.Index2];
71      int edge2target = permutation.GetCircular(m.Index2 + 1);
72      double moveQuality = 0;
73      // remove two edges
74      moveQuality -= distanceMatrix[edge1source, edge1target];
75      moveQuality -= distanceMatrix[edge2source, edge2target];
76      // add two edges
77      moveQuality += distanceMatrix[edge1source, edge2source];
78      moveQuality += distanceMatrix[edge1target, edge2target];
79      return moveQuality;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.