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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
29 | /// <summary>
|
---|
30 | /// An operator to evaluate inversion moves (2-opt).
|
---|
31 | /// </summary>
|
---|
32 | [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.")]
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {
|
---|
35 | public ILookupParameter<InversionMove> InversionMoveParameter {
|
---|
36 | get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public TSPInversionMovePathEvaluator()
|
---|
40 | : base() {
|
---|
41 | Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
|
---|
45 | InversionMove move = InversionMoveParameter.ActualValue;
|
---|
46 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
47 | int edge1target = permutation[move.Index1];
|
---|
48 | int edge2source = permutation[move.Index2];
|
---|
49 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
50 | if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
|
---|
51 | double moveQuality = 0;
|
---|
52 | // remove two edges
|
---|
53 | moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
54 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
55 | moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
56 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
57 | // add two edges
|
---|
58 | moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
59 | coordinates[edge2source, 0], coordinates[edge2source, 1]);
|
---|
60 | moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
|
---|
61 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
62 | return moveQuality;
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override double EvaluateByDistanceMatrix(Permutation permutation, DoubleMatrix distanceMatrix) {
|
---|
66 | InversionMove move = InversionMoveParameter.ActualValue;
|
---|
67 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
68 | int edge1target = permutation[move.Index1];
|
---|
69 | int edge2source = permutation[move.Index2];
|
---|
70 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
71 | if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
|
---|
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 | }
|
---|