[3209] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3209] | 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[3209] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
| 30 | /// <summary>
|
---|
[3232] | 31 | /// An operator to evaluate inversion moves (2-opt).
|
---|
[3209] | 32 | /// </summary>
|
---|
[3232] | 33 | [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.")]
|
---|
[3209] | 34 | [StorableClass]
|
---|
[14454] | 35 | public class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
|
---|
[3232] | 36 | public ILookupParameter<InversionMove> InversionMoveParameter {
|
---|
| 37 | get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
|
---|
[3209] | 38 | }
|
---|
| 39 |
|
---|
[4722] | 40 | [StorableConstructor]
|
---|
| 41 | protected TSPInversionMovePathEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected TSPInversionMovePathEvaluator(TSPInversionMovePathEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3232] | 43 | public TSPInversionMovePathEvaluator()
|
---|
[3209] | 44 | : base() {
|
---|
[3232] | 45 | Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
|
---|
[3209] | 46 | }
|
---|
| 47 |
|
---|
[14454] | 48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 49 | return new TSPInversionMovePathEvaluator(this, cloner);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
|
---|
[3221] | 53 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
| 54 | int edge1target = permutation[move.Index1];
|
---|
| 55 | int edge2source = permutation[move.Index2];
|
---|
| 56 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
| 57 | if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
|
---|
[3209] | 58 | double moveQuality = 0;
|
---|
| 59 | // remove two edges
|
---|
[14454] | 60 | moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
[3209] | 61 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
[14454] | 62 | moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
[3209] | 63 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 64 | // add two edges
|
---|
[14454] | 65 | moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
[3209] | 66 | coordinates[edge2source, 0], coordinates[edge2source, 1]);
|
---|
[14454] | 67 | moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1target, 0], coordinates[edge1target, 1],
|
---|
[3209] | 68 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
| 69 | return moveQuality;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[7743] | 72 | public static double EvaluateByDistanceMatrix(Permutation permutation, InversionMove move, DistanceMatrix distanceMatrix) {
|
---|
[3221] | 73 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
| 74 | int edge1target = permutation[move.Index1];
|
---|
| 75 | int edge2source = permutation[move.Index2];
|
---|
| 76 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
| 77 | if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
|
---|
[3209] | 78 | double moveQuality = 0;
|
---|
| 79 | // remove two edges
|
---|
| 80 | moveQuality -= distanceMatrix[edge1source, edge1target];
|
---|
| 81 | moveQuality -= distanceMatrix[edge2source, edge2target];
|
---|
| 82 | // add two edges
|
---|
| 83 | moveQuality += distanceMatrix[edge1source, edge2source];
|
---|
| 84 | moveQuality += distanceMatrix[edge1target, edge2target];
|
---|
| 85 | return moveQuality;
|
---|
| 86 | }
|
---|
[7743] | 87 |
|
---|
[14454] | 88 | protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) {
|
---|
| 89 | return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, distanceFunction);
|
---|
[7743] | 90 | }
|
---|
| 91 |
|
---|
| 92 | protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
|
---|
| 93 | return EvaluateByDistanceMatrix(permutation, InversionMoveParameter.ActualValue, distanceMatrix);
|
---|
| 94 | }
|
---|
[3209] | 95 | }
|
---|
| 96 | }
|
---|