Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/RepeatedRuggednessAnalyzer.cs @ 15290

Last change on this file since 15290 was 7128, checked in by epitzer, 12 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 9.9 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Analysis.FitnessLandscape.DataTables;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.Analysis.FitnessLandscape.Algorithms {
13  public interface IRepeatsAnalyzer : IOperator { }
14
15  [Item("RepeatedRuggednessAnalyzer", "Analyzes and consolidates repeated ruggedness analyses.")]
16  [StorableClass]
17  public class RepeatedRuggednessAnalyzer : SingleSuccessorOperator, IRepeatsAnalyzer {
18
19    #region Parameters
20    public ScopeTreeLookupParameter<DoubleValue> AutoCorrelationValuesParameter {
21      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["AutoCorrelationValues"]; }
22    }
23    public LookupParameter<DoubleValue> AutoCorrelation1Parameter {
24      get { return (LookupParameter<DoubleValue>)Parameters["AutoCorrelation1"]; }
25    }
26    public LookupParameter<DoubleValue> AutoCorrelation1VarianceParameter {
27      get { return (LookupParameter<DoubleValue>)Parameters["AutoCorrelation1Variance"]; }
28    }
29
30    public ScopeTreeLookupParameter<IntValue> CorrelationLengthsParameter {
31      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["CorrelationLengths"]; }
32    }
33    public LookupParameter<DoubleValue> CorrelationLengthParameter {
34      get { return (LookupParameter<DoubleValue>)Parameters["CorrelationLength"]; }
35    }
36    public LookupParameter<DoubleValue> CorrelationLengthVarianceParameter {
37      get { return (LookupParameter<DoubleValue>)Parameters["CorrelationLengthVariance"]; }
38    }
39
40    public ScopeTreeLookupParameter<DataTable> AutoCorrelationTablesParameter {
41      get { return (ScopeTreeLookupParameter<DataTable>)Parameters["AutoCorrelationTables"]; }
42    }
43    public LookupParameter<DataTable> AutoCorrelationParameter {
44      get { return (LookupParameter<DataTable>)Parameters["AutoCorrelation"]; }
45    }
46    public LookupParameter<DataTable> AllAutoCorrelationsParameter {
47      get { return (LookupParameter<DataTable>)Parameters["AllAutoCorrelations"]; }
48    }
49    public LookupParameter<DataTable> AutoCorrelationVarianceParameter {
50      get { return (LookupParameter<DataTable>)Parameters["AutoCorrelationVariance"]; }
51    }
52    public LookupParameter<ResultCollection> ResultsParameter {
53      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
54    }
55    public ValueParameter<BoolValue> SaveAllAutocorrelationsParameter {
56      get { return (ValueParameter<BoolValue>)Parameters["SaveAllAutocorrelations"]; }
57    }
58   
59    #endregion
60
61    #region Construction & Cloning
62    [StorableConstructor]
63    protected RepeatedRuggednessAnalyzer(bool deserializing) : base(deserializing) { }
64    protected RepeatedRuggednessAnalyzer(RepeatedRuggednessAnalyzer original, Cloner cloner) : base(original, cloner) { }
65    public RepeatedRuggednessAnalyzer() {
66      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("AutoCorrelationValues", "Auto correlation values of repeats."));
67      Parameters.Add(new LookupParameter<DoubleValue>("AutoCorrelation1", "Consolidated auto correlation 1."));
68      Parameters.Add(new LookupParameter<DoubleValue>("AutoCorrelation1Variance", "Variance between auto correlations in repeats."));
69
70      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("CorrelationLengths", "Correlation lengths of the individual repeats."));
71      Parameters.Add(new LookupParameter<DoubleValue>("CorrelationLength", "Consolidated (average) correlation length."));
72      Parameters.Add(new LookupParameter<DoubleValue>("CorrelationLengthVariance", "Variance of correlation lengths."));
73
74      Parameters.Add(new ScopeTreeLookupParameter<DataTable>("AutoCorrelationTables", "List of autocorrelation tables for each repeat."));
75      Parameters.Add(new LookupParameter<DataTable>("AutoCorrelation", "Consolidated auto correlation table."));
76      Parameters.Add(new LookupParameter<DataTable>("AllAutoCorrelations", "All auto correlations merged into a single table."));
77      Parameters.Add(new LookupParameter<DataTable>("AutoCorrelationVariance", "Variance of autocorrelations."));
78      Parameters.Add(new ValueParameter<BoolValue>("SaveAllAutocorrelations", "Wether to include all repeated autocorrelation curves. Turn of to save memory instead.", new BoolValue(false)));
79
80      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection of all results."));
81
82      AutoCorrelationValuesParameter.ActualName = "AutoCorrelation1";
83      AutoCorrelationValuesParameter.Depth = 2;
84      AutoCorrelationTablesParameter.ActualName = "Autocorrelation";
85      AutoCorrelationTablesParameter.Depth = 2;
86      CorrelationLengthsParameter.ActualName = "CorrelationLength";
87      CorrelationLengthsParameter.Depth = 2;
88    }
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new RepeatedRuggednessAnalyzer(this, cloner);
91    }
92    [StorableHook(HookType.AfterDeserialization)]
93    private void AfterDeserialization() {
94      if (!Parameters.ContainsKey("SaveAllAutocorrelations"))
95        Parameters.Add(new ValueParameter<BoolValue>("SaveAllAutocorrelations", "Whether to include all repeated autocorrelation curves. Turn of to save memory instead.", new BoolValue(false)));
96    }
97    #endregion
98
99    public override IOperation Apply() {
100      AggregateAutoCorrelationValues();
101      AggregateCorrelationLengths();
102      AggregateAutoCorrelationFunctions();
103      return base.Apply();
104    }
105
106    private void AggregateAutoCorrelationValues() {
107      ResultCollection results = ResultsParameter.ActualValue;
108      var autocorrelations = AutoCorrelationValuesParameter.ActualValue.Select(v => v.Value).ToList();
109      if (autocorrelations.Count > 0) {
110        var avgAutoCorrelation1 = new DoubleValue(autocorrelations.Average());
111        var varAutoCorrelation1 = new DoubleValue(autocorrelations.Variance());
112        AutoCorrelation1Parameter.ActualValue = avgAutoCorrelation1;
113        AutoCorrelation1VarianceParameter.ActualValue = varAutoCorrelation1;
114        results.Remove("AutoCorrelation1");
115        results.Add(new Result("AutoCorrelation1", avgAutoCorrelation1));
116        results.Add(new Result("AutoCorrelation1 Variance", varAutoCorrelation1));
117      }
118    }
119
120    private void AggregateCorrelationLengths() {
121      ResultCollection results = ResultsParameter.ActualValue;
122      var correlationLengths = CorrelationLengthsParameter.ActualValue.Select(v => (double)v.Value).ToList();
123      if (correlationLengths.Count > 0) {
124        var avgCorrelationLength = new DoubleValue(correlationLengths.Average());
125        var varCorrelationLength = new DoubleValue(correlationLengths.Variance());
126        CorrelationLengthParameter.ActualValue = avgCorrelationLength;
127        CorrelationLengthVarianceParameter.ActualValue = varCorrelationLength;
128        results.Remove("CorrelationLength");
129        results.Add(new Result("CorrelationLength", avgCorrelationLength));
130        results.Add(new Result("CorrelationLength Variance", varCorrelationLength));
131      }
132    }
133
134    private void AggregateAutoCorrelationFunctions() {
135      ResultCollection results = ResultsParameter.ActualValue;
136      bool saveAllAutocorrelations = SaveAllAutocorrelationsParameter.Value.Value;
137      var tables = AutoCorrelationTablesParameter.ActualValue.ToList();
138      if (tables.Count > 0) {
139        DataTable allValues = saveAllAutocorrelations ? new DataTable("All AutoCorrelations") : null;
140        DataRow avgRow = new DataRow("Average Auto Correlation");
141        DataRow stdRow = new DataRow("Std.Dev. of Auto Correlations");
142        DataRow countRow = new DataRow("n");
143        avgRow.VisualProperties.StartIndexZero = true;
144        stdRow.VisualProperties.StartIndexZero = true;
145        countRow.VisualProperties.StartIndexZero = true;
146        countRow.VisualProperties.SecondYAxis = true;
147        for (int i = 0; i<tables.Count; i++) {
148          DataRow row = tables[i].Rows.First();
149          var newRow = new DataRow(i.ToString(), "", row.Values);
150          newRow.VisualProperties.StartIndexZero = true;
151          if (allValues != null)
152            allValues.Rows.Add(newRow);
153          while (avgRow.Values.Count < row.Values.Count) {
154            stdRow.Values.Add(0);
155            avgRow.Values.Add(0);
156            countRow.Values.Add(0);
157          }
158          for (int j = 0; j<row.Values.Count; j++) {
159            avgRow.Values[j] += row.Values[j];
160            countRow.Values[j]++;
161          }
162        }
163        for (int i = 0; i<avgRow.Values.Count; i++) {
164          avgRow.Values[i] = avgRow.Values[i] / tables.Count;
165        }
166        foreach (var table in tables) {
167          DataRow row = table.Rows.First();
168          for (int j = 0; j<row.Values.Count; j++) {
169            stdRow.Values[j] += square(avgRow.Values[j] - row.Values[j]);
170          }
171        }
172        for (int i = 0; i<avgRow.Values.Count; i++) {
173          stdRow.Values[i] = Math.Sqrt(stdRow.Values[i] / tables.Count);
174        }
175        AutoCorrelationTable avgTable = new AutoCorrelationTable("Average repeated auto correlation");
176        AutoCorrelationTable stdTable = new AutoCorrelationTable("Std.Dev. of repeated auto correlation");
177        avgTable.Rows.Add(avgRow);
178        avgTable.Rows.Add(countRow);
179        stdTable.Rows.Add(stdRow);
180        AutoCorrelationParameter.ActualValue = avgTable;
181        AutoCorrelationVarianceParameter.ActualValue = stdTable;
182        results.Remove("Autocorrelation");
183        results.Add(new Result("Auto Correlation", avgTable));
184        results.Add(new Result("auto correlation deviations", stdTable));
185        if (allValues != null) {
186          AllAutoCorrelationsParameter.ActualValue = allValues;
187          results.Add(new Result("All auto correlations", allValues));
188        }
189      }
190    }
191
192    public static double square(double x) { return x * x; }
193  }
194}
Note: See TracBrowser for help on using the repository browser.