#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Analysis.FitnessLandscape {
[StorableType("FBA53C13-1456-42F7-B594-01FCA2C58809")]
public class InformationAnalyzer : SingleSuccessorOperator, IQualityTrailAnalyzer {
public bool EnabledByDefault {
get { return false; }
}
#region Parameters
public LookupParameter QualityTrailParameter {
get { return (LookupParameter)Parameters["Quality Trail"]; }
}
public LookupParameter ResultsParameter {
get { return (LookupParameter)Parameters["Results"]; }
}
public ValueLookupParameter NQuantilesParameter {
get { return (ValueLookupParameter)Parameters["NQuantiles"]; }
}
public ValueLookupParameter ShapeSizeParameter {
get { return (ValueLookupParameter)Parameters["ShapeSize"]; }
}
public LookupParameter InformationContentParameter {
get { return (LookupParameter)Parameters["InformationContent"]; }
}
public LookupParameter PartialInformationContentParameter {
get { return (LookupParameter)Parameters["PartialInformationContent"]; }
}
public LookupParameter DensityBasinInformationParameter {
get { return (LookupParameter)Parameters["DensityBasinInformation"]; }
}
public LookupParameter TotalEntropyParameter {
get { return (LookupParameter)Parameters["TotalEntropy"]; }
}
public LookupParameter InformationStabilityParameter {
get { return (LookupParameter)Parameters["InformationStability"]; }
}
public LookupParameter RegularityParameter {
get { return (LookupParameter)Parameters["Regularity"]; }
}
public LookupParameter DiversityParameter {
get { return (LookupParameter)Parameters["Diversity"]; }
}
public LookupParameter PeakInformationContentParameter {
get { return (LookupParameter)Parameters["PeakInformationContent"]; }
}
public LookupParameter PeakDensityBasinInformationParameter {
get { return (LookupParameter)Parameters["PeakDensityBasinInformation"]; }
}
public ILookupParameter InformationAnalysisParameter {
get { return (ILookupParameter)Parameters["InformationAnalysis"]; }
}
#endregion
[StorableConstructor]
protected InformationAnalyzer(StorableConstructorFlag _) : base(_) { }
protected InformationAnalyzer(InformationAnalyzer original, Cloner cloner) : base(original, cloner) { }
public InformationAnalyzer() {
Parameters.Add(new LookupParameter("Quality Trail", "The qualities of the solutions"));
Parameters.Add(new LookupParameter("Results", "The collection of all results of this algorithm"));
Parameters.Add(new ValueLookupParameter("NQuantiles", "The number of delta quantiles to display", new IntValue(20)));
Parameters.Add(new ValueLookupParameter("ShapeSize", "The number of slopes to consider as shapes.", new IntValue(2)));
Parameters.Add(new LookupParameter("InformationContent", "The information content H(0) at eps = 0"));
Parameters.Add(new LookupParameter("PartialInformationContent", "Partial information content M(0) at eps = 0"));
Parameters.Add(new LookupParameter("DensityBasinInformation", "Density Basin Information h(0) at eps = 0"));
Parameters.Add(new LookupParameter("TotalEntropy", "The overall disorder in the trajectory"));
Parameters.Add(new LookupParameter("InformationStability", "Information Stability Value"));
Parameters.Add(new LookupParameter("Regularity", "The number of different quality differences"));
Parameters.Add(new LookupParameter("Diversity", "The number of different quality values"));
Parameters.Add(new LookupParameter("PeakInformationContent", "Maximum information content at any quality delta."));
Parameters.Add(new LookupParameter("PeakDensityBasinInformation", "Maximum density basin information at any quality delta."));
Parameters.Add(new LookupParameter("InformationAnalysis", "Graphical analysis of information theoretic features."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new InformationAnalyzer(this, cloner);
}
public override IOperation Apply() {
var qualityTrail = QualityTrailParameter.ActualValue;
if (qualityTrail == null || qualityTrail.Rows.Count == 0) return base.Apply();
var qualities = QualityTrailParameter.ActualValue.Rows.First().Values.ToList();
if (qualities.Count <= 2) return base.Apply();
var nQuantiles = NQuantilesParameter.ActualValue.Value;
var shapeSize = ShapeSizeParameter.ActualValue.Value;
var results = ResultsParameter.ActualValue;
var analysis = new InformationAnalysis(qualities, nQuantiles, shapeSize);
var ic = new DoubleValue(analysis.InformationContent.FirstOrDefault());
InformationContentParameter.ActualValue = ic;
AddOrUpdateResult(results, InformationContentParameter.Name, ic);
var pic = new DoubleValue(analysis.PartialInformationContent.FirstOrDefault());
PartialInformationContentParameter.ActualValue = pic;
AddOrUpdateResult(results, PartialInformationContentParameter.Name, pic);
var dbi = new DoubleValue(analysis.DensityBasinInformation.FirstOrDefault());
DensityBasinInformationParameter.ActualValue = dbi;
AddOrUpdateResult(results, DensityBasinInformationParameter.Name, dbi);
var @is = new DoubleValue(analysis.InformationStability);
InformationStabilityParameter.ActualValue = @is;
AddOrUpdateResult(results, InformationStabilityParameter.Name, @is);
var regularity = new DoubleValue(1.0 * analysis.Regularity / qualities.Count);
RegularityParameter.ActualValue = regularity;
AddOrUpdateResult(results, RegularityParameter.Name, regularity);
var diversity = new DoubleValue(1.0 * analysis.Diversity / qualities.Count);
DiversityParameter.ActualValue = diversity;
AddOrUpdateResult(results, DiversityParameter.Name, diversity);
var totalEntropy = new DoubleValue(analysis.TotalEntropy.FirstOrDefault());
TotalEntropyParameter.ActualValue = totalEntropy;
AddOrUpdateResult(results, TotalEntropyParameter.Name, totalEntropy);
var peakIc = new DoubleValue(analysis.PeakInformationContent.Value);
PeakInformationContentParameter.ActualValue = peakIc;
AddOrUpdateResult(results, PeakInformationContentParameter.Name, peakIc);
var peakDbi = new DoubleValue(analysis.PeakDensityBasinInformation.Value);
PeakDensityBasinInformationParameter.ActualValue = peakDbi;
AddOrUpdateResult(results, PeakDensityBasinInformationParameter.Name, peakDbi);
var itable = GetInformationTable(analysis);
AddOrUpdateResult(results, InformationAnalysisParameter.Name, itable);
return base.Apply();
}
private static DataTable GetInformationTable(InformationAnalysis analysis) {
var dt = new DataTable("Information Analysis");
var ic = new DataRow("Information Content");
var pic = new DataRow("Partial Information Content");
var dbi = new DataRow("Density Basin Information");
var te = new DataRow("Total Entropy");
var e = new DataRow("Epsilon");
e.VisualProperties.SecondYAxis = true;
ic.Values.AddRange(analysis.InformationContent);
pic.Values.AddRange(analysis.PartialInformationContent);
dbi.Values.AddRange(analysis.DensityBasinInformation);
te.Values.AddRange(analysis.TotalEntropy);
e.Values.AddRange(analysis.QualityDelta);
dt.Rows.Add(ic);
dt.Rows.Add(pic);
dt.Rows.Add(dbi);
dt.Rows.Add(te);
dt.Rows.Add(e);
return dt;
}
private static void AddOrUpdateResult(ResultCollection results, string name, IItem item, bool clone = false) {
IResult r;
if (!results.TryGetValue(name, out r)) {
results.Add(new Result(name, clone ? (IItem)item.Clone() : item));
} else r.Value = clone ? (IItem)item.Clone() : item;
}
}
}