Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Problems.PTSP/3.3/Moves/AnalyticalPTSPMoveEvaluator.cs @ 16724

Last change on this file since 16724 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.PTSP {
32  [Item("AnalyticalPTSPMoveEvaluator", "A base class for operators which evaluate PTSP moves.")]
33  [StorableClass]
34  public abstract class AnalyticalPTSPMoveEvaluator : SingleSuccessorOperator, IAnalyticalPTSPMoveEvaluator {
35
36    public override bool CanChangeName {
37      get { return false; }
38    }
39
40    public ILookupParameter<Permutation> PermutationParameter {
41      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
42    }
43    public ILookupParameter<DoubleMatrix> CoordinatesParameter {
44      get { return (ILookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
45    }
46    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
47      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
48    }
49    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
50      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
51    }
52    public ILookupParameter<DoubleArray> ProbabilitiesParameter {
53      get { return (ILookupParameter<DoubleArray>)Parameters["Probabilities"]; }
54    }
55    public ILookupParameter<DoubleValue> QualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
57    }
58    public ILookupParameter<DoubleValue> MoveQualityParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
60    }
61    public ILookupParameter<DistanceCalculator> DistanceCalculatorParameter {
62      get { return (ILookupParameter<DistanceCalculator>)Parameters["DistanceCalculator"]; }
63    }
64
65    [StorableConstructor]
66    protected AnalyticalPTSPMoveEvaluator(bool deserializing) : base(deserializing) { }
67    protected AnalyticalPTSPMoveEvaluator(AnalyticalPTSPMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
68    protected AnalyticalPTSPMoveEvaluator()
69      : base() {
70      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
71      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The city's coordinates."));
72      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
73      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."));
74      Parameters.Add(new LookupParameter<DoubleArray>("Probabilities", "The list of probabilities for each city to appear."));
75      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a TSP solution."));
76      Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a TSP solution."));
77      Parameters.Add(new LookupParameter<DistanceCalculator>("DistanceCalculator", "The class that can compute distances between coordinates."));
78    }
79
80    public override IOperation Apply() {
81      var permutation = PermutationParameter.ActualValue;
82      var coordinates = CoordinatesParameter.ActualValue;
83      var probabilities = ProbabilitiesParameter.ActualValue;
84      Func<int, int, double> distance = null;
85      if (UseDistanceMatrixParameter.ActualValue.Value) {
86        var distanceMatrix = DistanceMatrixParameter.ActualValue;
87        if (distanceMatrix == null) throw new InvalidOperationException("The distance matrix has not been calculated.");
88        distance = (a, b) => distanceMatrix[a, b];
89      } else {
90        if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
91        var distanceCalculator = DistanceCalculatorParameter.ActualValue;
92        if (distanceCalculator == null) throw new InvalidOperationException("Distance calculator is null!");
93        distance = (a, b) => distanceCalculator.Calculate(a, b, coordinates);
94      }
95      // here moves are not delta-evaluated
96      var newQuality = EvaluateMove(permutation, distance, probabilities);
97      var moveQuality = MoveQualityParameter.ActualValue;
98      if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(newQuality);
99      else moveQuality.Value = newQuality;
100
101      return base.Apply();
102    }
103
104    protected abstract double EvaluateMove(Permutation permutation, Func<int, int, double> distance, DoubleArray probabilities);
105  }
106}
Note: See TracBrowser for help on using the repository browser.