Permutation Component (generic indices)

OK, so I often need to try out all combinations of things and so I implemented a Permutation Component which takes any list with any type of data items, and based on the size of the input list, produces a new list of indices (integers) with all the possible combinations (output PIndices). These indices can be used to “scramble” also the input data into all the same possible combinations, or let the component do the job for you directly by tapping the PData output.

In case someone else also have use for something as gross as this (all possible combinations by “brute force”) I’ll post it here.

Fig 1. Input data can be combined by using a ListItem (indirect method) or tapped ready-made directly from the PData output. Mouse-over gives relevant port- and component info:

Edit: R1.2 (removed some taxing & annoying debug messages)
The gh example: (16.0 KB)

The component (this UserObject will end up in a “#RIL” tab. Re-save as your own UserObject to change that):
Permutations.ghuser (4.6 KB)

// Rolf



I made a quick Python version for you to compare with:

"""Permutations on an input list of data.
        Data: List of data to permutate
        PIndices: Permutated indices
        PData: Permutated data"""

__author__ = "Nathan 'jesterKing' Letwory"

import clr
clr.AddReference('Grasshopper, Culture=neutral, PublicKeyToken=dda4f5ec2cd80803')

from Grasshopper import DataTree as Tree
from Grasshopper.Kernel.Data import GH_Path as Path

from System import Array

import itertools

if Data and len(Data)>0:
    # create simple indices list
    indices = list(range(len(Data)))
    # permutations of input Data
    pdata = itertools.permutations(Data)
    pdata = [[i for i in p] for p in pdata]
    # permutations of indices from Data
    pindices = list(itertools.permutations(indices))
    pindices = [[i for i in p] for p in pindices]

    # create trees for PData and PIndices
    datatree = Tree[object]()
    indicestree = Tree[object]()
    if len(pdata)==len(pindices):
        for p in range(len(pdata)):
            # create path
            path = Path(Array[int]([0, p]))
            # append list items per path
            # for both PData and PIndices
            for i in pdata[p]:
                datatree.Add(i, path)
            for j in pindices[p]:
                indicestree.Add(j, path)
    PIndices = indicestree
    PData = datatree

I tried the code but I must have done something wrong because I couldn’t get the component to produce similar output. I’m not fluent int Python so I don’t really know where to look for any glitches:

Permutations_R1.4_ (18.7 KB)

// Rolf

@RIL, you need to right-click the Data input and set it to List Access.

Unbelievable. How could I overlook that? Well, I could. :slight_smile:

I should have looked in the mirror for glitches. Sigh.

// Rolf