Generate distinct list of numbers using Random Reduce & Jitter

grasshopper
(Md01) #1

Dear Rhino Community,

I would like to create several distinct sets of indices (to select polygons). By changing the seed of “Random Reduce” I want to obtain several sets where:

  1. there are no repetitive indices within each set.
    For instance, this set (1,4,1) is not correct because there are two times “1”

AND

  1. Each scenario is unique (hence the position of the indices in the list does not matter) For instance the sets (1,5,2) and (2,1,5) should be considered as the same set and should appear only once in the final list

I have used Jitter and Random Reduce. The first condition (no repetitive indices) has been already included in the model. I don’t know if it’s possible to manage the 2nd condition (distinct scenario).

Random Scenarios.gh (15.8 KB)

Thank you for the help!

0 Likes

(Riccardo Majewski) #2

Well… in my mind I imagined it simpler than this…
2019-03-13%2000_01_51-Window
random_unique_subsets.gh (24.7 KB)

It lacks descriptions inside…
The steps are something like this:

  • creating main set with series
  • creating sub-sets with random reduce (in a quantity more than requested)
  • internally sorting each sub-set
  • transforming every index in text string with zeroes (ex 6 > “006” or 75 > “075”)
  • merging every index of same set to get a “unique” string of concatenated indexes
  • creating a set those strings and removing the excess
  • deconstructing strings again into lists (branches) of indexes

Hope it helps

0 Likes

(Sean L) #3

If I understand correctly you’re looking for a random bunch of combinations: sets of items with no repetition. The python itertools module is great for combinatorics stuff: counting, permutations, combinations…


get_combinations.gh (9.4 KB)

0 Likes

(Pfotiad0) #4

You may find the attached “fun” as well. Works either in unique permutations (of length: take) or just samples the unique integers acc the take value in a tree.

List_GroupRndIntegers.gh (122.0 KB)

WARNING: For a given collection of n items and permutation length pL all possible permutations are: factorial(n)/factorial(n-pL). This means BIG business. Thus if you choose permutations (as result) that number is calculated BEFORE any action and if you click yes … well … blame you, not me:

0 Likes

(Md01) #5

Thank you for the idea! I don’t understand how you check if there are duplicates (the last part of the Gh definition, why “x+y+u+z” ?, is there a way to connect this check up directly to the list output?

0 Likes

(Md01) #6

It looks like an easier way to create the lists, thank you! But these are not unique (the second condition I wrote).

0 Likes

(Md01) #7

thank you for the test! I will have a look at the code, I am not very familiar to this:)

0 Likes

(Riccardo Majewski) #8

… it already is.
The last [Int] component of the first big group is what you need… I made the checking part only during testing and then left it there, you can remove it.
For every set I made:
x - mass addition of the indexes in the subset; a subset of different indexes would likely have different result, but not always…
y - mass addition of the partial result of the previous step; again two different subsets should/might have different results
u and z are the same but starting with mass multiplication…
x+y+u+z to have a (probably) unique value for every different subset…
Probably best would have be doing xyu*z instead, but probably some integer decimal/rounding limit might be involved… i’m not sure.

0 Likes

(Sean L) #9

I think you might be wrong about the python script. It generates combinations, combinations are always unique by definition: https://en.wikipedia.org/wiki/Combination
Unless I’m misunderstanding - did you want the digits to be shuffled so that (2,1,3) is a possibility, for example?

0 Likes

(Md01) #10

Sorry for the misunderstanding, I may have expressed myself incorrectly.
What I meant is that:
if the set (1,2,3) appears then I DON’T want to get (2,1,3) or (3,1,2) or (2,3,1)…No matter the position.
So if the numbers 1, 2 and 3 appeared together, the next possible set which could be generated is maybe (1,2,4) or (4,2,3)

0 Likes

(Md01) #11

ok! I will check this out now:)

0 Likes

(Sean L) #12

Yeah, if we are understanding one-another correctly then the script I provided does exactly what you want.

In the example screenshot I say that I want to pick numbers from 1 to 5, and I want sets of 3 numbers.
The output from the python component is the following list:
(1,2,3)
(1,2,4)
(1,2,5)
(1,3,4)
(1,3,5)
(1,4,5)
(2,3,4)
(2,3,5)
(2,4,5)
(3,4,5)

that is all the possible combinations of 3 numbers where the numbers are between one and five. Combinations are selections of items such that the order does not matter.
After the python script generates all these combinations, all you have to do is randomly pick some. In the screenshot example I picked five combinations using random reduce.

If you don’t believe it, try checking it yourself. Think of a triplet of numbers and see if it’s in the list above. Unless I am missing something here, this is what you’re after.

Riccardo’s idea of checking for uniqueness by doing a sum is probably fine for this case, but if you want a general way of picking combinations regardless of data type then the python script does it.

0 Likes

(Riccardo Majewski) #13

I did solve this problem in that way because i didn’t want sequential combinations (or sub-sets)… to have random polygons (back to first post);
Still, indeed, my method is not greatly optimized.

0 Likes

(Sean L) #14

Ah, okay I think I get it now. So the OP doesn’t strictly want combinations. Unless I’ve confused myself even more. Sorry about that.

0 Likes

(Md01) #15

I was wondering about the numbers of scenarios possible. I have thought that the binomial coefficient was the formula I should have considered (so : factorial(n)/(factorial (pL) * factorial(n-pL)) )

But @PeterFotiadis wrote
For a given collection of n items and permutation length pL all possible permutations are:
factorial(n)/factorial(n-pL)

Which one is the right formula while excluding repetitions such as (1,2,4) and (2,1,4)?

0 Likes

(Pfotiad0) #16

Get the trad update, the unique perm (new option: if result ==3) , the perm count calculator, the proof (see inside C# how it works) and be a happy bunny (I do hope).

List_GroupRndIntegers_V1.gh (120.9 KB)

0 Likes

(Md01) #17

Thank you very much for all these great solutions !!! It works very well into my Grasshopper model

0 Likes