使用了局部贪心算法加横向损失. 目前我认为是最好的时序比较函数.
#=====对于波的比较函数的优化
muban999=[0,1,1,0,-1,-1,-1,-1,-1,-1,-1,1]
beipipei999=[0,1,1,0,-1,0,1,1,1,1]
# beipipei999=[0,1,1,0,-1,-1,-1,-1,-1,-1,-1,1]
def jisuan_diff(b,m):
print(1)
changdu=0 # 横坐标的偏移 产生的diff
gaodu=0 # 纵坐标的偏移 产生的diff
kaishi=1
saving=0
jilu=0 # 记录上一个匹配成功的位置.
for i in range(1,len(m)): # 模板第0个,跟被查数组0个假设已经对齐, 从索引1开始对齐.
chagndu=3
tmpjuli=float('inf')
for j in range(kaishi,kaishi+chagndu+1):
if j<len(b):
gaodu=abs(m[i]-b[j])
if i!=1:
changdu=(abs(abs(j-kaishi)-1))*0.5
diff=gaodu+changdu
if diff<tmpjuli:
tmpjuli=diff
jilu=j
# 加上横坐标偏移惩罚: 移动一个格作为0惩罚.
# tmpjuli+=abs(jilu-kaishi-1)
kaishi=jilu
print(i,jilu,tmpjuli,kaishi)
saving+=tmpjuli
return (saving+abs(b[0]-m[0]))/len(m)
sadfasdfadsfasd=jisuan_diff(beipipei999,muban999)
print(sadfasdfadsfasd)
import matplotlib.pyplot as plt
plt.plot(muban999,c='r')
plt.plot(beipipei999,c='g')
plt.savefig('sdfaasd.png')
标签:函数,range,beipipei999,diff,chagndu,比较,kaishi
From: https://www.cnblogs.com/zhangbo2008/p/18048323