#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.EvolutionTracking;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
[StorableClass]
[Item("SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer", "An analyzer which records the best and average genetic operator improvement")]
public class SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer : EvolutionTrackingAnalyzer {
public const string QualityParameterName = "Quality";
public const string PopulationParameterName = "SymbolicExpressionTree";
public const string CountIntermediateChildrenParameterName = "CountIntermediateChildren";
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters[QualityParameterName]; }
}
public IScopeTreeLookupParameter PopulationParameter {
get { return (IScopeTreeLookupParameter)Parameters[PopulationParameterName]; }
}
public IFixedValueParameter CountIntermediateChildrenParameter {
get { return (IFixedValueParameter)Parameters[CountIntermediateChildrenParameterName]; }
}
public bool CountIntermediateChildren {
get { return CountIntermediateChildrenParameter.Value.Value; }
set { CountIntermediateChildrenParameter.Value.Value = value; }
}
public SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer() {
Parameters.Add(new ScopeTreeLookupParameter(PopulationParameterName, "The population of individuals."));
Parameters.Add(new ScopeTreeLookupParameter(QualityParameterName, "The individual qualities."));
Parameters.Add(new FixedValueParameter(CountIntermediateChildrenParameterName, "Specifies whether to consider intermediate children (when crossover was followed by mutation). This should be set to false for offspring selection.", new BoolValue(true)));
CountIntermediateChildrenParameter.Hidden = true;
}
[StorableConstructor]
protected SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer(bool deserializing) : base(deserializing) { }
public SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer(
SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer original, Cloner cloner) : base(original, cloner) {
CountIntermediateChildren = original.CountIntermediateChildren;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey(CountIntermediateChildrenParameterName))
Parameters.Add(new FixedValueParameter(CountIntermediateChildrenParameterName, "Specifies whether to consider intermediate children (when crossover was followed by mutation", new BoolValue(true)));
CountIntermediateChildrenParameter.Hidden = true;
}
public override IOperation Apply() {
IntValue updateCounter = UpdateCounterParameter.ActualValue;
if (updateCounter == null) {
updateCounter = new IntValue(0);
UpdateCounterParameter.ActualValue = updateCounter;
}
updateCounter.Value++;
if (updateCounter.Value != UpdateInterval.Value) return base.Apply();
updateCounter.Value = 0;
var graph = PopulationGraph;
if (graph == null || Generation.Value == 0)
return base.Apply();
var generation = Generation.Value;
var averageQuality = QualityParameter.ActualValue.Average(x => x.Value);
var population = PopulationParameter.ActualValue;
var populationSize = population.Length;
var vertices = population.Select(graph.GetByContent).ToList();
DataTable table;
double aac = 0; // ratio of above average children produced
double aacp = 0; // ratio of above average children from above average parents
#region crossover improvement
if (!Results.ContainsKey("Crossover improvement")) {
table = new DataTable("Crossover improvement");
Results.Add(new Result("Crossover improvement", table));
table.Rows.AddRange(new[]
{
new DataRow("Average crossover improvement (root parent)") { VisualProperties = { StartIndexZero = true } },
new DataRow("Average crossover improvement (non-root parent)") { VisualProperties = { StartIndexZero = true } },
new DataRow("Average child-parents quality difference") { VisualProperties = { StartIndexZero = true } },
new DataRow("Best crossover improvement (root parent)") { VisualProperties = { StartIndexZero = true } },
new DataRow("Best crossover improvement (non-root parent)") { VisualProperties = { StartIndexZero = true }},
new DataRow("Above average children") { VisualProperties = { StartIndexZero = true }},
new DataRow("Above average children from above average parents") { VisualProperties = { StartIndexZero = true } },
});
} else {
table = (DataTable)Results["Crossover improvement"].Value;
}
var crossoverChildren = vertices.Where(x => x.InDegree == 2).ToList();
if (CountIntermediateChildren)
crossoverChildren.AddRange(vertices.Where(x => x.InDegree == 1).Select(v => v.Parents.First()).Where(p => p.Rank.IsAlmost(generation - 0.5))); // add intermediate children
foreach (var c in crossoverChildren) {
if (c.Quality > averageQuality) {
aac++;
if (c.Parents.All(x => x.Quality > averageQuality))
aacp++;
}
}
var avgRootParentQualityImprovement = crossoverChildren.Average(x => x.Quality - x.Parents.First().Quality);
var avgNonRootParentQualityImprovement = crossoverChildren.Average(x => x.Quality - x.Parents.Last().Quality);
var avgChildParentQuality = crossoverChildren.Average(x => x.Quality - x.Parents.Average(p => p.Quality));
var bestRootParentQualityImprovement = crossoverChildren.Max(x => x.Quality - x.Parents.First().Quality);
var bestNonRootParentQualityImprovement = crossoverChildren.Max(x => x.Quality - x.Parents.Last().Quality);
table.Rows["Average crossover improvement (root parent)"].Values.Add(avgRootParentQualityImprovement);
table.Rows["Average crossover improvement (non-root parent)"].Values.Add(avgNonRootParentQualityImprovement);
table.Rows["Best crossover improvement (root parent)"].Values.Add(bestRootParentQualityImprovement);
table.Rows["Best crossover improvement (non-root parent)"].Values.Add(bestNonRootParentQualityImprovement);
table.Rows["Average child-parents quality difference"].Values.Add(avgChildParentQuality);
table.Rows["Above average children"].Values.Add(aac / populationSize);
table.Rows["Above average children from above average parents"].Values.Add(aacp / populationSize);
#endregion
#region mutation improvement
if (!Results.ContainsKey("Mutation improvement")) {
table = new DataTable("Mutation improvement");
Results.Add(new Result("Mutation improvement", table));
table.Rows.AddRange(new[]
{
new DataRow("Average mutation improvement") { VisualProperties = { StartIndexZero = true } },
new DataRow("Best mutation improvement") { VisualProperties = { StartIndexZero = true } },
new DataRow("Above average children") { VisualProperties = { StartIndexZero = true } },
new DataRow("Above average children from above average parents") { VisualProperties = { StartIndexZero = true } },
});
} else {
table = (DataTable)Results["Mutation improvement"].Value;
}
aac = 0;
aacp = 0;
var mutationChildren = vertices.Where(x => x.InDegree == 1).ToList();
foreach (var c in mutationChildren) {
if (c.Quality > averageQuality) {
aac++;
if (c.Parents.All(x => x.Quality > averageQuality))
aacp++;
}
}
var avgMutationImprovement = mutationChildren.Average(x => x.Quality - x.Parents.First().Quality);
var bestMutationImprovement = mutationChildren.Max(x => x.Quality - x.Parents.First().Quality);
table.Rows["Average mutation improvement"].Values.Add(avgMutationImprovement);
table.Rows["Best mutation improvement"].Values.Add(bestMutationImprovement);
table.Rows["Above average children"].Values.Add(aac / populationSize);
table.Rows["Above average children from above average parents"].Values.Add(aacp / populationSize);
#endregion
return base.Apply();
}
}
}