Changeset 17241 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPAlleleFrequencyAnalyzer.cs
- Timestamp:
- 09/10/19 21:55:35 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPAlleleFrequencyAnalyzer.cs
r17226 r17241 20 20 #endregion 21 21 22 using System;22 using HEAL.Attic; 23 23 using HeuristicLab.Analysis; 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 26 using HeuristicLab.Encodings.PermutationEncoding; 28 27 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 28 31 29 namespace HeuristicLab.Problems.TravelingSalesman { … … 34 32 /// </summary> 35 33 [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")] 37 35 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; } 44 37 45 38 [StorableConstructor] 46 39 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 } 48 44 public TSPAlleleFrequencyAnalyzer() 49 45 : 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.")); 52 47 } 53 48 … … 57 52 58 53 protected override Allele[] CalculateAlleles(Permutation solution) { 54 var tspData = TSPDataParameter.ActualValue; 59 55 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.");63 56 int source, target, h; 64 57 double impact; … … 68 61 target = solution[i + 1]; 69 62 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); 71 64 alleles[i] = new Allele(source.ToString() + "-" + target.ToString(), impact); 72 65 } … … 74 67 target = solution[0]; 75 68 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); 77 70 alleles[alleles.Length - 1] = new Allele(source.ToString() + "-" + target.ToString(), impact); 78 71 79 72 return alleles; 80 73 } 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 }85 74 } 86 75 }
Note: See TracChangeset
for help on using the changeset viewer.