Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPTwoOptPathMoveEvaluator.cs @ 3221

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

updated permutation moves, fixed 3-opt #889

File size: 3.9 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 move = TwoOptMoveParameter.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      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
53      double moveQuality = 0;
54      // remove two edges
55      moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
56            coordinates[edge1target, 0], coordinates[edge1target, 1]);
57      moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
58        coordinates[edge2target, 0], coordinates[edge2target, 1]);
59      // add two edges
60      moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
61        coordinates[edge2source, 0], coordinates[edge2source, 1]);
62      moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
63        coordinates[edge2target, 0], coordinates[edge2target, 1]);
64      return moveQuality;
65    }
66
67    protected override double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix) {
68      TwoOptMove move = TwoOptMoveParameter.ActualValue;
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}
Note: See TracBrowser for help on using the repository browser.