Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18242 was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 4.3 KB
RevLine 
[12265]1#region License information
[12225]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;
[17434]23using HEAL.Attic;
[12225]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.EvolutionTracking;
[12265]29using HeuristicLab.Parameters;
[12225]30
[17434]31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[12225]32  [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")]
[17434]33  [StorableType("F07AA5A1-49AF-4286-A56A-2B451571906E")]
[12225]34  public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
[12265]35    private const string CacheTraceNodesParameterName = "CacheTraceNodes";
[12225]36    private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
37
[12265]38    #region parameters
39    public IFixedValueParameter<BoolValue> CacheTraceNodesParameter {
40      get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; }
41    }
42    #endregion
43
44    #region parameter properties
45    public bool CacheTraceNodes {
46      get { return CacheTraceNodesParameter.Value.Value; }
47      set { CacheTraceNodesParameter.Value.Value = value; }
48    }
49    #endregion
50
[12225]51    public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
[12265]52      UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter";
53
54      Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true)));
[12225]55    }
56
[12966]57    protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner)
[12225]58      : base(original, cloner) {
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner);
63    }
64
65    public override void ClearState() {
66      base.ClearState();
67      traceCalculator.ResetState();
68    }
69
70    public override void InitializeState() {
71      traceCalculator.ResetState();
72    }
73
74    [StorableConstructor]
[17434]75    protected SymbolicDataAnalysisSubtreeSampleCountAnalyzer(StorableConstructorFlag _) : base(_) { }
[12225]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
[12265]96      traceCalculator.CacheTraceNodes = CacheTraceNodes;
[12225]97      if (Generation.Value > 0) {
98        var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
[12265]99        foreach (var n in rank)
100          traceCalculator.Trace(n, 2, false); // during this call weights will be updated
101
102        foreach (var v in traceCalculator.TraceGraph.Vertices) {
103          var g = PopulationGraph.GetByContent(v.Data);
104          g.Weight = v.Weight;
[12225]105        }
106      }
[12283]107
[12225]108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.