using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("InformationAnalysis Aggregator", "Aggregates information anlyses.")] [StorableType("30A0DDD6-CF94-4061-95C2-5717617EB7C1")] public class InformationAnalysisAggregator : Aggregator { [StorableConstructor] protected InformationAnalysisAggregator(StorableConstructorFlag _) : base(_) { } protected InformationAnalysisAggregator(InformationAnalysisAggregator original, Cloner cloner) : base(original, cloner) { } public InformationAnalysisAggregator() { } public override IDeepCloneable Clone(Cloner cloner) { return new InformationAnalysisAggregator(this, cloner); } public override IResult CreateResult() { return new Result("Information Analysis Summary", Aggregate(items, 10)); } private class InformationAnalysis : IComparable { public double Delta { get; private set; } public double InformationContent { get; private set; } public double PartialInformationContent { get; private set; } public double DensityBasinInformation { get; private set; } public InformationAnalysis(double delta, double informationContent, double partialInformationContent, double densityBasinInformation) { Delta = delta; InformationContent = informationContent; PartialInformationContent = partialInformationContent; DensityBasinInformation = densityBasinInformation; } public int CompareTo(InformationAnalysis ia) { return Delta.CompareTo(ia.Delta); } } public static DataTable Aggregate(List informationAnalysisTables, int nQuantiles) { var values = new List(); foreach (var table in informationAnalysisTables) { List deltas = table.Rows["Quality Delta"].Values.ToList(); for (int i = 0; i < deltas.Count; i++) { values.Add(new InformationAnalysis( deltas[i], table.Rows["Information Content"].Values[i], table.Rows["Partial Information Content"].Values[i], table.Rows["Density Basin Information"].Values[i])); } } values.Sort(); Rows informationContent = new Rows("Information Content", Color.Red); Rows partialInformationContent = new Rows("Partial Information Content", Color.FromArgb(255, 0, 255)); Rows densityBasinInformation = new Rows("Density Basin Information", Color.FromArgb(0, 255, 0)); Rows qualityDelta = new Rows("Quality Delta", Color.Blue, true); var aggregatedValues = new List(); var valueIt = values.GetEnumerator(); int it = 0; for (int q = 0; q < nQuantiles; q++) { aggregatedValues.Clear(); for (; it < values.Count * (q + 1) / nQuantiles && valueIt.MoveNext(); it++) aggregatedValues.Add(valueIt.Current); if (aggregatedValues.Count == 0) break; informationContent.AppendValues(aggregatedValues.Select(v => v.InformationContent)); partialInformationContent.AppendValues(aggregatedValues.Select(v => v.PartialInformationContent)); densityBasinInformation.AppendValues(aggregatedValues.Select(v => v.DensityBasinInformation)); qualityDelta.AppendValues(aggregatedValues.Select(v => v.Delta)); } DataTable summaryTable = new DataTable("Information Analysis Summary"); informationContent.Incorporate(summaryTable); partialInformationContent.Incorporate(summaryTable); densityBasinInformation.Incorporate(summaryTable); qualityDelta.Incorporate(summaryTable); return summaryTable; } private class Rows { private DataRow MinRow, AvgRow, MaxRow; public Rows(string name, Color baseColor) { MaxRow = new DataRow(name + " Max"); AvgRow = new DataRow(name + " Avg"); MinRow = new DataRow(name + " Min"); MaxRow.VisualProperties.Color = baseColor; AvgRow.VisualProperties.Color = Color.FromArgb( (int)Math.Round(baseColor.R * 0.75), (int)Math.Round(baseColor.G * 0.75), (int)Math.Round(baseColor.B * 0.75)); MinRow.VisualProperties.Color = Color.FromArgb( (int)Math.Round(baseColor.R * 0.5), (int)Math.Round(baseColor.G * 0.5), (int)Math.Round(baseColor.B * 0.5)); } public Rows(string name, Color baseColor, bool secondYAxis) : this(name, baseColor) { MinRow.VisualProperties.SecondYAxis = secondYAxis; AvgRow.VisualProperties.SecondYAxis = secondYAxis; MaxRow.VisualProperties.SecondYAxis = secondYAxis; } public void AppendValues(IEnumerable values) { MinRow.Values.Add(values.Min()); AvgRow.Values.Add(values.Average()); MaxRow.Values.Add(values.Max()); } public void Incorporate(DataTable table) { table.Rows.Add(MinRow); table.Rows.Add(AvgRow); table.Rows.Add(MaxRow); } } } }