Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2866_SymRegHyperbolicFunctions/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeNodeComparer.cs @ 17259

Last change on this file since 17259 was 16654, checked in by gkronber, 6 years ago

#2866: merged r16364:16653 from trunk to branch to prepare for trunk reintegration (resolving conflicts in the project file)

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