我实现了模拟退火,以使用
找到给定函数的全局最小值https://perso.crans.org/besson/publis/notebooks/Simulated_annealing_in_Python.html
但是,尽管温度先是升高,然后缓慢降低(由于步进),但这有时却给了我错误的答案。(局部最小值)
我必须补充一点,我尝试使用随机开始爬山来解决问题,以下是给定间隔内的局部最小值列表:
x = 0.55 0.75 0.95 1.15 1.35 1.54 1.74 1.94 2.14 2.34 2.5
y = -0.23 -0.37 -0.47 -0.57 -0.66 -0.68 -0.55 -0.16 0.65 2.10 5.06
和optimize.basinhopping()
证明全局最小值为(1.54, -.68)
代码如下:
import math
import numpy as np
import numpy.random as rn
import matplotlib.pyplot as plt # to plot
from scipy import optimize # to compare
import seaborn as sns
def annealing(random_start, func, func_interval, random_neighbour, acceptance, temperature, maxsteps=1000, debug=True):
""" Optimize the black-box function 'func' with the simulated annealing algorithm."""
x = random_start(func_interval)
y = func(x)
x_list, y_list = [x], [y]
for step in range(maxsteps):
fraction = step / float(maxsteps)
T = temperature(fraction)
new_x = random_neighbour(x, func_interval, fraction)
new_y = func(new_x)
if debug: print("Step #{:>2}/{:>2} : T = {:>4.3g}, x = {:>4.3g}, y = {:>4.3g}, new_x = {:>4.3g}, new_y = {:>4.3g} ...".format(step, maxsteps, T, x, y, new_x, new_y))
if acceptance_probability(y, new_y, T) > rn.random():
x, y = new_x, new_y
x_list.append(x)
y_list.append(y)
# print(" ==> Accept it!")
# else:
# print(" ==> Reject it...")
return x, func(x), x_list, y_list
def clip(x, func_interval):
""" Force x to be in the interval."""
a, b = func_interval
return max(min(x, b), a)
def random_start(func_interval):
""" Random point in the interval."""
a, b = func_interval
return a + (b - a) * rn.random_sample()
def random_neighbour(x, func_interval, fraction=1):
"""Move a little bit x, from the left or the right."""
amplitude = (max(func_interval) - min(func_interval)) * fraction / 10
delta = (-amplitude/2.) + amplitude * rn.random_sample()
return clip(x + delta, func_interval)
def acceptance_probability(y, new_y, temperature):
if new_y < y:
# print(" - Acceptance probabilty = 1 as new_y = {} < y = {}...".format(new_y, y))
return 1
else:
p = np.exp(- (new_y - y) / temperature)
# print(" - Acceptance probabilty = {:.3g}...".format(p))
return p
def temperature(fraction):
""" Example of temperature dicreasing as the process goes on."""
return max(0.01, min(1, 1 - fraction))
def see_annealing(x, y, x_list, y_list):
sns.set(context="talk", style="darkgrid", palette="hls", font="sans-serif", font_scale=1.05)
xs = np.linspace(func_interval[0], func_interval[1], 1000) # Get 1000 evenly spaced numbers between .5 and 2.5
plt.plot(xs, np.vectorize(func)(xs))
plt.scatter(x_list, y_list, c="b")
plt.scatter(x, y, c="r")
plt.title("Simulated annealing")
plt.show()
if __name__ == '__main__':
func = lambda x: math.sin(10 * math.pi * x) / 2 * x + (x - 1) ** 4
func_interval = (.5, 2.5)
x, y, x_list, y_list = annealing(random_start, func, func_interval, random_neighbour, acceptance_probability, temperature, maxsteps=1000, debug=False)
see_annealing(x, y, x_list, y_list)
print(x, y)
print(optimize.basinhopping(lambda x: math.sin(10*math.pi*x)/2*x + (x-1)**4, [random_start(func_interval)]))
但是怎么了?
编辑:
@ user3184950您是正确的,该算法现在运行更好,但这是AIMA第三版的伪代码
next只是 random 选择的current的后继者。
此外,我在我的AI课程中写了一条笔记,如果我们从T高开始并缓慢降低它,则可以保证模拟退火收敛到全局最大值。(我的意思是我的教授没有对“下一个点说什么” ” 或我以某种方式错过了它,也许没关系)。
顺便说一句,我一直认为问题出在“下一个点”上,如果y和new_y均为负,那么即使T足够小,下一个点的可能性也很高。例如
在步骤891中可以看到y和new_y均为负,我们采用了new_y,但是T为0.109
同样的问题是,伪代码中给出的概率公式与我在代码中使用的概率公式相同
答案 0 :(得分:1)
似乎邻居不是最优的。
def random_neighbour(x, func_interval, fraction=1):
"""Move a little bit x, from the left or the right."""
amplitude = (max(func_interval) - min(func_interval)) * 1 / (fraction + 0.1)
delta = 1 * amplitude * (.5 - rn.random_sample())
print(delta)
return clip(x + delta, func_interval)
您需要以相同的概率向左/向右移动的东西,但是在退火开始时移动的可能性更大,而在结束时移动的可能性较小。
以上只是一个看起来更好的建议。