# Permutation Grid with a constraint of 50%

Hi dear community,

I would like to find all the possible “permutations” in a grid which look like this (see piece attachee)

,
my “constraint” is that for all the results of the possible permutations I need 50 % of x and 50 % of Y.
All the boxes in the grid must be filled by x or y.

I would like also to have 2 colors instead of the “X” and the “Y”, could you help me to write the good definition ?

Thank you very much in advance

You have only two items really, “XY” and “YX”. How about a product with repeat set to 5.

In the following example repeat set to 3.

``````import itertools

l = ("XY", "YX")

for p in itertools.product(l, repeat=3):
for n in p:
print n
print "-" * 20
``````

This gives as output

``````XY
XY
XY
--------------------
XY
XY
YX
--------------------
XY
YX
XY
--------------------
XY
YX
YX
--------------------
YX
XY
XY
--------------------
YX
XY
YX
--------------------
YX
YX
XY
--------------------
YX
YX
YX
--------------------
``````

Is that what you are looking for?

1 Like

Thank you very much, I was more looking for a grasshopper definition eheh, but thank you for the idea… I will try to find an application with grasshopper rhino

You can use Nathan’s script in Grasshopper by making it into a custom component:

I’ve modified it slightly to integrate it better.

permutations.gh (3.9 KB)

1 Like

For a purely GH approach, it sound like you are looking for something like Cross Reference (CrossRef).

1 Like

Thank you very much for your grasshopper approach its much more explicit to me (sorry not a great mathematic background!), In fact I’m looking for all the possible permutation of X and Y (with 50 perrcent of X and 50 percent of Y) In grids like this… I would like to know how many combinaisons are possible…

Theophile, I don’t mean to be rude, but that’s literally what both Nathan and me already showed you.
You probably should have taken a closer look and experimented a little yourself, since that’s really what learning is about.

There seem to be 5120 permutations for what you’ve sketched above!
I strongly suggest you take another look.

permutations-2.gh (6.2 KB)

2 Likes

Ok sorry about that, It seems that I have not enough maths/grasshopper skills to use your definition in this case ^^ sorry about that !
I don’t need all the possible permutations between XY I would like only the permutations with 50% of X and 50% of Y in the same grid… I’m sorry if I’m not so clear :S like in the following drawing…
There are less more possible permutations in this case…

Thank you in advance for your patience…

How to apply this “rule” / constraint" of 50% of X and 50% of Y to your definition

Here on for this particular setup, slight adaptation of the definition posted by @diff-arch

permutations-2_jK.gh (8.9 KB)

``````ghenv.Component.Name = "Pythonic Cartesian Product"
ghenv.Component.NickName = "CProd"

from ghpythonlib import treehelpers
import itertools

def satisfy_50(I,l):
s = "".join(l)
c = s.count(I[0])
if c == 0:
return False
return (len(s) / c) == 2

if __name__ == "__main__":

product_one = ["".join(p) for p in itertools.product(I, repeat=2)]
all_permutations = [list(p) for p in itertools.product(product_one, repeat=R)]

permutations = [p for p in all_permutations if satisfy_50(I, p)]

P = treehelpers.list_to_tree(permutations)
AP = treehelpers.list_to_tree(all_permutations)
``````

2 Likes

It sounds amazing!

Hi again dear community,

Do you have any idea of the best way to transpose visually this approach ?
I mean the best would be to have instead of the X and the Y some colors or pictures in tables (like in my following drawing in the piece attachee) how would you proceed to achieve that ?

Thank you very Much

Hi Nathan!

I think I missed something or maybe there is a mistake in your permutation script, @nathanletwory, because for example I can’t find the following permutations (see attachee) in the results that is given by your pythonic cartesian product… It would means that there are not all the possible permutations between X and Y (with the constraint of 50% of X and 50% of Y) with this approach of the cartesian product !?

Do you have an idea ?

Cheers and thank you very much for your Help…

These are found at indices 102 and 98 respectively:

permutations-2_jK__locating_hits.gh (14.5 KB)

Thank you very much for your feedback, I tcheck that

Here a small update to the script so that it is easier to check if a certain pattern exists

permutations-2_jK__itemindex.gh (14.0 KB)

1 Like

@nathanletwory @diff-arch @Adam_ Everything works great amazing
I achieved what I was trying to achieve thanks to you !

1 Like

Good fortunes in your projects with Rhino and Grasshopper!