Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: worked on refactoring TSP

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