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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
|
---|
35 | [StorableClass]
|
---|
36 | [Item("SymbolicDataAnalysisInternalDiversityAnalyzer", "An analyzer that determines the average diversity of symbolic expression trees.")]
|
---|
37 | public class SymbolicDataAnalysisInternalDiversityAnalyzer : SingleSuccessorOperator, ISymbolicDataAnalysisAnalyzer {
|
---|
38 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
39 | private const string QualityParameterName = "Quality";
|
---|
40 | private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
|
---|
41 | private const string ResultCollectionParameterName = "Results";
|
---|
42 | private readonly BottomUpSimilarityCalculator busCalculator;
|
---|
43 |
|
---|
44 | public SymbolicDataAnalysisInternalDiversityAnalyzer() {
|
---|
45 | busCalculator = new BottomUpSimilarityCalculator();
|
---|
46 |
|
---|
47 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
|
---|
48 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName));
|
---|
49 | Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
|
---|
50 | Parameters.Add(new FixedValueParameter<PercentValue>(PercentageOfBestSolutionsParameterName));
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected SymbolicDataAnalysisInternalDiversityAnalyzer(SymbolicDataAnalysisInternalDiversityAnalyzer original, Cloner cloner)
|
---|
54 | : base(original, cloner) {
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new SymbolicDataAnalysisInternalDiversityAnalyzer(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | #region parameter properties
|
---|
62 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
63 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
67 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public ILookupParameter<ResultCollection> ResultCollectionParameter {
|
---|
71 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IFixedValueParameter<PercentValue> PercentageOfBestSolutionsParameter {
|
---|
75 | get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageOfBestSolutionsParameterName]; }
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | public double PercentageOfBestSolutions {
|
---|
80 | get { return PercentageOfBestSolutionsParameter.Value.Value; }
|
---|
81 | set { PercentageOfBestSolutionsParameter.Value.Value = value; }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public bool EnabledByDefault { get { return true; } }
|
---|
85 |
|
---|
86 | public override IOperation Apply() {
|
---|
87 |
|
---|
88 | var results = ResultCollectionParameter.ActualValue;
|
---|
89 |
|
---|
90 | DataTable table;
|
---|
91 | if (!results.ContainsKey("Avg. Internal Diversity")) {
|
---|
92 | table = new DataTable("Internal Diversity") { VisualProperties = { YAxisTitle = "Internal Diversity" } };
|
---|
93 | var row = new DataRow("Avg. Internal Diversity") { VisualProperties = { StartIndexZero = true } };
|
---|
94 | table.Rows.Add(row);
|
---|
95 | results.Add(new Result("Avg. Internal Diversity", table));
|
---|
96 | } else {
|
---|
97 | table = (DataTable)results["Avg. Internal Diversity"].Value;
|
---|
98 | }
|
---|
99 |
|
---|
100 | var trees = SymbolicExpressionTreeParameter.ActualValue.ToArray();
|
---|
101 | var qualities = QualityParameter.ActualValue.ToArray();
|
---|
102 |
|
---|
103 | var pairs = trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q }).OrderByDescending(x => x.Quality);
|
---|
104 | int n = (int)Math.Floor(trees.Length * PercentageOfBestSolutions);
|
---|
105 | double avgInternalDiversity = pairs.Take(n).Average(x => CalculateInternalDiversity(x.Tree));
|
---|
106 |
|
---|
107 | table.Rows["Avg. Internal Diversity"].Values.Add(avgInternalDiversity);
|
---|
108 | return base.Apply();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private double CalculateInternalDiversity(ISymbolicExpressionTree tree) {
|
---|
112 | var branchPoint = tree.IterateNodesPrefix().FirstOrDefault(x => x.SubtreeCount == 2);
|
---|
113 | if (branchPoint == null)
|
---|
114 | return 1;
|
---|
115 |
|
---|
116 | var s1 = branchPoint.GetSubtree(0);
|
---|
117 | var s2 = branchPoint.GetSubtree(1);
|
---|
118 |
|
---|
119 | // set parents to null because otherwise the bottom-up calculator will throw an exception
|
---|
120 | var p1 = s1.Parent;
|
---|
121 | s1.Parent = null;
|
---|
122 | var p2 = s2.Parent;
|
---|
123 | s2.Parent = null;
|
---|
124 |
|
---|
125 | var m = busCalculator.ComputeBottomUpMapping(s1, s2);
|
---|
126 | var diversity = 1 - 2.0 * m.Count / (s1.GetLength() + s2.GetLength());
|
---|
127 |
|
---|
128 | // restore parents
|
---|
129 | s1.Parent = p1;
|
---|
130 | s2.Parent = p2;
|
---|
131 |
|
---|
132 | return diversity;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|