Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/QAPPermutationFitnessDistanceCorrelationAnalyzer.cs @ 11589

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

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

File size: 4.7 KB
RevLine 
[7128]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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis.FitnessLandscape {
32
33  [Item("QAPPermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")]
34  [StorableClass]
35  public class QAPPermutationFitnessDistanceCorrelationAnalyzer : FitnessDistanceCorrelationAnalyzer, IPermutationOperator {
36
37    #region Parameters
38    public ScopeTreeLookupParameter<Permutation> PermutationParameter {
39      get { return (ScopeTreeLookupParameter<Permutation>)Parameters["Permutation"]; }
40    }
41    public LookupParameter<Permutation> BestKnownSolution {
42      get { return (LookupParameter<Permutation>)Parameters["BestKnownSolution"]; }
43    }
44    public ILookupParameter<DoubleMatrix> WeightsParameter {
45      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
46    }
47    public ILookupParameter<DoubleMatrix> DistancesParameter {
48      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    protected QAPPermutationFitnessDistanceCorrelationAnalyzer(bool deserializing) : base(deserializing) { }
54    protected QAPPermutationFitnessDistanceCorrelationAnalyzer(QAPPermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
55
56    public QAPPermutationFitnessDistanceCorrelationAnalyzer() {
57      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The permutation encoded solution"));
58      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution"));
59      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
60      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new QAPPermutationFitnessDistanceCorrelationAnalyzer(this, cloner);
65    }
66
67    public static double Distance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
68      Dictionary<string, int> alleles = new Dictionary<string, int>(a.Length * a.Length);
69      int distance = 0;
70      for (int x = 0; x < a.Length; x++) {
71        for (int y = 0; y < a.Length; y++) {
72          string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString();
73          string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString();
74          if (alleleA == alleleB) continue;
75
76          int countA = 1, countB = -1;
77          if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA];
78          if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB];
79
80          if (countA <= 0) distance--; // we've found in A an allele that was present in B
81          else distance++; // we've found in A a new allele
82          alleles[alleleA] = countA;
83
84          if (countB >= 0) distance--; // we've found in B an allele that was present in A
85          else distance++; // we've found in B a new allele
86          alleles[alleleB] = countB;
87        }
88      }
89      return distance;
90    }
91
92    protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
93      if (PermutationParameter.ActualName == null)
94        return new double[0];
95      Permutation bestKnownValue = BestKnownSolution.ActualValue;
96      if (bestKnownValue == null)
97        return PermutationParameter.ActualValue.Select(v => 0d);
98      return PermutationParameter.ActualValue.Select(v => Distance(v, bestKnownValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue));
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.