#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using HeuristicLab.Data;
using HeuristicLab.Encodings.PermutationEncoding;
namespace HeuristicLab.Problems.QuadraticAssignment {
public static class QAPPermutationProximityCalculator {
public static double CalculateGenotypeSimilarity(Permutation a, Permutation b) {
int similar = 0;
for (int i = 0; i < a.Length; i++) {
if (a[i] == b[i]) similar++;
}
return similar / (double)a.Length;
}
public static double CalculateGenotypeDistance(Permutation a, Permutation b) {
return 1.0 - CalculateGenotypeSimilarity(a, b);
}
public static double CalculatePhenotypeSimilarity(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
return 1.0 - CalculatePhenotypeDistance(a, b, weights, distances);
}
public static double CalculatePhenotypeDistance(Permutation a, Permutation b, DoubleMatrix weights, DoubleMatrix distances) {
Dictionary alleles = new Dictionary();
int distance = 0;
for (int x = 0; x < a.Length; x++) {
for (int y = 0; y < a.Length; y++) {
string alleleA = weights[x, y].ToString() + ">" + distances[a[x], a[y]].ToString();
string alleleB = weights[x, y].ToString() + ">" + distances[b[x], b[y]].ToString();
if (alleleA == alleleB) continue;
int countA = 1, countB = -1;
if (alleles.ContainsKey(alleleA)) countA += alleles[alleleA];
if (alleles.ContainsKey(alleleB)) countB += alleles[alleleB];
if (countA <= 0) distance--; // we've found in A an allele that was present in B
else distance++; // we've found in A a new allele
alleles[alleleA] = countA;
if (countB >= 0) distance--; // we've found in B an allele that was present in A
else distance++; // we've found in B a new allele
alleles[alleleB] = countB;
}
}
return distance / (double)(2 * a.Length * a.Length);
}
}
}