生成从整数 1-18 中选择的 3 组 6 个数字的所有可能集合 [英] Generating all possible sets of 3 groups of 6 numbers chosen from the integers 1-18

查看:97
本文介绍了生成从整数 1-18 中选择的 3 组 6 个数字的所有可能集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 6 组代表骰子,因此这些组中整数的顺序无关紧要.每组中的组的顺序确实很重要,但要检查每个组,找到每个组的排列很简单,我可以这样做.

Since the groups of 6 represent dice, the order of the integers in these groups does not matter. The order of the groups within each set does matter, but to check each one it would be simple to find the permutations of each set, which I can do.

现在,我想出了一种方法来实现这一切,问题是效率.我想检查每一组,但到目前为止我只想出了一种使用方法:

Now, I've come up with a way to achieve all this, the problem is efficiency. I want to check each set but so far I've only come up with a way using:

for i in itertools.combinations(num, 6):
    W.append([i])
print(W)
print("Done building list of ",len(W)," possible dice.")

这给了我从 18 个整数中选择的 6 个数字的所有可能骰子,其中数字的顺序无关紧要.这是 18,564 组,每组 6.然后,我使用以下方法找到这些骰子的组合:

which gives me all possible dice of 6 numbers chosen from 18 integers where the order of the numbers doesn't matter. This is 18,564 groups of 6. Then, I find the combination of these dice using:

for j in itertools.combinations(W, 3):
    check = list(itertools.chain(*list(itertools.chain(*j))))
    check = sorted(check)
#print(check)
if check == num:
   

问题源于第二个组合将迭代 10^15 种可能性,其中只有一小部分是我想要生成的.

The issue arises from the fact that the second combination will iterate through 10^15 possibilities where only a small fraction of that is what I want to generate.

我还有一个问题:

我很确定将 18 个事物分配给 3 个不同的 6 个组(其中组内的顺序无关紧要)的总方法为:18!/(6!)^3 ~ 1700 万种方式,可以相对较快地迭代.这是正确的吗?有没有办法迭代地生成这些方式,而无需过滤更多的可能性?

I'm pretty sure that the total ways to assign 18 things to 3 distinct groups of 6 where the order within the groups doesn't matter is given by: 18!/(6!)^3 ~ 17 million ways, which can be iterated through relatively quickly. Is this right? Is there a way to generate these ways iteratively without filtering through a much larger set of possibilities?

最后一点:

我正在做的是尝试生成所有非传递性六面骰子集,其中每个骰子的每个面都有一个不同的数字.我已经有了可以检查集合并确定它是否符合此条件并存储最佳结果的代码,即骰子击败该行中下一个骰子的最高概率.

What I'm doing is trying to generate all sets of non-transitive six sided dice where each face of each dice has a distinct number. I already have the code that can check a set and decide if it fits this condition and store the best result, i.e. the highest probability of a dice beating the next dice in the line.

 for k in itertools.permutations(j):
        B = k[0]
        G = k[1]
        R = k[2]

        b = 0
        g = 0
        r = 0

        for m in B:
            for n in G:
                if m > n:
                    b = b + 1
                else:
                    continue
        for m in G:
            for n in R:
                if m > n:
                    g = g + 1
                else:
                    continue
        for m in R:
            for n in B:
                if m > n:
                    r = r + 1
                else:
                    continue

        w = w + 1
        print(w)
        if b <= 18 or g <= 18 or r <= 18:
            continue
        else:
            if b >= blue and g >= green  and r >= red:
                Blue = B
                blue = b

                Green = G
                green = g

                Red = R
                red = r

                print(B)
                print(b/36)
                print(G)
                print(g/36)
                print(R)
                print(r/36)
                continue
            else:
                continue
else:
    continue

推荐答案

首先,从 18 个事物中选择 6 个事物的所有组合.这些是你的第一个骰子.有 18564 种可能性.

First, take all the combinations of choosing 6 things from 18. These are your first dice. There are 18564 possibilities.

然后,从剩下的 12 个(924 个)中选择 6 个事物的所有组合,类似地.考虑到你的第一个骰子,这会给你第二个骰子.

Then, take all the combinations of choosing 6 things from 12 remaining (924), similarly. This will give you a second die, given your first one.

最后,第三个骰子就是剩下的所有数字.这是 18564x924x1=17,153,136 种可能性.

Finally, the third die is just all the remaining numbers. That's 18564x924x1=17,153,136 possibilities.

但实际上,等等.我们也不关心三个骰子的顺序.所以我们可以假设1"是在第一个死.然后不在第一个骰子上的最低数字在第二个骰子上.这是 6188x462x1=2,858,856 种可能性.

But actually, wait. We don't care about the order of the three dice, either. So we can assume that "1" is on the first die. And then that the lowest number not on the first die, is on the second die. That's 6188x462x1=2,858,856 possibilities.

这是一些python代码.它不像您自己进行组合那样快,但它应该运行良好.

Here's some python code. It's not as fast as if you did the combinations yourself, but it should run fine.

如果你在真正的可传递骰子上运行这个,尝试删除唯一"的约束!我很好奇是否有有趣的重复骰子.

And if you run this on real transitive dice, try removing the "unique" constraint! I'm curious if there are interesting dice with repeats.

import itertools
def iter_dice():
    nums = list(range(1,19))
    for first_die in itertools.combinations(nums[1:], 5):
        first_die = (1,) + first_die
        remaining = sorted(set(nums) - set(first_die))
        for second_die in itertools.combinations(remaining[1:], 5):
            second_die = (remaining[0],) + second_die
            third_die = sorted(set(remaining) - set(second_die))
            yield first_die, second_die, third_die
print(len(list(iter_dice())))
print(next(iter_dice()))

这篇关于生成从整数 1-18 中选择的 3 组 6 个数字的所有可能集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆