2

I'm trying to generate a list in NetLogo that contains several different unique lists of 0 and 1. The number of 1s depends j and the number of lists depends on i. For example, I have these lines of code:

if (i = 4) and (j=1) [set mylist = [[1 0 0 0][0 1 0 0][0 0 1 0][0 0 0 1]]]
if (i = 4) and (j=2) [set mylist = [[1 1 0 0][1 0 1 0][1 0 0 1][0 1 1 0][0 1 0 1] [0 0 1 1]]]

that I wrote to make all possible unique combinations of 0 and 1 without any repetitions within the lists. I would like to be able to the same thing but for values of both i and j, ranging from 1-10. Is there an example of how to do this, or some sort of pseudocode algorithm that anyone knows of that I could check out? Thanks!

Sam
  • 23
  • 3
  • 1
    I think you mean permutations, not combinations. see https://stackoverflow.com/questions/33815666/generating-permutations-of-a-list-in-netlogo – Seth Tisue Jan 22 '18 at 21:22

1 Answers1

2
to-report combinations [_m _s]
  if (_m = 0) [ report [[]] ]
  if (_s = []) [ report [] ]
  let _rest butfirst _s
  let _lista map [? -> fput item 0 _s ?] combinations (_m - 1) _rest
  let _listb combinations _m _rest
  report (sentence _lista _listb)
end

;convert location list to bitstring
to-report bitstring [#len #locs]
  report map [? -> ifelse-value (member? ? #locs) [1] [0]] range #len
end

;try it out:
to-report test-values [#i #j]  ;e.g., test-values 4 2
  report map [? -> bitstring #i ?] combinations #j range #i
end
Alan
  • 9,410
  • 15
  • 20