[7128] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Data;
|
---|
| 6 | using HeuristicLab.Operators;
|
---|
| 7 | using HeuristicLab.Parameters;
|
---|
| 8 | using System.Collections.Generic;
|
---|
[16573] | 9 | using HEAL.Attic;
|
---|
[7128] | 10 |
|
---|
| 11 | namespace HeuristicLab.Analysis.FitnessLandscape.Analysis {
|
---|
| 12 | [Item("Ruggedness Calculator", "Calculates ruggedness descriptors froma given quality trail.")]
|
---|
[16573] | 13 | [StorableType("1739820A-DC44-4371-A912-E44C1DD46940")]
|
---|
[7128] | 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]
|
---|
[16573] | 31 | protected RuggednessCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
[7128] | 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 | }
|
---|