Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/PermutationFitnessDistanceCorrelationAnalyzer.cs @ 16573

Last change on this file since 16573 was 16573, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HEAL.Attic;
32
33namespace HeuristicLab.Analysis.FitnessLandscape {
34
35  [Item("PermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")]
36  [StorableType("2021A1AA-FE98-4D42-8730-23C5EA552B21")]
37  public class PermutationFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IPermutationOperator {
38
39    #region Parameters
40    public ScopeTreeLookupParameter<Permutation> PermutationParameter {
41      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
42    }
43    public LookupParameter<Permutation> BestKnownSolution {
44      get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
45    }
46    #endregion
47
48    [StorableConstructor]
49    protected PermutationFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
50    protected PermutationFitnessDistanceCorrelationAnalyzer(PermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
51
52    public PermutationFitnessDistanceCorrelationAnalyzer() {
53      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The permutation encoded solution"));
54      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution"));
55    }
56
57    public override IDeepCloneable Clone(Cloner cloner) {
58      return new PermutationFitnessDistanceCorrelationAnalyzer(this, cloner);
59    }
60
61    private static Point GetEdge(Permutation a, int index) {
62      switch (a.PermutationType) {
63        case PermutationTypes.RelativeDirected:
64          return new Point(a[index], a[(index + 1) % a.Length]);
65        case PermutationTypes.RelativeUndirected:
66          return new Point(
67            Math.Min(a[index], a[(index + 1) % a.Length]),
68            Math.Max(a[index], a[(index + 1) % a.Length]));
69        default:
70          throw new ArgumentException("cannot derive edge from non-relative permutation type");
71      }
72    }
73
74    public static double Distance(Permutation a, Permutation b) {
75      if (a.PermutationType != b.PermutationType)
76        throw new ArgumentException(
77          "Cannot calculate distance between different permuatation types: " +
78          a.PermutationType + " and " + b.PermutationType);
79      if (a.PermutationType == PermutationTypes.Absolute) {
80        int nEqual = 0;
81        for (int i = 0; i < Math.Min(a.Length, b.Length); i++) {
82          if (a[i] == b[i])
83            nEqual++;
84        }
85        return Math.Max(a.Length, b.Length) - nEqual;
86      } else {
87        HashSet<Point> edges = new HashSet<Point>();
88        for (int i = 0; i < a.Length; i++)
89          edges.Add(GetEdge(a, i));
90        int nCommonEdges = 0;
91        for (int i = 0; i < b.Length; i++) {
92          if (edges.Contains(GetEdge(b, i)))
93            nCommonEdges++;
94        }
95        return Math.Max(a.Length, b.Length) - nCommonEdges;
96      }
97    }
98
99    protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
100      if (PermutationParameter.ActualName == null)
101        return new double[0];
102      Permutation bestKnownValue = BestKnownSolution.ActualValue;
103      if (bestKnownValue == null)
104        return PermutationParameter.ActualValue.Select(v => 0d);
105      return PermutationParameter.ActualValue.Select(v => Distance(v, bestKnownValue));
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.