- Timestamp:
- 09/23/14 16:19:11 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs
r10562 r11384 1 using System; 1 #region License Information 2 3 /* HeuristicLab 4 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 5 * 6 * This file is part of HeuristicLab. 7 * 8 * HeuristicLab is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * HeuristicLab is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #endregion 23 24 using System; 2 25 using System.Collections.Generic; 3 26 using System.Linq; … … 45 68 return matrix[m, n] + 1; 46 69 } 70 71 /// <summary> 72 /// Calculates the difference between two symbolic expression trees. 73 /// </summary> 74 /// <param name="tree">The first symbolic expression tree</param> 75 /// <param name="other">The second symbolic expression tree</param> 76 /// <returns>Returns the root of the subtree (from T1) by which T1 differs from T2, or null if no difference is found.</returns> 77 public static ISymbolicExpressionTreeNode Difference(this ISymbolicExpressionTree tree, ISymbolicExpressionTree other) { 78 return Difference(tree.Root, other.Root); 79 } 80 81 public static ISymbolicExpressionTreeNode Difference(this ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNode other) { 82 var these = node.IterateNodesPrefix().ToList(); 83 var others = other.IterateNodesPrefix().ToList(); 84 85 var minCount = Math.Min(these.Count, others.Count); 86 var list = new List<ISymbolicExpressionTreeNode>(); 87 88 for (int i = 0; i < minCount; ++i) { 89 if (these[i].ToString() != others[i].ToString()) 90 list.Add(these[i]); 91 } 92 93 return list.Count > 0 ? LowestCommonAncestor(node, list) : null; 94 } 95 96 private static ISymbolicExpressionTreeNode LowestCommonAncestor(ISymbolicExpressionTreeNode root, List<ISymbolicExpressionTreeNode> nodes) { 97 if (nodes.Count == 0) 98 throw new ArgumentException("The nodes list should contain at least one element."); 99 100 if (nodes.Count == 1) 101 return nodes[0]; 102 103 int lowestLevel = nodes.Min(x => root.GetBranchLevel(x)); 104 105 // bring the nodes in the nodes to the same level (relative to the root) 106 for (int i = 0; i < nodes.Count; ++i) { 107 var node = nodes[i]; 108 var level = root.GetBranchLevel(node); 109 for (int j = lowestLevel; j < level; ++j) 110 node = node.Parent; 111 nodes[i] = node; 112 } 113 114 // while not all the elements in the nodes are equal, go one level up 115 while (nodes.Any(x => x != nodes[0])) { 116 for (int i = 0; i < nodes.Count; ++i) 117 nodes[i] = nodes[i].Parent; 118 } 119 120 return nodes[0]; 121 } 47 122 } 48 123 }
Note: See TracChangeset
for help on using the changeset viewer.