Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.QuadraticAssignment/3.3/QAPPermutationProximityCalculator.cs @ 14559

Last change on this file since 14559 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Data;
24using HeuristicLab.Encodings.PermutationEncoding;
25
26namespace HeuristicLab.Problems.QuadraticAssignment {
27  public static class QAPPermutationProximityCalculator {
28
29    public static double CalculateGenotypeSimilarity(Permutation a, Permutation b) {
30      int similar = 0;
31      for (int i = 0; i < a.Length; i++) {
32        if (a[i] == b[i]) similar++;
33      }
34      return similar / (double)a.Length;
35    }
36
37    public static double CalculateGenotypeDistance(Permutation a, Permutation b) {
38      return 1.0 - CalculateGenotypeSimilarity(a, b);
39    }
40
41    public static double CalculatePhenotypeSimilarity(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
42      return 1.0 - CalculatePhenotypeDistance(a, b, weights, distances);
43    }
44
45    public static double CalculatePhenotypeDistance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
46      Dictionary<double, Dictionary<double, int>> alleles = new Dictionary<double, Dictionary<double, int>>();
47      int distance = 0, len = a.Length;
48      for (int x = 0; x < len; x++) {
49        for (int y = 0; y < len; y++) {
50          // there's a limited universe of double values as they're all drawn from the same matrix
51          double dA = distances[a[x], a[y]], dB = distances[b[x], b[y]];
52          if (dA == dB) continue;
53
54          Dictionary<double, int> dAlleles;
55          if (!alleles.ContainsKey(weights[x, y])) {
56            dAlleles = new Dictionary<double, int>();
57            alleles.Add(weights[x, y], dAlleles);
58          } else dAlleles = alleles[weights[x, y]];
59
60          int countA = 1, countB = -1;
61
62          if (dAlleles.ContainsKey(dA)) countA += dAlleles[dA];
63          if (dAlleles.ContainsKey(dB)) countB += dAlleles[dB];
64
65          if (countA <= 0) distance--; // we've found in A an allele that was present in B
66          else distance++; // we've found in A a new allele
67          dAlleles[dA] = countA;
68
69          if (countB >= 0) distance--; // we've found in B an allele that was present in A
70          else distance++; // we've found in B a new allele
71          dAlleles[dB] = countB;
72        }
73      }
74      return distance / (double)(2 * len * len);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.