using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Parameters; using HeuristicLab.Data; using HEAL.Attic; namespace HeuristicLab.Optimization.RunModification { [Item("RunCollection MultiDiscretizer", "Creates several levels from the distribution of several results accross a run collection and " + "assigns a discretized value. Non-existing numbers as well as NaN and infinities are excluded from the caluclation.")] [StorableType("74B90692-F3F2-4E0B-AA17-C3CDD710D009")] public class RunCollectionMultiDiscretizer : ParameterizedNamedItem, IRunCollectionModifier { public override bool CanChangeName { get { return false; } } public override bool CanChangeDescription { get { return false; } } #region Parameters public ValueParameter> SourceParameter { get { return (ValueParameter>)Parameters["Sources"]; } } public ValueParameter SuffixParameter { get { return (ValueParameter)Parameters["Suffix"]; } } public ValueParameter SpreadParameter { get { return (ValueParameter)Parameters["Spread"]; } } public ValueParameter GroupByParameter { get { return (ValueParameter)Parameters["GroupBy"]; } } public ValueParameter> LevelsParameter { get { return (ValueParameter>)Parameters["Levels"]; } } #endregion private IEnumerable Sources { get { return SourceParameter.Value.CheckedItems.Select(i => i.Value.Value); } } private string Suffix { get { return SuffixParameter.Value.Value; } } private double Spread { get { return SpreadParameter.Value.Value; } } private string GroupBy { get { return GroupByParameter.Value.Value; } } private List Levels { get { return LevelsParameter.Value.Select(v => v.Value).ToList(); } } #region Construction & Cloning [StorableConstructor] protected RunCollectionMultiDiscretizer(StorableConstructorFlag _) : base(_) { } protected RunCollectionMultiDiscretizer(RunCollectionMultiDiscretizer original, Cloner cloner) : base(original, cloner) { } public RunCollectionMultiDiscretizer() { Parameters.Add(new ValueParameter>("Sources", "Source value names to be discretized.")); Parameters.Add(new ValueParameter("Suffix", "Suffix attached to new discretized values.", new StringValue("/Level"))); Parameters.Add(new ValueParameter("Spread", "The number of standard deviations considered one additional level. Set to zero to use empirical distribution instead.", new DoubleValue(1))); Parameters.Add(new ValueParameter("GroupBy", "Create separate analyzes for different values of this variable.", new StringValue(""))); Parameters.Add(new ValueParameter>("Levels", "The list of levels to be assigned.", new ItemList { new StringValue("Very Low"), new StringValue("Low"), new StringValue("Average"), new StringValue("High"), new StringValue("Very High"), })); } public override IDeepCloneable Clone(Cloner cloner) { return new RunCollectionMultiDiscretizer(this, cloner); } #endregion #region IRunCollectionModifier Members public void Modify(List runs) { foreach (var source in Sources) { var newName = source + Suffix; foreach (var group in runs .Select(r => new {Run = r, Value = GetValue(source, r)}) .Where(r => r.Value.HasValue && !double.IsNaN(r.Value.Value) && !double.IsInfinity(r.Value.Value)) .Select(r => new {r.Run, r.Value.Value, Bin = GetGroupByValue(r.Run)}) .GroupBy(r => r.Bin).ToList()) { var values = group.Select(r => r.Value).ToList(); if (values.Count > 0) { if (Spread > 0) { var avg = values.Average(); var stdDev = values.StandardDeviation(); foreach (var r in group) { r.Run.Results[newName] = new StringValue(Discretize(r.Value, avg, stdDev)); } } else { values.Sort(); var a = values.ToArray(); foreach (var r in group) { r.Run.Results[newName] = new StringValue(Discretize(r.Value, a)); } } } } } } private double? GetValue(string name, IRun run) { return CastValue(name, run.Results) ?? CastValue(name, run.Parameters); } private string GetGroupByValue(IRun run) { if (string.IsNullOrWhiteSpace(GroupBy)) return String.Empty; IItem value; run.Results.TryGetValue(GroupBy, out value); if (value == null) run.Parameters.TryGetValue(GroupBy, out value); if (value != null) return value.ToString(); else return String.Empty; } private double? CastValue(string name, IDictionary variables) { IItem value; variables.TryGetValue(name, out value); var intValue = value as IntValue; if (intValue != null) return intValue.Value; var doubleValue = value as DoubleValue; if (doubleValue != null) return doubleValue.Value; return null; } private string Discretize(double value, double avg, double stdDev) { double dev = (value - avg)/(stdDev*Spread); int index; if (Levels.Count % 2 == 1) { index = (int) Math.Floor(Math.Abs(dev)); index = (Levels.Count - 1)/2 + Math.Sign(dev) * index; } else { index = (int) Math.Ceiling(Math.Abs(dev)); if (dev > 0) index = Levels.Count/2 + index; else index = Levels.Count/2 + 1 - index; } return Levels[Math.Min(Levels.Count - 1, Math.Max(0, index))]; } private string Discretize(double value, double[] values) { var index = Array.BinarySearch(values, value); var pos = 1.0*(index < 0 ? ~index : index)/(values.Length-1); return Levels[Math.Min(Levels.Count - 1, Math.Max(0, (int) Math.Round(pos*(Levels.Count-1))))]; } #endregion } }