Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemCharacteristicAnalysis/CurveAnalysis.cs @ 16096

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

#2457:

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