Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisInternalDiversityAnalyzer.cs @ 11221

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

#2215: Fixed incorrect namespace of the BottomUpSimilarityCalculator. Changed signature of ComputeBottomMapping method to take tree nodes as arguments rather than trees, because we should be able to compute the bottom-up distance for any two subtrees. Added internal diversity calculator based on the bottom-up distance, which computes the average diversity of all the nodes inside a tree individual.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
33  [StorableClass]
34  [Item("SymbolicDataAnalysisInternalDiversityAnalyzer", "An analyzer that determines the average diversity of symbolic expression trees.")]
35  public class SymbolicDataAnalysisInternalDiversityAnalyzer : SingleSuccessorOperator, ISymbolicDataAnalysisAnalyzer {
36    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
37    private const string ResultCollectionParameterName = "Results";
38    private readonly BottomUpSimilarityCalculator busCalculator;
39
40    public SymbolicDataAnalysisInternalDiversityAnalyzer() {
41      busCalculator = new BottomUpSimilarityCalculator();
42
43      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
44      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
45    }
46
47    protected SymbolicDataAnalysisInternalDiversityAnalyzer(SymbolicDataAnalysisInternalDiversityAnalyzer original, Cloner cloner)
48      : base(original, cloner) {
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new SymbolicDataAnalysisInternalDiversityAnalyzer(this, cloner);
53    }
54
55    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
56      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
57    }
58    public ILookupParameter<ResultCollection> ResultCollectionParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
60    }
61
62    public bool EnabledByDefault { get { return true; } }
63
64    public override IOperation Apply() {
65
66      var results = ResultCollectionParameter.ActualValue;
67
68      DataTable table;
69      if (!results.ContainsKey("Avg. Internal Diversity")) {
70        table = new DataTable("Internal Diversity") { VisualProperties = { YAxisTitle = "Internal Diversity" } };
71        table.Rows.Add(new DataRow("Avg. Internal Diversity"));
72        results.Add(new Result("Avg. Internal Diversity", table));
73      } else {
74        table = (DataTable)results["Avg. Internal Diversity"].Value;
75      }
76
77      double diversity = 0;
78      var trees = SymbolicExpressionTreeParameter.ActualValue;
79      foreach (var tree in trees) {
80        diversity += CalculateInternalDiversity(tree);
81      }
82
83      table.Rows["Avg. Internal Diversity"].Values.Add(diversity / trees.Length);
84      return base.Apply();
85    }
86
87    private double CalculateInternalDiversity(ISymbolicExpressionTree tree) {
88      var branchPoint = tree.IterateNodesPrefix().FirstOrDefault(x => x.SubtreeCount == 2);
89      if (branchPoint == null)
90        return 1;
91
92      var s1 = branchPoint.GetSubtree(0);
93      var s2 = branchPoint.GetSubtree(1);
94
95      // set parents to null because otherwise the bottom-up calculator will throw an exception
96      var p1 = s1.Parent;
97      s1.Parent = null;
98      var p2 = s2.Parent;
99      s2.Parent = null;
100
101      var m = busCalculator.ComputeBottomUpMapping(s1, s2);
102      var diversity = 1 - 2.0 * m.Count / (s1.GetLength() + s2.GetLength());
103
104      // restore parents
105      s1.Parent = p1;
106      s2.Parent = p2;
107
108      return diversity;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.