1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
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) {
|
---|
46 | Dictionary<string, int> alleles = new Dictionary<string, int>();
|
---|
47 | int distance = 0;
|
---|
48 | for (int x = 0; x < a.Length; x++) {
|
---|
49 | for (int y = 0; y < a.Length; y++) {
|
---|
50 | string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString();
|
---|
51 | string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString();
|
---|
52 | if (alleleA == alleleB) continue;
|
---|
53 |
|
---|
54 | int countA = 1, countB = -1;
|
---|
55 | if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA];
|
---|
56 | if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB];
|
---|
57 |
|
---|
58 | if (countA <= 0) distance--; // we've found in A an allele that was present in B
|
---|
59 | else distance++; // we've found in A a new allele
|
---|
60 | alleles[alleleA] = countA;
|
---|
61 |
|
---|
62 | if (countB >= 0) distance--; // we've found in B an allele that was present in A
|
---|
63 | else distance++; // we've found in B a new allele
|
---|
64 | alleles[alleleB] = countB;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return distance / (double)(2 * a.Length * a.Length);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|