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