Changeset 16382
 Timestamp:
 12/14/18 13:30:28 (7 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs
r16305 r16382 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 70 71 } 71 72 72 // this will only work if lh and rh are sorted 73 // this will only work if lh and rh are sorted 73 74 private static double ComputeSimilarity(ulong[] lh, ulong[] rh) { 74 75 double count = 0; … … 90 91 } 91 92 92 public static double ComputeAverageSimilarity(I SymbolicExpressionTree[]trees, bool simplify = false, bool strict = false) {93 var total = (double)trees. Length * (trees.Length 1) / 2;93 public static double ComputeAverageSimilarity(IList<ISymbolicExpressionTree> trees, bool simplify = false, bool strict = false) { 94 var total = (double)trees.Count * (trees.Count  1) / 2; 94 95 double avg = 0; 95 var hashes = new ulong[trees. Length][];96 var hashes = new ulong[trees.Count][]; 96 97 // build hash arrays 97 for (int i = 0; i < trees. Length; ++i) {98 for (int i = 0; i < trees.Count; ++i) { 98 99 var nodes = trees[i].MakeNodes(strict); 99 100 hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); … … 101 102 } 102 103 // compute similarity matrix 103 for (int i = 0; i < trees. Length 1; ++i) {104 for (int j = i + 1; j < trees. Length; ++j) {104 for (int i = 0; i < trees.Count  1; ++i) { 105 for (int j = i + 1; j < trees.Count; ++j) { 105 106 avg += ComputeSimilarity(hashes[i], hashes[j]); 106 107 } … … 109 110 } 110 111 111 public static double[,] ComputeSimilarityMatrix(I SymbolicExpressionTree[]trees, bool simplify = false, bool strict = false) {112 var sim = new double[trees. Length, trees.Length];113 var hashes = new ulong[trees. Length][];112 public static double[,] ComputeSimilarityMatrix(IList<ISymbolicExpressionTree> trees, bool simplify = false, bool strict = false) { 113 var sim = new double[trees.Count, trees.Count]; 114 var hashes = new ulong[trees.Count][]; 114 115 // build hash arrays 115 for (int i = 0; i < trees. Length; ++i) {116 for (int i = 0; i < trees.Count; ++i) { 116 117 var nodes = trees[i].MakeNodes(strict); 117 118 hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray(); … … 119 120 } 120 121 // compute similarity matrix 121 for (int i = 0; i < trees. Length 1; ++i) {122 for (int j = i + 1; j < trees. Length; ++j) {122 for (int i = 0; i < trees.Count  1; ++i) { 123 for (int j = i + 1; j < trees.Count; ++j) { 123 124 sim[i, j] = sim[j, i] = ComputeSimilarity(hashes[i], hashes[j]); 124 125 } … … 218 219 #region tree simplification 219 220 // these simplification methods rely on the assumption that child nodes of the current node have already been simplified 220 // (in other words simplification should be applied in a bottomup fashion) 221 // (in other words simplification should be applied in a bottomup fashion) 221 222 public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree tree) { 222 223 ulong hashFunction(byte[] bytes) => HashUtil.JSHash(bytes); … … 248 249 } 249 250 250 // simplify multiplications by reducing constants and div terms 251 // simplify multiplications by reducing constants and div terms 251 252 public static void SimplifyMultiplication(ref HashNode<ISymbolicExpressionTreeNode>[] nodes, int i) { 252 253 var node = nodes[i];
Note: See TracChangeset
for help on using the changeset viewer.