Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeNodeEqualityComparer.cs @ 12687

Last change on this file since 12687 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 3.8 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  [Item("SymbolicExpressionTreeNodeEqualityComparer", "An operator that checks node equality based on different similarity measures.")]
29  [StorableClass]
30  public class SymbolicExpressionTreeNodeEqualityComparer : Item, ISymbolicExpressionTreeNodeSimilarityComparer {
31    [StorableConstructor]
32    protected SymbolicExpressionTreeNodeEqualityComparer(bool deserializing) : base(deserializing) { }
33    protected SymbolicExpressionTreeNodeEqualityComparer(SymbolicExpressionTreeNodeEqualityComparer original, Cloner cloner)
34      : base(original, cloner) {
35      matchConstantValues = original.matchConstantValues;
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 matchConstantValues;
44    public bool MatchConstantValues {
45      get { return matchConstantValues; }
46      set { matchConstantValues = 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      matchConstantValues = 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      var ca = a as ConstantTreeNode;
89      if (ca != null) {
90        var cb = b as ConstantTreeNode;
91        if (cb == null) return false;
92        return (!MatchConstantValues || ca.Value.Equals(cb.Value));
93      }
94      return false;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.