Advertisement
Anomalistic

Generate Edges In Cell

Jun 24th, 2021
1,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.02 KB | None | 0 0
  1. using Combinatorics
  2. using Memoize
  3.  
  4. ###################################################################
  5. #                                                                 #
  6. #  The function generate_edges_in_cell generates a random         #
  7. #    selection of valid edges in a specified region. It           #
  8. #    supports rectangular regions of any rank.                    #
  9. #                                                                 #
  10. #                    Runtime = O(output size)                     #
  11. #                                                                 #
  12. #  R is the list of group sizes                                   #
  13. #  m is the multi-index of the sub-tensor we are selectring from  #
  14. #  p is the edge density                                          #
  15. #                                                                 #
  16. ###                                                             ###
  17. @memoize collected_combinations(r, d) = collect(combinations(1:r, d))
  18. function generate_edges_in_cell(group_sizes, m, probability)
  19.   nodes_from_each_group = [count(==(i), m) for i=1:length(group_sizes)]
  20.   ways_to_choose_nodes_from_each_group =
  21.     map(collected_combinations, group_sizes, nodes_from_each_group)
  22.   option_count = map(length, ways_to_choose_nodes_from_each_group)
  23.   Π_option_count = vcat([1],cumprod(option_count))
  24.   Σ_group_sizes = vcat([0],cumsum(group_sizes))
  25.   [vcat((ways_to_choose_nodes_from_each_group[i][
  26.              (n-1)÷Π_option_count[i]%option_count[i]+1]
  27.          .+ Σ_group_sizes[i] for i = 1:length(group_sizes))...)
  28.    for n = grasshopper(last(Π_option_count),probability)]
  29. end
  30.  
  31.  
  32. ###################################################################
  33. #                                                                 #
  34. #  grasshopper(n,p) is equivalent to filter(rand() < p, 1:n)      #
  35. #     in output probability distribution, but runs faster.        #
  36. #                                                                 #
  37. #                       Runtime = O(output size)                  #
  38. #               Average runtime = O(n*p)                          #
  39. #                                                                 #
  40. #  n is the size of the list we select from                       #
  41. #  p is the expected fraction of elements to keep                 #
  42. #                                                                 #
  43. ###                                                             ###
  44. function grasshopper(n,probability)
  45.   if probability == 0 return [] end
  46.   out = Array{typeof(n)}(undef, min(n,ceil(Int,
  47.     n*probability+(n*probability)^(3/4))))
  48.   count = 0
  49.   k = 1/log(1-probability)
  50.   current = floor(k*log(1-rand()))+1
  51.   while current <= n
  52.     count += 1
  53.     if count > length(out)
  54.       resize!(out, min(n,length(out)*2))
  55.     end
  56.     out[count] = current
  57.     current += floor(k*log(1-rand()))+1
  58.   end
  59.   resize!(out, count)
  60.   out
  61. end
  62.  
  63. group_sizes = [2,4,3]
  64. m = [1,2,2,2]
  65. probability = .1
  66. println(generate_edges_in_cell(group_sizes, m, probability))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement