#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.Collections.Generic; 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(); var crossoverChildren = new List>(); var mutationChildren = new List>(); var vertices = graph.Vertices.Where(x => x.Rank > generation - 1); foreach (var v in vertices) { if (v.InDegree == 2) { crossoverChildren.Add(v); } else { var parent = v.Parents.First(); // mutation is always preceded by crossover // so the parent vertex should have an intermediate rank // otherwise, it is the previos generation elite if (parent.Rank.IsAlmost(generation - 1) && parent.IsElite) continue; mutationChildren.Add(v); } } DataTable table; #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 child quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Average crossover parent quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Best crossover child quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Best crossover parent quality") { VisualProperties = { StartIndexZero = true } }, }); } else { table = (DataTable)Results["Crossover improvement"].Value; } var avgCrossoverParentQuality = crossoverChildren.SelectMany(x => x.Parents).Average(x => x.Quality); var avgCrossoverChildQuality = crossoverChildren.Average(x => x.Quality); var bestCrossoverChildQuality = crossoverChildren.OrderBy(x => x.Quality).Last().Quality; var bestCrossoverParentQuality = crossoverChildren.OrderBy(x => x.Quality).Last().Parents.First().Quality; table.Rows["Average crossover child quality"].Values.Add(avgCrossoverChildQuality); table.Rows["Average crossover parent quality"].Values.Add(avgCrossoverParentQuality); table.Rows["Best crossover child quality"].Values.Add(bestCrossoverChildQuality); table.Rows["Best crossover parent quality"].Values.Add(bestCrossoverParentQuality); #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 child quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Average mutation parent quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Best mutation child quality") { VisualProperties = { StartIndexZero = true } }, new DataRow("Best mutation parent quality") { VisualProperties = { StartIndexZero = true } }, }); } else { table = (DataTable)Results["Mutation improvement"].Value; } var avgMutationParentQuality = mutationChildren.SelectMany(x => x.Parents).Average(x => x.Quality); var avgMutationChildQuality = mutationChildren.Average(x => x.Quality); var bestMutationChildQuality = mutationChildren.OrderBy(x => x.Quality).Last().Quality; var bestMutationParentQuality = mutationChildren.OrderBy(x => x.Quality).Last().Parents.First().Quality; table.Rows["Average mutation child quality"].Values.Add(avgMutationChildQuality); table.Rows["Average mutation parent quality"].Values.Add(avgMutationParentQuality); table.Rows["Best mutation child quality"].Values.Add(bestMutationChildQuality); table.Rows["Best mutation parent quality"].Values.Add(bestMutationParentQuality); #endregion return base.Apply(); } } }