Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/PermutationFitnessDistanceCorrelationAnalyzer.cs @ 7128

Last change on this file since 7128 was 7128, checked in by epitzer, 12 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

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