Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16302 for trunk


Ignore:
Timestamp:
11/14/18 16:32:22 (5 years ago)
Author:
bburlacu
Message:

#2950: Add support for strict hashing (taking constants and variable weights into account)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r16291 r16302  
    9090    }
    9191
    92     public static double ComputeAverageSimilarity(ISymbolicExpressionTree[] trees, bool simplify = false) {
     92    public static double ComputeAverageSimilarity(ISymbolicExpressionTree[] trees, bool simplify = false, bool strict = false) {
    9393      var total = (double)trees.Length * (trees.Length - 1) / 2;
    9494      double avg = 0;
     
    9696      // build hash arrays
    9797      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();
    99100        Array.Sort(hashes[i]);
    100101      }
     
    108109    }
    109110
    110     public static double[,] ComputeSimilarityMatrix(ISymbolicExpressionTree[] trees, bool simplify = false) {
     111    public static double[,] ComputeSimilarityMatrix(ISymbolicExpressionTree[] trees, bool simplify = false, bool strict = false) {
    111112      var sim = new double[trees.Length, trees.Length];
    112113      var hashes = new ulong[trees.Length][];
    113114      // build hash arrays
    114115      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();
    116118        Array.Sort(hashes[i]);
    117119      }
     
    125127    }
    126128
    127     public static ulong ComputeHash(this ISymbolicExpressionTreeNode treeNode) {
     129    public static ulong ComputeHash(this ISymbolicExpressionTreeNode treeNode, bool strict = false) {
    128130      ulong hashFunction(byte[] input) => HashUtil.JSHash(input);
    129       var hashNodes = treeNode.MakeNodes();
     131      var hashNodes = treeNode.MakeNodes(strict);
    130132      var simplified = hashNodes.Simplify(hashFunction);
    131133      return simplified.Last().CalculatedHashValue;
    132134    }
    133135
    134     public static HashNode<ISymbolicExpressionTreeNode> ToHashNode(this ISymbolicExpressionTreeNode node) {
     136    public static HashNode<ISymbolicExpressionTreeNode> ToHashNode(this ISymbolicExpressionTreeNode node, bool strict = false) {
    135137      var symbol = node.Symbol;
    136138      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;
    140143      }
    141144      var hash = (ulong)name.GetHashCode();
     
    163166    }
    164167
    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();
    171174    }
    172175
Note: See TracChangeset for help on using the changeset viewer.