1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using HEAL.Attic;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
28 | [Item("SymbolicExpressionTreeNodeEqualityComparer", "An operator that checks node equality based on different similarity measures.")]
|
---|
29 | [StorableType("F5BC06AA-3F08-4692-93E8-E44CE8205A46")]
|
---|
30 | public class SymbolicExpressionTreeNodeEqualityComparer : Item, ISymbolicExpressionTreeNodeSimilarityComparer {
|
---|
31 | [StorableConstructor]
|
---|
32 | protected SymbolicExpressionTreeNodeEqualityComparer(StorableConstructorFlag _) : base(_) { }
|
---|
33 | protected SymbolicExpressionTreeNodeEqualityComparer(SymbolicExpressionTreeNodeEqualityComparer original, Cloner cloner)
|
---|
34 | : base(original, cloner) {
|
---|
35 | matchNumericValues = original.matchNumericValues;
|
---|
36 | matchVariableNames = original.matchVariableNames;
|
---|
37 | matchVariableWeights = original.matchVariableWeights;
|
---|
38 | }
|
---|
39 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeNodeEqualityComparer(this, cloner); }
|
---|
40 |
|
---|
41 | // more flexible matching criteria
|
---|
42 | [Storable]
|
---|
43 | private bool matchNumericValues;
|
---|
44 | public bool MatchNumericValues {
|
---|
45 | get { return matchNumericValues; }
|
---|
46 | set { matchNumericValues = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [Storable]
|
---|
50 | private bool matchVariableNames;
|
---|
51 | public bool MatchVariableNames {
|
---|
52 | get { return matchVariableNames; }
|
---|
53 | set { matchVariableNames = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | [Storable]
|
---|
57 | private bool matchVariableWeights;
|
---|
58 | public bool MatchVariableWeights {
|
---|
59 | get { return matchVariableWeights; }
|
---|
60 | set { matchVariableWeights = value; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
64 | private void AfterDeserialization() {
|
---|
65 | }
|
---|
66 |
|
---|
67 | public SymbolicExpressionTreeNodeEqualityComparer() {
|
---|
68 | matchNumericValues = true;
|
---|
69 | matchVariableNames = true;
|
---|
70 | matchVariableWeights = true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public int GetHashCode(ISymbolicExpressionTreeNode n) {
|
---|
74 | return n.ToString().ToLower().GetHashCode();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public bool Equals(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
78 | if (!(a is SymbolicExpressionTreeTerminalNode))
|
---|
79 | // if a and b are non terminal nodes, check equality of symbol names
|
---|
80 | return !(b is SymbolicExpressionTreeTerminalNode) && a.Symbol.Name.Equals(b.Symbol.Name);
|
---|
81 | var va = a as VariableTreeNode;
|
---|
82 | if (va != null) {
|
---|
83 | var vb = b as VariableTreeNode;
|
---|
84 | if (vb == null) return false;
|
---|
85 |
|
---|
86 | return (!MatchVariableNames || va.VariableName.Equals(vb.VariableName)) && (!MatchVariableWeights || va.Weight.Equals(vb.Weight));
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (a is INumericTreeNode ca) {
|
---|
90 | var cb = b as INumericTreeNode;
|
---|
91 | if (cb == null) return false;
|
---|
92 | return (!MatchNumericValues || ca.Value.Equals(cb.Value));
|
---|
93 | }
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|