ここで私は私の問題の最小限の完全な検証可能な例を提供しています:
サイズ 3 X 17 の長方形行列が考慮されます: 行 = [10,6,9] が考慮されます。ここで、列はそれぞれ値に関連付けられたパターンです
example file: "patterns_list" <pattern <space> associated value>
['2','3'] 12
['2','1'] 11
['2','5'] 11
['3','4'] 10
['3','5'] 9
['4','1'] 9
['4','5'] 9
['3','6'] 8
['4','6'] 8
['1','5'] 8
['2'] 7
['1','6'] 7
['3'] 5
['4'] 5
['1'] 4
['5'] 4
['6'] 3
現在、列の値と行の値の差は、マトリックス 3 X 17 のコストと見なされ、コストが負であることが判明した場合は、すべての列の値の合計に置き換えられます (大きな値を確保するためだけに具体的なことは何もありません)。ここで、最小コストの割り当てを行う必要があります。Sudo apt-get install python-munkresを使用してライブラリmunkresをインストール し、次のコードを実行しました。
from munkres import Munkres, print_matrix
import linecache
rows=[10,6,9]
v=[]
diff=[]
value=[]
f = open("patterns_list","r")
for line in f:
line=line.strip()
line=line.split(' ')
v.append(int(line[1]))
total=sum(v)
for i in range(0,len(rows)):
for j in range(0,len(v)):
x=v[j]-rows[i]
if x<0:
value.append(total)
else:
value.append(v[j]-rows[i])
diff.append(value)
value=[]
matrix=diff
m = Munkres()
indexes = m.compute(matrix)
print_matrix(matrix, msg='Lowest cost through this matrix:\n')
total = 0
patterns=[]
print "Allocation indices:"
for row, column in indexes:
value = matrix[row][column]
total += value
print '(%d, %d) -> %d' % (row, column, value)
patterns.append(int(column))
print 'total cost: %d' % total
print "Corresponding allocated patterns:\n"
for i in range(0,len(patterns)):
line = linecache.getline("patterns_list",patterns[i])
print line
次の出力が生成されます。
Lowest cost through this matrix:
[ 2, 1, 1, 0, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130]
[ 6, 5, 5, 4, 3, 3, 3, 2, 2, 2, 1, 1, 130, 130, 130, 130, 130]
[ 3, 2, 2, 1, 0, 0, 0, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130]
Allocation indices:
(0, 3) -> 0
(1, 10) -> 1
(2, 4) -> 0
total cost: 1
Corresponding allocated patterns:
['2','5'] 11
['1','5'] 8
['3','4'] 10
問題は、[2,5]、[1,5]、[3,4] が、最小コストに対応する最終的に割り当てられたパターンであることです。ここで、パターン [2,5]、[1,5] は相互に排他的ではありません。「5」は共通です。r1 が [2,5] 割り当てられると、割り当てられた要素のいずれかを含むパターンの残りの部分、つまりここでは 2,5 は割り当てに使用できなくなります。または、マトリックス内の対応するパターン関連コストを非常に高い値に更新する必要があります。次の行と見なされなくなったものは、このように続行する必要があります。
最終的には、割り当てが可能な場合、対応するパターンは本質的に相互に排他的である必要があります。
誰でもこれに取り組む方法を提案できますか?