#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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;
using System.Linq;
namespace HeuristicLab.Analysis.FitnessLandscape {
public static class CurveAnalysis {
public static CurveAnalysisResult GetCharacteristics(IEnumerable>> trajectories, Func distFunc) {
var traj = trajectories.Where(x => x.Count > 5).ToList();
if (traj.Count == 0) return new CurveAnalysisResult(0, 0, 0, new[] { 0.0, 0.0, 0.0 }, new[] { 0.0, 0.0, 0.0 }, new[] { 0.0, 0.0, 0.0 });
var symbols = GetSymbols(traj);
var f1 = traj.Select(path => ApproximateDerivative(path, distFunc).ToList()).ToList();
var f2 = f1.Select(d1 => ApproximateDerivative(d1, distFunc).ToList()).ToList();
var sharpness = f1.Average(x => x.Average(y => Math.Abs(y.Item2)));
var bumpiness = 0.0;
var flatness = 0.0;
var count = 0;
for (var p = 0; p < f2.Count; p++) {
if (f2[p].Count <= 2) continue;
count++;
var bump = 0;
var flat = 0;
for (var i = 0; i < f2[p].Count - 1; i++) {
if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) {
bump++;
} else if (f2[p][i].Item2 == 0) {
flat++;
}
}
bumpiness += bump / (f2[p].Count - 1.0);
flatness += flat / (f2[p].Count - 1.0);
}
bumpiness /= count;
flatness /= count;
var per = new[] { 25, 50, 75 };
return new CurveAnalysisResult(sharpness, bumpiness, flatness,
per.Select(p => symbols.Downward.GetPercentileOrDefault(p, 0)).ToArray(),
per.Select(p => symbols.Neutral.GetPercentileOrDefault(p, 0)).ToArray(),
per.Select(p => symbols.Upward.GetPercentileOrDefault(p, 0)).ToArray());
}
private static Symbols GetSymbols(List>> trajectories) {
var sym = new Symbols();
foreach (var t in trajectories) {
var prev = t[0];
for (var i = 1; i < t.Count; i++) {
sym.Add(i / (double)t.Count, t[i].Item2, prev.Item2);
prev = t[i];
}
}
return sym;
}
private static IEnumerable> ApproximateDerivative(IEnumerable> data, Func distFunc) {
Tuple prev = null, prev2 = null;
foreach (var d in data) {
if (prev == null) {
prev = d;
continue;
}
if (prev2 == null) {
prev2 = prev;
prev = d;
continue;
}
var dist = distFunc(prev2.Item1, d.Item1);
yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / dist);
prev2 = prev;
prev = d;
}
}
}
public enum CurveAnalysisFeature { Sharpness, Bumpiness, Flatness,
DownQ1, DownQ2, DownQ3,
NeutQ1, NeutQ2, NeutQ3,
UpQ1, UpQ2, UpQ3 }
public class CurveAnalysisResult {
private Dictionary results = new Dictionary();
public double GetValue(CurveAnalysisFeature name) {
return results[name];
}
public static IEnumerable AllFeatures {
get { return Enum.GetValues(typeof(CurveAnalysisFeature)).Cast(); }
}
public double[] GetValues() {
return AllFeatures.Select(x => results[x]).ToArray();
}
public CurveAnalysisResult(double sharpness, double bumpiness, double flatness, double[] down, double[] neut, double[] up) {
foreach (var v in AllFeatures.Zip(new[] { sharpness, bumpiness, flatness }.Concat(down).Concat(neut).Concat(up), (n, v) => Tuple.Create(n, v))) {
results[v.Item1] = v.Item2;
}
}
}
public class Symbols {
public Statistics Downward { get; } = new Statistics();
public Statistics Neutral { get; } = new Statistics();
public Statistics Upward { get; } = new Statistics();
public void Add(double step, double fit, double prev) {
if (fit < prev) Downward.Add(step);
else if (fit > prev) Upward.Add(step);
else Neutral.Add(step);
}
}
public sealed class Statistics {
private List values = new List();
public int Count { get { return values.Count; } }
public double Min { get; private set; }
public double Max { get; private set; }
public double Total { get; private set; }
public double Mean { get; private set; }
public double StdDev { get { return Math.Sqrt(Variance); } }
public double Variance { get { return Count > 0 ? variance / Count : 0.0; } }
private double variance;
private bool sorted = false;
public double GetPercentileOrDefault(int p, double @default = default(double)) {
if (p < 0 || p > 100) throw new ArgumentOutOfRangeException(nameof(p), p, "Must be in range [0;100]");
SortIfNecessary();
if (Count == 0) return @default;
else if (Count == 1) return values[0];
if (p == 100) return values[Count - 1];
var x = p / 100.0 * (Count - 1);
var inte = (int)x;
var frac = x - inte;
return values[inte] + frac * (values[inte + 1] - values[inte]);
}
public void Add(double value) {
sorted = false;
values.Add(value);
if (Count == 1) {
Min = Max = Mean = Total = value;
} else {
if (value < Min) Min = value;
if (value > Max) Max = value;
Total += value;
var oldMean = Mean;
Mean = oldMean + (value - oldMean) / Count;
variance = variance + (value - oldMean) * (value - Mean);
}
}
public void AddRange(IEnumerable values) {
foreach (var v in values) Add(v);
}
private void SortIfNecessary() {
if (!sorted) {
values.Sort();
sorted = true;
}
}
}
}