Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs @ 17317

Last change on this file since 17317 was 14454, checked in by abeham, 8 years ago

#2701:

  • Worked on MemPR algorithm for permutations
  • Refactored TSP
File size: 4.7 KB
Line 
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
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [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    }
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    public ILookupParameter<EnumValue<TSPDistanceFunction>> DistanceFunctionParameter {
50      get { return (ILookupParameter<EnumValue<TSPDistanceFunction>>)Parameters["DistanceFunction"]; }
51    }
52
53    [StorableConstructor]
54    protected TSPPathMoveEvaluator(bool deserializing) : base(deserializing) { }
55    protected TSPPathMoveEvaluator(TSPPathMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
56    protected TSPPathMoveEvaluator()
57      : base() {
58      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
59      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The city's coordinates."));
60      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
61      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."));
62      Parameters.Add(new LookupParameter<EnumValue<TSPDistanceFunction>>("DistanceFunction", "The distance function to use when the distance matrix is not being used."));
63    }
64
65    public override IOperation Apply() {
66      var permutation = PermutationParameter.ActualValue;
67      var coordinates = CoordinatesParameter.ActualValue;
68      var distanceFunction = DistanceFunctionParameter.ActualValue.Value;
69      double relativeQualityDifference = 0;
70      if (UseDistanceMatrixParameter.ActualValue.Value) {
71        DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue;
72        if (distanceMatrix == null) {
73          throw new InvalidOperationException("Distance matrix is not given.");
74        }
75        relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix);
76      } else {
77        if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
78        relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates, distanceFunction);
79      }
80      DoubleValue moveQuality = MoveQualityParameter.ActualValue;
81      if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
82      else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
83      return base.Apply();
84    }
85    protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix);
86    protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction);
87  }
88}
Note: See TracBrowser for help on using the repository browser.