Python 装料桶:“;“横向”;或;“垂直”吗;?
让我们有N个可以容纳M个项目的桶。 数组保存每个bucket中的当前项目计数:status=np。零(N):最大值M 我需要一个函数来为两种不同类型的添加项序列返回下一个bucket候选者 我还想知道是否可以通过状态数组进行循环,但使用numpy操作Python 装料桶:“;“横向”;或;“垂直”吗;?,python,algorithm,bucket,Python,Algorithm,Bucket,让我们有N个可以容纳M个项目的桶。 数组保存每个bucket中的当前项目计数:status=np。零(N):最大值M 我需要一个函数来为两种不同类型的添加项序列返回下一个bucket候选者 我还想知道是否可以通过状态数组进行循环,但使用numpy操作 1. algo=hop ... here is the order : add item to the buckets one after another, when it reaches N start from the first bu
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
因此,根据算法,如果我通过“status”,我应该得到下一个要添加的bucket。。并更新状态
next_bucket(status, algo) -return-> bucket-x, is_empty?
--------> None if full
我的想法是“跳跃”,在状态>零中搜索最小的数字,除非之前的最后一个桶达到M “填充”,用最大值搜索最后一个桶,除非所有桶都小于M,在这种情况下,选择第一个桶
如果M=5,则填充:
:s4
:数组([5,5,5,0,0],dtype=int8)
:np.其中(s4<5)[0][0]
: 3
:s3
:数组([5,3,0,0,0],dtype=int8)
:np.其中(s3<5)[0][0]
: 1
到目前为止,我想到的是:
def bucket(status, vmax, seq='fill', reverse=False) :
# print status
if seq == 'fill' :
res = np.where(status[::-1] < vmax)[0] if reverse else np.where(status < vmax)[0]
if len(res) == 0 : return None #FULL
if seq == 'hop' :
vmin = status.min()
if vmin == vmax : return None #FULL
res = np.where(status[::-1] == vmin)[0] if reverse else np.where(status == vmin)[0]
b = res[0]
inc_dec = + 1
if reverse :
inc_dec = -1
b = len(status) - b - 1
if status[b] == vmax :
if status[b + inc_dec] < vmax : status[b + inc_dec] += 1
else : status[b] += 1
print status
return b
def bucket(status, vmax, seq='fill', reverse=False) :
# print status
if seq == 'fill' :
res = np.where(status[::-1] < vmax)[0] if reverse else np.where(status < vmax)[0]
if len(res) == 0 : return None #FULL
if seq == 'hop' :
vmin = status.min()
if vmin == vmax : return None #FULL
res = np.where(status[::-1] == vmin)[0] if reverse else np.where(status == vmin)[0]
b = res[0]
inc_dec = + 1
if reverse :
inc_dec = -1
b = len(status) - b - 1
if status[b] == vmax :
if status[b + inc_dec] < vmax : status[b + inc_dec] += 1
else : status[b] += 1
print status
return b
In [498]: s0 = np.zeros(3, dtype=np.byte)
In [499]: for _ in xrange(10) : bucket(s0, vmax=3, seq='fill')
[1 0 0]
[2 0 0]
[3 0 0]
[3 1 0]
[3 2 0]
[3 3 0]
[3 3 1]
[3 3 2]
[3 3 3]
In [500]: s0 = np.zeros(3, dtype=np.byte)
In [501]: for _ in xrange(10) : bucket(s0, vmax=3, seq='hop')
[1 0 0]
[1 1 0]
[1 1 1]
[2 1 1]
[2 2 1]
[2 2 2]
[3 2 2]
[3 3 2]
[3 3 3]
[3 3 3]