using System.Collections.Generic; using System.Linq; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Common; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("Auto Correlation Aggregator", "Aggregates auto correlation analyses.")] [StorableType("969A27FA-5E06-4945-BCA2-87FD869F02A4")] public class AutoCorrelationAggregator : Aggregator { [StorableConstructor] protected AutoCorrelationAggregator(StorableConstructorFlag _) : base(_) { } protected AutoCorrelationAggregator(AutoCorrelationAggregator original, Cloner cloner) : base(original, cloner) { } public AutoCorrelationAggregator() { } public override IDeepCloneable Clone(Cloner cloner) { return new AutoCorrelationAggregator(this, cloner); } public override IResult CreateResult() { var groupings = items.GroupBy(act => act.Name).ToList(); switch (groupings.Count) { case 0: return new Result("Empty Autocorrelation Summary", new AutoCorrelationTable("Empty Autocorrelations")); case 1: return CreateResult(groupings[0].Key, groupings[0]); default: ResultCollection newResults = new ResultCollection(); foreach (var group in groupings) { newResults.Add(CreateResult(group.Key, group)); } return new Result("Auto Correlations", newResults); } } public static IResult CreateResult(string name, IEnumerable tables) { return new Result(string.Format("{0} Summary", name), Aggregate(tables.ToList())); } public static DataTable Aggregate(List autoCorrelationTables) { DataRow minRow = new DataRow("min"); DataRow maxRow = new DataRow("max"); DataRow avgRow = new DataRow("avg"); DataRow medRow = new DataRow("med"); DataRow q5Row = new DataRow("Q5"); DataRow q25Row = new DataRow("Q25"); DataRow q75Row = new DataRow("Q75"); DataRow q95Row = new DataRow("Q95"); for (int i = 0; ; i++) { double[] autocorrelation_i = new double[autoCorrelationTables.Count]; bool allzero = true; for (int j = 0; j < autoCorrelationTables.Count; j++) { if (autoCorrelationTables[j].Rows.First().Values.Count > i) { autocorrelation_i[j] = autoCorrelationTables[j].Rows.First().Values[i]; allzero = false; } else { autocorrelation_i[j] = 0; } } if (allzero) break; DistributionAnalyzer analyzer = new DistributionAnalyzer(autocorrelation_i); minRow.Values.Add(analyzer[0]); q25Row.Values.Add(analyzer[0.25]); medRow.Values.Add(analyzer[0.5]); q75Row.Values.Add(analyzer[0.75]); maxRow.Values.Add(analyzer[1]); avgRow.Values.Add(analyzer.Mean); q5Row.Values.Add(analyzer[0.05]); q95Row.Values.Add(analyzer[0.95]); } DataTable table = new DataTable("Auto correlations"); table.Rows.Add(minRow); table.Rows.Add(maxRow); table.Rows.Add(avgRow); table.Rows.Add(medRow); table.Rows.Add(q25Row); table.Rows.Add(q75Row); table.Rows.Add(q5Row); table.Rows.Add(q95Row); foreach (var row in table.Rows) row.VisualProperties.StartIndexZero = true; return table; } } }