using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Analysis.FitnessLandscape.DataTables; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape.Algorithms { [Item("RepeatedInformationAnalyzer", "Analyzes and consolidates repeated information analyses.")] [StorableType("A53A3F9B-BC38-47E9-B435-42DEA10A4ABC")] public class RepeatedInformationAnalyzer : SingleSuccessorOperator, IRepeatsAnalyzer { #region Parameters public ScopeTreeLookupParameter InformationsParameter { get { return (ScopeTreeLookupParameter)Parameters["Information"]; } } public ScopeTreeLookupParameter InformationContentValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["InformationContentValue"]; } } public ScopeTreeLookupParameter PartialInformationContentValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["PartialInformationContentValue"]; } } public ScopeTreeLookupParameter DensityBasinInformationValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["DensityBasinInformationValue"]; } } public ScopeTreeLookupParameter InformationStabilityValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["InformationStabilityValue"]; } } public ScopeTreeLookupParameter RegularityValuesParameter { get { return (ScopeTreeLookupParameter)Parameters["RegularityValue"]; } } public LookupParameter ResultsParameter { get { return (LookupParameter)Parameters["Results"]; } } #endregion #region Construction & Cloning [StorableConstructor] protected RepeatedInformationAnalyzer(StorableConstructorFlag _) : base(_) { } protected RepeatedInformationAnalyzer(RepeatedInformationAnalyzer original, Cloner cloner) : base(original, cloner) { } public RepeatedInformationAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("Information", "A data table that information theoretic fitness landscape characteristics")); Parameters.Add(new ScopeTreeLookupParameter("InformationContentValue", "The information content H(0) at eps = 0")); Parameters.Add(new ScopeTreeLookupParameter("PartialInformationContentValue", "Partial information content M(0) at eps = 0")); Parameters.Add(new ScopeTreeLookupParameter("DensityBasinInformationValue", "Density Basin Information h(0) at eps = 0")); Parameters.Add(new ScopeTreeLookupParameter("InformationStabilityValue", "Information Stability Value")); Parameters.Add(new ScopeTreeLookupParameter("RegularityValue", "The number of different quality differences")); Parameters.Add(new LookupParameter("Results", "The collection of all results of this algorithm")); InformationsParameter.Depth = 2; InformationContentValuesParameter.Depth = 2; PartialInformationContentValuesParameter.Depth = 2; DensityBasinInformationValuesParameter.Depth = 2; InformationStabilityValuesParameter.Depth = 2; RegularityValuesParameter.Depth = 2; } public override IDeepCloneable Clone(Cloner cloner) { return new RepeatedInformationAnalyzer(this, cloner); } #endregion private class Entry : IComparable { public double InformationContent { get; private set; } public double PartialInformationContent { get; private set; } public double DensityBasinInformation { get; private set; } public double Epsilon { get; private set; } public Entry(double epsilon, double informationContent, double partialInformationContent, double densityBasinInformation) { Epsilon = epsilon; InformationContent = informationContent; PartialInformationContent = partialInformationContent; DensityBasinInformation = densityBasinInformation; } public int CompareTo(Entry other) { return Epsilon.CompareTo(other.Epsilon); } public override bool Equals(object obj) { Entry e = obj as Entry; if (e == null) throw new NotSupportedException(); return this.Epsilon == e.Epsilon; } public override int GetHashCode() { return Epsilon.GetHashCode(); } } public override IOperation Apply() { ResultCollection results = ResultsParameter.ActualValue; var informationTables = InformationsParameter.ActualValue.ToList(); List entries = new List(); foreach (var t in informationTables) { var values = t.Rows["Quality Delta"].Values; var info = t.Rows["Information Content"].Values; var partInfo = t.Rows["Partial Information Content"].Values; var densBas = t.Rows["Density Basin Information"].Values; var eps = t.Rows["Quality Delta"].Values; for (int i = 0; i < info.Count; i++) { entries.Add(new Entry(eps[i], info[i], partInfo[i], densBas[i])); } } entries.Sort(); double informationSum = 0; double partialInformationSum = 0; double densityBasinSum = 0; double epsilonSum = 0; DataRow informationRow = new DataRow("Information Content"); DataRow partialInformationRow = new DataRow("Partial Information Content"); DataRow densityBasinRow = new DataRow("Density Basin Information"); DataRow epsilonRow = new DataRow("Quality Delta"); epsilonRow.VisualProperties.SecondYAxis = true; int n = 0; foreach (var entry in entries) { informationSum += entry.InformationContent; partialInformationSum += entry.PartialInformationContent; densityBasinSum += entry.DensityBasinInformation; epsilonSum += entry.Epsilon; n++; if (n == informationTables.Count) { informationRow.Values.Add(informationSum/n); informationSum = 0; partialInformationRow.Values.Add(partialInformationSum/n); partialInformationSum = 0; densityBasinRow.Values.Add(densityBasinSum/n); densityBasinSum = 0; epsilonRow.Values.Add(epsilonSum/n); epsilonSum = 0; n = 0; } } if (n > 0) { informationRow.Values.Add(informationSum / n); partialInformationRow.Values.Add(partialInformationSum / n); densityBasinRow.Values.Add(densityBasinSum / n); epsilonRow.Values.Add(epsilonSum / n); } InformationAnalysisTable table = new InformationAnalysisTable("Avg. Information Analysis"); table.Rows.Add(informationRow); table.Rows.Add(partialInformationRow); table.Rows.Add(densityBasinRow); table.Rows.Add(epsilonRow); results.Remove("Information"); results.Add(new Result("Information", table)); results.Remove("InformationContentValue"); results.Remove("PartialInformationContentValue"); results.Remove("DensityBasinInformationValue"); results.Remove("InformationStabilityValue"); results.Add(new Result("InformationContentValue", new DoubleValue(InformationContentValuesParameter.ActualValue.Select(v => v.Value).Average()))); results.Add(new Result("PartialInformationContentValue", new DoubleValue(PartialInformationContentValuesParameter.ActualValue.Select(v => v.Value).Average()))); results.Add(new Result("DensityBasinInformationValue", new DoubleValue(DensityBasinInformationValuesParameter.ActualValue.Select(v => v.Value).Average()))); results.Add(new Result("InformationStabilityValue", new DoubleValue(InformationStabilityValuesParameter.ActualValue.Select(v => v.Value).Average()))); return base.Apply(); } } }