Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16591 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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