Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSubtreeSampleCountAnalyzer.cs @ 12225

Last change on this file since 12225 was 12225, checked in by bburlacu, 10 years ago

#1772: Work in progress for calculating sampling counts for subtrees in the population: updated TraceCalculator to aggregate tracing statistics, updated SymbolicDataAnalysisGenealogyGraphView, added SymbolicDataAnalysisSubtreeSampleCountAnalyzer.

File size: 3.8 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.EvolutionTracking;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
31  [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")]
32  [StorableClass]
33  public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
34    private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
35
36    public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
37      UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter";
38    }
39
40    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner)
41      : base(original, cloner) {
42    }
43
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner);
46    }
47
48    public override void ClearState() {
49      base.ClearState();
50      traceCalculator.ResetState();
51    }
52
53    public override void InitializeState() {
54      traceCalculator.ResetState();
55    }
56
57    [StorableConstructor]
58    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(bool deserializing) : base(deserializing) { }
59
60    public override IOperation Apply() {
61      int updateInterval = UpdateIntervalParameter.Value.Value;
62      IntValue updateCounter = UpdateCounterParameter.ActualValue;
63      // if counter does not yet exist then initialize it with update interval
64      // to make sure the solutions are analyzed on the first application of this operator
65      if (updateCounter == null) {
66        updateCounter = new IntValue(updateInterval);
67        UpdateCounterParameter.ActualValue = updateCounter;
68      }
69      //analyze solutions only every 'updateInterval' times
70      if (updateCounter.Value != updateInterval) {
71        updateCounter.Value++;
72        return base.Apply();
73      }
74      updateCounter.Value = 1;
75
76      if (PopulationGraph == null)
77        return base.Apply();
78
79      double avgTraceOverlap = 0;
80
81      if (Generation.Value > 0) {
82        var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
83        foreach (var n in rank) {
84          // during the trace the SymbolicExpressionTreeNode weights will be updated behind the scenes
85          // the weights represent the sample count of each subtree
86          var trace = traceCalculator.Trace(n, 2, false);
87          foreach (var v in trace.Vertices) {
88            var g = PopulationGraph.GetByContent(v.Data);
89            g.Weight = v.Weight;
90          }
91        }
92      }
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.