Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TravelingSalesman/3.3/Evaluators/TSPCoordinatesPathEvaluator.cs @ 17204

Last change on this file since 17204 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.5 KB
RevLine 
[2975]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2975]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
[7621]22using System;
[4722]23using HeuristicLab.Common;
[2975]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[3053]26using HeuristicLab.Encodings.PermutationEncoding;
[2975]27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[2975]29
[3158]30namespace HeuristicLab.Problems.TravelingSalesman {
[2975]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.")]
[16565]35  [StorableType("4A8D4CC9-C6A6-4611-A140-C21FAA93EDC0")]
[2988]36  public abstract class TSPCoordinatesPathEvaluator : TSPEvaluator, ITSPCoordinatesPathEvaluator {
[5210]37    private object locker = new object();
38
[2988]39    public ILookupParameter<Permutation> PermutationParameter {
40      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
41    }
[3048]42    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
43      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
[2975]44    }
[4825]45    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
46      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
[3066]47    }
48    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
49      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
50    }
[2975]51
[4722]52    [StorableConstructor]
[16565]53    protected TSPCoordinatesPathEvaluator(StorableConstructorFlag _) : base(_) { }
[4722]54    protected TSPCoordinatesPathEvaluator(TSPCoordinatesPathEvaluator original, Cloner cloner) : base(original, cloner) { }
[2975]55    protected TSPCoordinatesPathEvaluator()
56      : base() {
[2988]57      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
[3048]58      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
[4825]59      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
[3066]60      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
[2975]61    }
62
[4825]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
[7558]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
[10291]86    public sealed override IOperation InstrumentedApply() {
[3066]87      if (UseDistanceMatrixParameter.ActualValue.Value) {
88        Permutation p = PermutationParameter.ActualValue;
[4825]89        DistanceMatrix dm = DistanceMatrixParameter.ActualValue;
[2988]90
[3066]91        if (dm == null) {  // calculate distance matrix
[5210]92          lock (locker) {
93            dm = DistanceMatrixParameter.ActualValue;
94            if (dm == null) {  // check again to avoid race condition
95              DoubleMatrix c = CoordinatesParameter.ActualValue;
[7621]96              if (c == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
[5210]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            }
[3066]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;
[7621]115        if (c == null) throw new InvalidOperationException("No coordinates were given.");
[3066]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      }
[10291]122      return base.InstrumentedApply();
[2975]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}
Note: See TracBrowser for help on using the repository browser.