Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMovePathEvaluator.cs @ 3376

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 4.0 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TravelingSalesman {
32  /// <summary>
33  /// An operator to evaluate inversion moves (2-opt).
34  /// </summary>
35  [Item("TSPInversionMovePathEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
36  [StorableClass]
37  public abstract class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
38    public ILookupParameter<InversionMove> InversionMoveParameter {
39      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
40    }
41
42    public TSPInversionMovePathEvaluator()
43      : base() {
44      Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
45    }
46
47    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
48      InversionMove move = InversionMoveParameter.ActualValue;
49      int edge1source = permutation.GetCircular(move.Index1 - 1);
50      int edge1target = permutation[move.Index1];
51      int edge2source = permutation[move.Index2];
52      int edge2target = permutation.GetCircular(move.Index2 + 1);
53      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
54      double moveQuality = 0;
55      // remove two edges
56      moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
57            coordinates[edge1target, 0], coordinates[edge1target, 1]);
58      moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
59        coordinates[edge2target, 0], coordinates[edge2target, 1]);
60      // add two edges
61      moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
62        coordinates[edge2source, 0], coordinates[edge2source, 1]);
63      moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
64        coordinates[edge2target, 0], coordinates[edge2target, 1]);
65      return moveQuality;
66    }
67
68    protected override double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix) {
69      InversionMove move = InversionMoveParameter.ActualValue;
70      int edge1source = permutation.GetCircular(move.Index1 - 1);
71      int edge1target = permutation[move.Index1];
72      int edge2source = permutation[move.Index2];
73      int edge2target = permutation.GetCircular(move.Index2 + 1);
74      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
75      double moveQuality = 0;
76      // remove two edges
77      moveQuality -= distanceMatrix[edge1source, edge1target];
78      moveQuality -= distanceMatrix[edge2source, edge2target];
79      // add two edges
80      moveQuality += distanceMatrix[edge1source, edge2source];
81      moveQuality += distanceMatrix[edge1target, edge2target];
82      return moveQuality;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.