[6416] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6416] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 25 |
|
---|
| 26 | namespace 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) {
|
---|
[6627] | 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;
|
---|
[6416] | 53 |
|
---|
[6627] | 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 |
|
---|
[6416] | 60 | int countA = 1, countB = -1;
|
---|
| 61 |
|
---|
[6627] | 62 | if (dAlleles.ContainsKey(dA)) countA += dAlleles[dA];
|
---|
| 63 | if (dAlleles.ContainsKey(dB)) countB += dAlleles[dB];
|
---|
| 64 |
|
---|
[6416] | 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
|
---|
[6627] | 67 | dAlleles[dA] = countA;
|
---|
[6416] | 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
|
---|
[6627] | 71 | dAlleles[dB] = countB;
|
---|
[6416] | 72 | }
|
---|
| 73 | }
|
---|
[6627] | 74 | return distance / (double)(2 * len * len);
|
---|
[6416] | 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|