- Timestamp:
- 07/17/19 22:02:08 (5 years ago)
- Location:
- stable
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 17076
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs
r17099 r17141 25 25 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 26 26 public static class SymbolicExpressionHashExtensions { 27 /// <summary> 28 /// Holds data that is necessary to handle tree nodes in hashing / simplification. 29 /// </summary> 30 /// <typeparam name="T">The tree node type</typeparam> 27 31 public sealed class HashNode<T> : IComparable<HashNode<T>>, IEquatable<HashNode<T>> where T : class { 28 32 public T Data; … … 38 42 public SimplifyAction Simplify; 39 43 40 //public IComparer<T> Comparer;41 42 44 public bool IsLeaf => Arity == 0; 43 44 //public HashNode(IComparer<T> comparer) {45 // Comparer = comparer;46 //}47 48 //public HashNode() { }49 45 50 46 public int CompareTo(HashNode<T> other) { … … 170 166 171 167 /// <summary> 172 /// Get a function node's child indices t168 /// Get a function node's child indices 173 169 /// </summary> 174 170 /// <typeparam name="T">The data type encapsulated by a hash node</typeparam> 175 /// <param name="nodes">An array of hash nodes with up-to-date node sizes </param>171 /// <param name="nodes">An array of hash nodes with up-to-date node sizes (see UpdateNodeSizes)</param> 176 172 /// <param name="i">The index in the array of hash nodes of the node whose children we want to iterate</param> 177 173 /// <returns>An array containing child indices</returns> … … 188 184 } 189 185 186 /// <summary> 187 /// Determines size of each branch and sets the results for each node. 188 /// </summary> 189 /// <typeparam name="T">The data type encapsulated by a hash node</typeparam> 190 /// <param name="nodes">An array of hash nodes in postfix order.</param> 191 /// <returns>The array with updated node sizes. The array is not copied.</returns> 190 192 public static HashNode<T>[] UpdateNodeSizes<T>(this HashNode<T>[] nodes) where T : class { 191 193 for (int i = 0; i < nodes.Length; ++i) { … … 196 198 } 197 199 node.Size = node.Arity; 198 200 // visit all children and sum up their size (assumes postfix order). 199 201 for (int j = i - 1, k = 0; k < node.Arity; j -= 1 + nodes[j].Size, ++k) { 200 202 node.Size += nodes[j].Size; … … 204 206 } 205 207 208 // disables duplicate branches and removes the disabled nodes 206 209 public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class { 207 210 int count = 0; -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs
r17099 r17141 48 48 ulong hashFunction(byte[] input) => HashUtil.DJBHash(input); 49 49 50 var hashNodes = simplify ? node.MakeNodes(strict).Simplify(hashFunction) : node.MakeNodes(strict).Sort(hashFunction); 50 var hashNodes = simplify ? node.MakeNodes(strict).Simplify(hashFunction) : node.MakeNodes(strict).Sort(hashFunction); // simplify sorts implicitly 51 51 var hashes = new ulong[hashNodes.Length]; 52 52 for (int i = 0; i < hashes.Length; ++i) { … … 210 210 var treeNodes = nodes.Select(x => x.Data.Symbol.CreateTreeNode()).ToArray(); 211 211 212 // construct tree top down (assumes postfix order for nodes) 212 213 for (int i = nodes.Length - 1; i >= 0; --i) { 213 214 var node = nodes[i]; … … 244 245 // (in other words simplification should be applied in a bottom-up fashion) 245 246 public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree tree) { 246 ulong hashFunction(byte[] bytes) => HashUtil. JSHash(bytes);247 var root = tree. Root.GetSubtree(0).GetSubtree(0);247 ulong hashFunction(byte[] bytes) => HashUtil.DJBHash(bytes); 248 var root = tree.ActualRoot(); 248 249 var nodes = root.MakeNodes(); 249 250 var simplified = nodes.Simplify(hashFunction); … … 369 370 nodes[i].Enabled = false; 370 371 } else if ((parentSymbol is Exponential && childSymbol is Logarithm) || (parentSymbol is Logarithm && childSymbol is Exponential)) { 372 // exp(log(x)) == x only for positive x. We consider this as equivalent for hashing anyway. 371 373 child.Enabled = parent.Enabled = false; 372 374 }
Note: See TracChangeset
for help on using the changeset viewer.