Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Analysis/RuggednessCalculator.cs @ 16591

Last change on this file since 16591 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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