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

Last change on this file since 12156 was 12156, checked in by bburlacu, 6 years ago

#1772: Added base class for genealogy analyzers, added fragment length analyzer and trace overlap analyzer.

File size: 3.7 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)
64        return base.Apply();
65
66      var children = PopulationGraph.GetByRank(Generation.Value).Where(x => x.InDegree > 0).ToList();
67      double avgFragmentLength = 0;
68      if (children.Any())
69        avgFragmentLength = children.Select(x => (IFragment<ISymbolicExpressionTreeNode>)x.InArcs.Last().Data).Where(f => f != null).Average(x => x.Root.GetLength());
70
71      DataTable table;
72      if (!Results.ContainsKey("AverageFragmentLength")) {
73        table = new DataTable("Average fragment length");
74        var row = new DataRow("Average fragment length") { VisualProperties = { StartIndexZero = true } };
75        row.Values.Add(avgFragmentLength);
76        table.Rows.Add(row);
77        Results.Add(new Result("AverageFragmentLength", table));
78      } else {
79        table = (DataTable)Results["AverageFragmentLength"].Value;
80        table.Rows["Average fragment length"].Values.Add(avgFragmentLength);
81      }
82      return base.Apply();
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.