Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/19 21:55:35 (5 years ago)
Author:
abeham
Message:

#2521: worked on refactoring TSP

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
Files:
2 added
11 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs

    r17226 r17241  
    1 #region License Information
    2 /* HeuristicLab
    3  * Copyright (C) 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 
    22 using System;
     1using HEAL.Attic;
    232using HeuristicLab.Common;
    243using HeuristicLab.Core;
    254using HeuristicLab.Data;
     5using HeuristicLab.Encodings.PermutationEncoding;
    266using HeuristicLab.Operators;
    27 using HeuristicLab.Optimization;
    287using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    308
    319namespace HeuristicLab.Problems.TravelingSalesman {
    32   /// <summary>
    33   /// A base class for operators which evaluate TSP solutions.
    34   /// </summary>
    35   [Item("TSPMoveEvaluator", "A base class for operators which evaluate TSP moves.")]
    36   [StorableType("25573174-DE4B-4076-B439-CCB5F01CE652")]
    37   public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator, IMoveOperator {
     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  }
    3817
    39     public abstract Type EvaluatorType { get; }
    40     public override bool CanChangeName {
    41       get { return false; }
    42     }
    43 
    44     public ILookupParameter<DoubleValue> QualityParameter {
    45       get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    46     }
    47     public ILookupParameter<DoubleValue> MoveQualityParameter {
    48       get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; }
    49     }
     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; }
    5025
    5126    [StorableConstructor]
    5227    protected TSPMoveEvaluator(StorableConstructorFlag _) : base(_) { }
    53     protected TSPMoveEvaluator(TSPMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
    54     protected TSPMoveEvaluator()
    55       : base() {
    56       Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a TSP solution."));
    57       Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a TSP solution."));
     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);
    5834    }
     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);
    5953  }
    6054}
Note: See TracChangeset for help on using the changeset viewer.