Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/RuggednessCalculator.cs @ 14574

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

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

File size: 3.4 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Operators;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using System.Collections.Generic;
10
11namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
12  [Item("Ruggedness Calculator", "Calculates ruggedness descriptors froma given quality trail.")]
13  [StorableClass]
14  public class RuggednessCalculator : SingleSuccessorOperator {
15
16    #region Parameters
17    public LookupParameter<DataTable> QualityTrailParameter {
18      get { return (LookupParameter<DataTable>)Parameters["QualityTrail"]; }
19    }
20    public LookupParameter<IntValue> CorrelationLengthParameter {
21      get { return (LookupParameter<IntValue>)Parameters["CorrelationLength"]; }
22    }
23    public LookupParameter<DoubleArray> AutoCorrelationParameter {
24      get { return (LookupParameter<DoubleArray>)Parameters["AutoCorrelation"]; }
25    }
26    #endregion
27
28
29    #region Constructors & Cloning
30    [StorableConstructor]
31    protected RuggednessCalculator(bool deserializing) : base(deserializing) { }
32    protected RuggednessCalculator(RuggednessCalculator original, Cloner cloner) : base(original, cloner) { }
33    public RuggednessCalculator() {
34      Parameters.Add(new LookupParameter<DataTable>("QualityTrail", "Historical values of walk qualities"));
35      Parameters.Add(new LookupParameter<IntValue>("CorrelationLength", "Average maximum distances between correlated quality values."));
36      Parameters.Add(new LookupParameter<DoubleArray>("AutoCorrelation", "AutoCorrelation"));
37    }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new RuggednessCalculator(this, cloner);
40    }
41    #endregion
42
43    public override IOperation Apply() {
44      double[] qualities = QualityTrailParameter.ActualValue.Rows.First().Values.ToArray();
45      double[] autocorrelation;
46      CorrelationLengthParameter.ActualValue = new IntValue(CalculateCorrelationLength(qualities, out autocorrelation));
47      AutoCorrelationParameter.ActualValue = new DoubleArray(autocorrelation);
48      return base.Apply();
49    }
50
51    public static int CalculateCorrelationLength(double[] qualities, out double[] acf) {
52      double[] correlations = new double[qualities.Length];
53      alglib.corr.corrr1dcircular(qualities, qualities.Length, qualities, qualities.Length, ref correlations);
54      double mean = 0;
55      double variance = 0;
56      double skewness = 0;
57      double kurtosis = 0;
58      alglib.basestat.samplemoments(qualities, qualities.Length, ref mean, ref variance, ref skewness, ref kurtosis);
59      List<double> autocorrelation = new List<double>() { 1.0 };
60      double bound = Math.Min(2 / Math.Sqrt(qualities.Length), 1.0);
61      int correlationLength = 1;
62      for (; correlationLength < qualities.Length; correlationLength++) {
63        double value = correlations[correlationLength]/qualities.Length - mean * mean;
64        if (variance > 0)
65          value = Math.Max(Math.Min(value/variance, 1.0), -1.0);
66        else
67          value = 1;
68        autocorrelation.Add(value);
69        if (Math.Abs(value) < bound) break;
70      }
71      acf = autocorrelation.ToArray();
72      return correlationLength-1;
73    }
74
75    public static bool AnyGreaterOne(double[] values) {
76      return values.Any(d => d > 1);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.