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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs

    r17226 r17241  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    2829using HeuristicLab.Optimization;
    2930using HeuristicLab.Parameters;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.TravelingSalesman {
     
    3838  /// </remarks>
    3939  [Item("TSPImprovementOperator", "An operator that improves traveling salesman solutions. The operator tries to improve the traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")]
    40   [StorableType("9C3B53A4-8FE7-45FC-833B-FF3DAE578010")]
     40  [StorableType("1b3cbc66-6dcc-4f61-9cbe-8b50cc54413a")]
    4141  public sealed class TSPImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator {
    42     #region Parameter properties
    43     public ScopeParameter CurrentScopeParameter {
    44       get { return (ScopeParameter)Parameters["CurrentScope"]; }
     42   
     43    [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
     44    [Storable] public IValueParameter<IntValue> ImprovementAttemptsParameter { get; private set; }
     45    [Storable] public ILookupParameter<IRandom> RandomParameter { get; private set; }
     46    [Storable] public IValueLookupParameter<IItem> SolutionParameter { get; private set; }
     47
     48    public int ImprovementAttempts {
     49      get { return ImprovementAttemptsParameter.Value.Value; }
     50      set { ImprovementAttemptsParameter.Value.Value = value; }
    4551    }
    46     public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
    47       get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
    48     }
    49     public IValueParameter<IntValue> ImprovementAttemptsParameter {
    50       get { return (IValueParameter<IntValue>)Parameters["ImprovementAttempts"]; }
    51     }
    52     public ILookupParameter<IRandom> RandomParameter {
    53       get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    54     }
    55     public IValueLookupParameter<IItem> SolutionParameter {
    56       get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }
    57     }
    58     #endregion
    59 
    60     #region Properties
    61     public IScope CurrentScope {
    62       get { return CurrentScopeParameter.ActualValue; }
    63     }
    64     public DistanceMatrix DistanceMatrix {
    65       get { return DistanceMatrixParameter.ActualValue; }
    66       set { DistanceMatrixParameter.ActualValue = value; }
    67     }
    68     public IntValue ImprovementAttempts {
    69       get { return ImprovementAttemptsParameter.Value; }
    70       set { ImprovementAttemptsParameter.Value = value; }
    71     }
    72     public IRandom Random {
    73       get { return RandomParameter.ActualValue; }
    74       set { RandomParameter.ActualValue = value; }
    75     }
    76     #endregion
    7752
    7853    [StorableConstructor]
    7954    private TSPImprovementOperator(StorableConstructorFlag _) : base(_) { }
    80     private TSPImprovementOperator(TSPImprovementOperator original, Cloner cloner) : base(original, cloner) { }
     55    private TSPImprovementOperator(TSPImprovementOperator original, Cloner cloner)
     56      : base(original, cloner) {
     57      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
     58      ImprovementAttemptsParameter = cloner.Clone(original.ImprovementAttemptsParameter);
     59      RandomParameter = cloner.Clone(original.RandomParameter);
     60      SolutionParameter = cloner.Clone(original.SolutionParameter);
     61    }
    8162    public TSPImprovementOperator()
    8263      : base() {
    8364      #region Create parameters
    84       Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved."));
    85       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    86       Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
    87       Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
    88       Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
     65      Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
     66      Parameters.Add(ImprovementAttemptsParameter = new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100)));
     67      Parameters.Add(RandomParameter = new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
     68      Parameters.Add(SolutionParameter = new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only."));
    8969      #endregion
    9070    }
     
    9575
    9676    public override IOperation Apply() {
    97       var solution = CurrentScope.Variables[SolutionParameter.ActualName].Value as Permutation;
     77      var random = RandomParameter.ActualValue;
     78      var solution = ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value as Permutation;
    9879      if (solution == null)
    9980        throw new ArgumentException("Cannot improve solution because it has the wrong type.");
    10081      if (solution.PermutationType != PermutationTypes.RelativeUndirected)
    10182        throw new ArgumentException("Cannot improve solution because the permutation type is not supported.");
     83      var tspData = TSPDataParameter.ActualValue;
    10284
    103       for (int i = 0; i < ImprovementAttempts.Value; i++) {
    104         var move = StochasticInversionSingleMoveGenerator.Apply(solution, Random);
    105         double moveQualtiy = TSPInversionMovePathEvaluator.EvaluateByDistanceMatrix(solution, move, DistanceMatrix);
     85      for (int i = 0; i < ImprovementAttempts; i++) {
     86        var move = StochasticInversionSingleMoveGenerator.Apply(solution, random);
     87        double moveQualtiy = TSPInversionMoveEvaluator.CalculateTourLengthDelta(tspData, solution, move);
    10688        if (moveQualtiy < 0)
    10789          InversionManipulator.Apply(solution, move.Index1, move.Index2);
    10890      }
    10991
    110       CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));
     92      ExecutionContext.Scope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(ImprovementAttempts)));
    11193
    11294      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.