Python&Matla實(shí)現(xiàn)模擬退火法的示例代碼
1 Python實(shí)現(xiàn)
1.1 源碼實(shí)現(xiàn)
我在前面已經(jīng)給出了模擬退火法的完整知識(shí)點(diǎn)和源碼實(shí)現(xiàn):智能優(yōu)化算法—蟻群算法(Python實(shí)現(xiàn))
模擬退火和蒙特卡洛實(shí)驗(yàn)一樣,全局隨機(jī),由于沒(méi)有自適應(yīng)的過(guò)程(例如向最優(yōu)靠近、權(quán)重梯度下降等),對(duì)于復(fù)雜函數(shù)尋優(yōu),很難會(huì)找到最優(yōu)解,都是近似最優(yōu)解;然而像蝙蝠算法、粒子群算法等有向最優(yōu)逼近且通過(guò)最優(yōu)最差調(diào)整參數(shù)的步驟,雖然對(duì)于下圖函數(shù)易陷入局部最優(yōu),但是尋優(yōu)精度相對(duì)較高。如果理解這段話應(yīng)該就明白了為什么神經(jīng)網(wǎng)絡(luò)訓(xùn)練前如果初步尋優(yōu)一組較好的網(wǎng)絡(luò)參數(shù),會(huì)使訓(xùn)練效果提高很多,也會(huì)更快達(dá)到誤差精度。
1.2 sko.SA 實(shí)現(xiàn)
#===========1導(dǎo)包================ import matplotlib.pyplot as plt import pandas as pd from sko.SA import SA #============2定義問(wèn)題=============== fun = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2 #=========3運(yùn)行模擬退火算法=========== 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畫出結(jié)果======= 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實(shí)現(xiàn)
2.1 模擬退火法
clear clc T=1000; %初始化溫度值 T_min=1; %設(shè)置溫度下界 alpha=0.99; %溫度的下降率 num=1000; %顆??倲?shù) n=2; %自變量個(gè)數(shù) 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 %以最小化為例 [bestf,a]=min(fx); bestx=x(a,:); trace(1)=bestf; while(T>T_min) for i=1:num for j=1:n xx(i,j)=(up(j)-sub(j))*rand+sub(j); end ff(i,1)=fun(xx(i,1),xx(i,2)); delta=ff(i,1)-fx(i,1); if delta<0 fx(i,1)=ff(i,1); x(i,:)=xx(i,:); else P=exp(-delta/T); if P>rand fx(i,1)=ff(i,1); x(i,:)=xx(i,:); end end end if min(fx)<bestf [bestf,a]=min(fx); bestx=x(a,:); end trace=[trace;bestf]; T=T*alpha; end disp('最優(yōu)解為:') disp(bestx) disp('最優(yōu)值為:') disp(bestf) hold on plot3(bestx(1),bestx(2),bestf,'ro','LineWidth',5) figure plot(trace) xlabel('迭代次數(shù)') ylabel('函數(shù)值') title('模擬退火算法') legend('最優(yōu)值')
function z=fun(x,y) z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20;
function tu [x,y] = meshgrid(-5:0.1:5,-5:0.1:5); z = x.^2 + y.^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20; figure mesh(x,y,z)%建一個(gè)網(wǎng)格圖,該網(wǎng)格圖為三維曲面,有實(shí)色邊顏色,無(wú)面顏色 hold on xlabel('x') ylabel('y') zlabel('z') title('z = x^2 + y^2 - 10*cos(2*pi*x) - 10*cos(2*pi*y) + 20')
這里有一個(gè)待嘗試的想法,先用蒙特卡洛/模擬退火迭代幾次全局去找最優(yōu)的區(qū)域,再通過(guò)其他有向最優(yōu)逼近過(guò)程的算法再進(jìn)一步尋優(yōu),或許會(huì)很大程度降低產(chǎn)生局部最優(yōu)解的概率。
下面是模擬退火和蒙特卡洛對(duì)上述函數(shù)尋優(yōu)的程序,迭代次數(shù)已設(shè)為一致,可以思考下兩種程序?qū)懛ǖ男?、共同點(diǎn)、缺點(diǎn)。理論研究講究結(jié)果好,實(shí)際應(yīng)用既要保證結(jié)果好也要保證程序運(yùn)算效率。
2.2 蒙特卡諾法
clear clc num=689000; %顆??倲?shù) n=2; %自變量個(gè)數(shù) sub=[-5,-5]; %自變量下限 up=[5,5]; %自變量上限 tu x=zeros(num,n); fx=zeros(num,1); 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 [bestf,a]=min(fx); bestx=x(a,:); disp('最優(yōu)解為:') disp(bestx) disp('最優(yōu)值為:') disp(bestf) hold on plot3(bestx(1),bestx(2),bestf,'ro','LineWidth',5)
效果確實(shí)值得商榷。
到此這篇關(guān)于Python&Matla實(shí)現(xiàn)模擬退火法的示例代碼的文章就介紹到這了,更多相關(guān)Python&Matla 模擬退火法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows中安裝使用Virtualenv來(lái)創(chuàng)建獨(dú)立Python環(huán)境
有時(shí)我們的程序中需要調(diào)用不同版本的Python包和模塊,那么借助Virtualenv的虛擬環(huán)境就可以幫助我們隔離使用,接下來(lái)我們就來(lái)看一下在Windows中安裝使用Virtualenv來(lái)創(chuàng)建獨(dú)立Python環(huán)境的方法2016-05-05使用coverage統(tǒng)計(jì)python web項(xiàng)目代碼覆蓋率的方法詳解
這篇文章主要介紹了使用coverage統(tǒng)計(jì)python web項(xiàng)目代碼覆蓋率的方法,詳細(xì)分析了coverage的安裝以及coverage命令統(tǒng)計(jì)py文件相關(guān)操作技巧,需要的朋友可以參考下2019-08-08使用python實(shí)現(xiàn)CNN-GRU故障診斷的代碼示例
這篇文章主要給大家詳細(xì)介紹了如何使用python實(shí)現(xiàn)CNN-GRU故障診斷,文章中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07PyTorch中Tensor的維度變換實(shí)現(xiàn)
這篇文章主要介紹了PyTorch中Tensor的維度變換實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08舉例區(qū)分Python中的淺復(fù)制與深復(fù)制
這篇文章主要介紹了舉例區(qū)分Python中的淺復(fù)制與深復(fù)制,是Python入門學(xué)習(xí)中的重要知識(shí),需要的朋友可以參考下2015-07-07在Python的gevent框架下執(zhí)行異步的Solr查詢的教程
這篇文章主要介紹了在Python的gevent框架下執(zhí)行異步的Solr查詢的教程,Solr請(qǐng)求在處理I/O方面較為高效,需要的朋友可以參考下2015-04-04matlab中imadjust函數(shù)的作用及應(yīng)用舉例
這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應(yīng)用舉例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02