赞
踩
1 Python实现
我在前面已经给出了模拟退火法的完整知识点和源码实现:智能优化算法—蚁群算法(Python实现)
模拟退火和蒙特卡洛实验一样,全局随机,由于没有自适应的过程(例如向最优靠近、权重梯度下降等),对于复杂函数寻优,很难会找到最优解,都是近似最优解;然而像蝙蝠算法、粒子群算法等有向最优逼近且通过最优最差调整参数的步骤,虽然对于下图函数易陷入局部最优,但是寻优精度相对较高。如果理解这段话应该就明白了为什么神经网络训练前如果初步寻优一组较好的网络参数,会使训练效果提高很多,也会更快达到误差精度。
#=1导包======
import matplotlib.pyplot as plt
import pandas as pd
from sko.SA import SA
#2定义问题===
fun = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
#=3运行模拟退火算法===
sa = SA(func=fun, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150)
best_x, best_y = sa.run()
print(‘best_x:’, best_x, ‘best_y’, best_y)
#=4画出结果=
plt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0))
plt.show()
#scikit-opt 还提供了三种模拟退火流派: Fast, Boltzmann, Cauchy.
#=1.1 Fast Simulated Annealing===========
from sko.SA import SAFast
sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)
sa_fast.run()
print('Fast Simulated Annealing: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y)
#=1.2 Fast Simulated Annealing with bounds===========
from sko.SA import SAFast
sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,
lb=[-1, 1, -1], ub=[2, 3, 4])
sa_fast.run()
print('Fast Simulated Annealing with bounds: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y)
#=2.1 Boltzmann Simulated Annealing==========
from sko.SA import SABoltzmann
sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)
sa_boltzmann.run()
print('Boltzmann Simulated Annealing: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_fast.best_y)
#=2.2 Boltzmann Simulated Annealing with bounds==========
from sko.SA import SABoltzmann
sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,
lb=-1, ub=[2, 3, 4])
sa_boltzmann.run()
print('Boltzmann Simulated Annealing with bounds: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_fast.best_y)
#3.1 Cauchy Simulated Annealing
from sko.SA import SACauchy
sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150)
sa_cauchy.run()
print('Cauchy Simulated Annealing: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y)
#3.2 Cauchy Simulated Annealing with bounds
from sko.SA import SACauchy
sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150,
lb=[-1, 1, -1], ub=[2, 3, 4])
sa_cauchy.run()
print('Cauchy Simulated Annealing with bounds: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y)
2 Matlab实现
clear
clc
T=1000; %初始化温度值
T_min=1; %设置温度下界
alpha=0.99; %温度的下降率
num=1000; %颗粒总数
n=2; %自变量个数
sub=[-5,-5]; %自变量下限
up=[5,5]; %自变量上限
tu
for i=1:num
for j=1:n
x(i,j)=(up(j)-sub(j))*rand+sub(j);
end
fx(i,1)=fun(x(i,1),x(i,2));
end
%以最小化为例
Python崛起并且风靡,因为优点多、应用领域广、被大牛们认可。学习 Python 门槛很低,但它的晋级路线很多,通过它你能进入机器学习、数据挖掘、大数据,CS等更加高级的领域。Python可以做网络应用,可以做科学计算,数据分析,可以做网络爬虫,可以做机器学习、自然语言处理、可以写游戏、可以做桌面应用…Python可以做的很多,你需要学好基础,再选择明确的方向。这里给大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。