#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
namespace HeuristicLab.Analysis.FitnessLandscape {
public static class RuggednessCalculator {
///
/// Calculates statistical correlation length as defined by Hordijk, W., 1996. A measure of landscapes. Evolutionary computation, 4(4), pp.335-360.
///
/// The quality trail observed.
/// The autocorrelation values for each step s, including 0 => acf[0] = 1.
/// The statistical limit, correlation length will be the last step before acf falls within this limit. If omitted it is calculated as 2 / sqrt(qualities.Length).
/// The statistical correlation length
public static int CalculateCorrelationLength(double[] qualities, out double[] acf, double? limit = null) {
if (!limit.HasValue) limit = 2.0 / Math.Sqrt(qualities.Length);
double[] correlations = new double[qualities.Length];
alglib.corr.corrr1dcircular(qualities, qualities.Length, qualities, qualities.Length, ref correlations);
double mean = 0;
double variance = 0;
double skewness = 0;
double kurtosis = 0;
alglib.basestat.samplemoments(qualities, qualities.Length, ref mean, ref variance, ref skewness, ref kurtosis);
List autocorrelation = new List() { 1.0 };
int correlationLength = -1, counter = 1;
for (; counter < qualities.Length / 2; counter++) {
double value = correlations[counter] / qualities.Length - mean * mean;
if (variance > 0)
value = Math.Max(Math.Min(value / variance, 1.0), -1.0);
else
value = 1;
autocorrelation.Add(value);
if (Math.Abs(value) < limit && correlationLength < 0) correlationLength = counter;
}
acf = autocorrelation.ToArray();
return correlationLength - 1;
}
}
}