[3074] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3074] | 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] | 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3074] | 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 |
|
---|
[3158] | 30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
[3074] | 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 | [StorableClass]
|
---|
| 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 | }
|
---|
[4825] | 43 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 44 | get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
[3074] | 45 | }
|
---|
| 46 | public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 47 | get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[4722] | 50 | [StorableConstructor]
|
---|
| 51 | protected TSPPathMoveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 52 | protected TSPPathMoveEvaluator(TSPPathMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3074] | 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."));
|
---|
[4825] | 57 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
[3074] | 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 | }
|
---|
[3209] | 60 |
|
---|
[4825] | 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 |
|
---|
[3209] | 74 | public override IOperation Apply() {
|
---|
| 75 | Permutation permutation = PermutationParameter.ActualValue;
|
---|
| 76 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
| 77 | double relativeQualityDifference = 0;
|
---|
| 78 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
[4825] | 79 | DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
[3209] | 80 | if (distanceMatrix == null) {
|
---|
[7621] | 81 | if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
|
---|
[3209] | 82 | distanceMatrix = CalculateDistanceMatrix(coordinates);
|
---|
| 83 | DistanceMatrixParameter.ActualValue = distanceMatrix;
|
---|
| 84 | }
|
---|
| 85 | relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
|
---|
[7621] | 86 | } else {
|
---|
| 87 | if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
|
---|
| 88 | relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates);
|
---|
| 89 | }
|
---|
[3209] | 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);
|
---|
[4825] | 97 | protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix);
|
---|
[3209] | 98 | protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates);
|
---|
| 99 |
|
---|
[4825] | 100 | private DistanceMatrix CalculateDistanceMatrix(DoubleMatrix c) {
|
---|
| 101 | DistanceMatrix distanceMatrix = new DistanceMatrix(c.Rows, c.Rows);
|
---|
[3209] | 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 | }
|
---|
[4825] | 106 | return (DistanceMatrix)distanceMatrix.AsReadOnly();
|
---|
[3209] | 107 | }
|
---|
[3074] | 108 | }
|
---|
| 109 | }
|
---|