Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/RuggednessAnalyzer.cs @ 7128

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

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

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis.FitnessLandscape.DataTables;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
33
34  [StorableClass]
35  public class RuggednessAnalyzer : AlgorithmOperator, IQualityTrailAnalyzer {
36
37    #region Parameters
38    public LookupParameter<DataTable> QualityTrailParameter {
39      get { return (LookupParameter<DataTable>)Parameters["Quality Trail"]; }
40    }
41    public LookupParameter<DataTable> CorrelationLengthTableParameter {
42      get { return (LookupParameter<DataTable>)Parameters["CorrelationLengthTable"]; }
43    }
44    public LookupParameter<AutoCorrelationTable> AutocorrelationParameter {
45      get { return (LookupParameter<AutoCorrelationTable>)Parameters["Autocorrelation"]; }
46    }
47    public LookupParameter<VariableCollection> ResultsParameter {
48      get { return (LookupParameter<VariableCollection>)Parameters["Results"]; }
49    }
50    public LookupParameter<DoubleValue> AutoCorrelation1Parameter {
51      get { return (LookupParameter<DoubleValue>)Parameters["AutoCorrelation1"]; }
52    }
53    public LookupParameter<IntValue> CorrelationLengthParameter {
54      get { return (LookupParameter<IntValue>)Parameters["CorrelationLength"]; }
55    }
56    #endregion
57
58    [StorableConstructor]
59    protected RuggednessAnalyzer(bool deserializing) : base(deserializing) { }
60    protected RuggednessAnalyzer(RuggednessAnalyzer original, Cloner cloner) : base(original, cloner) { }
61
62    public RuggednessAnalyzer() {
63      Parameters.Add(new LookupParameter<DataTable>("Quality Trail", "The quality of the solution"));
64      Parameters.Add(new LookupParameter<DataTable>("CorrelationLengthTable", "Maximum nr of steps between statistically significantly correlated quality values"));
65      Parameters.Add(new LookupParameter<AutoCorrelationTable>("Autocorrelation", "Autocorrelation function of successive quality values"));
66      Parameters.Add(new LookupParameter<VariableCollection>("Results", "The collection of all results of this algorithm"));
67      Parameters.Add(new LookupParameter<DoubleValue>("AutoCorrelation1", "Autocorrelation for one mutation step."));
68      Parameters.Add(new LookupParameter<IntValue>("CorrelationLength", "The correlation length."));
69
70      var resultsCollector = new ResultsCollector();
71      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(CorrelationLengthTableParameter.Name));
72      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>(AutocorrelationParameter.Name));
73      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(AutoCorrelation1Parameter.Name));
74      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(CorrelationLengthParameter.Name));
75
76      OperatorGraph.InitialOperator = resultsCollector;
77      resultsCollector.Successor = null;
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new RuggednessAnalyzer(this, cloner);
82    }
83
84    public override IOperation Apply() {
85      DataTable correlationLengthTable = GetOrCreateCorrelationLengthTable();
86      AutoCorrelationTable autocorrelationTable = GetOrCreateAutoCorrelationTable();
87      DataTable qualityTrail = QualityTrailParameter.ActualValue;
88      if (qualityTrail != null && qualityTrail.Rows.Count > 0) {
89        double[] autocorrelationValues;
90        int correlationLength = RuggednessCalculator.CalculateCorrelationLength(qualityTrail.Rows.First().Values.ToArray(), out autocorrelationValues);
91        correlationLengthTable.Rows["Correlation Length"].Values.Add(correlationLength);
92        autocorrelationTable.Rows["Auto Correlation"].Values.Clear();
93        autocorrelationTable.Rows["Auto Correlation"].Values.AddRange(autocorrelationValues);
94        CorrelationLengthParameter.ActualValue = new IntValue(correlationLength);
95        AutoCorrelation1Parameter.ActualValue = new DoubleValue(autocorrelationValues.Length > 1 ? autocorrelationValues[1] : 0.0);
96      }
97      return base.Apply();
98    }
99
100    private AutoCorrelationTable GetOrCreateAutoCorrelationTable() {
101      AutoCorrelationTable autocorrelationTable = AutocorrelationParameter.ActualValue;
102      if (autocorrelationTable == null) {
103        autocorrelationTable = new AutoCorrelationTable("Auto Correlation");
104        AutocorrelationParameter.ActualValue = autocorrelationTable;
105        var row = new DataRow("Auto Correlation");
106        row.VisualProperties.StartIndexZero = true;
107        autocorrelationTable.Rows.Add(row);
108      }
109      return autocorrelationTable;
110    }
111
112    private DataTable GetOrCreateCorrelationLengthTable() {
113      DataTable correlationLengthTable = CorrelationLengthTableParameter.ActualValue;
114      if (correlationLengthTable == null) {
115        correlationLengthTable = new DataTable("Correlation Length");
116        CorrelationLengthTableParameter.ActualValue = correlationLengthTable;
117        correlationLengthTable.Rows.Add(new DataRow("Correlation Length"));
118      }
119      return correlationLengthTable;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.