Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/RunCollectionMultiDiscretizer.cs @ 17717

Last change on this file since 17717 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 6.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Parameters;
7using HeuristicLab.Data;
8using HEAL.Attic;
9
10namespace HeuristicLab.Optimization.RunModification {
11
12  [Item("RunCollection MultiDiscretizer",
13    "Creates several levels from the distribution of several results accross a run collection and " +
14    "assigns a discretized value. Non-existing numbers as well as NaN and infinities are excluded from the caluclation.")]
15  [StorableType("74B90692-F3F2-4E0B-AA17-C3CDD710D009")]
16  public class RunCollectionMultiDiscretizer : ParameterizedNamedItem, IRunCollectionModifier {
17
18    public override bool CanChangeName { get { return false; } }
19    public override bool CanChangeDescription { get { return false; } }
20
21    #region Parameters
22    public ValueParameter<CheckedItemList<StringValue>> SourceParameter {
23      get { return (ValueParameter<CheckedItemList<StringValue>>)Parameters["Sources"]; }
24    }
25    public ValueParameter<StringValue> SuffixParameter {
26      get { return (ValueParameter<StringValue>)Parameters["Suffix"]; }
27    }
28    public ValueParameter<DoubleValue> SpreadParameter {
29      get { return (ValueParameter<DoubleValue>)Parameters["Spread"]; }
30    }
31    public ValueParameter<StringValue> GroupByParameter {
32      get { return (ValueParameter<StringValue>)Parameters["GroupBy"]; }
33    }   
34    public ValueParameter<ItemList<StringValue>> LevelsParameter {
35      get { return (ValueParameter<ItemList<StringValue>>)Parameters["Levels"]; }
36    }   
37    #endregion
38
39    private IEnumerable<string> Sources { get { return SourceParameter.Value.CheckedItems.Select(i => i.Value.Value); } }
40    private string Suffix { get { return SuffixParameter.Value.Value; } }
41    private double Spread { get { return SpreadParameter.Value.Value; } }
42    private string GroupBy { get { return GroupByParameter.Value.Value; } }
43    private List<string> Levels { get { return LevelsParameter.Value.Select(v => v.Value).ToList(); } }
44
45      #region Construction & Cloning
46    [StorableConstructor]
47    protected RunCollectionMultiDiscretizer(StorableConstructorFlag _) : base(_) { }
48    protected RunCollectionMultiDiscretizer(RunCollectionMultiDiscretizer original, Cloner cloner) : base(original, cloner) { }
49    public RunCollectionMultiDiscretizer() {
50      Parameters.Add(new ValueParameter<CheckedItemList<StringValue>>("Sources", "Source value names to be discretized."));
51      Parameters.Add(new ValueParameter<StringValue>("Suffix", "Suffix attached to new discretized values.", new StringValue("/Level")));
52      Parameters.Add(new ValueParameter<DoubleValue>("Spread", "The number of standard deviations considered one additional level. Set to zero to use empirical distribution instead.", new DoubleValue(1)));
53      Parameters.Add(new ValueParameter<StringValue>("GroupBy", "Create separate analyzes for different values of this variable.", new StringValue("")));
54      Parameters.Add(new ValueParameter<ItemList<StringValue>>("Levels", "The list of levels to be assigned.",
55        new ItemList<StringValue> {
56          new StringValue("Very Low"),
57          new StringValue("Low"),
58          new StringValue("Average"),
59          new StringValue("High"),
60          new StringValue("Very High"),
61        }));
62    }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new RunCollectionMultiDiscretizer(this, cloner);
65    }
66    #endregion
67
68    #region IRunCollectionModifier Members
69
70    public void Modify(List<IRun> runs) {
71      foreach (var source in Sources) {
72        var newName = source + Suffix;
73        foreach (var group in runs
74          .Select(r => new {Run = r, Value = GetValue(source, r)})
75          .Where(r => r.Value.HasValue && !double.IsNaN(r.Value.Value) && !double.IsInfinity(r.Value.Value))
76          .Select(r => new {r.Run, r.Value.Value, Bin = GetGroupByValue(r.Run)})
77          .GroupBy(r => r.Bin).ToList()) {
78          var values = group.Select(r => r.Value).ToList();
79          if (values.Count > 0) {
80            if (Spread > 0) {
81              var avg = values.Average();
82              var stdDev = values.StandardDeviation();
83              foreach (var r in group) {
84                r.Run.Results[newName] = new StringValue(Discretize(r.Value, avg, stdDev));
85              }
86            }
87            else {
88              values.Sort();
89              var a = values.ToArray();
90              foreach (var r in group) {
91                r.Run.Results[newName] = new StringValue(Discretize(r.Value, a));
92              }
93            }
94          }
95        }
96      }
97    }
98
99    private double? GetValue(string name, IRun run) {
100      return CastValue(name, run.Results) ?? CastValue(name, run.Parameters);
101    }
102
103    private string GetGroupByValue(IRun run) {
104      if (string.IsNullOrWhiteSpace(GroupBy))
105        return String.Empty;
106      IItem value;
107      run.Results.TryGetValue(GroupBy, out value);
108      if (value == null)
109        run.Parameters.TryGetValue(GroupBy, out value);
110      if (value != null)
111        return value.ToString();
112      else
113        return String.Empty;
114    }
115
116    private double? CastValue(string name, IDictionary<string, IItem> variables) {
117      IItem value;
118      variables.TryGetValue(name, out value);
119      var intValue = value as IntValue;
120      if (intValue != null)
121        return intValue.Value;
122      var doubleValue = value as DoubleValue;
123      if (doubleValue != null)
124        return doubleValue.Value;
125      return null;
126    }
127
128    private string Discretize(double value, double avg, double stdDev) {
129      double dev = (value - avg)/(stdDev*Spread);
130      int index;
131      if (Levels.Count % 2 == 1) {
132        index = (int) Math.Floor(Math.Abs(dev));
133        index = (Levels.Count - 1)/2 + Math.Sign(dev) * index;
134      } else {
135        index = (int) Math.Ceiling(Math.Abs(dev));
136        if (dev > 0)
137          index = Levels.Count/2 + index;
138        else
139          index = Levels.Count/2 + 1 - index;
140      }
141      return Levels[Math.Min(Levels.Count - 1, Math.Max(0, index))];
142    }
143
144    private string Discretize(double value, double[] values) {
145      var index = Array.BinarySearch(values, value);
146      var pos = 1.0*(index < 0 ? ~index : index)/(values.Length-1);
147      return Levels[Math.Min(Levels.Count - 1, Math.Max(0, (int) Math.Round(pos*(Levels.Count-1))))];
148    }
149
150    #endregion
151  }
152}
Note: See TracBrowser for help on using the repository browser.