瞎BB
代码
导入库以及参数设置
import matplotlib.pyplot as plt
import math
import random
T_init = 100
alpha = 0.95
T_min = 1e-3
目标函数
def obj(x):
y = 10 * math.sin(5 * x) + 7 * math.cos(4 * x)
return -y
主函数
def SA(T_init,alpha,T_min):
T = T_init
x_new = random.random() * 10
x_current = x_new
y_current = float('inf')
x_best = x_new
y_best = float('inf')
while T > T_min:
for i in range(100):
delta_x = random.random() - 0.5
if 0 < (x_new + delta_x) < 10:
x_new = x_new + delta_x
else:
x_new = x_new - delta_x
y_new = obj(x_new)
if (y_new<y_current):
y_current=y_new
x_current=x_new
if (y_new<y_best):
y_best=y_new
x_best=x_new
else:
if random.random() < math.exp(-(y_new - y_current) / T):
y_current=y_new
x_current=x_new
else:
x_new=x_current
T *= alpha
print('最优解',x_best,obj(x_best))
SA(T_init,alpha,T_min)