Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMoveDistanceMatrixEvaluator.cs @ 16687

Last change on this file since 16687 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.8 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.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// An operator to evaluate inversion moves (2-opt).
33  /// </summary>
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.")]
35  [StorableClass]
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    }
47    public ILookupParameter<InversionMove> InversionMoveParameter {
48      get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; }
49    }
50
51    [StorableConstructor]
52    protected TSPInversionMoveDistanceMatrixEvaluator(bool deserializing) : base(deserializing) { }
53    protected TSPInversionMoveDistanceMatrixEvaluator(TSPInversionMoveDistanceMatrixEvaluator original, Cloner cloner) : base(original, cloner) { }
54    public TSPInversionMoveDistanceMatrixEvaluator()
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<InversionMove>("InversionMove", "The move to evaluate."));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new TSPInversionMoveDistanceMatrixEvaluator(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 = InversionMoveParameter.ActualValue;
71      relativeQualityDifference = TSPInversionMovePathEvaluator.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}
Note: See TracBrowser for help on using the repository browser.