[12380] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12380] | 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 |
|
---|
| 22 | using System;
|
---|
[12191] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[12380] | 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[13412] | 27 | using HeuristicLab.Operators;
|
---|
[12380] | 28 | using HeuristicLab.Parameters;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[12191] | 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.PTSP {
|
---|
[13470] | 32 | [Item("EstimatedPTSPMoveEvaluator", "A base class for operators which evaluate PTSP moves.")]
|
---|
[16565] | 33 | [StorableType("F2F7F857-F2CD-4AB2-8656-5158BD04EFDD")]
|
---|
[13412] | 34 | public abstract class EstimatedPTSPMoveEvaluator : SingleSuccessorOperator, IEstimatedPTSPMoveEvaluator {
|
---|
[12191] | 35 |
|
---|
| 36 | public override bool CanChangeName {
|
---|
| 37 | get { return false; }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[12380] | 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 | }
|
---|
[13412] | 52 | public ILookupParameter<ItemList<BoolArray>> RealizationsParameter {
|
---|
| 53 | get { return (ILookupParameter<ItemList<BoolArray>>)Parameters["Realizations"]; }
|
---|
[12380] | 54 | }
|
---|
[12191] | 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 | }
|
---|
[13412] | 61 | public ILookupParameter<DistanceCalculator> DistanceCalculatorParameter {
|
---|
| 62 | get { return (ILookupParameter<DistanceCalculator>)Parameters["DistanceCalculator"]; }
|
---|
| 63 | }
|
---|
[12191] | 64 |
|
---|
| 65 | [StorableConstructor]
|
---|
[16565] | 66 | protected EstimatedPTSPMoveEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[13412] | 67 | protected EstimatedPTSPMoveEvaluator(EstimatedPTSPMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 68 | protected EstimatedPTSPMoveEvaluator()
|
---|
[12191] | 69 | : base() {
|
---|
[12380] | 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."));
|
---|
[13412] | 74 | Parameters.Add(new LookupParameter<ItemList<BoolArray>>("Realizations", "The list of samples drawn from all possible stochastic instances."));
|
---|
[12191] | 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."));
|
---|
[13412] | 77 | Parameters.Add(new LookupParameter<DistanceCalculator>("DistanceCalculator", "The class that can compute distances between coordinates."));
|
---|
[12191] | 78 | }
|
---|
[12380] | 79 |
|
---|
| 80 | public override IOperation Apply() {
|
---|
[13412] | 81 | var permutation = PermutationParameter.ActualValue;
|
---|
| 82 | var coordinates = CoordinatesParameter.ActualValue;
|
---|
[13470] | 83 | var realizations = RealizationsParameter.ActualValue;
|
---|
| 84 | Func<int, int, double> distance = null;
|
---|
[12380] | 85 | if (UseDistanceMatrixParameter.ActualValue.Value) {
|
---|
[13412] | 86 | var distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
| 87 | if (distanceMatrix == null) throw new InvalidOperationException("The distance matrix has not been calculated.");
|
---|
[13470] | 88 | distance = (a, b) => distanceMatrix[a, b];
|
---|
[12380] | 89 | } else {
|
---|
| 90 | if (coordinates == null) throw new InvalidOperationException("No coordinates were given.");
|
---|
[13412] | 91 | var distanceCalculator = DistanceCalculatorParameter.ActualValue;
|
---|
| 92 | if (distanceCalculator == null) throw new InvalidOperationException("Distance calculator is null!");
|
---|
[13470] | 93 | distance = (a, b) => distanceCalculator.Calculate(a, b, coordinates);
|
---|
[12380] | 94 | }
|
---|
[13470] | 95 | var relativeQualityDifference = EvaluateMove(permutation, distance, realizations);
|
---|
[13412] | 96 | var moveQuality = MoveQualityParameter.ActualValue;
|
---|
[12380] | 97 | if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
|
---|
| 98 | else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
|
---|
| 99 | return base.Apply();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[13470] | 102 | protected abstract double EvaluateMove(Permutation permutation, Func<int, int, double> distance, ItemList<BoolArray> realizations);
|
---|
[12191] | 103 | }
|
---|
| 104 | }
|
---|