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.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
31 | /// <summary>
|
---|
32 | /// An operator to evaluate 3-opt moves.
|
---|
33 | /// </summary>
|
---|
34 | [Item("ThreeOptMoveTSPCoordinatesPathEvaluator", "Base class for evaluating 3-opt moves given the cities' coordinates.")]
|
---|
35 | [StorableClass]
|
---|
36 | public abstract class ThreeOptMoveTSPCoordinatesPathEvaluator : TSPPathMoveEvaluator, IThreeOptPermutationMoveOperator {
|
---|
37 | public ILookupParameter<ThreeOptMove> ThreeOptMoveParameter {
|
---|
38 | get { return (ILookupParameter<ThreeOptMove>)Parameters["ThreeOptMove"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ThreeOptMoveTSPCoordinatesPathEvaluator()
|
---|
42 | : base() {
|
---|
43 | Parameters.Add(new LookupParameter<ThreeOptMove>("ThreeOptMove", "The move to evaluate."));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IOperation Apply() {
|
---|
47 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
48 | ThreeOptMove move = ThreeOptMoveParameter.ActualValue;
|
---|
49 | double moveQuality = QualityParameter.ActualValue.Value;
|
---|
50 | int edge1source = permutation.GetCircular(move.Index1 - 1);
|
---|
51 | int edge1target = permutation[move.Index1];
|
---|
52 | int edge2source = permutation[move.Index2];
|
---|
53 | int edge2target = permutation.GetCircular(move.Index2 + 1);
|
---|
54 | int edge3source = permutation.GetCircular(move.Index3 - 1);
|
---|
55 | int edge3target = permutation[move.Index3];
|
---|
56 |
|
---|
57 | if (!(move.Index1 == 0 && move.Index2 == permutation.Length - 1)) { // a change like this would break the calculation (it doesn't actually change the quality anyway)
|
---|
58 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
59 | DoubleMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
60 | if (distanceMatrix == null) {
|
---|
61 | distanceMatrix = CalculateDistanceMatrix(CoordinatesParameter.ActualValue);
|
---|
62 | DistanceMatrixParameter.ActualValue = distanceMatrix;
|
---|
63 | }
|
---|
64 | // remove three edges
|
---|
65 | moveQuality -= distanceMatrix[edge1source, edge1target];
|
---|
66 | moveQuality -= distanceMatrix[edge2source, edge2target];
|
---|
67 | moveQuality -= distanceMatrix[edge3source, edge3target];
|
---|
68 | // add three edges
|
---|
69 | moveQuality += distanceMatrix[edge3source, edge1target];
|
---|
70 | moveQuality += distanceMatrix[edge2source, edge3target];
|
---|
71 | moveQuality += distanceMatrix[edge1source, edge2target];
|
---|
72 | } else {
|
---|
73 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
74 | // remove three edges
|
---|
75 | moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
76 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
77 | moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
78 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
79 | moveQuality -= CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
|
---|
80 | coordinates[edge3target, 0], coordinates[edge3target, 1]);
|
---|
81 | // add three edges
|
---|
82 | moveQuality += CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
|
---|
83 | coordinates[edge1target, 0], coordinates[edge1target, 1]);
|
---|
84 | moveQuality += CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
|
---|
85 | coordinates[edge3target, 0], coordinates[edge3target, 1]);
|
---|
86 | moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
|
---|
87 | coordinates[edge2target, 0], coordinates[edge2target, 1]);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if (MoveQualityParameter.ActualValue == null) MoveQualityParameter.ActualValue = new DoubleValue(moveQuality);
|
---|
91 | else MoveQualityParameter.ActualValue.Value = moveQuality;
|
---|
92 | return base.Apply();
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
|
---|
96 |
|
---|
97 | protected DoubleMatrix CalculateDistanceMatrix(DoubleMatrix c) {
|
---|
98 | DoubleMatrix distanceMatrix = new DoubleMatrix(c.Rows, c.Rows);
|
---|
99 | for (int i = 0; i < distanceMatrix.Rows; i++) {
|
---|
100 | for (int j = 0; j < distanceMatrix.Columns; j++)
|
---|
101 | distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
|
---|
102 | }
|
---|
103 | return distanceMatrix;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|