1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
31 | [StorableClass]
|
---|
32 | public class SymbolicDataAnalysisExpressionTreeSimilarityCalculator : SingleSuccessorOperator {
|
---|
33 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
34 | private const string CurrentSymbolicExpressionTreeParameterName = "CurrentSymbolicExpressionTree";
|
---|
35 | private const string SimilarityValuesParmeterName = "Similarity";
|
---|
36 | // comparer parameters
|
---|
37 | private const string MatchVariablesParameterName = "MatchVariableNames";
|
---|
38 | private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
|
---|
39 | private const string MatchConstantValuesParameterName = "MatchConstantValues";
|
---|
40 |
|
---|
41 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
42 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public IValueParameter<ISymbolicExpressionTree> CurrentSymbolicExpressionTreeParameter {
|
---|
46 | get { return (IValueParameter<ISymbolicExpressionTree>)Parameters[CurrentSymbolicExpressionTreeParameterName]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ILookupParameter<BoolValue> MatchVariableNamesParameter {
|
---|
50 | get { return (ILookupParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public ILookupParameter<BoolValue> MatchVariableWeightsParameter {
|
---|
54 | get { return (ILookupParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ILookupParameter<BoolValue> MatchConstantValuesParameter {
|
---|
58 | get { return (ILookupParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public ILookupParameter<DoubleValue> SimilarityParameter {
|
---|
62 | get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ISymbolicExpressionTree CurrentSymbolicExpressionTree {
|
---|
66 | get { return CurrentSymbolicExpressionTreeParameter.Value; }
|
---|
67 | set { CurrentSymbolicExpressionTreeParameter.Value = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public SymbolicExpressionTreeNodeSimilarityComparer SimilarityComparer { get; set; }
|
---|
71 |
|
---|
72 | public int MaximumTreeDepth { get; set; }
|
---|
73 |
|
---|
74 | protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(
|
---|
75 | SymbolicDataAnalysisExpressionTreeSimilarityCalculator original, Cloner cloner)
|
---|
76 | : base(original, cloner) {
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
80 | return new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(this, cloner);
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableConstructor]
|
---|
84 | protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(bool deserializing)
|
---|
85 | : base(deserializing) {
|
---|
86 | }
|
---|
87 |
|
---|
88 | public SymbolicDataAnalysisExpressionTreeSimilarityCalculator()
|
---|
89 | : base() {
|
---|
90 | Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
91 | Parameters.Add(new ValueParameter<ISymbolicExpressionTree>(CurrentSymbolicExpressionTreeParameterName, ""));
|
---|
92 | Parameters.Add(new LookupParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names."));
|
---|
93 | Parameters.Add(new LookupParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights."));
|
---|
94 | Parameters.Add(new LookupParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values."));
|
---|
95 | Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IOperation Apply() {
|
---|
99 | var trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
100 |
|
---|
101 | double similarity = 0.0;
|
---|
102 | var current = CurrentSymbolicExpressionTree;
|
---|
103 |
|
---|
104 | bool found = false;
|
---|
105 | foreach (var tree in trees) {
|
---|
106 | if (tree == current) {
|
---|
107 | found = true;
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (found) {
|
---|
112 | similarity += MaxCommonSubtreeSimilarity(current, tree, SimilarityComparer);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | lock (SimilarityParameter.ActualValue) {
|
---|
117 | SimilarityParameter.ActualValue.Value += similarity;
|
---|
118 | }
|
---|
119 | return base.Apply();
|
---|
120 | }
|
---|
121 |
|
---|
122 | public static double CalculateSimilarity(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b,
|
---|
123 | SymbolicExpressionTreeNodeSimilarityComparer comp) {
|
---|
124 | return 2.0 * SymbolicExpressionTreeMatching.Match(a, b, comp) / (a.GetLength() + b.GetLength());
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b,
|
---|
128 | SymbolicExpressionTreeNodeSimilarityComparer comparer) {
|
---|
129 | double max = 0;
|
---|
130 | var rootA = a.Root.GetSubtree(0).GetSubtree(0);
|
---|
131 | var rootB = b.Root.GetSubtree(0).GetSubtree(0);
|
---|
132 | foreach (var aa in rootA.IterateNodesBreadth()) {
|
---|
133 | int lenA = aa.GetLength();
|
---|
134 | if (lenA <= max) continue;
|
---|
135 | foreach (var bb in rootB.IterateNodesBreadth()) {
|
---|
136 | int lenB = bb.GetLength();
|
---|
137 | if (lenB <= max) continue;
|
---|
138 | int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
|
---|
139 | if (max < matches) max = matches;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return 2.0 * max / (rootA.GetLength() + rootB.GetLength());
|
---|
143 | }
|
---|
144 |
|
---|
145 | // returns true if both nodes are variables, or both are constants, or both are functions
|
---|
146 | private static bool SameType(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
147 | if (a is VariableTreeNode) {
|
---|
148 | return b is VariableTreeNode;
|
---|
149 | }
|
---|
150 | if (a is ConstantTreeNode) {
|
---|
151 | return b is ConstantTreeNode;
|
---|
152 | }
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|