public static float CalculateCompatibilityDistance(GeneComparsionResult result, float c1, float c2, float c3) { int matching = result.MatchingGenes.Count; int disjoint = result.DisjointGenes.Count; int excess = result.ExcessGenes.Count; float avgWeightDiff = matching == 0 ? 0 : result.WeightDifference / matching; return(CalculateCompatibilityDistance(c1, c2, c3, 1, avgWeightDiff, disjoint, excess)); }
public static GeneComparsionResult CompareGenomes(Genome genome1, Genome genome2) { int maxCon1 = genome1.ConnectionCollection.MaxInnovationId; int maxCon2 = genome2.ConnectionCollection.MaxInnovationId; var gen1Set = genome1.ConnectionCollection.Connections; var gen2Set = genome2.ConnectionCollection.Connections; var allConnectionKeys = GetConnectionKeys(genome1, genome2); GeneComparsionResult result = new GeneComparsionResult(genome1, genome2); foreach (var key in allConnectionKeys) { var i = key.Key; gen1Set.TryGetValue(key.Key, out var c1); gen2Set.TryGetValue(key.Key, out var c2); //if(c1 == null && c2 == null) // continue; if (c1 != null && c2 != null) { result.MatchingGenes.Add((genome1, c1, genome2, c2)); var con1 = c1; var con2 = c2; result.WeightDifference += System.Math.Abs(con1.Weight - con2.Weight); } else if (c1 == null && i < maxCon1) { result.DisjointGenes.Add((genome2, c2)); } else if (c1 != null && i < maxCon2) { result.DisjointGenes.Add((genome1, c1)); } else if (c1 == null && i > maxCon1) { result.ExcessGenes.Add((genome2, c2)); } else if (c1 != null && i > maxCon2) { result.ExcessGenes.Add((genome1, c1)); } } return(result); }