Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Changed the way node highlighting is performed (taking into account sampling count relative to current generation). Made NodeWeight field storable in the SymbolicExpressionTreeNode. Updated the statistics counting in the TraceCalculator.

File size: 4.3 KB
RevLine 
[12156]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.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
[12231]32  [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")]
[12156]33  [StorableClass]
[12231]34  public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
35    private const string CacheTraceNodesParameterName = "CacheTraceNodes";
36    private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
[12156]37
38    #region parameters
[12231]39    public IFixedValueParameter<BoolValue> CacheTraceNodesParameter {
40      get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; }
[12156]41    }
42    #endregion
43
[12231]44    #region parameter properties
45    public bool CacheTraceNodes {
46      get { return CacheTraceNodesParameter.Value.Value; }
47      set { CacheTraceNodesParameter.Value.Value = value; }
[12156]48    }
[12231]49    #endregion
[12156]50
[12231]51    public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
52      UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter";
53
54      Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true)));
55    }
56
57    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner)
[12156]58      : base(original, cloner) {
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
[12231]62      return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner);
[12156]63    }
64
[12231]65    public override void ClearState() {
66      base.ClearState();
67      traceCalculator.ResetState();
68    }
69
70    public override void InitializeState() {
71      traceCalculator.ResetState();
72    }
73
[12156]74    [StorableConstructor]
[12231]75    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(bool deserializing) : base(deserializing) { }
[12156]76
77    public override IOperation Apply() {
78      int updateInterval = UpdateIntervalParameter.Value.Value;
79      IntValue updateCounter = UpdateCounterParameter.ActualValue;
80      // if counter does not yet exist then initialize it with update interval
81      // to make sure the solutions are analyzed on the first application of this operator
82      if (updateCounter == null) {
83        updateCounter = new IntValue(updateInterval);
84        UpdateCounterParameter.ActualValue = updateCounter;
85      }
86      //analyze solutions only every 'updateInterval' times
87      if (updateCounter.Value != updateInterval) {
88        updateCounter.Value++;
89        return base.Apply();
90      }
91      updateCounter.Value = 1;
92
93      if (PopulationGraph == null)
94        return base.Apply();
95
[12231]96      traceCalculator.CacheTraceNodes = CacheTraceNodes;
[12156]97      if (Generation.Value > 0) {
[12231]98        var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
99        foreach (var n in rank)
100          traceCalculator.Trace(n, 2, false); // during this call weights will be updated
[12156]101
[12231]102        foreach (var v in traceCalculator.TraceGraph.Vertices) {
103          var g = PopulationGraph.GetByContent(v.Data);
104          g.Weight = v.Weight;
105        }
[12156]106      }
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.