#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { /// /// Compares the quality against that of its parents. /// This is more or less the same code as in the WeightedParentsQualityComparator operator, but without the operator part /// public static class WeightedParentsQualityComparer { public static bool Compare(double leftQuality, double rightQuality1, double rightQuality2, double compFact, bool maximization) { return Compare(leftQuality, new double[] { rightQuality1, rightQuality2 }, compFact, maximization); } public static bool Compare(double leftQuality, IList rightQualities, double compFact, bool maximization) { if (rightQualities.Count < 1) throw new InvalidOperationException("No rightQualities found. "); double threshold = 0; if (rightQualities.Count == 2) { // this case will probably be used most often double minQuality = Math.Min(rightQualities[0], rightQualities[1]); double maxQuality = Math.Max(rightQualities[0], rightQualities[1]); if (maximization) threshold = minQuality + (maxQuality - minQuality) * compFact; else threshold = maxQuality - (maxQuality - minQuality) * compFact; } else if (rightQualities.Count == 1) { // case for just one parent threshold = rightQualities[0]; } else { // general case extended to 3 or more parents List sortedQualities = rightQualities.ToList(); sortedQualities.Sort(); double minimumQuality = sortedQualities.First(); double integral = 0; for (int i = 0; i < sortedQualities.Count - 1; i++) { integral += (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; // sum of the trapezoid } integral -= minimumQuality * sortedQualities.Count; if (integral == 0) threshold = sortedQualities[0]; // all qualities are equal else { double selectedArea = integral * (maximization ? compFact : (1 - compFact)); integral = 0; for (int i = 0; i < sortedQualities.Count - 1; i++) { double currentSliceArea = (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; double windowedSliceArea = currentSliceArea - minimumQuality; if (windowedSliceArea == 0) continue; integral += windowedSliceArea; if (integral >= selectedArea) { double factor = 1 - ((integral - selectedArea) / (windowedSliceArea)); threshold = sortedQualities[i] + (sortedQualities[i + 1] - sortedQualities[i]) * factor; break; } } } } bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold; return result; } } }