Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/21/14 19:56:48 (10 years ago)
Author:
bburlacu
Message:

#2215: Renamed BottomUpTreeSimilarityCalculator to BottomUpSimilarityCalculator, improved performance by 10% by using the SymbolicExpressionTreeNodeComparer for ordering nodes (instead of string.Compare on node.ToString()). Updated the rest of the files accordingly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeNodeComparer.cs

    r10562 r11486  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2014 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 System;
    223using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    324
     
    930  public class SymbolicExpressionTreeNodeComparer : ISymbolicExpressionTreeNodeComparer {
    1031    public int Compare(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
    11       if (!(a is SymbolicExpressionTreeTerminalNode)) {
    12         return b is SymbolicExpressionTreeTerminalNode
    13           ? -1
    14           : string.Compare(a.Symbol.Name, b.Symbol.Name, StringComparison.Ordinal);
    15       }
    16       if (!(b is SymbolicExpressionTreeTerminalNode)) return 1;
    17       // at this point we know a and b are terminal nodes
     32      var ta = a as SymbolicExpressionTreeTerminalNode;
     33      var tb = b as SymbolicExpressionTreeTerminalNode;
     34
     35      if (ta == null)
     36        return tb == null ? String.CompareOrdinal(a.Symbol.Name, b.Symbol.Name) : -1;
     37
     38      if (tb == null)
     39        return 1;
     40
     41      // at this point we know a and b are both terminals
    1842      var va = a as VariableTreeNode;
    19       if (va != null) {
    20         if (b is ConstantTreeNode) return -1;
    21         var vb = (VariableTreeNode)b;
    22         return (va.VariableName.Equals(vb.VariableName)
    23           ? va.Weight.CompareTo(vb.Weight)
    24           : string.Compare(va.VariableName, vb.VariableName, StringComparison.Ordinal));
    25       }
    26       // at this point we know for sure that a is a constant tree node
    27       if (b is VariableTreeNode) return 1;
    28       var ca = (ConstantTreeNode)a;
    29       var cb = (ConstantTreeNode)b;
    30       return ca.Value.CompareTo(cb.Value);
     43      var vb = b as VariableTreeNode;
     44
     45      if (va != null)
     46        return vb == null ? -1 : CompareVariables(va, vb);
     47
     48      if (vb != null)
     49        return 1;
     50
     51      // at this point we know a and b are not variables
     52      var ca = a as ConstantTreeNode;
     53      var cb = b as ConstantTreeNode;
     54
     55      if (ca != null && cb != null)
     56        return ca.Value.CompareTo(cb.Value);
     57
     58      // for other unknown terminal types, compare strings
     59      return string.CompareOrdinal(a.ToString(), b.ToString());
     60    }
     61
     62    private static int CompareVariables(VariableTreeNode a, VariableTreeNode b) {
     63      int result = string.CompareOrdinal(a.VariableName, b.VariableName);
     64      return result == 0 ? a.Weight.CompareTo(b.Weight) : result;
    3165    }
    3266  }
     67
    3368}
Note: See TracChangeset for help on using the changeset viewer.