using System; using System.Linq; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.FitnessLandscape.Algorithms { public interface IRepeatsAnalyzer : IOperator { } [Item("RepeatedRuggednessAnalyzer", "Analyzes and consolidates repeated ruggedness analyses.")] [StorableClass] public class RepeatedRuggednessAnalyzer : SingleSuccessorOperator, IRepeatsAnalyzer { #region Parameters public ScopeTreeLookupParameter AutoCorrelationValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["AutoCorrelationValues"]; } } public LookupParameter AutoCorrelation1Parameter { get { return (LookupParameter)Parameters["AutoCorrelation1"]; } } public LookupParameter AutoCorrelation1VarianceParameter { get { return (LookupParameter)Parameters["AutoCorrelation1Variance"]; } } public ScopeTreeLookupParameter CorrelationLengthsParameter { get { return (ScopeTreeLookupParameter)Parameters["CorrelationLengths"]; } } public LookupParameter CorrelationLengthParameter { get { return (LookupParameter)Parameters["CorrelationLength"]; } } public LookupParameter CorrelationLengthVarianceParameter { get { return (LookupParameter)Parameters["CorrelationLengthVariance"]; } } public ScopeTreeLookupParameter AutoCorrelationTablesParameter { get { return (ScopeTreeLookupParameter)Parameters["AutoCorrelationTables"]; } } public LookupParameter AutoCorrelationParameter { get { return (LookupParameter)Parameters["AutoCorrelation"]; } } public LookupParameter AllAutoCorrelationsParameter { get { return (LookupParameter)Parameters["AllAutoCorrelations"]; } } public LookupParameter AutoCorrelationVarianceParameter { get { return (LookupParameter)Parameters["AutoCorrelationVariance"]; } } public LookupParameter ResultsParameter { get { return (LookupParameter)Parameters["Results"]; } } public ValueParameter SaveAllAutocorrelationsParameter { get { return (ValueParameter)Parameters["SaveAllAutocorrelations"]; } } #endregion #region Construction & Cloning [StorableConstructor] protected RepeatedRuggednessAnalyzer(bool deserializing) : base(deserializing) { } protected RepeatedRuggednessAnalyzer(RepeatedRuggednessAnalyzer original, Cloner cloner) : base(original, cloner) { } public RepeatedRuggednessAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("AutoCorrelationValues", "Auto correlation values of repeats.")); Parameters.Add(new LookupParameter("AutoCorrelation1", "Consolidated auto correlation 1.")); Parameters.Add(new LookupParameter("AutoCorrelation1Variance", "Variance between auto correlations in repeats.")); Parameters.Add(new ScopeTreeLookupParameter("CorrelationLengths", "Correlation lengths of the individual repeats.")); Parameters.Add(new LookupParameter("CorrelationLength", "Consolidated (average) correlation length.")); Parameters.Add(new LookupParameter("CorrelationLengthVariance", "Variance of correlation lengths.")); Parameters.Add(new ScopeTreeLookupParameter("AutoCorrelationTables", "List of autocorrelation tables for each repeat.")); Parameters.Add(new LookupParameter("AutoCorrelation", "Consolidated auto correlation table.")); Parameters.Add(new LookupParameter("AllAutoCorrelations", "All auto correlations merged into a single table.")); Parameters.Add(new LookupParameter("AutoCorrelationVariance", "Variance of autocorrelations.")); Parameters.Add(new ValueParameter("SaveAllAutocorrelations", "Wether to include all repeated autocorrelation curves. Turn of to save memory instead.", new BoolValue(false))); Parameters.Add(new LookupParameter("Results", "The collection of all results.")); AutoCorrelationValuesParameter.ActualName = "AutoCorrelation1"; AutoCorrelationValuesParameter.Depth = 2; AutoCorrelationTablesParameter.ActualName = "Autocorrelation"; AutoCorrelationTablesParameter.Depth = 2; CorrelationLengthsParameter.ActualName = "CorrelationLength"; CorrelationLengthsParameter.Depth = 2; } public override IDeepCloneable Clone(Cloner cloner) { return new RepeatedRuggednessAnalyzer(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (!Parameters.ContainsKey("SaveAllAutocorrelations")) Parameters.Add(new ValueParameter("SaveAllAutocorrelations", "Whether to include all repeated autocorrelation curves. Turn of to save memory instead.", new BoolValue(false))); } #endregion public override IOperation Apply() { AggregateAutoCorrelationValues(); AggregateCorrelationLengths(); AggregateAutoCorrelationFunctions(); return base.Apply(); } private void AggregateAutoCorrelationValues() { ResultCollection results = ResultsParameter.ActualValue; var autocorrelations = AutoCorrelationValuesParameter.ActualValue.Select(v => v.Value).ToList(); if (autocorrelations.Count > 0) { var avgAutoCorrelation1 = new DoubleValue(autocorrelations.Average()); var varAutoCorrelation1 = new DoubleValue(autocorrelations.Variance()); AutoCorrelation1Parameter.ActualValue = avgAutoCorrelation1; AutoCorrelation1VarianceParameter.ActualValue = varAutoCorrelation1; results.Remove("AutoCorrelation1"); results.Add(new Result("AutoCorrelation1", avgAutoCorrelation1)); results.Add(new Result("AutoCorrelation1 Variance", varAutoCorrelation1)); } } private void AggregateCorrelationLengths() { ResultCollection results = ResultsParameter.ActualValue; var correlationLengths = CorrelationLengthsParameter.ActualValue.Select(v => (double)v.Value).ToList(); if (correlationLengths.Count > 0) { var avgCorrelationLength = new DoubleValue(correlationLengths.Average()); var varCorrelationLength = new DoubleValue(correlationLengths.Variance()); CorrelationLengthParameter.ActualValue = avgCorrelationLength; CorrelationLengthVarianceParameter.ActualValue = varCorrelationLength; results.Remove("CorrelationLength"); results.Add(new Result("CorrelationLength", avgCorrelationLength)); results.Add(new Result("CorrelationLength Variance", varCorrelationLength)); } } private void AggregateAutoCorrelationFunctions() { ResultCollection results = ResultsParameter.ActualValue; bool saveAllAutocorrelations = SaveAllAutocorrelationsParameter.Value.Value; var tables = AutoCorrelationTablesParameter.ActualValue.ToList(); if (tables.Count > 0) { DataTable allValues = saveAllAutocorrelations ? new DataTable("All AutoCorrelations") : null; DataRow avgRow = new DataRow("Average Auto Correlation"); DataRow stdRow = new DataRow("Std.Dev. of Auto Correlations"); DataRow countRow = new DataRow("n"); avgRow.VisualProperties.StartIndexZero = true; stdRow.VisualProperties.StartIndexZero = true; countRow.VisualProperties.StartIndexZero = true; countRow.VisualProperties.SecondYAxis = true; for (int i = 0; i