所有可能的方法将彩球放入垃圾箱

时间:2017-05-18 10:24:46

标签: python numpy combinatorics

我有很多箱子。每个垃圾箱只能容纳一个球。

让我们说,我有

Na数量的红球

Nb数量的蓝球

Nc数量的绿球

等等。

我想找出将球放入长度(Na + Nb + Nc)箱子的所有可能方法。

例如,我们说我只有两个红球和两个蓝球。我想把它们放进4个箱子里。可能的安排方式是:

R R B B
R B R B
R B B R
B R R B
B R B R
B B R R

(我希望我没有错过任何组合)

有没有简单的方法来生成不同颜色的索引,例如:

First row is : R=(0,1) B=(2,3)
Second row is : R=(0,2) B=(1,3)

有一种简单的方法可以在numpy中生成这个吗?

箱子实际上有不同的重量,如:

[0.1, 0.3, 0.2, 0.5]

因此,对于表示为R at (0,1) and B at (2,3)的R R B B组合,R的总权重为0.1+0.3=0.4,而B的总权重为0.2+0.5=0.7

我最终对不同排列中每种颜色的总重量感兴趣,并希望从另一个成本函数f(total_weight(R),total_weight(B))中选择最佳颜色。如果总体重的产生可以在numpy中以不同的简单方式完成,那还有什么意见吗?

4 个答案:

答案 0 :(得分:1)

生成排列并删除重复项。

>>> import itertools
>>> Na = 2
>>> Nb = 2
>>> p = itertools.permutations(['R']*Na + ['B']*Nb)
>>> for perm in sorted(list(set(p)), reverse=True):
...     print perm
... 
('R', 'R', 'B', 'B')
('R', 'B', 'R', 'B')
('R', 'B', 'B', 'R')
('B', 'R', 'R', 'B')
('B', 'R', 'B', 'R')
('B', 'B', 'R', 'R')

答案 1 :(得分:1)

您可以使用itertools解决这个问题:

import itertools
last = None
result = []
for v in itertools.permutations(['A','A','B','B']):
    key = "".join(v)
    if key == last: continue
    last = key
    result.append(v)
print result

答案 2 :(得分:1)

这是一个“多组合”实现,不需要消除重复的排列。第一个参数n是列表[Na,Nb,Nc,...]。

它被实现为一个递归生成器,因此您可以迭代组合,而不必将它们全部存储在内存中。你在评论中说Na + Nb + ...通常在20左右但可能高达50或100.这意味着你几乎肯定不想将所有组合存储在内存中。考虑具有四个“颜色”的示例,其中Na = Nb = Nc = Nd = 5.组合的数量是choose(20, 5) * choose(15, 5) * choose(10, 5) = 11732745024,其中choose(n, k)binomial coefficient。我的计算机只有16 GB的RAM,因此这些组合所需的存储量将远远超过我计算机的内存。

from itertools import combinations


def multicombinations(n, bins=None):
    if bins is None:
        bins = set(range(sum(n)))
    if len(n) == 0:
        yield []
    else:
        for c in combinations(bins, n[0]):
            for t in multicombinations(n[1:], bins - set(c)):
                yield [c] + t

它生成一个元组列表。也就是说,如果您对第一行的描述是“第一行是:R =(0,1)B =(2,3)”,则multicombinations([2, 2])生成的第一个值是[(0, 1), (2, 3)]。 (考虑到您接下来要做的重量计算的描述,这可能不是结果的最佳格式。)

一些例子:

In [74]: list(multicombinations([2, 2]))
Out[74]: 
[[(0, 1), (2, 3)],
 [(0, 2), (1, 3)],
 [(0, 3), (1, 2)],
 [(1, 2), (0, 3)],
 [(1, 3), (0, 2)],
 [(2, 3), (0, 1)]]

In [75]: list(multicombinations([3, 2]))
Out[75]: 
[[(0, 1, 2), (3, 4)],
 [(0, 1, 3), (2, 4)],
 [(0, 1, 4), (2, 3)],
 [(0, 2, 3), (1, 4)],
 [(0, 2, 4), (1, 3)],
 [(0, 3, 4), (1, 2)],
 [(1, 2, 3), (0, 4)],
 [(1, 2, 4), (0, 3)],
 [(1, 3, 4), (0, 2)],
 [(2, 3, 4), (0, 1)]]

In [76]: list(multicombinations([2, 3, 2]))
Out[76]: 
[[(0, 1), (2, 3, 4), (5, 6)],
 [(0, 1), (2, 3, 5), (4, 6)],
 [(0, 1), (2, 3, 6), (4, 5)],
 [(0, 1), (2, 4, 5), (3, 6)],
 [(0, 1), (2, 4, 6), (3, 5)],
 [(0, 1), (2, 5, 6), (3, 4)],
 [(0, 1), (3, 4, 5), (2, 6)],
 [(0, 1), (3, 4, 6), (2, 5)],
 [(0, 1), (3, 5, 6), (2, 4)],
 [(0, 1), (4, 5, 6), (2, 3)],
 [(0, 2), (1, 3, 4), (5, 6)],
 [(0, 2), (1, 3, 5), (4, 6)],
 [(0, 2), (1, 3, 6), (4, 5)],
 [(0, 2), (1, 4, 5), (3, 6)],
 [(0, 2), (1, 4, 6), (3, 5)],
 [(0, 2), (1, 5, 6), (3, 4)],
 [(0, 2), (3, 4, 5), (1, 6)],
 [(0, 2), (3, 4, 6), (1, 5)],
 [(0, 2), (3, 5, 6), (1, 4)],
 [(0, 2), (4, 5, 6), (1, 3)],
 [(0, 3), (1, 2, 4), (5, 6)],
 [(0, 3), (1, 2, 5), (4, 6)],
 [(0, 3), (1, 2, 6), (4, 5)],
 [(0, 3), (1, 4, 5), (2, 6)],
 [(0, 3), (1, 4, 6), (2, 5)],
 [(0, 3), (1, 5, 6), (2, 4)],
 [(0, 3), (2, 4, 5), (1, 6)],
 [(0, 3), (2, 4, 6), (1, 5)],
 [(0, 3), (2, 5, 6), (1, 4)],
 [(0, 3), (4, 5, 6), (1, 2)],
 [(0, 4), (1, 2, 3), (5, 6)],
 [(0, 4), (1, 2, 5), (3, 6)],
 [(0, 4), (1, 2, 6), (3, 5)],
 [(0, 4), (1, 3, 5), (2, 6)],
 [(0, 4), (1, 3, 6), (2, 5)],
 [(0, 4), (1, 5, 6), (2, 3)],
 [(0, 4), (2, 3, 5), (1, 6)],
 [(0, 4), (2, 3, 6), (1, 5)],
 [(0, 4), (2, 5, 6), (1, 3)],
 [(0, 4), (3, 5, 6), (1, 2)],
 [(0, 5), (1, 2, 3), (4, 6)],
 [(0, 5), (1, 2, 4), (3, 6)],
 [(0, 5), (1, 2, 6), (3, 4)],
 [(0, 5), (1, 3, 4), (2, 6)],
 [(0, 5), (1, 3, 6), (2, 4)],
 [(0, 5), (1, 4, 6), (2, 3)],
 [(0, 5), (2, 3, 4), (1, 6)],
 [(0, 5), (2, 3, 6), (1, 4)],
 [(0, 5), (2, 4, 6), (1, 3)],
 [(0, 5), (3, 4, 6), (1, 2)],
 [(0, 6), (1, 2, 3), (4, 5)],
 [(0, 6), (1, 2, 4), (3, 5)],
 [(0, 6), (1, 2, 5), (3, 4)],
 [(0, 6), (1, 3, 4), (2, 5)],
 [(0, 6), (1, 3, 5), (2, 4)],
 [(0, 6), (1, 4, 5), (2, 3)],
 [(0, 6), (2, 3, 4), (1, 5)],
 [(0, 6), (2, 3, 5), (1, 4)],
 [(0, 6), (2, 4, 5), (1, 3)],
 [(0, 6), (3, 4, 5), (1, 2)],
 [(1, 2), (0, 3, 4), (5, 6)],
 [(1, 2), (0, 3, 5), (4, 6)],
 [(1, 2), (0, 3, 6), (4, 5)],
 [(1, 2), (0, 4, 5), (3, 6)],
 [(1, 2), (0, 4, 6), (3, 5)],
 [(1, 2), (0, 5, 6), (3, 4)],
 [(1, 2), (3, 4, 5), (0, 6)],
 [(1, 2), (3, 4, 6), (0, 5)],
 [(1, 2), (3, 5, 6), (0, 4)],
 [(1, 2), (4, 5, 6), (0, 3)],
 [(1, 3), (0, 2, 4), (5, 6)],
 [(1, 3), (0, 2, 5), (4, 6)],
 [(1, 3), (0, 2, 6), (4, 5)],
 [(1, 3), (0, 4, 5), (2, 6)],
 [(1, 3), (0, 4, 6), (2, 5)],
 [(1, 3), (0, 5, 6), (2, 4)],
 [(1, 3), (2, 4, 5), (0, 6)],
 [(1, 3), (2, 4, 6), (0, 5)],
 [(1, 3), (2, 5, 6), (0, 4)],
 [(1, 3), (4, 5, 6), (0, 2)],
 [(1, 4), (0, 2, 3), (5, 6)],
 [(1, 4), (0, 2, 5), (3, 6)],
 [(1, 4), (0, 2, 6), (3, 5)],
 [(1, 4), (0, 3, 5), (2, 6)],
 [(1, 4), (0, 3, 6), (2, 5)],
 [(1, 4), (0, 5, 6), (2, 3)],
 [(1, 4), (2, 3, 5), (0, 6)],
 [(1, 4), (2, 3, 6), (0, 5)],
 [(1, 4), (2, 5, 6), (0, 3)],
 [(1, 4), (3, 5, 6), (0, 2)],
 [(1, 5), (0, 2, 3), (4, 6)],
 [(1, 5), (0, 2, 4), (3, 6)],
 [(1, 5), (0, 2, 6), (3, 4)],
 [(1, 5), (0, 3, 4), (2, 6)],
 [(1, 5), (0, 3, 6), (2, 4)],
 [(1, 5), (0, 4, 6), (2, 3)],
 [(1, 5), (2, 3, 4), (0, 6)],
 [(1, 5), (2, 3, 6), (0, 4)],
 [(1, 5), (2, 4, 6), (0, 3)],
 [(1, 5), (3, 4, 6), (0, 2)],
 [(1, 6), (0, 2, 3), (4, 5)],
 [(1, 6), (0, 2, 4), (3, 5)],
 [(1, 6), (0, 2, 5), (3, 4)],
 [(1, 6), (0, 3, 4), (2, 5)],
 [(1, 6), (0, 3, 5), (2, 4)],
 [(1, 6), (0, 4, 5), (2, 3)],
 [(1, 6), (2, 3, 4), (0, 5)],
 [(1, 6), (2, 3, 5), (0, 4)],
 [(1, 6), (2, 4, 5), (0, 3)],
 [(1, 6), (3, 4, 5), (0, 2)],
 [(2, 3), (0, 1, 4), (5, 6)],
 [(2, 3), (0, 1, 5), (4, 6)],
 [(2, 3), (0, 1, 6), (4, 5)],
 [(2, 3), (0, 4, 5), (1, 6)],
 [(2, 3), (0, 4, 6), (1, 5)],
 [(2, 3), (0, 5, 6), (1, 4)],
 [(2, 3), (1, 4, 5), (0, 6)],
 [(2, 3), (1, 4, 6), (0, 5)],
 [(2, 3), (1, 5, 6), (0, 4)],
 [(2, 3), (4, 5, 6), (0, 1)],
 [(2, 4), (0, 1, 3), (5, 6)],
 [(2, 4), (0, 1, 5), (3, 6)],
 [(2, 4), (0, 1, 6), (3, 5)],
 [(2, 4), (0, 3, 5), (1, 6)],
 [(2, 4), (0, 3, 6), (1, 5)],
 [(2, 4), (0, 5, 6), (1, 3)],
 [(2, 4), (1, 3, 5), (0, 6)],
 [(2, 4), (1, 3, 6), (0, 5)],
 [(2, 4), (1, 5, 6), (0, 3)],
 [(2, 4), (3, 5, 6), (0, 1)],
 [(2, 5), (0, 1, 3), (4, 6)],
 [(2, 5), (0, 1, 4), (3, 6)],
 [(2, 5), (0, 1, 6), (3, 4)],
 [(2, 5), (0, 3, 4), (1, 6)],
 [(2, 5), (0, 3, 6), (1, 4)],
 [(2, 5), (0, 4, 6), (1, 3)],
 [(2, 5), (1, 3, 4), (0, 6)],
 [(2, 5), (1, 3, 6), (0, 4)],
 [(2, 5), (1, 4, 6), (0, 3)],
 [(2, 5), (3, 4, 6), (0, 1)],
 [(2, 6), (0, 1, 3), (4, 5)],
 [(2, 6), (0, 1, 4), (3, 5)],
 [(2, 6), (0, 1, 5), (3, 4)],
 [(2, 6), (0, 3, 4), (1, 5)],
 [(2, 6), (0, 3, 5), (1, 4)],
 [(2, 6), (0, 4, 5), (1, 3)],
 [(2, 6), (1, 3, 4), (0, 5)],
 [(2, 6), (1, 3, 5), (0, 4)],
 [(2, 6), (1, 4, 5), (0, 3)],
 [(2, 6), (3, 4, 5), (0, 1)],
 [(3, 4), (0, 1, 2), (5, 6)],
 [(3, 4), (0, 1, 5), (2, 6)],
 [(3, 4), (0, 1, 6), (2, 5)],
 [(3, 4), (0, 2, 5), (1, 6)],
 [(3, 4), (0, 2, 6), (1, 5)],
 [(3, 4), (0, 5, 6), (1, 2)],
 [(3, 4), (1, 2, 5), (0, 6)],
 [(3, 4), (1, 2, 6), (0, 5)],
 [(3, 4), (1, 5, 6), (0, 2)],
 [(3, 4), (2, 5, 6), (0, 1)],
 [(3, 5), (0, 1, 2), (4, 6)],
 [(3, 5), (0, 1, 4), (2, 6)],
 [(3, 5), (0, 1, 6), (2, 4)],
 [(3, 5), (0, 2, 4), (1, 6)],
 [(3, 5), (0, 2, 6), (1, 4)],
 [(3, 5), (0, 4, 6), (1, 2)],
 [(3, 5), (1, 2, 4), (0, 6)],
 [(3, 5), (1, 2, 6), (0, 4)],
 [(3, 5), (1, 4, 6), (0, 2)],
 [(3, 5), (2, 4, 6), (0, 1)],
 [(3, 6), (0, 1, 2), (4, 5)],
 [(3, 6), (0, 1, 4), (2, 5)],
 [(3, 6), (0, 1, 5), (2, 4)],
 [(3, 6), (0, 2, 4), (1, 5)],
 [(3, 6), (0, 2, 5), (1, 4)],
 [(3, 6), (0, 4, 5), (1, 2)],
 [(3, 6), (1, 2, 4), (0, 5)],
 [(3, 6), (1, 2, 5), (0, 4)],
 [(3, 6), (1, 4, 5), (0, 2)],
 [(3, 6), (2, 4, 5), (0, 1)],
 [(4, 5), (0, 1, 2), (3, 6)],
 [(4, 5), (0, 1, 3), (2, 6)],
 [(4, 5), (0, 1, 6), (2, 3)],
 [(4, 5), (0, 2, 3), (1, 6)],
 [(4, 5), (0, 2, 6), (1, 3)],
 [(4, 5), (0, 3, 6), (1, 2)],
 [(4, 5), (1, 2, 3), (0, 6)],
 [(4, 5), (1, 2, 6), (0, 3)],
 [(4, 5), (1, 3, 6), (0, 2)],
 [(4, 5), (2, 3, 6), (0, 1)],
 [(4, 6), (0, 1, 2), (3, 5)],
 [(4, 6), (0, 1, 3), (2, 5)],
 [(4, 6), (0, 1, 5), (2, 3)],
 [(4, 6), (0, 2, 3), (1, 5)],
 [(4, 6), (0, 2, 5), (1, 3)],
 [(4, 6), (0, 3, 5), (1, 2)],
 [(4, 6), (1, 2, 3), (0, 5)],
 [(4, 6), (1, 2, 5), (0, 3)],
 [(4, 6), (1, 3, 5), (0, 2)],
 [(4, 6), (2, 3, 5), (0, 1)],
 [(5, 6), (0, 1, 2), (3, 4)],
 [(5, 6), (0, 1, 3), (2, 4)],
 [(5, 6), (0, 1, 4), (2, 3)],
 [(5, 6), (0, 2, 3), (1, 4)],
 [(5, 6), (0, 2, 4), (1, 3)],
 [(5, 6), (0, 3, 4), (1, 2)],
 [(5, 6), (1, 2, 3), (0, 4)],
 [(5, 6), (1, 2, 4), (0, 3)],
 [(5, 6), (1, 3, 4), (0, 2)],
 [(5, 6), (2, 3, 4), (0, 1)]]

答案 3 :(得分:0)

以下是一种可行的实施方式:

def permutations(colors, l):
    if l == 0:
        yield []
    else:
        for i in range(len(colors)):
            if colors[i]:
                la = colors[:i]
                lb = [colors[i][1:]]
                lc = colors[i + 1:]
                choice = colors[i][0]
                for rest in permutations(la + lb + lc, l - 1):
                    yield [choice] + rest

用法:

for choice in permutations([['R'] * 2, ['B'] * 2], 4):
    print(choice)


['R', 'R', 'B', 'B']
['R', 'B', 'R', 'B']
['R', 'B', 'B', 'R']
['B', 'R', 'R', 'B']
['B', 'R', 'B', 'R']
['B', 'B', 'R', 'R']