Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionDiscretizer.cs @ 9793

Last change on this file since 9793 was 7228, checked in by epitzer, 13 years ago

#1622: Move run collection modifiers into a separate folder

File size: 7.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Optimization {
12  [Item("RunCollection Discretizer",
13    "Creates several levels from the distribution of a certain result 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  [StorableClass]
16  public class RunCollectionDiscretizer : 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<StringValue> SourceParameter {
23      get { return (ValueParameter<StringValue>)Parameters["Source"]; }
24    }
25    public ValueParameter<StringValue> TargetParameter {
26      get { return (ValueParameter<StringValue>)Parameters["Target"]; }
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 string Source { get { return SourceParameter.Value.Value; } }
40    private string Target {
41      get { return TargetParameter.Value.Value; }
42      set { TargetParameter.Value.Value = value; }
43    }   
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 RunCollectionDiscretizer(bool deserializing) : base(deserializing) { }
51    protected RunCollectionDiscretizer(RunCollectionDiscretizer original, Cloner cloner)
52      : base(original, cloner) {
53      RegisterEvents();
54    }
55    public RunCollectionDiscretizer() {
56      Parameters.Add(new ValueParameter<StringValue>("Source", "Source value name to be fuzzified.", new StringValue("Value")));
57      Parameters.Add(new ValueParameter<StringValue>("Target", "Target value name. The new, fuzzified variable to be created.", new StringValue("Calc.Value")));
58      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)));
59      Parameters.Add(new ValueParameter<StringValue>("GroupBy", "Create separate analyzes for different values of this variable.", new StringValue("")));
60      Parameters.Add(new ValueParameter<ItemList<StringValue>>("Levels", "The list of levels to be assigned.",
61        new ItemList<StringValue> {
62          new StringValue("Very Low"),
63          new StringValue("Low"),
64          new StringValue("Average"),
65          new StringValue("High"),
66          new StringValue("Very High"),
67        }));     
68      RegisterEvents();
69      UpdateName();
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new RunCollectionDiscretizer(this, cloner);
73    }
74    [StorableHook(HookType.AfterDeserialization)]
75    private void AfterDeserialization() {
76      RegisterEvents();
77    }
78    #endregion
79
80    private void RegisterEvents() {
81      SourceParameter.ToStringChanged += SourceParameter_NameChanged;
82      TargetParameter.ToStringChanged += Parameter_NameChanged;
83      GroupByParameter.ToStringChanged += Parameter_NameChanged;
84    }
85
86    private void SourceParameter_NameChanged(object sender, EventArgs e) {
87      Target = string.Format("{0}/Level", Source);
88    }
89
90    private void Parameter_NameChanged(object sender, EventArgs e) {
91      UpdateName();
92    }
93
94    private void UpdateName() {
95      name = string.Format("{0} := Discrete({1}{2})",
96        Target,
97        Source,
98        string.IsNullOrWhiteSpace(GroupBy) ? "" : string.Format("/{0}", GroupBy));       
99      OnNameChanged();
100    }
101
102    #region IRunCollectionModifier Members
103
104    public void Modify(List<IRun> runs) {
105      foreach (var group in runs
106        .Select(r => new {Run=r, Value=GetSourceValue(r)})
107        .Where(r => r.Value.HasValue && !double.IsNaN(r.Value.Value) && !double.IsInfinity(r.Value.Value))
108        .Select(r => new {r.Run, r.Value.Value, Bin=GetGroupByValue(r.Run)})
109        .GroupBy(r => r.Bin).ToList()) {
110        var values = group.Select(r => r.Value).ToList();
111        if (values.Count > 0) {
112          if (Spread > 0) {
113            var avg = values.Average();
114            var stdDev = values.StandardDeviation();
115            foreach (var r in group) {
116              r.Run.Results[Target] = new StringValue(Discretize(r.Value, avg, stdDev));
117            }
118          } else {
119            values.Sort();
120            var a = values.ToArray();
121            foreach (var r in group) {
122              r.Run.Results[Target] = new StringValue(Discretize(r.Value, a));
123            }
124          }
125        }
126      }     
127    }
128
129    private double? GetSourceValue(IRun run) {
130      return CastSourceValue(run.Results) ?? CastSourceValue(run.Parameters);
131    }
132
133    private string GetGroupByValue(IRun run) {
134      if (string.IsNullOrWhiteSpace(GroupBy))
135        return String.Empty;
136      IItem value;
137      run.Results.TryGetValue(GroupBy, out value);
138      if (value == null)
139        run.Parameters.TryGetValue(GroupBy, out value);
140      if (value != null)
141        return value.ToString();
142      else
143        return String.Empty;
144    }
145
146    private double? CastSourceValue(IDictionary<string, IItem> variables) {
147      IItem value;
148      variables.TryGetValue(Source, out value);
149      var intValue = value as IntValue;
150      if (intValue != null)
151        return intValue.Value;
152      var doubleValue = value as DoubleValue;
153      if (doubleValue != null)
154        return doubleValue.Value;
155      return null;
156    }
157
158    private string Discretize(double value, double avg, double stdDev) {
159      double dev = (value - avg)/(stdDev*Spread);
160      int index;
161      if (Levels.Count % 2 == 1) {
162        index = (int) Math.Floor(Math.Abs(dev));
163        index = (Levels.Count - 1)/2 + Math.Sign(dev) * index;
164      } else {
165        index = (int) Math.Ceiling(Math.Abs(dev));
166        if (dev > 0)
167          index = Levels.Count/2 + index;
168        else
169          index = Levels.Count/2 + 1 - index;
170      }
171      return Levels[Math.Min(Levels.Count - 1, Math.Max(0, index))];
172    }
173
174    private string Discretize(double value, double[] values) {
175      var index = Array.BinarySearch(values, value);
176      var pos = 1.0*(index < 0 ? ~index : index)/(values.Length-1);
177      return Levels[Math.Min(Levels.Count - 1, Math.Max(0, (int) Math.Round(pos*(Levels.Count-1))))];
178    }
179
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.