添加约束到排列

时间:2018-01-25 12:16:56

标签: python permutation

我试图用list = [0,1,2,3,4,5,6]计算所有排列,并遵守一些约束条件。

  • 位置0必须是> 3
  • 位置3 +位置5<职位4

使用我当前的代码,我确定了每个的所有排列和迭代,应用约束。

import itertools

Used_permutations = []    
numbers = [0,1,2,3,4,5,6]
all_permutations = list(itertools.permutations(numbers)) #Determine all permutations

for permutation in all_permutations:
    if permutation[0] > 3 and permutation[3] + permutation[5] < permutation[4]: #Constraints applied to permutations
        Used_permutations.append(permutation)  
        ####################################################
        ###   Apply calculations to current permutation ###
        ####################################################

这段代码的问题在于我浪费时间找到所有可能的排列,只是为了再次过滤掉它们。有人可以协助一种方法来应用约束,同时确定排列,所以不是所有的N!确定了?

1 个答案:

答案 0 :(得分:5)

不是首先创建所有排列的list,然后将其中一些元素添加到第二个列表并丢弃其余的(在这种情况下约为90%),您可以使用列表推导来过滤itertools在创建时产生的排列。

>>> numbers = [0,1,2,3,4,5,6]
>>> [p for p in itertools.permutations(numbers) if p[0] > 3 and p[3] + p[5] < p[4]]
[(4, 0, 1, 2, 6, 3, 5),
 (4, 0, 1, 3, 6, 2, 5),
 ... a few more ...
 (6, 5, 4, 1, 3, 0, 2),
 (6, 5, 4, 2, 3, 0, 1)]
>>> len(_)
516

如果检查变得更复杂,您甚至不必使用列表推导。您可以在for条件的常规if循环中执行相同操作,唯一的区别是您不首先收集list中的所有排列,而是直接迭代生成器:< / p>

all_permutations = itertools.permutations(numbers)  # no list(...)
for permutation in all_permutations:
    ...

两种方法仍将生成所有N!排列,但大多数被立即丢弃,只有&#34;右&#34;其中的存储在列表中。

如果您甚至不想生成它们,则必须实施自定义递归permutations算法并手动检查是否例如对于p[3]的给定值,p[5]可以有任何有效值,依此类推。像这样:

def manual_permutations(numbers, n=0, last=0, lastlast=0):
    if len(numbers) <= 1:
        yield tuple(numbers)
    else:
        for i, first in enumerate(numbers):
            # first constraint
            if n == 0 and first <= 3: continue
            # second constraint: 3 + 5 < 4 === 3 - 4 < -5 === 3 < 4 - 5
            # assuming numbers are ordered: rest[0] is min, rest[-1] is max
            rest = numbers[:i] + numbers[i+1:]
            if n == 3 and first >= rest[-1] - rest[0]: continue
            if n == 4 and last - first >= - rest[0]: continue
            if n == 5 and lastlast - last >= - first: continue
            # constraints okay, recurse
            for perm in manual_permutations(rest, n+1, first, last):
                yield (first,) + perm

这在生成排列时检查两个约束,因此,例如,所有以<= 3开头的排列都不会生成。第二个检查有点复杂,可能会进一步改进(如果我们在函数的开头添加一个计数器,我们看到有~1200个递归调用)。无论如何,使用IPython&#39; %timeit,我们看到&#34;手册&#34;与使用itertools相比,动态检查的方法仍然是的三倍,所以即使改进检查也可能不会比它更快。 *)而且,你自己的原始循环实际上也不是那么慢。

>>> %timeit original_loop(numbers)
1000 loops, best of 3: 736 µs per loop

>>> %timeit list(itertools_perms(numbers))
1000 loops, best of 3: 672 µs per loop

>>> %timeit list(manual_permutations(numbers))
100 loops, best of 3: 2.11 ms per loop

当然,根据输入列表的大小和约束,手动方法可以或多或少地提供保存,但也可以(很多)或多或少地难以实现或适应变化的约束。我个人仍然使用itertools.permutations和一些简单易读的过滤器。

*)更新:在我之前的编辑中,手动方法出现得更快;这是因为我忘了实际上使用两个函数返回的生成器。