Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/RuggednessCalculator.cs

Last change on this file was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Analysis.FitnessLandscape {
33  [Item("Ruggedness Calculator", "Calculates ruggedness descriptors froma given quality trail.")]
34  [StorableType("E52780B3-27EE-4C14-9D14-F0F0265EB8DB")]
35  public class RuggednessCalculator : SingleSuccessorOperator {
36
37    #region Parameters
38    public LookupParameter<DataTable> QualityTrailParameter {
39      get { return (LookupParameter<DataTable>)Parameters["QualityTrail"]; }
40    }
41    public LookupParameter<IntValue> CorrelationLengthParameter {
42      get { return (LookupParameter<IntValue>)Parameters["CorrelationLength"]; }
43    }
44    public LookupParameter<DoubleArray> AutoCorrelationParameter {
45      get { return (LookupParameter<DoubleArray>)Parameters["AutoCorrelation"]; }
46    }
47    #endregion
48
49    #region Constructors & Cloning
50    [StorableConstructor]
51    protected RuggednessCalculator(StorableConstructorFlag _) : base(_) { }
52    protected RuggednessCalculator(RuggednessCalculator original, Cloner cloner) : base(original, cloner) { }
53    public RuggednessCalculator() {
54      Parameters.Add(new LookupParameter<DataTable>("QualityTrail", "Historical values of walk qualities"));
55      Parameters.Add(new LookupParameter<IntValue>("CorrelationLength", "Average maximum distances between correlated quality values."));
56      Parameters.Add(new LookupParameter<DoubleArray>("AutoCorrelation", "AutoCorrelation"));
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new RuggednessCalculator(this, cloner);
60    }
61    #endregion
62
63    public override IOperation Apply() {
64      double[] qualities = QualityTrailParameter.ActualValue.Rows.First().Values.ToArray();
65      double[] autocorrelation;
66      CorrelationLengthParameter.ActualValue = new IntValue(CalculateCorrelationLength(qualities, out autocorrelation));
67      AutoCorrelationParameter.ActualValue = new DoubleArray(autocorrelation);
68      return base.Apply();
69    }
70
71    public static int CalculateCorrelationLength(double[] qualities, out double[] acf) {
72      double[] correlations = new double[qualities.Length];
73      alglib.corr.corrr1dcircular(qualities, qualities.Length, qualities, qualities.Length, ref correlations);
74      double mean = 0;
75      double variance = 0;
76      double skewness = 0;
77      double kurtosis = 0;
78      alglib.basestat.samplemoments(qualities, qualities.Length, ref mean, ref variance, ref skewness, ref kurtosis);
79      List<double> autocorrelation = new List<double>() { 1.0 };
80      int correlationLength = -1, counter = 1;
81      for (; counter < qualities.Length / 2; counter++) {
82        double value = correlations[counter] / qualities.Length - mean * mean;
83        if (variance > 0)
84          value = Math.Max(Math.Min(value / variance, 1.0), -1.0);
85        else
86          value = 1;
87        autocorrelation.Add(value);
88        if (value < 0 && correlationLength < 0) correlationLength = counter;
89      }
90      acf = autocorrelation.ToArray();
91      return correlationLength - 1;
92    }
93
94    public static bool AnyGreaterOne(double[] values) {
95      return values.Any(d => d > 1);
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.