Ha *_*ran 4 python arrays algorithm
我有两个不相等的长度数组:
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])
Run Code Online (Sandbox Code Playgroud)
我需要从B中选择7个数字,以使A的点积最小或min(7x1 + 6x2 + 5x3 +...+2x6 + x7)。两个阵列的顺序不能改变,例如,如果x1 = 32.41 (index 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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
所以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
Run Code Online (Sandbox Code Playgroud)
您在此处解决的问题具有以下形式:
给定两个长度为m和n的序列A和B以及一个数字k,找出长度为m的B的子序列与A的点积最低。
在您最初的问题中,我们有m = 7,但更一般地说,我想可以用任何数字代替7。
这个问题有一个不错的动态编程解决方案。其背后的直觉如下。假设您从数组B中选取了一些数字作为m元素向量的第一个元素。然后,您需要确定剩余的m-1个元素是什么。如果考虑一下,则希望选择它们,以便使m-1个元素与A的最后m-1个元素的点积尽可能小。因此,问题就变成了“您如何确定第一个项目应该是什么”和“如何选择后续项目?”。
一种方法是使用递归搜索。这个想法是这样的:
这种方法可以工作,但是按照书面说明,它太慢了,无法实用。幸运的是,它恰好可以很好地转换为动态编程解决方案。请注意,每个递归调用都可以构造为解决以下形式的问题:
给定索引i到A和索引j到B,从位置j开始的B子序列和从位置i开始的A的子向量可以形成的最小点积是多少?
在这里,i和j只能接受O(mn)个可能的值,因此我们可以像填写此大小的表一样处理此问题。具体来说,让T [i,j]作为我们的表。我们将其填写如下:
填写每个表项需要时间O(1),因此我们可以在时间O(mn)中填充表。从那里,我们可以使用标准的DP表格遍历算法来重构要包含的确切项目集。(我们实际上可以通过注意以下事实来加快速度,因为我们需要长度为B的B子序列,由于m 2个元素将是无穷大,因此我们不需要填写整个表,因此可以跳过填写。这给出了O(m(n-m + 1)))的运行时间。
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |