1

Sorry if my question is basic but I have not coded in the past 15 years although I am trying to learn to code again for a research project. I have a set of 12 objects [A B C D E F G H I J K L] and I want to create a list of any possible K-multiset for any K between 1 and 6. (I do have a list of selection probabilities for them but at this stage of the project I can assume an equal probability of selection for all). Order matters and repetition is allowed (so they are just k-tuples). For example: [A], [A A] [B A A] [A B A], [B A A A A A] etc. I tried to use weighted-n-of-with-repeats in the RND extension of NetLogo but it seems that order does not matter in it so [B A] and [A B] are the same thing and are reported as [A B]. Can you please help with the NetLogo code?

This is what I have tried so far but unfortunately it does not recognize order:

to k-multiset let n 0 let pairs [[ "A" 0.1 ] [ "B" 0.1 ] ["C" 0.1] [“D” 0.1] [“E” 0.1] [“F” 0.1] [“G” 0.1] [“H” 0.1] [“I” 0.1] [“J” 0.1] [“K” 0.1]] while [n < 7] [print map first rnd:weighted-n-of-list-with-repeats n pairs [[p] -> last p ]] end

MoResearch
  • 11
  • 4

1 Answers1

0

Note that every multiset containing K items can be represented as integer value in 12-ary numeral system, where A corresponds to 0, B corresponds to 1 and so on until L=11.

So you can just walk through all integers in range 0..12^K-1 (about 3 millions for K=6) and get 12-ary digits as indexes of items.

python code for smaller item list and output range:

List = list('ABC')
L = len(List)
for K in range(1,4):
    for i in range(L**K):
        j = i
        output = []
        for n in range(K):   #K digits
             d = j % L      #integer modulo for n-th digit
             j = j // L     #integer division
             output.insert(0,List[d])
        print(output)
part of output:
['A']
['B']
['C']
['A', 'A']
['A', 'B']
['A', 'C']
...
['C', 'C']
['A', 'A', 'A']
['A', 'A', 'B']
['A', 'A', 'C']
...
['C', 'B', 'C']
['C', 'C', 'A']
['C', 'C', 'B']
['C', 'C', 'C']
MBo
  • 77,366
  • 5
  • 53
  • 86