Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeNodeComparer.cs @ 14878

Last change on this file since 14878 was 14312, checked in by bburlacu, 8 years ago

#1772: Merge trunk changes. Delete unnecessary files (sliding window).

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 License Information
21
22using System;
23using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24
25namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
26  // this comparer considers that a < b if the type of a is "greater" than the type of b, for example:
27  // - A function node is "greater" than a terminal node
28  // - A variable terminal is "greater" than a constant terminal
29  // - used for bringing subtrees to a "canonical" form when the operation allows reordering of arguments
30  public class SymbolicExpressionTreeNodeComparer : ISymbolicExpressionTreeNodeComparer {
31
32    public static int CompareNodes(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
33      var ta = a as SymbolicExpressionTreeTerminalNode;
34      var tb = b as SymbolicExpressionTreeTerminalNode;
35
36      if (ta == null)
37        return tb == null ? String.CompareOrdinal(a.Symbol.Name, b.Symbol.Name) : -1;
38
39      if (tb == null)
40        return 1;
41
42      // at this point we know a and b are both terminals
43      var va = a as VariableTreeNode;
44      var vb = b as VariableTreeNode;
45
46      if (va != null)
47        return vb == null ? -1 : CompareVariables(va, vb);
48
49      if (vb != null)
50        return 1;
51
52      // at this point we know a and b are not variables
53      var ca = a as ConstantTreeNode;
54      var cb = b as ConstantTreeNode;
55
56      if (ca != null && cb != null)
57        return ca.Value.CompareTo(cb.Value);
58
59      // for other unknown terminal types, compare strings
60      return string.CompareOrdinal(a.ToString(), b.ToString());
61    }
62
63    private static int CompareVariables(VariableTreeNode a, VariableTreeNode b) {
64      int result = string.CompareOrdinal(a.VariableName, b.VariableName);
65      return result == 0 ? a.Weight.CompareTo(b.Weight) : result;
66    }
67
68    public int Compare(ISymbolicExpressionTreeNode x, ISymbolicExpressionTreeNode y) {
69      return CompareNodes(x, y);
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.