I need to iterate over all multi-set permutations of parts of an array, and get the value of a function which depends on the order of elements in the array. What’s the most efficient way to do this?
Only a subset of the array is permuted, however the results of the function depend on the value of every element in the array.
I’ve come up with:
using Combinatorics, Statistics
my_array = rand('a':'d', 40)
# Example function, my function is about as trivial in terms of runtime
my_func(x, y) = mean(cumsum(x .== y))
function get_all_results(arr)
# Only permute part of the array, e.g. here all a's and c's in the array
ps = multiset_permutations(arr[in(['a', 'c']).(arr)], sum(in(['a', 'c']).(arr)))
result = zeros(length(ps))
for (i, p) ∈ enumerate(ps)
# Replace the a's and c's with the permuted a's and c's
# This is the bit that feels suboptimal?
arr[in(['a', 'c']).(arr)] = p
result[i] = my_func(arr, 'c')
end
return result
end
But I wonder whether there’s a better way to “shuffle around” the elements in the existing array?
Furthermore, it seems to me that the way I’m doing it currently is not threadsafe/parallelizable, as different threads would try to shuffle the elements in my_array
, so if there’s a way around this as well that’d be great!