Changeset 16382


Ignore:
Timestamp:
12/14/18 13:30:28 (7 months ago)
Author:
bburlacu
Message:

#2950: Change signature of ComputeSimilarity methods to accent a generic list of trees. This enables us to directly pass HL ItemAray or ItemList without overhead.

File:
1 edited

Legend:

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

    r16305 r16382  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    7071    }
    7172
    72     // this will only work if lh and rh are sorted 
     73    // this will only work if lh and rh are sorted
    7374    private static double ComputeSimilarity(ulong[] lh, ulong[] rh) {
    7475      double count = 0;
     
    9091    }
    9192
    92     public static double ComputeAverageSimilarity(ISymbolicExpressionTree[] 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;
    9495      double avg = 0;
    95       var hashes = new ulong[trees.Length][];
     96      var hashes = new ulong[trees.Count][];
    9697      // build hash arrays
    97       for (int i = 0; i < trees.Length; ++i) {
     98      for (int i = 0; i < trees.Count; ++i) {
    9899        var nodes = trees[i].MakeNodes(strict);
    99100        hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray();
     
    101102      }
    102103      // 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) {
    105106          avg += ComputeSimilarity(hashes[i], hashes[j]);
    106107        }
     
    109110    }
    110111
    111     public static double[,] ComputeSimilarityMatrix(ISymbolicExpressionTree[] 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][];
    114115      // build hash arrays
    115       for (int i = 0; i < trees.Length; ++i) {
     116      for (int i = 0; i < trees.Count; ++i) {
    116117        var nodes = trees[i].MakeNodes(strict);
    117118        hashes[i] = (simplify ? nodes.Simplify(HashUtil.DJBHash) : nodes.Sort(HashUtil.DJBHash)).Select(x => x.CalculatedHashValue).ToArray();
     
    119120      }
    120121      // 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) {
    123124          sim[i, j] = sim[j, i] = ComputeSimilarity(hashes[i], hashes[j]);
    124125        }
     
    218219    #region tree simplification
    219220    // 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 bottom-up fashion) 
     221    // (in other words simplification should be applied in a bottom-up fashion)
    221222    public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree tree) {
    222223      ulong hashFunction(byte[] bytes) => HashUtil.JSHash(bytes);
     
    248249    }
    249250
    250     // simplify multiplications by reducing constants and div terms 
     251    // simplify multiplications by reducing constants and div terms
    251252    public static void SimplifyMultiplication(ref HashNode<ISymbolicExpressionTreeNode>[] nodes, int i) {
    252253      var node = nodes[i];
Note: See TracChangeset for help on using the changeset viewer.