Python&Matla實現(xiàn)模擬退火法的示例代碼
1 Python實現(xiàn)
1.1 源碼實現(xiàn)
我在前面已經(jīng)給出了模擬退火法的完整知識點和源碼實現(xiàn):智能優(yōu)化算法—蟻群算法(Python實現(xiàn))
模擬退火和蒙特卡洛實驗一樣,全局隨機,由于沒有自適應的過程(例如向最優(yōu)靠近、權重梯度下降等),對于復雜函數(shù)尋優(yōu),很難會找到最優(yōu)解,都是近似最優(yōu)解;然而像蝙蝠算法、粒子群算法等有向最優(yōu)逼近且通過最優(yōu)最差調整參數(shù)的步驟,雖然對于下圖函數(shù)易陷入局部最優(yōu),但是尋優(yōu)精度相對較高。如果理解這段話應該就明白了為什么神經(jīng)網(wǎng)絡訓練前如果初步尋優(yōu)一組較好的網(wǎng)絡參數(shù),會使訓練效果提高很多,也會更快達到誤差精度。
1.2 sko.SA 實現(xiàn)
#===========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實現(xiàn)
2.1 模擬退火法
clear clc T=1000; %初始化溫度值 T_min=1; %設置溫度下界 alpha=0.99; %溫度的下降率 num=1000; %顆??倲?shù) n=2; %自變量個數(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)%建一個網(wǎng)格圖,該網(wǎng)格圖為三維曲面,有實色邊顏色,無面顏色 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')
這里有一個待嘗試的想法,先用蒙特卡洛/模擬退火迭代幾次全局去找最優(yōu)的區(qū)域,再通過其他有向最優(yōu)逼近過程的算法再進一步尋優(yōu),或許會很大程度降低產(chǎn)生局部最優(yōu)解的概率。
下面是模擬退火和蒙特卡洛對上述函數(shù)尋優(yōu)的程序,迭代次數(shù)已設為一致,可以思考下兩種程序寫法的效率、共同點、缺點。理論研究講究結果好,實際應用既要保證結果好也要保證程序運算效率。
2.2 蒙特卡諾法
clear clc num=689000; %顆粒總數(shù) n=2; %自變量個數(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)
效果確實值得商榷。
到此這篇關于Python&Matla實現(xiàn)模擬退火法的示例代碼的文章就介紹到這了,更多相關Python&Matla 模擬退火法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows中安裝使用Virtualenv來創(chuàng)建獨立Python環(huán)境
有時我們的程序中需要調用不同版本的Python包和模塊,那么借助Virtualenv的虛擬環(huán)境就可以幫助我們隔離使用,接下來我們就來看一下在Windows中安裝使用Virtualenv來創(chuàng)建獨立Python環(huán)境的方法2016-05-05使用coverage統(tǒng)計python web項目代碼覆蓋率的方法詳解
這篇文章主要介紹了使用coverage統(tǒng)計python web項目代碼覆蓋率的方法,詳細分析了coverage的安裝以及coverage命令統(tǒng)計py文件相關操作技巧,需要的朋友可以參考下2019-08-08使用python實現(xiàn)CNN-GRU故障診斷的代碼示例
這篇文章主要給大家詳細介紹了如何使用python實現(xiàn)CNN-GRU故障診斷,文章中有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-07-07在Python的gevent框架下執(zhí)行異步的Solr查詢的教程
這篇文章主要介紹了在Python的gevent框架下執(zhí)行異步的Solr查詢的教程,Solr請求在處理I/O方面較為高效,需要的朋友可以參考下2015-04-04matlab中imadjust函數(shù)的作用及應用舉例
這篇文章主要介紹了matlab中imadjust函數(shù)的作用及應用舉例,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02