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