填充桶:“水平”还是“垂直”?

问题描述 投票:0回答:1

让我们有可以容纳M个物品的N个桶。数组保存每个存储区中的当前项目计数:status = np.zeros(N):最大值M

我需要一个函数来为两种不同类型的添加项目序列返回下一个存储桶候选对象。

我还想知道是否可以不通过状态数组循环但使用numpy ops。

1. algo=hop ... here is the order :
    add item to the buckets one after another, when it reaches N start from the first bucket again..
    1,2,3,1,2,3,1,2,3...
2. algo=fill ... here is the order :
    fill the 1st bucket, then fill 2nd bucket , ..... fill n-th bucket
    1,1,1,.. m-times, 2,2,2,... m-times, .... m,m,m,m, ...m-times

因此,根据算法,如果我传递“状态”,则应该获取要添加..的下一个存储桶,并更新状态。

next_bucket(status, algo) -return-> bucket-x, is_empty? 
                          --------> None if full

我的想法是“跳跃”,在状态中搜索最小的数字>零,除非倒数第二个桶达到M。

“ fill”,用Max搜索最后一个存储桶,除非所有都为


if M=5, fill :

: s4 
: array([5, 5, 5, 0, 0], dtype=int8)

: np.where(s4 < 5)[0][0]
: 3


: s3
: array([5, 3, 0, 0, 0], dtype=int8)

: np.where(s3 < 5)[0][0]
: 1

python algorithm bucket
1个回答
0
投票

到目前为止,这是我想出的:

def bucket(status, vmax, seq='fill') :
#       print status
        is_empty = False
        if seq == 'fill' :
                res = np.where(status < vmax)[0]
                if len(res) == 0 : return None
                b = res[0]

        if seq == 'hop' :
                vmin = status.min()
                res = np.argwhere(status == vmin).T[0]
                if len(res) == 0 : return None
                b = res[0]

        if status[b] == vmax : status[b+1] + 1
        else : status[b] += 1
        print status
        return b, is_empty
© www.soinside.com 2019 - 2024. All rights reserved.