using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; using HeuristicLab.Common.Resources; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using System.Drawing; using HeuristicLab.Analysis.FitnessLandscape.Analysis; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("Ruggedness Aggregator", "Aggregates scalar ruggedness analyses.")] [StorableClass] public class RuggednessAggregator : NamedItem, IAggregator { public static new Image StaticItemImage { get { return VSImageLibrary.Database; } } public override bool CanChangeName { get { return false; } } private List correlationLengthValues = new List(); private List autocorrelations = new List(); #region Construction & Cloning [StorableConstructor] protected RuggednessAggregator(bool deserializing) : base(deserializing) { } protected RuggednessAggregator(RuggednessAggregator original, Cloner cloner) : base(original, cloner) { autocorrelations = original.autocorrelations.ToList(); correlationLengthValues = original.correlationLengthValues.ToList(); } public RuggednessAggregator() { name = ItemName; description = ItemDescription; } public override IDeepCloneable Clone(Cloner cloner) { return new RuggednessAggregator(this, cloner); } #endregion #region IAggregator interface public virtual void MaybeAddResult(IResult result) { MaybeAddAutoCorrelation1(result); MaybeAddCorrelationLength(result); } public virtual IResult CreateResult() { var results = new ResultCollection(); results.AddRange(CreateSummary("AutoCorrelation1", autocorrelations)); results.AddRange(CreateSummary("CorrelationLength", correlationLengthValues.Select(l => (double)l))); return new Result("Ruggedness", results); } public virtual void Reset() { autocorrelations.Clear(); correlationLengthValues.Clear(); } #endregion private void MaybeAddAutoCorrelation1(IResult result) { if (result.Name != "AutoCorrelation1") return; DoubleValue autocorrelation1 = result.Value as DoubleValue; if (autocorrelation1 != null) autocorrelations.Add(autocorrelation1.Value); } private void MaybeAddCorrelationLength(IResult result) { if (result.Name != "CorrelationLength") return; IntValue correlationLength = result.Value as IntValue; if (correlationLength != null) correlationLengthValues.Add(correlationLength.Value); else { DoubleValue correlationLength2 = result.Value as DoubleValue; if (correlationLength2 != null) correlationLengthValues.Add(correlationLength2.Value); } } 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])), }; } } }