Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMoveDistanceMatrixEvaluator.cs @ 17398

Last change on this file since 17398 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.7 KB
RevLine 
[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]22using System;
[4722]23using HeuristicLab.Common;
[3209]24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[3209]29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
[3232]32  /// An operator to evaluate inversion moves (2-opt).
[3209]33  /// </summary>
[12978]34  [Item("TSPInversionMoveDistanceMatrixEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")]
[16565]35  [StorableType("1EA2C5B2-96E0-4C64-9089-F08F6D2B1CAE")]
[12978]36  public class TSPInversionMoveDistanceMatrixEvaluator : TSPMoveEvaluator, IPermutationInversionMoveOperator {
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    }
[3232]47    public ILookupParameter<InversionMove> InversionMoveParameter {
48      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
[3209]49    }
50
[4722]51    [StorableConstructor]
[16565]52    protected TSPInversionMoveDistanceMatrixEvaluator(StorableConstructorFlag _) : base(_) { }
[12978]53    protected TSPInversionMoveDistanceMatrixEvaluator(TSPInversionMoveDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }
54    public TSPInversionMoveDistanceMatrixEvaluator()
[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."));
[3232]58      Parameters.Add(new LookupParameter<InversionMove>("InversionMove", "The move to evaluate."));
[3209]59    }
60
[12978]61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new TSPInversionMoveDistanceMatrixEvaluator(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 = InversionMoveParameter.ActualValue;
71      relativeQualityDifference = TSPInversionMovePathEvaluator.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}
Note: See TracBrowser for help on using the repository browser.