Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisFragmentLengthAnalyzer.cs @ 13495

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

#1772: Improve the average operator improvement and average fragment length analyzers. Update genealogy analyzer to always evaluate the intermediate children.

File size: 5.1 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.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
33  [Item("SymbolicDataAnalysisFragmentLengthAnalyzer", "An analyzer for fragment length.")]
34  [StorableClass]
35  public class SymbolicDataAnalysisFragmentLengthAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
36    [StorableConstructor]
37    protected SymbolicDataAnalysisFragmentLengthAnalyzer(bool deserializing) : base(deserializing) { }
38
39    public SymbolicDataAnalysisFragmentLengthAnalyzer() {
40      UpdateCounterParameter.ActualName = "FragmentLengthAnalyzerUpdateCounter";
41    }
42
43    protected SymbolicDataAnalysisFragmentLengthAnalyzer(SymbolicDataAnalysisFragmentLengthAnalyzer original, Cloner cloner) : base(original, cloner) { }
44
45    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisFragmentLengthAnalyzer(this, cloner); }
46
47    public override IOperation Apply() {
48      int updateInterval = UpdateIntervalParameter.Value.Value;
49      IntValue updateCounter = UpdateCounterParameter.ActualValue;
50      // if counter does not yet exist then initialize it with update interval
51      // to make sure the solutions are analyzed on the first application of this operator
52      if (updateCounter == null) {
53        updateCounter = new IntValue(updateInterval);
54        UpdateCounterParameter.ActualValue = updateCounter;
55      }
56      //analyze solutions only every 'updateInterval' times
57      if (updateCounter.Value != updateInterval) {
58        updateCounter.Value++;
59        return base.Apply();
60      }
61      updateCounter.Value = 1;
62
63      if (PopulationGraph == null || Generation.Value == 0)
64        return base.Apply();
65
66      // consider all fragments (including those of the intermediate crossover children)
67      double averageCrossoverFragmentLength = 0;
68      double averageMutationFragmentLength = 0;
69      int crossoverChildren = 0;
70      int mutationChildren = 0;
71
72      var vertices = PopulationGraph.Vertices.Where(x => x.Rank > Generation.Value - 1);
73      foreach (var v in vertices) {
74        if (!v.InArcs.Any() || v.InArcs.Last().Data == null)
75          continue;
76        var fragment = (IFragment<ISymbolicExpressionTreeNode>)v.InArcs.Last().Data;
77        if (v.InDegree == 2) {
78          averageCrossoverFragmentLength += fragment.Root.GetLength();
79          crossoverChildren++;
80        } else {
81          averageMutationFragmentLength += fragment.Root.GetLength();
82          mutationChildren++;
83        }
84      }
85      var averageFragmentLength = (averageCrossoverFragmentLength + averageMutationFragmentLength) / (crossoverChildren + mutationChildren);
86      averageCrossoverFragmentLength /= crossoverChildren;
87      averageMutationFragmentLength /= mutationChildren;
88
89      DataTable table;
90      if (!Results.ContainsKey("AverageFragmentLength")) {
91        table = new DataTable("Average fragment length");
92        var row = new DataRow("Average fragment length") { VisualProperties = { StartIndexZero = true } };
93        row.Values.Add(averageFragmentLength);
94        table.Rows.Add(row);
95        row = new DataRow("Average crossover fragment length") { VisualProperties = { StartIndexZero = true } };
96        row.Values.Add(averageCrossoverFragmentLength);
97        table.Rows.Add(row);
98        row = new DataRow("Average mutation fragment length") { VisualProperties = { StartIndexZero = true } };
99        row.Values.Add(averageMutationFragmentLength);
100        table.Rows.Add(row);
101        Results.Add(new Result("AverageFragmentLength", table));
102      } else {
103        table = (DataTable)Results["AverageFragmentLength"].Value;
104        table.Rows["Average fragment length"].Values.Add(averageFragmentLength);
105        table.Rows["Average crossover fragment length"].Values.Add(averageCrossoverFragmentLength);
106        table.Rows["Average mutation fragment length"].Values.Add(averageMutationFragmentLength);
107      }
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.