Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer.cs @ 12892

Last change on this file since 12892 was 12892, checked in by bburlacu, 9 years ago

#1772: Made GenealogyAnalyzer class abstract and added abstract method EvaluateIntermediateChildren in order to assign qualities to intermediate vertices in the genealogy graph. Implemented SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer which shows parent-child quality statistics for crossover and mutation.

File size: 10.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.EvolutionTracking;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
34  [StorableClass]
35  [Item("SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer", "An analyzer which records the best and average genetic operator improvement")]
36  public class SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
37    public const string QualityParameterName = "Quality";
38    public const string PopulationParameterName = "SymbolicExpressionTree";
39    public const string CountIntermediateChildrenParameterName = "CountIntermediateChildren";
40
41    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
42      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
43    }
44
45    public IScopeTreeLookupParameter<ISymbolicExpressionTree> PopulationParameter {
46      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[PopulationParameterName]; }
47    }
48
49    public IFixedValueParameter<BoolValue> CountIntermediateChildrenParameter {
50      get { return (IFixedValueParameter<BoolValue>)Parameters[CountIntermediateChildrenParameterName]; }
51    }
52
53    public bool CountIntermediateChildren {
54      get { return CountIntermediateChildrenParameter.Value.Value; }
55      set { CountIntermediateChildrenParameter.Value.Value = value; }
56    }
57
58    public SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer() {
59      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(PopulationParameterName, "The population of individuals."));
60      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The individual qualities."));
61      Parameters.Add(new FixedValueParameter<BoolValue>(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)));
62
63      CountIntermediateChildrenParameter.Hidden = true;
64    }
65
66    public SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer(
67    SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer original, Cloner cloner) : base(original, cloner) {
68      CountIntermediateChildren = original.CountIntermediateChildren;
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new SymbolicDataAnalysisGeneticOperatorImprovementAnalyzer(this, cloner);
73    }
74
75    [StorableHook(HookType.AfterDeserialization)]
76    private void AfterDeserialization() {
77      if (!Parameters.ContainsKey(CountIntermediateChildrenParameterName))
78        Parameters.Add(new FixedValueParameter<BoolValue>(CountIntermediateChildrenParameterName, "Specifies whether to consider intermediate children (when crossover was followed by mutation", new BoolValue(true)));
79      CountIntermediateChildrenParameter.Hidden = true;
80    }
81
82    public override void ClearState() {
83      base.ClearState();
84      UpdateCounter.Value = 0;
85    }
86
87    public override IOperation Apply() {
88      int updateInterval = UpdateIntervalParameter.Value.Value;
89      IntValue updateCounter = UpdateCounterParameter.ActualValue;
90      if (updateCounter == null) {
91        updateCounter = new IntValue(updateInterval);
92        UpdateCounterParameter.ActualValue = updateCounter;
93      } else updateCounter.Value++;
94
95      if (updateCounter.Value == updateInterval) {
96        updateCounter.Value = 0;
97
98        var graph = PopulationGraph;
99        if (graph == null || Generation.Value == 0)
100          return base.Apply();
101
102        var generation = Generation.Value;
103        var averageQuality = QualityParameter.ActualValue.Average(x => x.Value);
104        var population = PopulationParameter.ActualValue;
105        var populationSize = population.Length;
106
107        var vertices = population.Select(graph.GetByContent).ToList();
108        DataTable table;
109        double aac = 0; // ratio of above average children produced
110        double aacp = 0; // ratio of above average children from above average parents
111        #region crossover improvement
112        if (!Results.ContainsKey("Crossover improvement")) {
113          table = new DataTable("Crossover improvement");
114          Results.Add(new Result("Crossover improvement", table));
115          table.Rows.AddRange(
116            new[]
117            {
118              new DataRow("Average crossover improvement (root parent)") {VisualProperties = {StartIndexZero = true}},
119              new DataRow("Average crossover improvement (non-root parent)") {VisualProperties = {StartIndexZero = true}},
120              new DataRow("Average child-parents quality difference") {VisualProperties = {StartIndexZero = true}},
121              new DataRow("Best crossover improvement (root parent)") {VisualProperties = {StartIndexZero = true}},
122              new DataRow("Best crossover improvement (non-root parent)") {VisualProperties = {StartIndexZero = true}},
123              new DataRow("Above average children") {VisualProperties = {StartIndexZero = true}},
124              new DataRow("Above average children from above average parents") {VisualProperties = {StartIndexZero = true}},
125            });
126        } else {
127          table = (DataTable)Results["Crossover improvement"].Value;
128        }
129        var crossoverChildren = vertices.Where(x => x.InDegree == 2).ToList();
130        if (CountIntermediateChildren)
131          crossoverChildren.AddRange(vertices.Where(x => x.InDegree == 1).Select(v => v.Parents.First()).Where(p => p.Rank.IsAlmost(generation - 0.5))); // add intermediate children
132
133        foreach (var c in crossoverChildren) {
134          if (c.Quality > averageQuality) {
135            aac++;
136            if (c.Parents.All(x => x.Quality > averageQuality))
137              aacp++;
138          }
139        }
140        var avgRootParentQualityImprovement = crossoverChildren.Average(x => x.Quality - x.Parents.First().Quality);
141        var avgNonRootParentQualityImprovement = crossoverChildren.Average(x => x.Quality - x.Parents.Last().Quality);
142        var avgChildParentQuality = crossoverChildren.Average(x => x.Quality - x.Parents.Average(p => p.Quality));
143        var bestRootParentQualityImprovement = crossoverChildren.Max(x => x.Quality - x.Parents.First().Quality);
144        var bestNonRootParentQualityImprovement = crossoverChildren.Max(x => x.Quality - x.Parents.Last().Quality);
145        table.Rows["Average crossover improvement (root parent)"].Values.Add(avgRootParentQualityImprovement);
146        table.Rows["Average crossover improvement (non-root parent)"].Values.Add(avgNonRootParentQualityImprovement);
147        table.Rows["Best crossover improvement (root parent)"].Values.Add(bestRootParentQualityImprovement);
148        table.Rows["Best crossover improvement (non-root parent)"].Values.Add(bestNonRootParentQualityImprovement);
149        table.Rows["Average child-parents quality difference"].Values.Add(avgChildParentQuality);
150        table.Rows["Above average children"].Values.Add(aac / populationSize);
151        table.Rows["Above average children from above average parents"].Values.Add(aacp / populationSize);
152        #endregion
153
154        #region mutation improvement
155        if (!Results.ContainsKey("Mutation improvement")) {
156          table = new DataTable("Mutation improvement");
157          Results.Add(new Result("Mutation improvement", table));
158          table.Rows.AddRange(
159            new[]
160            {
161              new DataRow("Average mutation improvement") {VisualProperties = {StartIndexZero = true}},
162              new DataRow("Best mutation improvement") {VisualProperties = {StartIndexZero = true}},
163              new DataRow("Above average children") {VisualProperties = {StartIndexZero = true}},
164              new DataRow("Above average children from above average parents") {VisualProperties = {StartIndexZero = true}},
165            });
166        } else {
167          table = (DataTable)Results["Mutation improvement"].Value;
168        }
169
170        aac = 0;
171        aacp = 0;
172        var mutationChildren = vertices.Where(x => x.InDegree == 1).ToList();
173
174        foreach (var c in mutationChildren) {
175          if (c.Quality > averageQuality)
176            aac++;
177          if (c.Parents.All(x => x.Quality > averageQuality))
178            aacp++;
179        }
180        var avgMutationImprovement = mutationChildren.Average(x => x.Quality - x.Parents.First().Quality);
181        var bestMutationImprovement = mutationChildren.Max(x => x.Quality - x.Parents.First().Quality);
182
183        table.Rows["Average mutation improvement"].Values.Add(avgMutationImprovement);
184        table.Rows["Best mutation improvement"].Values.Add(bestMutationImprovement);
185        table.Rows["Above average children"].Values.Add(aac / populationSize);
186        table.Rows["Above average children from above average parents"].Values.Add(aacp / populationSize);
187        #endregion
188      }
189      return base.Apply();
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.