Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs @ 18242

Last change on this file since 18242 was 17248, checked in by abeham, 5 years ago

#2521: working on TSP refactoring

File size: 3.1 KB
Line 
1using HEAL.Attic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.PermutationEncoding;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9
10namespace HeuristicLab.Problems.TravelingSalesman {
11  [StorableType("17477ad2-2c84-4b02-b5ac-f35ef6cc667f")]
12  public interface ITSPMoveEvaluator : IOperator, ISingleObjectiveMoveEvaluator, IMoveOperator {
13    ILookupParameter<Permutation> TSPTourParameter { get; }
14    ILookupParameter<ITSPData> TSPDataParameter { get; }
15    ILookupParameter<DoubleValue> TourLengthParameter { get; }
16    ILookupParameter<DoubleValue> TourLengthWithMoveParameter { get; }
17  }
18
19  [Item("TSP Move Evaluator", "Base class for all move evaluators of the TSP.")]
20  [StorableType("44af3845-ccdf-4014-805a-5878c64f67f5")]
21  public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator {
22    [Storable] public ILookupParameter<Permutation> TSPTourParameter { get; private set; }
23    [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
24    [Storable] public ILookupParameter<DoubleValue> TourLengthParameter { get; private set; }
25    [Storable] public ILookupParameter<DoubleValue> TourLengthWithMoveParameter { get; private set; }
26
27    ILookupParameter<DoubleValue> ISingleObjectiveMoveEvaluator.QualityParameter => TourLengthParameter;
28    ILookupParameter<DoubleValue> ISingleObjectiveMoveEvaluator.MoveQualityParameter => TourLengthWithMoveParameter;
29
30    [StorableConstructor]
31    protected TSPMoveEvaluator(StorableConstructorFlag _) : base(_) { }
32    protected TSPMoveEvaluator(TSPMoveEvaluator original, Cloner cloner)
33      : base(original, cloner) {
34      TSPTourParameter = cloner.Clone(original.TSPTourParameter);
35      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
36      TourLengthParameter = cloner.Clone(original.TourLengthParameter);
37      TourLengthWithMoveParameter = cloner.Clone(original.TourLengthWithMoveParameter);
38    }
39    public TSPMoveEvaluator() {
40      Parameters.Add(TSPTourParameter = new LookupParameter<Permutation>("TSPTour", "The tour that describes a solution to the TSP."));
41      Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
42      Parameters.Add(TourLengthParameter = new LookupParameter<DoubleValue>("TourLength", "The length of a TSP tour."));
43      Parameters.Add(TourLengthWithMoveParameter = new LookupParameter<DoubleValue>("TourLengthWithMove", "The length of the TSP tour if the move was applied."));
44    }
45
46    public sealed override IOperation Apply() {
47      var tour = TSPTourParameter.ActualValue;
48      var tspData = TSPDataParameter.ActualValue;
49      var tourLength = TourLengthParameter.ActualValue.Value;
50      TourLengthWithMoveParameter.ActualValue = new DoubleValue(
51        CalculateTourLengthWithMove(tspData, tour, tourLength)
52      );
53      return base.Apply();
54    }
55
56    protected abstract double CalculateTourLengthWithMove(ITSPData tspData, Permutation tspTour, double tourLength);
57  }
58}
Note: See TracBrowser for help on using the repository browser.