Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeCanonicalSorter.cs @ 14545

Last change on this file since 14545 was 10562, checked in by bburlacu, 11 years ago

#2164: Committed initial version of the tree matching functionality.

File size: 1019 bytes
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6
7namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
8  public class SymbolicExpressionTreeCanonicalSorter {
9    private readonly HashSet<Type> nonSymmetricSymbols = new HashSet<Type> { typeof(Subtraction), typeof(Division) };
10
11    public void SortSubtrees(ISymbolicExpressionTree tree) {
12      SortSubtrees(tree.Root);
13    }
14
15    public void SortSubtrees(ISymbolicExpressionTreeNode node) {
16      if (node.SubtreeCount == 0) return;
17      var subtrees = node.Subtrees as List<ISymbolicExpressionTreeNode> ?? node.Subtrees.ToList();
18      if (IsSymmetric(node.Symbol)) {
19        var comparer = new SymbolicExpressionTreeNodeComparer();
20        subtrees.Sort(comparer);
21      }
22      foreach (var s in subtrees)
23        SortSubtrees(s);
24    }
25
26    private bool IsSymmetric(ISymbol s) {
27      return !nonSymmetricSymbols.Contains(s.GetType());
28    }
29  }
30}
Note: See TracBrowser for help on using the repository browser.