我想填充一個名為 answer 的二維表,我有 cols,rows,w : cols: 一個一維陣列,它cols[i]是每列答案的總和。w:一個一維陣列,其中 w[i] 是每列答案的權重。rows :一個一維陣列,它rows[i] = answer[i][0]*w[0] answer[i][1]*w[1] ...
和sum(cols*w)=sum(rows)
import numpy
rows=[17,21,18,52,16]
cols=[4,8,9,9,15]
answer=numpy.zeros((len(rows),len(cols)),dtype=int)
w=[4,3,1,5,2]
print('sum rows = {}'.format(sum(rows)))
temp=[]
for a,b in zip(cols,w):
temp.append(a*b)
print('sum cols = {}'.format(sum(temp)))
while True :
found=False
row,col=-1,-1
for i in range(0,len(rows)) :
for j in range(0,len(cols)):
if (rows[i]>=w[j] and cols[j]>0 and (found == False or
min(rows[i],cols[j]*w[j])>min(rows[row],cols[col]*w[col]))):
found=True
row=i
col=j
if not found :
break
answer[row][col]=answer[row][col] 1
rows[row]=rows[row]-w[col]
cols[col]=cols[col]-1
print(answer)
print(rows)
print(cols)
我用小整數測驗這段代碼,它可以作業。但使用不同的數字,不起作用。
rows=[84000,55000,22200,156000]
cols=[13,2,8,12]
w=[6000,56000,14400,1000]
uj5u.com熱心網友回復:
您可以將其視為線性規劃問題,并讓求解器為您完成所有繁重的作業。例如:
# !pip install pulp
import pulp
import numpy as np
rows=[17,21,18,52,16]
cols=[4,8,9,9,15]
w=[4,3,1,5,2]
row_list = list(range(len(rows)))
col_list = list(range(len(cols)))
problem = pulp.LpProblem("FillArr")
fill = pulp.LpVariable.dicts("Fill", (row_list, col_list), cat="Integer")
# set dummy objecitve function:
problem = 0
# numbers are non-negative:
for i in row_list:
for j in col_list:
problem = fill[i][j] >= 0
# row constraints:
for i in row_list:
problem = sum(fill[i][j] * w[j] for j in col_list) == rows[i]
# column constraints:
for j in col_list:
problem = sum(fill[i][j] for i in col_list) == cols[j]
# make sure that solver found an optimal solution
assert problem.solve() == 1
# recover result
answer = np.array([[fill[i][j].value() for j in col_list] for i in row_list], dtype=int)
# array([[ 0, 1, 9, 1, 0],
# [ 0, 7, 0, 0, 0],
# [ 2, 0, 0, 2, 0],
# [ 0, 0, 0, 6, 11],
# [ 2, 0, 0, 0, 4]])
# check if column sums are correct
assert np.all(answer.sum(0) == np.array(cols))
# check if row sums are correct
assert np.all((answer * np.array(w)[None,:]).sum(1) == np.array(rows))
求解器還會告訴您問題是否不可行,如您的第二個示例所示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/420163.html
標籤:
下一篇:使用遞回回溯。為什么代碼有效
