길이가 다른 두 개의 배열이 있습니다.
A = np.array([7,6,5,4,3,2,1])
B = np.array([37.97, 34.45, 32.41, 32.17, 35.48, 35.91, 33.81, 32.23, 33.46,
35.35, 33.03, 37.36, 32.18, 29.29, 30.23, 30.94, 34.26, 31.74,
29.24, 25.93, 29.26, 33.64, 33.28])
A의 내적이 가장 작도록 B에서 7 개의 숫자를 선택해야합니다. 또는 min (7x1 6x2 5x3 ... 2x6 x7)
. 두 배열의 순서를 변경해서는 안됩니다. 예를 들어 x1 = 32.41 (색인 2)
인 경우 x2
는 이전 색인이 아니어야합니다.
A와 B의 길이는 실제로 예제보다 길기 때문에 무차별 대입 대신 효율적인 알고리즘을 찾고 있습니다.
편집 : 순서를 변경하지 않음으로써 인덱스 2에서 요소를 선택하면 다음 항목은 인덱스 5 또는 10이 될 수 있지만 인덱스 0 또는 1이 아닐 수 있습니다. 인덱스 2,3,4와 같이 연속적 일 필요가 없습니다. 5 ....
답변 업데이트 : 그래서 이것은 @templatetypedef 및 @Damien 답변을 기반으로 지금까지 한 것입니다.
def min_dot_product(A,B,m,n):
P = np.zeros((n,m))
A_ = np.zeros((n,m))
B_ = np.zeros((n,m))
#P[0,0] = 0
P[1,1] = A[1]*B[1]
S[1,1] = 1
for k in range(2,m):
P[1,k] = np.inf
for i in range(2,n):
P[i,0] = 0
for k in range(1,m):
P[i,k] = min (P[i-1,k], P[i-1,k-1] + B[i] * A[k])
if (P[i-1,k] > P[i-1,k-1] + B[i] * A[k]):
A_[i,k] = A[k]
B_[i,k] = B[i]
S[i,k] = 1
return P,A_,B_,S
A = np.array([0,7,6,5,4,3,2,1]) # -> Insert 1 dummy value at the first position
B = np.array([0,37.97, 34.45, 32.41, 32.17, 35.48, 35.91, 33.81, 32.23, 33.46,
35.35, 33.03, 37.36, 32.18, 29.29, 30.23, 30.94, 34.26, 31.74,
29.24, 25.93, 29.26, 33.64, 33.28]) # -> Insert 1 dummy value at the first position
m = len(A)
n = len(B)
mat,A_,B_,S = min_dot_product(A,B,m,n)
Out:
mat
array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 241.15, inf, inf, inf, inf, inf, inf],
[ 0. , 226.87, 435.61, inf, inf, inf, inf, inf],
[ 0. , 225.19, 419.89, 596.46, inf, inf, inf, inf],
[ 0. , 225.19, 419.89, 596.46, 738.38, inf, inf, inf],
[ 0. , 225.19, 419.89, 596.46, 738.38, 846.11, inf, inf],
[ 0. , 225.19, 419.89, 588.94, 731.7 , 839.81, 913.73, inf],
[ 0. , 225.19, 418.57, 581.04, 717.86, 828.39, 904.27, 945.96],
[ 0. , 225.19, 418.57, 581.04, 714.88, 818.24, 895.31, 937.73],
[ 0. , 225.19, 418.57, 581.04, 714.88, 818.24, 888.94, 930.66],
[ 0. , 225.19, 418.57, 581.04, 713.16, 813.97, 884.3 , 921.97],
[ 0. , 225.19, 418.57, 581.04, 713.16, 813.97, 884.3 , 921.66],
[ 0. , 225.19, 418.27, 579.47, 709.76, 809.7 , 878.33, 916.48],
[ 0. , 205.03, 400.93, 564.72, 696.63, 797.63, 868.28, 907.62],
[ 0. , 205.03, 386.41, 552.08, 685.64, 787.32, 858.09, 898.51],
[ 0. , 205.03, 386.41, 541.11, 675.84, 778.46, 849.2 , 889.03],
[ 0. , 205.03, 386.41, 541.11, 675.84, 778.46, 846.98, 883.46],
[ 0. , 205.03, 386.41, 541.11, 668.07, 771.06, 841.94, 878.72],
[ 0. , 204.68, 380.47, 532.61, 658.07, 755.79, 829.54, 871.18],
[ 0. , 181.51, 360.26, 510.12, 636.33, 735.86, 807.65, 855.47],
[ 0. , 181.51, 357.07, 506.56, 627.16, 724.11, 794.38, 836.91],
[ 0. , 181.51, 357.07, 506.56, 627.16, 724.11, 791.39, 828.02],
[ 0. , 181.51, 357.07, 506.56, 627.16, 724.11, 790.67, 824.67]])
A_
array([[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0.],
[0., 7., 6., 0., 0., 0., 0., 0.],
[0., 7., 6., 5., 0., 0., 0., 0.],
[0., 0., 0., 0., 4., 0., 0., 0.],
[0., 0., 0., 0., 0., 3., 0., 0.],
[0., 0., 0., 5., 4., 3., 2., 0.],
[0., 0., 6., 5., 4., 3., 2., 1.],
[0., 0., 0., 0., 4., 3., 2., 1.],
[0., 0., 0., 0., 0., 0., 2., 1.],
[0., 0., 0., 0., 4., 3., 2., 1.],
[0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 6., 5., 4., 3., 2., 1.],
[0., 7., 6., 5., 4., 3., 2., 1.],
[0., 0., 6., 5., 4., 3., 2., 1.],
[0., 0., 0., 5., 4., 3., 2., 1.],
[0., 0., 0., 0., 0., 0., 2., 1.],
[0., 0., 0., 0., 4., 3., 2., 1.],
[0., 7., 6., 5., 4., 3., 2., 1.],
[0., 7., 6., 5., 4., 3., 2., 1.],
[0., 0., 6., 5., 4., 3., 2., 1.],
[0., 0., 0., 0., 0., 0., 2., 1.],
[0., 0., 0., 0., 0., 0., 2., 1.]])
B_
array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0. , 32.41, 32.41, 0. , 0. , 0. , 0. , 0. ],
[ 0. , 32.17, 32.17, 32.17, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 35.48, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. , 35.91, 0. , 0. ],
[ 0. , 0. , 0. , 33.81, 33.81, 33.81, 33.81, 0. ],
[ 0. , 0. , 32.23, 32.23, 32.23, 32.23, 32.23, 32.23],
[ 0. , 0. , 0. , 0. , 33.46, 33.46, 33.46, 33.46],
[ 0. , 0. , 0. , 0. , 0. , 0. , 35.35, 35.35],
[ 0. , 0. , 0. , 0. , 33.03, 33.03, 33.03, 33.03],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 37.36],
[ 0. , 0. , 32.18, 32.18, 32.18, 32.18, 32.18, 32.18],
[ 0. , 29.29, 29.29, 29.29, 29.29, 29.29, 29.29, 29.29], x7
[ 0. , 0. , 30.23, 30.23, 30.23, 30.23, 30.23, 30.23], x6
[ 0. , 0. , 0. , 30.94, 30.94, 30.94, 30.94, 30.94],
[ 0. , 0. , 0. , 0. , 0. , 0. , 34.26, 34.26],
[ 0. , 0. , 0. , 0. , 31.74, 31.74, 31.74, 31.74],
[ 0. , 29.24, 29.24, 29.24, 29.24, 29.24, 29.24, 29.24], x5
[ 0. , 25.93, 25.93, 25.93, 25.93, 25.93, 25.93, 25.93], x4
[ 0. , 0. , 29.26, 29.26, 29.26, 29.26, 29.26, 29.26], x3
[ 0. , 0. , 0. , 0. , 0. , 0. , 33.64, 33.64], x2
[ 0. , 0. , 0. , 0. , 0. , 0. , 33.28, 33.28]]) x1
따라서 min = P [n-1] [m-1] = 824.67 = [7,6,5,4,3,2,1] * [33.28,33.64,29.26,25.93,29.24,30.23,29.29]
A_ 및 B_를 반대로하려면 :
def reverse(S,A_,B_):
n,m = S.shape
state = m-1
pos = []
if state >= 0:
for i in range(n-1,0,-1):
if (S[i][state] == 1):
state = state - 1
pos.append([i,state+1])
new_B = np.zeros(B_.shape)
new_A = np.zeros(A_.shape)
for p in pos:
new_B[p[0],p[1]] = B_[p[0],p[1]]
new_A[p[0],p[1]] = A_[p[0],p[1]]
return new_B,new_A