Last change
on this file since 13401 was
10562,
checked in by bburlacu, 11 years ago
|
#2164: Committed initial version of the tree matching functionality.
|
File size:
1019 bytes
|
Line | |
---|
1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 |
|
---|
7 | namespace 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.