using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Data; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("InformationAnalysis Peak Aggregator", "Aggregates peak information anlyses.")] [StorableType("BDB8568B-EBF5-437C-81C2-C68F77C3FCEF")] public class InformationAnalysisPeakAggregator : Aggregator { [StorableConstructor] protected InformationAnalysisPeakAggregator(StorableConstructorFlag _) : base(_) { } protected InformationAnalysisPeakAggregator(InformationAnalysisPeakAggregator original, Cloner cloner) : base(original, cloner) { } public InformationAnalysisPeakAggregator() { } public override IDeepCloneable Clone(Cloner cloner) { return new InformationAnalysisPeakAggregator(this, cloner); } public override IResult CreateResult() { return new Result("Inf. Peak", Aggregate(items, 10)); } public static ResultCollection Aggregate(List informationAnalysisTables, int nQuantiles) { var informationContent = new List(); var partialInformationContent = new List(); var densityBasinInformation = new List(); foreach (var table in informationAnalysisTables) { List deltas = table.Rows["Quality Delta"].Values.ToList(); informationContent.Add(GetPeak(deltas, table.Rows["Information Content"].Values)); partialInformationContent.Add(GetPeak(deltas, table.Rows["Partial Information Content"].Values)); densityBasinInformation.Add(GetPeak(deltas, table.Rows["Density Basin Information"].Values)); } var results = new ResultCollection(); results.AddRange(CreateSummary("Q Delt", "Inf Cont", informationContent, true)); results.AddRange(CreateSummary("Q Delt", "Part Inf Cont", partialInformationContent, false)); results.AddRange(CreateSummary("Q Delt", "Dens Bas Inf", densityBasinInformation, false)); return results; } private static PointD GetPeak(IEnumerable index, IEnumerable value) { var max = index.Zip(value, (i, v) => new { i, v }).OrderByDescending(p => p.v).First(); return new PointD(max.i, max.v); } private class PointD : IComparable { public double X; public double Y; public PointD(double x, double y) { X = x; Y = y; } public int CompareTo(PointD other) { return X.CompareTo(other.X); } } private static IEnumerable CreateSummary(string xName, string yName, IEnumerable values, bool showX) { var xAnalyzer = new DistributionAnalyzer(values.Select(p => p.X)); var yAnalyzer = new DistributionAnalyzer(values.Select(p => p.Y)); if (showX) { yield return new Result(xName + " Avg", new DoubleValue(xAnalyzer.Mean)); yield return new Result(xName + " Quart-Spread", new DoubleValue(xAnalyzer[0.75] - xAnalyzer[0.25])); yield return new Result(xName + " 90%-Spread", new DoubleValue(xAnalyzer[0.95] - xAnalyzer[0.05])); yield return new Result(xName + " Range", new DoubleValue(xAnalyzer[1] - xAnalyzer[0])); } yield return new Result(yName + " Avg", new DoubleValue(yAnalyzer.Mean)); yield return new Result(yName + " Quart-Spread", new DoubleValue(yAnalyzer[0.75] - yAnalyzer[0.25])); yield return new Result(yName + " 90%-Spread", new DoubleValue(yAnalyzer[0.95] - yAnalyzer[0.05])); yield return new Result(yName + " Range", new DoubleValue(yAnalyzer[1] - yAnalyzer[0])); yield return new Result(string.Format("{0}/{1} Quart-Spread", xName, yName), new DoubleValue( Math.Sqrt(sqr(xAnalyzer[0.75] - xAnalyzer[0.25]) + sqr(yAnalyzer[0.75] - yAnalyzer[0.25])))); yield return new Result(string.Format("{0}/{1} 90%-Spread", xName, yName), new DoubleValue( Math.Sqrt(sqr(xAnalyzer[0.95] - xAnalyzer[0.05]) + sqr(yAnalyzer[0.75] - yAnalyzer[0.25])))); yield return new Result(string.Format("{0}/{1} Range", xName, yName), new DoubleValue( Math.Sqrt(sqr(xAnalyzer[1] - xAnalyzer[0]) + sqr(yAnalyzer[1] - yAnalyzer[0])))); } private static double sqr(double x) { return x * x; } } }