Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/FDC/QAPPermutationFitnessDistanceCorrelationAnalyzer.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.7 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.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;
30using HEAL.Attic;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33
34  [Item("QAPPermutationFitnessDistanceCorrelationAnalyzer", "An operator that analyzes the correlation between fitness and distance to the best know solution for permutation encoding")]
35  [StorableType("31823F7B-55FB-4D57-80EE-89F4D90B6412")]
36  public class QAPPermutationFitnessDistanceCorrelationAnalyzer : 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    public ILookupParameter<DoubleMatrix> WeightsParameter {
46      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
47    }
48    public ILookupParameter<DoubleMatrix> DistancesParameter {
49      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    protected QAPPermutationFitnessDistanceCorrelationAnalyzer(StorableConstructorFlag _) : base(_) { }
55    protected QAPPermutationFitnessDistanceCorrelationAnalyzer(QAPPermutationFitnessDistanceCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
56
57    public QAPPermutationFitnessDistanceCorrelationAnalyzer() {
58      Parameters.Add(new ScopeTreeLookupParameter<Permutation>("Permutation", "The permutation encoded solution"));
59      Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The best known solution"));
60      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
61      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new QAPPermutationFitnessDistanceCorrelationAnalyzer(this, cloner);
66    }
67
68    public static double Distance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
69      Dictionary<string, int> alleles = new Dictionary<string, int>(a.Length * a.Length);
70      int distance = 0;
71      for (int x = 0; x < a.Length; x++) {
72        for (int y = 0; y < a.Length; y++) {
73          string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString();
74          string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString();
75          if (alleleA == alleleB) continue;
76
77          int countA = 1, countB = -1;
78          if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA];
79          if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB];
80
81          if (countA <= 0) distance--; // we've found in A an allele that was present in B
82          else distance++; // we've found in A a new allele
83          alleles[alleleA] = countA;
84
85          if (countB >= 0) distance--; // we've found in B an allele that was present in A
86          else distance++; // we've found in B a new allele
87          alleles[alleleB] = countB;
88        }
89      }
90      return distance;
91    }
92
93    protected override IEnumerable<double> GetDistancesToBestKnownSolution() {
94      if (PermutationParameter.ActualName == null)
95        return new double[0];
96      Permutation bestKnownValue = BestKnownSolution.ActualValue;
97      if (bestKnownValue == null)
98        return PermutationParameter.ActualValue.Select(v => 0d);
99      return PermutationParameter.ActualValue.Select(v => Distance(v, bestKnownValue, WeightsParameter.ActualValue, DistancesParameter.ActualValue));
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.