简单的一维连续空间爬山算法

时间:2014-07-28 20:38:11

标签: python algorithm hill-climbing

我正在寻找一种简单的爬山算法

用于大规模模拟, 这是“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 

我提出的算法正在运行,但我觉得最有效的东西...... 任何建议?

谢谢

0 个答案:

没有答案