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/Analyzers/TSPAlleleFrequencyAnalyzer.cs

    r17226 r17241  
    2020#endregion
    2121
    22 using System;
     22using HEAL.Attic;
    2323using HeuristicLab.Analysis;
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Data;
    2726using HeuristicLab.Encodings.PermutationEncoding;
    2827using HeuristicLab.Parameters;
    29 using HEAL.Attic;
    3028
    3129namespace HeuristicLab.Problems.TravelingSalesman {
     
    3432  /// </summary>
    3533  [Item("TSPAlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles in solutions of Traveling Salesman Problems given in path representation.")]
    36   [StorableType("C1BBEC5A-27EF-4882-AEC6-0919FC2EF1DB")]
     34  [StorableType("43a9ec34-c917-43f8-bd14-4d42e2d0a458")]
    3735  public sealed class TSPAlleleFrequencyAnalyzer : AlleleFrequencyAnalyzer<Permutation> {
    38     public LookupParameter<DoubleMatrix> CoordinatesParameter {
    39       get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
    40     }
    41     public LookupParameter<DistanceMatrix> DistanceMatrixParameter {
    42       get { return (LookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
    43     }
     36    [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
    4437
    4538    [StorableConstructor]
    4639    private TSPAlleleFrequencyAnalyzer(StorableConstructorFlag _) : base(_) { }
    47     private TSPAlleleFrequencyAnalyzer(TSPAlleleFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { }
     40    private TSPAlleleFrequencyAnalyzer(TSPAlleleFrequencyAnalyzer original, Cloner cloner)
     41      : base(original, cloner) {
     42      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
     43    }
    4844    public TSPAlleleFrequencyAnalyzer()
    4945      : base() {
    50       Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-coordinates of the cities."));
    51       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     46      Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
    5247    }
    5348
     
    5752
    5853    protected override Allele[] CalculateAlleles(Permutation solution) {
     54      var tspData = TSPDataParameter.ActualValue;
    5955      Allele[] alleles = new Allele[solution.Length];
    60       DoubleMatrix coords = CoordinatesParameter.ActualValue;
    61       DistanceMatrix dm = DistanceMatrixParameter.ActualValue;
    62       if (dm == null && coords == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");
    6356      int source, target, h;
    6457      double impact;
     
    6861        target = solution[i + 1];
    6962        if (source > target) { h = source; source = target; target = h; }
    70         impact = dm != null ? dm[source, target] : CalculateLength(coords[source, 0], coords[source, 1], coords[target, 0], coords[target, 1]);
     63        impact = tspData.GetDistance(source, target);
    7164        alleles[i] = new Allele(source.ToString() + "-" + target.ToString(), impact);
    7265      }
     
    7467      target = solution[0];
    7568      if (source > target) { h = source; source = target; target = h; }
    76       impact = dm != null ? dm[source, target] : CalculateLength(coords[source, 0], coords[source, 1], coords[target, 0], coords[target, 1]);
     69      impact = tspData.GetDistance(source, target);
    7770      alleles[alleles.Length - 1] = new Allele(source.ToString() + "-" + target.ToString(), impact);
    7871
    7972      return alleles;
    8073    }
    81 
    82     private double CalculateLength(double x1, double y1, double x2, double y2) {
    83       return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    84     }
    8574  }
    8675}
Note: See TracChangeset for help on using the changeset viewer.