我正在寻找一个简单的爬山算法
它用于大规模模拟,这是“CurrentLocation”如何变化的示例。
import time
targ = 1.5 # target of t
location = 1 #starting point
step = 0.9 # step change starting point
a=0
b=0
while abs(targ-location)>0.05:
if targ>location:
if b==1: # If I already been at b
b=0
a=0
step = step*(0.9)
print 'stepChangeA'
location =location + abs(location*step)
else:
location =location + abs(location*step)
a=1
print 'increase'
else:
if a==1: #If I already been at a
a=0
b=0
step = step*(0.9)
print 'stepChangeB'
location =location - abs(location*step)
else:
location = location - abs(location*step)
b=1
print 'decrease'
time.sleep(0.1) # just so it will be easy to see the change..
print location
我提出的算法是有效的,但我觉得最有效的东西......有什么建议吗?
谢谢你