[3209] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3209] | 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 |
|
---|
[12978] | 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[3209] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[3209] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.TravelingSalesman {
|
---|
| 31 | /// <summary>
|
---|
[3232] | 32 | /// An operator to evaluate inversion moves (2-opt).
|
---|
[3209] | 33 | /// </summary>
|
---|
[12978] | 34 | [Item("TSPTranslocationMoveDistanceMatrixEvaluator", "Evaluates a translocation or insertion move (3-opt).")]
|
---|
[16565] | 35 | [StorableType("895EF1F3-6FE0-43C0-A6D4-7EEAB7B06841")]
|
---|
[12978] | 36 | public class TSPTranslocationMoveDistanceMatrixEvaluator : TSPMoveEvaluator, IPermutationTranslocationMoveOperator {
|
---|
| 37 | public override Type EvaluatorType {
|
---|
| 38 | get { return typeof(TSPDistanceMatrixEvaluator); }
|
---|
[3209] | 39 | }
|
---|
| 40 |
|
---|
[12978] | 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 |
|
---|
[4722] | 51 | [StorableConstructor]
|
---|
[16565] | 52 | protected TSPTranslocationMoveDistanceMatrixEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[12978] | 53 | protected TSPTranslocationMoveDistanceMatrixEvaluator(TSPTranslocationMoveDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 54 | public TSPTranslocationMoveDistanceMatrixEvaluator()
|
---|
[3209] | 55 | : base() {
|
---|
[12978] | 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."));
|
---|
[3209] | 59 | }
|
---|
| 60 |
|
---|
[12978] | 61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 62 | return new TSPTranslocationMoveDistanceMatrixEvaluator(this, cloner);
|
---|
[3209] | 63 | }
|
---|
| 64 |
|
---|
[12978] | 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);
|
---|
[7743] | 72 |
|
---|
[12978] | 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();
|
---|
[7743] | 77 | }
|
---|
[3209] | 78 | }
|
---|
| 79 | }
|
---|