1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
31 | /// <summary>
|
---|
32 | /// A base class for operators which evaluate TSP solutions given in path representation using city coordinates.
|
---|
33 | /// </summary>
|
---|
34 | [Item("TSPCoordinatesPathEvaluator", "A base class for operators which evaluate TSP solutions given in path representation using city coordinates.")]
|
---|
35 | [StorableClass]
|
---|
36 | public abstract class TSPCoordinatesPathEvaluator : TSPEvaluator, ITSPCoordinatesPathEvaluator {
|
---|
37 | private object locker = new object();
|
---|
38 |
|
---|
39 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
40 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<DoubleMatrix> CoordinatesParameter {
|
---|
43 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
46 | get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
49 | get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [StorableConstructor]
|
---|
53 | protected TSPCoordinatesPathEvaluator(bool deserializing) : base(deserializing) { }
|
---|
54 | protected TSPCoordinatesPathEvaluator(TSPCoordinatesPathEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
55 | protected TSPCoordinatesPathEvaluator()
|
---|
56 | : base() {
|
---|
57 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
|
---|
58 | Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
|
---|
59 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
60 | Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
|
---|
61 | }
|
---|
62 |
|
---|
63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
64 | private void AfterDeserialization() {
|
---|
65 | // BackwardsCompatibility3.3
|
---|
66 | #region Backwards compatible code (remove with 3.4)
|
---|
67 | LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as LookupParameter<DoubleMatrix>;
|
---|
68 | if (oldDistanceMatrixParameter != null) {
|
---|
69 | Parameters.Remove(oldDistanceMatrixParameter);
|
---|
70 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
71 | DistanceMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static double Apply(TSPCoordinatesPathEvaluator evaluator, DoubleMatrix coordinates, Permutation tour) {
|
---|
77 | DoubleMatrix c = coordinates;
|
---|
78 | Permutation p = tour;
|
---|
79 | double length = 0;
|
---|
80 | for (int i = 0; i < p.Length - 1; i++)
|
---|
81 | length += evaluator.CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]);
|
---|
82 | length += evaluator.CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]);
|
---|
83 | return length;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public sealed override IOperation InstrumentedApply() {
|
---|
87 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
88 | Permutation p = PermutationParameter.ActualValue;
|
---|
89 | DistanceMatrix dm = DistanceMatrixParameter.ActualValue;
|
---|
90 |
|
---|
91 | if (dm == null) { // calculate distance matrix
|
---|
92 | lock (locker) {
|
---|
93 | dm = DistanceMatrixParameter.ActualValue;
|
---|
94 | if (dm == null) { // check again to avoid race condition
|
---|
95 | DoubleMatrix c = CoordinatesParameter.ActualValue;
|
---|
96 | if (c == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
|
---|
97 | dm = new DistanceMatrix(c.Rows, c.Rows);
|
---|
98 | for (int i = 0; i < dm.Rows; i++) {
|
---|
99 | for (int j = 0; j < dm.Columns; j++)
|
---|
100 | dm[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
|
---|
101 | }
|
---|
102 | DistanceMatrixParameter.ActualValue = (DistanceMatrix)dm.AsReadOnly();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | double length = 0;
|
---|
108 | for (int i = 0; i < p.Length - 1; i++)
|
---|
109 | length += dm[p[i], p[i + 1]];
|
---|
110 | length += dm[p[p.Length - 1], p[0]];
|
---|
111 | QualityParameter.ActualValue = new DoubleValue(length);
|
---|
112 | } else {
|
---|
113 | Permutation p = PermutationParameter.ActualValue;
|
---|
114 | DoubleMatrix c = CoordinatesParameter.ActualValue;
|
---|
115 | if (c == null) throw new InvalidOperationException("No coordinates were given.");
|
---|
116 | double length = 0;
|
---|
117 | for (int i = 0; i < p.Length - 1; i++)
|
---|
118 | length += CalculateDistance(c[p[i], 0], c[p[i], 1], c[p[i + 1], 0], c[p[i + 1], 1]);
|
---|
119 | length += CalculateDistance(c[p[p.Length - 1], 0], c[p[p.Length - 1], 1], c[p[0], 0], c[p[0], 1]);
|
---|
120 | QualityParameter.ActualValue = new DoubleValue(length);
|
---|
121 | }
|
---|
122 | return base.InstrumentedApply();
|
---|
123 | }
|
---|
124 |
|
---|
125 | /// <summary>
|
---|
126 | /// Calculates the distance between two points.
|
---|
127 | /// </summary>
|
---|
128 | /// <param name="x1">The x-coordinate of point 1.</param>
|
---|
129 | /// <param name="y1">The y-coordinate of point 1.</param>
|
---|
130 | /// <param name="x2">The x-coordinate of point 2.</param>
|
---|
131 | /// <param name="y2">The y-coordinate of point 2.</param>
|
---|
132 | /// <returns>The calculated distance.</returns>
|
---|
133 | protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
|
---|
134 | }
|
---|
135 | }
|
---|