- Timestamp:
- 11/14/18 16:32:22 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs
r16291 r16302 90 90 } 91 91 92 public static double ComputeAverageSimilarity(ISymbolicExpressionTree[] trees, bool simplify = false ) {92 public static double ComputeAverageSimilarity(ISymbolicExpressionTree[] trees, bool simplify = false, bool strict = false) { 93 93 var total = (double)trees.Length * (trees.Length - 1) / 2; 94 94 double avg = 0; … … 96 96 // build hash arrays 97 97 for (int i = 0; i < trees.Length; ++i) { 98 hashes[i] = (simplify ? trees[i].MakeNodes().Simplify(HashUtil.DJBHash) : trees[i].MakeNodes().Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); 98 var nodes = trees[i].MakeNodes(strict); 99 hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); 99 100 Array.Sort(hashes[i]); 100 101 } … … 108 109 } 109 110 110 public static double[,] ComputeSimilarityMatrix(ISymbolicExpressionTree[] trees, bool simplify = false ) {111 public static double[,] ComputeSimilarityMatrix(ISymbolicExpressionTree[] trees, bool simplify = false, bool strict = false) { 111 112 var sim = new double[trees.Length, trees.Length]; 112 113 var hashes = new ulong[trees.Length][]; 113 114 // build hash arrays 114 115 for (int i = 0; i < trees.Length; ++i) { 115 hashes[i] = (simplify ? trees[i].MakeNodes().Simplify(HashUtil.DJBHash) : trees[i].MakeNodes().Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); 116 var nodes = trees[i].MakeNodes(strict); 117 hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); 116 118 Array.Sort(hashes[i]); 117 119 } … … 125 127 } 126 128 127 public static ulong ComputeHash(this ISymbolicExpressionTreeNode treeNode ) {129 public static ulong ComputeHash(this ISymbolicExpressionTreeNode treeNode, bool strict = false) { 128 130 ulong hashFunction(byte[] input) => HashUtil.JSHash(input); 129 var hashNodes = treeNode.MakeNodes( );131 var hashNodes = treeNode.MakeNodes(strict); 130 132 var simplified = hashNodes.Simplify(hashFunction); 131 133 return simplified.Last().CalculatedHashValue; 132 134 } 133 135 134 public static HashNode<ISymbolicExpressionTreeNode> ToHashNode(this ISymbolicExpressionTreeNode node ) {136 public static HashNode<ISymbolicExpressionTreeNode> ToHashNode(this ISymbolicExpressionTreeNode node, bool strict = false) { 135 137 var symbol = node.Symbol; 136 138 var name = symbol.Name; 137 if (symbol is Variable) { 138 var variableTreeNode = (VariableTreeNode)node; 139 name = variableTreeNode.VariableName; 139 if (node is ConstantTreeNode constantNode) { 140 name = strict ? constantNode.Value.ToString() : symbol.Name; 141 } else if (node is VariableTreeNode variableNode) { 142 name = strict ? variableNode.Weight.ToString() + variableNode.VariableName : variableNode.VariableName; 140 143 } 141 144 var hash = (ulong)name.GetHashCode(); … … 163 166 } 164 167 165 public static HashNode<ISymbolicExpressionTreeNode>[] MakeNodes(this ISymbolicExpressionTree tree ) {166 return MakeNodes(tree.Root.GetSubtree(0).GetSubtree(0) );167 } 168 169 public static HashNode<ISymbolicExpressionTreeNode>[] MakeNodes(this ISymbolicExpressionTreeNode node ) {170 return node.IterateNodesPostfix().Select( ToHashNode).ToArray().UpdateNodeSizes();168 public static HashNode<ISymbolicExpressionTreeNode>[] MakeNodes(this ISymbolicExpressionTree tree, bool strict = false) { 169 return MakeNodes(tree.Root.GetSubtree(0).GetSubtree(0), strict); 170 } 171 172 public static HashNode<ISymbolicExpressionTreeNode>[] MakeNodes(this ISymbolicExpressionTreeNode node, bool strict = false) { 173 return node.IterateNodesPostfix().Select(x => x.ToHashNode(strict)).ToArray().UpdateNodeSizes(); 171 174 } 172 175
Note: See TracChangeset
for help on using the changeset viewer.