[11486] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17434] | 3 | * Copyright (C) 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 | */
|
---|
[12891] | 20 | #endregion License Information
|
---|
[11486] | 21 |
|
---|
| 22 | using System;
|
---|
[10562] | 23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[17434] | 24 | using HEAL.Attic;
|
---|
[10562] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[17434] | 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 {
|
---|
[12891] | 33 |
|
---|
[11910] | 34 | public static int CompareNodes(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
[11486] | 35 | var ta = a as SymbolicExpressionTreeTerminalNode;
|
---|
| 36 | var tb = b as SymbolicExpressionTreeTerminalNode;
|
---|
| 37 |
|
---|
| 38 | if (ta == null)
|
---|
| 39 | return tb == null ? String.CompareOrdinal(a.Symbol.Name, b.Symbol.Name) : -1;
|
---|
| 40 |
|
---|
| 41 | if (tb == null)
|
---|
| 42 | return 1;
|
---|
| 43 |
|
---|
| 44 | // at this point we know a and b are both terminals
|
---|
[10562] | 45 | var va = a as VariableTreeNode;
|
---|
[11486] | 46 | var vb = b as VariableTreeNode;
|
---|
| 47 |
|
---|
| 48 | if (va != null)
|
---|
| 49 | return vb == null ? -1 : CompareVariables(va, vb);
|
---|
| 50 |
|
---|
| 51 | if (vb != null)
|
---|
| 52 | return 1;
|
---|
| 53 |
|
---|
| 54 | // at this point we know a and b are not variables
|
---|
| 55 | var ca = a as ConstantTreeNode;
|
---|
| 56 | var cb = b as ConstantTreeNode;
|
---|
| 57 |
|
---|
| 58 | if (ca != null && cb != null)
|
---|
| 59 | return ca.Value.CompareTo(cb.Value);
|
---|
| 60 |
|
---|
| 61 | // for other unknown terminal types, compare strings
|
---|
| 62 | return string.CompareOrdinal(a.ToString(), b.ToString());
|
---|
[10562] | 63 | }
|
---|
[11486] | 64 |
|
---|
| 65 | private static int CompareVariables(VariableTreeNode a, VariableTreeNode b) {
|
---|
| 66 | int result = string.CompareOrdinal(a.VariableName, b.VariableName);
|
---|
| 67 | return result == 0 ? a.Weight.CompareTo(b.Weight) : result;
|
---|
| 68 | }
|
---|
[11894] | 69 |
|
---|
[11910] | 70 | public int Compare(ISymbolicExpressionTreeNode x, ISymbolicExpressionTreeNode y) {
|
---|
| 71 | return CompareNodes(x, y);
|
---|
[11894] | 72 | }
|
---|
[10562] | 73 | }
|
---|
[12891] | 74 | } |
---|