Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs @ 18091

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

#2875: Removed years in copyrights

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// A base class for operators which evaluate TSP solutions.
33  /// </summary>
34  [Item("TSPMoveEvaluator", "A base class for operators which evaluate TSP moves.")]
35  [StorableType("F4EEA3B3-BA91-4391-903B-2EDBD47CF439")]
36  public abstract class TSPPathMoveEvaluator : TSPMoveEvaluator, ITSPPathMoveEvaluator {
37    public ILookupParameter<Permutation> PermutationParameter {
38      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
39    }
40    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
41      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
42    }
43    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
44      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
45    }
46    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
47      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
48    }
49
50    [StorableConstructor]
51    protected TSPPathMoveEvaluator(StorableConstructorFlag _) : base(_) { }
52    protected TSPPathMoveEvaluator(TSPPathMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
53    protected TSPPathMoveEvaluator()
54      : base() {
55      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
56      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The city's coordinates."));
57      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
58      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false."));
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      // BackwardsCompatibility3.3
64      #region Backwards compatible code (remove with 3.4)
65      LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as LookupParameter<DoubleMatrix>;
66      if (oldDistanceMatrixParameter != null) {
67        Parameters.Remove(oldDistanceMatrixParameter);
68        Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
69        DistanceMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
70      }
71      #endregion
72    }
73
74    public override IOperation Apply() {
75      Permutation permutation = PermutationParameter.ActualValue;
76      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
77      double relativeQualityDifference = 0;
78      if (UseDistanceMatrixParameter.ActualValue.Value) {
79        DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
80        if (distanceMatrix == null) {
81          if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
82          distanceMatrix = CalculateDistanceMatrix(coordinates);
83          DistanceMatrixParameter.ActualValue = distanceMatrix;
84        }
85        relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
86      } else {
87        if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
88        relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
89      }
90      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
91      if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
92      else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
93      return base.Apply();
94    }
95
96    protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);
97    protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix);
98    protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
99
100    private DistanceMatrix CalculateDistanceMatrix(DoubleMatrix c) {
101      DistanceMatrix distanceMatrix = new DistanceMatrix(c.Rows, c.Rows);
102      for (int i = 0; i < distanceMatrix.Rows; i++) {
103        for (int j = 0; j < distanceMatrix.Columns; j++)
104          distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]);
105      }
106      return (DistanceMatrix)distanceMatrix.AsReadOnly();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.