[11889] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11889] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
[10562] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16565] | 25 | using HEAL.Attic;
|
---|
[10562] | 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[11910] | 28 | [Item("SymbolicExpressionTreeNodeEqualityComparer", "An operator that checks node equality based on different similarity measures.")]
|
---|
[16565] | 29 | [StorableType("F5BC06AA-3F08-4692-93E8-E44CE8205A46")]
|
---|
[11910] | 30 | public class SymbolicExpressionTreeNodeEqualityComparer : Item, ISymbolicExpressionTreeNodeSimilarityComparer {
|
---|
[10562] | 31 | [StorableConstructor]
|
---|
[16565] | 32 | protected SymbolicExpressionTreeNodeEqualityComparer(StorableConstructorFlag _) : base(_) { }
|
---|
[11910] | 33 | protected SymbolicExpressionTreeNodeEqualityComparer(SymbolicExpressionTreeNodeEqualityComparer original, Cloner cloner)
|
---|
[10562] | 34 | : base(original, cloner) {
|
---|
[18141] | 35 | matchNumericValues = original.matchNumericValues;
|
---|
[10562] | 36 | matchVariableNames = original.matchVariableNames;
|
---|
| 37 | matchVariableWeights = original.matchVariableWeights;
|
---|
| 38 | }
|
---|
[11910] | 39 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeNodeEqualityComparer(this, cloner); }
|
---|
[10562] | 40 |
|
---|
| 41 | // more flexible matching criteria
|
---|
| 42 | [Storable]
|
---|
[18141] | 43 | private bool matchNumericValues;
|
---|
[18148] | 44 | public bool MatchNumericValues {
|
---|
[18141] | 45 | get { return matchNumericValues; }
|
---|
| 46 | set { matchNumericValues = value; }
|
---|
[10562] | 47 | }
|
---|
| 48 |
|
---|
| 49 | [Storable]
|
---|
| 50 | private bool matchVariableNames;
|
---|
| 51 | public bool MatchVariableNames {
|
---|
| 52 | get { return matchVariableNames; }
|
---|
| 53 | set { matchVariableNames = value; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [Storable]
|
---|
| 57 | private bool matchVariableWeights;
|
---|
| 58 | public bool MatchVariableWeights {
|
---|
| 59 | get { return matchVariableWeights; }
|
---|
| 60 | set { matchVariableWeights = value; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void AfterDeserialization() {
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[11910] | 67 | public SymbolicExpressionTreeNodeEqualityComparer() {
|
---|
[18141] | 68 | matchNumericValues = true;
|
---|
[10562] | 69 | matchVariableNames = true;
|
---|
| 70 | matchVariableWeights = true;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public int GetHashCode(ISymbolicExpressionTreeNode n) {
|
---|
| 74 | return n.ToString().ToLower().GetHashCode();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public bool Equals(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 78 | if (!(a is SymbolicExpressionTreeTerminalNode))
|
---|
| 79 | // if a and b are non terminal nodes, check equality of symbol names
|
---|
| 80 | return !(b is SymbolicExpressionTreeTerminalNode) && a.Symbol.Name.Equals(b.Symbol.Name);
|
---|
| 81 | var va = a as VariableTreeNode;
|
---|
| 82 | if (va != null) {
|
---|
| 83 | var vb = b as VariableTreeNode;
|
---|
| 84 | if (vb == null) return false;
|
---|
| 85 |
|
---|
| 86 | return (!MatchVariableNames || va.VariableName.Equals(vb.VariableName)) && (!MatchVariableWeights || va.Weight.Equals(vb.Weight));
|
---|
| 87 | }
|
---|
[18132] | 88 |
|
---|
| 89 | if (a is INumericTreeNode ca) {
|
---|
| 90 | var cb = b as INumericTreeNode;
|
---|
[10562] | 91 | if (cb == null) return false;
|
---|
[18148] | 92 | return (!MatchNumericValues || ca.Value.Equals(cb.Value));
|
---|
[10562] | 93 | }
|
---|
| 94 | return false;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|