[13193] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13193] | 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;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[17097] | 28 | using HEAL.Attic;
|
---|
[13193] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// An operator to evaluate inversion moves (2-opt).
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("TSPTranslocationMoveDistanceMatrixEvaluator", "Evaluates a translocation or insertion move (3-opt).")]
|
---|
[17097] | 35 | [StorableType("895EF1F3-6FE0-43C0-A6D4-7EEAB7B06841")]
|
---|
[13193] | 36 | public class TSPTranslocationMoveDistanceMatrixEvaluator : TSPMoveEvaluator, IPermutationTranslocationMoveOperator {
|
---|
| 37 | public override Type EvaluatorType {
|
---|
| 38 | get { return typeof(TSPDistanceMatrixEvaluator); }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public ILookupParameter<Permutation> PermutationParameter {
|
---|
| 42 | get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
|
---|
| 43 | }
|
---|
| 44 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 45 | get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 46 | }
|
---|
| 47 | public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
|
---|
| 48 | get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | [StorableConstructor]
|
---|
[17097] | 52 | protected TSPTranslocationMoveDistanceMatrixEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[13193] | 53 | protected TSPTranslocationMoveDistanceMatrixEvaluator(TSPTranslocationMoveDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 54 | public TSPTranslocationMoveDistanceMatrixEvaluator()
|
---|
| 55 | : base() {
|
---|
| 56 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
| 57 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
| 58 | Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 62 | return new TSPTranslocationMoveDistanceMatrixEvaluator(this, cloner);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IOperation Apply() {
|
---|
| 66 | var permutation = PermutationParameter.ActualValue;
|
---|
| 67 | double relativeQualityDifference = 0;
|
---|
| 68 | var distanceMatrix = DistanceMatrixParameter.ActualValue;
|
---|
| 69 | if (distanceMatrix == null) throw new InvalidOperationException("The distance matrix is not given.");
|
---|
| 70 | var move = TranslocationMoveParameter.ActualValue;
|
---|
| 71 | relativeQualityDifference = TSPTranslocationMovePathEvaluator.EvaluateByDistanceMatrix(permutation, move, distanceMatrix);
|
---|
| 72 |
|
---|
| 73 | var moveQuality = MoveQualityParameter.ActualValue;
|
---|
| 74 | if (moveQuality == null) MoveQualityParameter.ActualValue = new DoubleValue(QualityParameter.ActualValue.Value + relativeQualityDifference);
|
---|
| 75 | else moveQuality.Value = QualityParameter.ActualValue.Value + relativeQualityDifference;
|
---|
| 76 | return base.Apply();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|