[16137] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
| 27 | public static class CurveAnalysis<T> {
|
---|
[16955] | 28 | public static CurveAnalysisResult GetCharacteristics(IEnumerable<List<Tuple<T, double>>> trajectories, Func<T, T, double> distFunc) {
|
---|
| 29 | var traj = trajectories.Where(x => x.Count > 5).ToList();
|
---|
| 30 | 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 });
|
---|
[16137] | 31 |
|
---|
[16955] | 32 | var symbols = GetSymbols(traj);
|
---|
| 33 | var f1 = traj.Select(path => ApproximateDerivative(path, distFunc).ToList()).ToList();
|
---|
[16137] | 34 | var f2 = f1.Select(d1 => ApproximateDerivative(d1, distFunc).ToList()).ToList();
|
---|
| 35 |
|
---|
| 36 | var sharpness = f1.Average(x => x.Average(y => Math.Abs(y.Item2)));
|
---|
| 37 | var bumpiness = 0.0;
|
---|
| 38 | var flatness = 0.0;
|
---|
| 39 | var count = 0;
|
---|
| 40 | for (var p = 0; p < f2.Count; p++) {
|
---|
| 41 | if (f2[p].Count <= 2) continue;
|
---|
| 42 | count++;
|
---|
| 43 | var bump = 0;
|
---|
| 44 | var flat = 0;
|
---|
| 45 | for (var i = 0; i < f2[p].Count - 1; i++) {
|
---|
| 46 | if ((f2[p][i].Item2 > 0 && f2[p][i + 1].Item2 < 0) || (f2[p][i].Item2 < 0 && f2[p][i + 1].Item2 > 0)) {
|
---|
| 47 | bump++;
|
---|
| 48 | } else if (f2[p][i].Item2 == 0) {
|
---|
| 49 | flat++;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | bumpiness += bump / (f2[p].Count - 1.0);
|
---|
| 53 | flatness += flat / (f2[p].Count - 1.0);
|
---|
| 54 | }
|
---|
| 55 | bumpiness /= count;
|
---|
| 56 | flatness /= count;
|
---|
| 57 | var per = new[] { 25, 50, 75 };
|
---|
| 58 | return new CurveAnalysisResult(sharpness, bumpiness, flatness,
|
---|
| 59 | per.Select(p => symbols.Downward.GetPercentileOrDefault(p, 0)).ToArray(),
|
---|
| 60 | per.Select(p => symbols.Neutral.GetPercentileOrDefault(p, 0)).ToArray(),
|
---|
| 61 | per.Select(p => symbols.Upward.GetPercentileOrDefault(p, 0)).ToArray());
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | private static Symbols GetSymbols(List<List<Tuple<T, double>>> trajectories) {
|
---|
| 65 | var sym = new Symbols();
|
---|
| 66 | foreach (var t in trajectories) {
|
---|
| 67 | var prev = t[0];
|
---|
| 68 | for (var i = 1; i < t.Count; i++) {
|
---|
| 69 | sym.Add(i / (double)t.Count, t[i].Item2, prev.Item2);
|
---|
| 70 | prev = t[i];
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return sym;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | private static IEnumerable<Tuple<T, double>> ApproximateDerivative(IEnumerable<Tuple<T, double>> data, Func<T, T, double> distFunc) {
|
---|
| 77 | Tuple<T, double> prev = null, prev2 = null;
|
---|
| 78 | foreach (var d in data) {
|
---|
| 79 | if (prev == null) {
|
---|
| 80 | prev = d;
|
---|
| 81 | continue;
|
---|
| 82 | }
|
---|
| 83 | if (prev2 == null) {
|
---|
| 84 | prev2 = prev;
|
---|
| 85 | prev = d;
|
---|
| 86 | continue;
|
---|
| 87 | }
|
---|
| 88 | var dist = distFunc(prev2.Item1, d.Item1);
|
---|
| 89 | yield return Tuple.Create(prev.Item1, (d.Item2 - prev2.Item2) / dist);
|
---|
| 90 | prev2 = prev;
|
---|
| 91 | prev = d;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public enum CurveAnalysisFeature { Sharpness, Bumpiness, Flatness,
|
---|
| 97 | DownQ1, DownQ2, DownQ3,
|
---|
| 98 | NeutQ1, NeutQ2, NeutQ3,
|
---|
| 99 | UpQ1, UpQ2, UpQ3 }
|
---|
| 100 |
|
---|
| 101 | public class CurveAnalysisResult {
|
---|
| 102 | private Dictionary<CurveAnalysisFeature, double> results = new Dictionary<CurveAnalysisFeature, double>();
|
---|
| 103 |
|
---|
| 104 | public double GetValue(CurveAnalysisFeature name) {
|
---|
| 105 | return results[name];
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public static IEnumerable<CurveAnalysisFeature> AllFeatures {
|
---|
| 109 | get { return Enum.GetValues(typeof(CurveAnalysisFeature)).Cast<CurveAnalysisFeature>(); }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public double[] GetValues() {
|
---|
| 113 | return AllFeatures.Select(x => results[x]).ToArray();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public CurveAnalysisResult(double sharpness, double bumpiness, double flatness, double[] down, double[] neut, double[] up) {
|
---|
| 117 | foreach (var v in AllFeatures.Zip(new[] { sharpness, bumpiness, flatness }.Concat(down).Concat(neut).Concat(up), (n, v) => Tuple.Create(n, v))) {
|
---|
| 118 | results[v.Item1] = v.Item2;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public class Symbols {
|
---|
| 124 | public Statistics Downward { get; } = new Statistics();
|
---|
| 125 | public Statistics Neutral { get; } = new Statistics();
|
---|
| 126 | public Statistics Upward { get; } = new Statistics();
|
---|
| 127 |
|
---|
| 128 | public void Add(double step, double fit, double prev) {
|
---|
| 129 | if (fit < prev) Downward.Add(step);
|
---|
| 130 | else if (fit > prev) Upward.Add(step);
|
---|
| 131 | else Neutral.Add(step);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public sealed class Statistics {
|
---|
| 136 | private List<double> values = new List<double>();
|
---|
| 137 |
|
---|
| 138 | public int Count { get { return values.Count; } }
|
---|
| 139 |
|
---|
| 140 | public double Min { get; private set; }
|
---|
| 141 | public double Max { get; private set; }
|
---|
| 142 | public double Total { get; private set; }
|
---|
| 143 | public double Mean { get; private set; }
|
---|
| 144 | public double StdDev { get { return Math.Sqrt(Variance); } }
|
---|
| 145 | public double Variance { get { return Count > 0 ? variance / Count : 0.0; } }
|
---|
| 146 |
|
---|
| 147 | private double variance;
|
---|
| 148 | private bool sorted = false;
|
---|
| 149 |
|
---|
| 150 | public double GetPercentileOrDefault(int p, double @default = default(double)) {
|
---|
| 151 | if (p < 0 || p > 100) throw new ArgumentOutOfRangeException(nameof(p), p, "Must be in range [0;100]");
|
---|
| 152 | SortIfNecessary();
|
---|
| 153 | if (Count == 0) return @default;
|
---|
| 154 | else if (Count == 1) return values[0];
|
---|
| 155 | if (p == 100) return values[Count - 1];
|
---|
| 156 |
|
---|
| 157 | var x = p / 100.0 * (Count - 1);
|
---|
| 158 | var inte = (int)x;
|
---|
| 159 | var frac = x - inte;
|
---|
| 160 |
|
---|
| 161 | return values[inte] + frac * (values[inte + 1] - values[inte]);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public void Add(double value) {
|
---|
| 165 | sorted = false;
|
---|
| 166 | values.Add(value);
|
---|
| 167 |
|
---|
| 168 | if (Count == 1) {
|
---|
| 169 | Min = Max = Mean = Total = value;
|
---|
| 170 | } else {
|
---|
| 171 | if (value < Min) Min = value;
|
---|
| 172 | if (value > Max) Max = value;
|
---|
| 173 |
|
---|
| 174 | Total += value;
|
---|
| 175 | var oldMean = Mean;
|
---|
| 176 | Mean = oldMean + (value - oldMean) / Count;
|
---|
| 177 | variance = variance + (value - oldMean) * (value - Mean);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public void AddRange(IEnumerable<double> values) {
|
---|
| 182 | foreach (var v in values) Add(v);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | private void SortIfNecessary() {
|
---|
| 186 | if (!sorted) {
|
---|
| 187 | values.Sort();
|
---|
| 188 | sorted = true;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|