Python3-在饥饿的狼群之间分配口粮的最佳方法是什么?

时间:2018-09-28 00:08:35

标签: python algorithm sorting distribution voting-system

我遇到一个问题,希望我编写一个程序来将x量的鱼分配给x量的狼。这是确切的问题。

50只饥饿的狼去钓鱼,抓了50条鱼。现在他们需要在其中分配这50条相同的鱼。他们的民主制度运作如下: 所有狼均按其资历进行排名。首先,最高级的狼(被称为“群雄”)提出一项分配计划,明确说明每只狼会得到多少鱼。这50头狼会投票表决该计划(没有阻挠),如果超过或等于一半的狼投票赞成该计划,则该票将通过。如果通过了,狼们就会把鱼吃掉。如果失败,则将提出该计划的人(在这种情况下为包长)杀死,然后第二高的狼将取代“包长”并提出他的计划。我们按照资历顺序重复上述相同过程,直到通过某人的计划为止。 假设每只狼都基于以下优先事项做出决定:

  1. 他不想死。
  2. 鉴于他不会死,他宁愿得到尽可能多的鱼。
  3. 鉴于他将获得相同数量的鱼,他希望尽可能多的其他狼死亡。

我已经写了大多数逻辑,但是我不确定如何优化分配,以便正确遵循优先级。如果有人可以指出正确的方向,我很乐意找出其余的问题,也许有一个模块可以基于二项式分布(scipy,pandas)使用

到目前为止,这是我的代码。

import math
import numpy as np

def findDistribution(num_w, num_f):
    ranked_wolves = list(range(num_w+1))[1:] # 1-50
    distribution = [0]*num_w


    for index, pack_leader in enumerate(ranked_wolves):
        num_w = len(ranked_wolves[index:])
        wolfpack = distribution[index:]
        remaining_fish = num_f

        # If Wolf is last one then takes all fish
        if len(wolfpack) == 1:
            distribution[index] = remaining_fish
            break

        for wolf, value in enumerate(distribution[index:]):
            portion = remaining_fish/len(wolfpack)

            if wolf == 0:
                amount = math.ceil(portion)
                distribution[index] = amount # Pack LEader Gets the Most
                wolfpack.pop()
                remaining_fish-= amount
                continue
            else:
                amount = math.ceil(portion)
                distribution[index+wolf] = amount
                wolfpack.pop()
                remaining_fish -= amount

        # Voting
        # Count all wolves with same number of fish
        mode = stats.mode(distribution[index:])
        total_votes = len(distribution[index:])
        vote_no = mode.count[0]
        vote_yes = total_votes - vote_no

        # If more wolves without food than wolves with food
        if num_f/len(distribution[index:]) < .5:
            distribution[index] = -1

        # Going to get same number of fish so vote no
        elif vote_yes >= vote_no :
            break
        else:
            distribution[index] = -1


    # Return a tuple, with first value being the wolf number whose proposal
    # is accepted and the second value being a list of the distribution for
    # every wolf (-1 for dead wolves).
    return pack_leader, distribution

1 个答案:

答案 0 :(得分:1)

我认为您错过了练习的重点。逻辑涉及更多。

考虑一例2头狼(#0和#1,这是头目)。领导者提议0, 2(采取一切措施),并对其投赞成票,从而确保通过率达到50%。

现在看看其中3个(#0,#1和#3,它是领导者)会发生什么。计划0, 0, 3会失败:#1很高兴成为领导者,#0很嗜血。因此,#2必须提出不同的计划,显然最好的做法是1, 0, 2。在这里#0会投赞成票,因为如果对首领的投票将被杀死,他们将处于2头狼案中,并且一无所获。

尝试用纸和铅笔审视4头和5头狼的情况,并观察那里的逻辑原理(计划分别为0, 1, 0, 31, 0, 1, 0, 3)。然后,您可以开始对该逻辑进行编程。