矢量化或优化循环,其中每次迭代都取决于前一次迭代的状态

2024-03-27

我有一个用 python 实现的算法。该算法可能会执行 1.000.000 次,所以我想尽可能地优化它。该算法的基础是三个列表(energy, point and valList)和两个计数器p and e.

两个清单energy and point包含 0 到 1 之间的数字,我以此为基础做出决定。p是一个计点器并且e是一个能量计数器。我可以用积分换取能量,每种能量的成本定义为valList(这是时间相关的)。我也可以通过其他方式进行交易。但我必须一次性全部交易。

算法概要:

  1. 获取一个布尔列表,其中的元素energy高于阈值并且元素point低于另一个阈值。这是一个用能量换取积分的决定。获取相应的积分列表,该列表给出了用积分换取能量的决策
  2. 在每个布尔列表中。删除另一个真值之后的所有真值(如果我已经用所有点换取能量,则不允许我在点后再次这样做)
  3. 对于每个项目对 (pB,点布尔值和eB, energy bool) 来自两个布尔列表:如果 pB 为真并且我有积分,我想用我的所有积分换取 enery。如果eB是真的,我有能量,我想把我所有的能量换成积分。

这是我想出的实现:

start = time.time()
import numpy as np

np.random.seed(2) #Seed for deterministic result, just for debugging

topLimit = 0.55
bottomLimit = 0.45

#Generate three random arrays, will not be random in the real world
res = np.random.rand(500,3) #Will probably not be much longer than 500
energy = res[:,0]        
point = res[:,1]
valList = res[:,2]

#Step 1:
#Generate two bools that (for ex. energy) is true when energy is above a threashold
#and point below another threshold). The opposite applies to point
energyListBool = ((energy > topLimit) & (point < bottomLimit))
pointListBool = ((point > topLimit) & (energy < bottomLimit))

#Step 2:
#Remove all 'true' that comes after another true since this is not valid
energyListBool[1:] &= energyListBool[1:] ^ energyListBool[:-1]
pointListBool[1:] &= pointListBool[1:] ^ pointListBool[:-1]

p = 100
e = 0

#Step 3:
#Loop through the lists, if point is true, I loose all p but gain p/valList[i] for e
#If energy is true I loose all e but gain valList[i]*e for p
for i in range(len(energyListBool)):
    if pointListBool[i] and e == 0:
        e = p/valList[i] #Trade all points to energy
        p = 0
    elif energyListBool[i] and p == 0:
        p = valList[i]*e #Trade all enery to points
        e = 0

print('p = {0} (correct for seed 2: 3.1108006690739174)'.format(p))
print('e = {0} (correct for seed 2: 0)'.format(e))

end = time.time()
print(end - start)

我正在努力解决的是如何(如果可以做到)对 for 循环进行矢量化,这样我就可以使用它来代替 for 循环,在我看来,for 循环可能会更快。


在当前的问题设置中这是不可能的,因为矢量化本质上要求您n-th 计算步骤不应依赖于前一个计算步骤n-1脚步。然而,有时可能会找到所谓的“封闭形式”的递归f(n) = F(f(n-1), f(n-2), ... f(n-k)),即找到一个明确的表达式f(n)这不取决于n,但这是一个单独的研究问题。

此外,从算法的角度来看,这种向量化不会给出太多,因为算法的复杂性仍然是C*n = O(n)。然而,由于“复杂性常数”C在实践中确实很重要,有不同的方法可以减少它。例如,用 C/C++ 重写关键循环应该不是什么大问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

矢量化或优化循环,其中每次迭代都取决于前一次迭代的状态 的相关文章

随机推荐