Imagine a small garden, divided into 8 equal parts, each a square foot. The garden is 4 ft x 2 ft, so the "bins" are in two rows. Let's number them as:
0 1 2 3
4 5 6 7
Now, each of these plants has other plants that they like, that's good for them to be next to. I can score a particular arrangement by how many of these good relationships I end up with. I've done this in a python script, described here.
Without repeating all those details here, my problem is that there are too many permutations. If I just run it through python's handy permutation generator, there's 8! cases. (I'm stating this problem here as 8 spaces, but my real garden is 16 bins. The problem is too large to solve with 16! possible arrangements.)
My math question is, how can I iterate through a list of unique permutations that also consider how these two rows are arranged? If they were all in one row, the answer is easy, 8!. With 2 rows, there are rotations and mirrors that are really the same answer.
0 1 2 3 is same when mirrored 3 2 1 0
4 5 6 7 7 6 5 4
0 1 2 3 is same when mirrored 4 5 6 7
4 5 6 7 0 1 2 3
0 1 2 3 is same when rotated 7 6 5 4
4 5 6 7 3 2 1 0
I'd like to score all the possible arrangements, but skip those that are mirrors or rotations of things I've already considered. My usual hack-job attempts at iterating through such things include a lookup table, where I'd simply look through a list of ones completed. In this case, that lookup through potentially 8! (16! in my real problem) would take much longer than simply scoring each permutation.
How can I iterate through this and potentially reduce my problem set from 16! (~20 trillion) to perhaps 5 trillion? Or, failing a direct answer, what would you call this kind of problem? I'm not sure what to look up and read about. If I knew enough to know what to tag this problem as, I'd be farther along!
You can do this using canonicalization.
As long as the garden is rectangular, exactly one of the equivalent permutations has the $0$ in the top left quarter (for $2\times4$, that’s the first two spots), so you can quickly check whether that’s the case and only process such permutations.
If the garden is square, things are only slightly more complicated. Now there are two equivalent permutations where $0$ is in the top left quarter. If $0$ is off the diagonal, take the one where it’s above the diagonal; if it’s on the diagonal, take the one where the off-diagonal elements in the top-left quarter are in ascending order.
Or you could always take the lexically smallest of the permutations. That’s the one where the least value in the four corners is in the top left corner, and in case of a square, additionally the value to the right of the top-left corner is smaller than the value below the top-left corner.
For $2\times4$ the first approach is more efficient (you just have to compare $2$ values against $0$), but for larger gardens the second approach is more efficient (you always only have to compare $4$ values for rectangles, $6$ for squares, independent of the size of the garden).