Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.IslandAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeNodeSimilarityComparer.cs

Last change on this file was 10563, checked in by bburlacu, 11 years ago

#2164: Fixed storable and cloning constructor access level (from private to protected).

File size: 3.0 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
7  [Item("SymbolicExpressionTreeNodeSimilarityComparer", "A comparison operator that checks node equality based on different similarity measures.")]
8  [StorableClass]
9  public class SymbolicExpressionTreeNodeSimilarityComparer : Item, ISymbolicExpressionTreeNodeSimilarityComparer {
10    [StorableConstructor]
11    protected SymbolicExpressionTreeNodeSimilarityComparer(bool deserializing) : base(deserializing) { }
12    protected SymbolicExpressionTreeNodeSimilarityComparer(SymbolicExpressionTreeNodeSimilarityComparer original, Cloner cloner)
13      : base(original, cloner) {
14      matchConstantValues = original.matchConstantValues;
15      matchVariableNames = original.matchVariableNames;
16      matchVariableWeights = original.matchVariableWeights;
17    }
18    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeNodeSimilarityComparer(this, cloner); }
19
20    // more flexible matching criteria
21    [Storable]
22    private bool matchConstantValues;
23    public bool MatchConstantValues {
24      get { return matchConstantValues; }
25      set { matchConstantValues = value; }
26    }
27
28    [Storable]
29    private bool matchVariableNames;
30    public bool MatchVariableNames {
31      get { return matchVariableNames; }
32      set { matchVariableNames = value; }
33    }
34
35    [Storable]
36    private bool matchVariableWeights;
37    public bool MatchVariableWeights {
38      get { return matchVariableWeights; }
39      set { matchVariableWeights = value; }
40    }
41
42    [StorableHook(HookType.AfterDeserialization)]
43    private void AfterDeserialization() {
44    }
45
46    public SymbolicExpressionTreeNodeSimilarityComparer() {
47      matchConstantValues = true;
48      matchVariableNames = true;
49      matchVariableWeights = true;
50    }
51
52    public int GetHashCode(ISymbolicExpressionTreeNode n) {
53      return n.ToString().ToLower().GetHashCode();
54    }
55
56    public bool Equals(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
57      if (!(a is SymbolicExpressionTreeTerminalNode))
58        // if a and b are non terminal nodes, check equality of symbol names
59        return !(b is SymbolicExpressionTreeTerminalNode) && a.Symbol.Name.Equals(b.Symbol.Name);
60      var va = a as VariableTreeNode;
61      if (va != null) {
62        var vb = b as VariableTreeNode;
63        if (vb == null) return false;
64
65        return (!MatchVariableNames || va.VariableName.Equals(vb.VariableName)) && (!MatchVariableWeights || va.Weight.Equals(vb.Weight));
66      }
67      var ca = a as ConstantTreeNode;
68      if (ca != null) {
69        var cb = b as ConstantTreeNode;
70        if (cb == null) return false;
71        return (!MatchConstantValues || ca.Value.Equals(cb.Value));
72      }
73      return false;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.