#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Optimization.Operators { [Item("WeightedParentsQualityComparator", "Compares the quality against that of its parents (assumes the parents are subscopes to the child scope). This operator works with any number of subscopes > 0.")] [StorableClass] public class WeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparator { public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } public ILookupParameter LeftSideParameter { get { return (ILookupParameter)Parameters["LeftSide"]; } } public ILookupParameter> RightSideParameter { get { return (ILookupParameter>)Parameters["RightSide"]; } } public ILookupParameter ResultParameter { get { return (ILookupParameter)Parameters["Result"]; } } public ValueLookupParameter ComparisonFactorParameter { get { return (ValueLookupParameter)Parameters["ComparisonFactor"]; } } public WeightedParentsQualityComparator() : base() { Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, false otherwise")); Parameters.Add(new LookupParameter("LeftSide", "The quality of the child.")); Parameters.Add(new ScopeTreeLookupParameter("RightSide", "The qualities of the parents.")); Parameters.Add(new LookupParameter("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents.")); Parameters.Add(new ValueLookupParameter("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them.")); } public override IOperation Apply() { ItemArray rightQualities = RightSideParameter.ActualValue; if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found."); double compFact = ComparisonFactorParameter.ActualValue.Value; if (compFact < 0 || compFact > 1) throw new InvalidOperationException(Name + ": Comparison Factor is outside the range [0;1]"); bool maximization = MaximizationParameter.ActualValue.Value; double leftQuality = LeftSideParameter.ActualValue.Value; double threshold = 0; #region Calculate threshold if (rightQualities.Length == 2) { // this case will probably be used most often threshold = (maximization ? (Math.Max(rightQualities[0].Value, rightQualities[1].Value) * compFact + Math.Min(rightQualities[0].Value, rightQualities[1].Value) * (1 - compFact)) : (Math.Min(rightQualities[0].Value, rightQualities[1].Value) * compFact + Math.Max(rightQualities[0].Value, rightQualities[1].Value) * (1 - compFact))); } else if (rightQualities.Length == 1) { // case for just one parent threshold = rightQualities[0].Value; } else { // general case extended to 3 or more parents List sortedQualities = rightQualities.Select(x => x.Value).ToList(); sortedQualities.Sort(); double min = sortedQualities.First() * -1; // min is used to pull the qualities to the 0 line double sum = min * sortedQualities.Count; for (int i = 0; i < sortedQualities.Count - 1; i++) { sum += (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; // sum of the trapezoid } if (sum == 0) threshold = sortedQualities[0]; // all qualities are equal else { double area = sum * (maximization ? compFact : (1 - compFact)); // the qualities are sorted ascending so in minimization a high comp factor (close to 1) means small area sum = 0; for (int i = 0; i < sortedQualities.Count - 1; i++) { double currentArea = (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; if (min + currentArea == 0) continue; // skip the first few consecutive 0s sum += min + currentArea; if (sum >= area) { double factor = 1 - ((sum - area) / (min + currentArea)); threshold = sortedQualities[i] + (sortedQualities[i + 1] - sortedQualities[i]) * factor; break; } } } } #endregion bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold; BoolValue resultValue = ResultParameter.ActualValue; if (resultValue == null) { ResultParameter.ActualValue = new BoolValue(result); } else { resultValue.Value = result; } return base.Apply(); } } }