using System.Collections.Generic; using System.Linq; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Data; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using System.Drawing; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("Up Down Aggregator", "Aggregates scalar up down analyses.")] [StorableType("597E3537-ADE8-4B7A-A34B-AD6AF105493F")] public class UpDownAggregator : NamedItem, IAggregator { public static new Image StaticItemImage { get { return VSImageLibrary.Database; } } public override bool CanChangeName { get { return false; } } [Storable] private List downWalkLengths = new List(); [Storable] private List upWalkLengths = new List(); [Storable] private List downWalkLenVars = new List(); [Storable] private List upWalkLenVars = new List(); #region Construction & Cloning [StorableConstructor] protected UpDownAggregator(StorableConstructorFlag _) : base(_) { } protected UpDownAggregator(UpDownAggregator original, Cloner cloner) : base(original, cloner) { downWalkLengths = original.downWalkLengths.ToList(); upWalkLengths = original.upWalkLengths.ToList(); downWalkLenVars = original.downWalkLenVars.ToList(); upWalkLenVars = original.upWalkLenVars.ToList(); } public UpDownAggregator() { name = ItemName; description = ItemDescription; } public override IDeepCloneable Clone(Cloner cloner) { return new UpDownAggregator(this, cloner); } #endregion #region IAggregator interface public virtual void MaybeAddResult(IResult result) { MaybeAddValue(downWalkLengths, "DownWalkLength", result); MaybeAddValue(upWalkLengths, "UpWalkLength", result); MaybeAddValue(downWalkLenVars, "DownWalkLenVar", result); MaybeAddValue(upWalkLenVars, "UpWalkLenVar", result); } private void MaybeAddValue(List values, string resultName, IResult result) { if (result.Name != resultName) return; var value = result.Value as DoubleValue; if (value != null) values.Add(value.Value); } public virtual IResult CreateResult() { var results = new ResultCollection(); results.AddRange(CreateSummary("DownWalkLength", downWalkLengths)); results.AddRange(CreateSummary("UpWalkLength", upWalkLengths)); results.AddRange(CreateSummary("DownWalkLenVar", downWalkLenVars)); results.AddRange(CreateSummary("UpWalkLenVar", upWalkLenVars)); return new Result("UpDown", results); } public virtual void Reset() { downWalkLengths.Clear(); upWalkLengths.Clear(); downWalkLenVars.Clear(); upWalkLenVars.Clear(); } #endregion private IEnumerable CreateSummary(string name, IEnumerable values) { DistributionAnalyzer analyzer = new DistributionAnalyzer(values); return new[] { new Result(name + " Avg", new DoubleValue(analyzer.Mean)), new Result(name + " Quartile-Spread", new DoubleValue(analyzer[0.75]-analyzer[0.25])), new Result(name + " 90%-Spread", new DoubleValue(analyzer[0.95]-analyzer[0.05])), new Result(name + " Range", new DoubleValue(analyzer[1]-analyzer[0])), }; } } }