matlab全局優(yōu)化與局部優(yōu)化

參考博客
matlab全局優(yōu)化與局部優(yōu)化
最優(yōu)化方法的Matlab實現(xiàn)

在實際的工作和生活過程中,優(yōu)化問題無處不在,比如資源如何分配效益最高,擬合問題,最小最大值問題等等。優(yōu)化問題一般分為局部最優(yōu)和全局最優(yōu),局部最優(yōu),就是在函數(shù)值空間的一個有限區(qū)域內(nèi)尋找最小值;而全局最優(yōu),是在函數(shù)值空間整個區(qū)域?qū)ふ易钚≈祮栴}。

  • 函數(shù)局部最小點是那種它的函數(shù)值小于或等于附近點的點。但是有可能大于較遠(yuǎn)距離的點。

  • 全局最小點是那種它的函數(shù)值小于或等于所有的可行點。


    全局優(yōu)化和局部優(yōu)化區(qū)別示意圖

matlab中的提供的傳統(tǒng)優(yōu)化工具箱(Optimization Tool),能實現(xiàn)局部最優(yōu),但要得全局最優(yōu),則要用全局最優(yōu)化算法(Global Optimization Tool),主要包括:
GlobalSearch 全局搜索和MultiStart多起點方法產(chǎn)生若干起始點,然后它們用局部求解器去找到起始點吸引盆處的最優(yōu)點。

ga 遺傳算法用一組起始點(稱為種群),通過迭代從種群中產(chǎn)生更好的點,只要初始種群覆蓋幾個盆,GA就能檢查幾個盆。

simulannealbnd模擬退火完成一個隨機搜索,通常,模擬退火算法接受一個點,只要這個點比前面那個好,它也偶而接受一個比較糟的點,目的是轉(zhuǎn)向不同的盆。

patternsearch模式搜索算法在接受一個點之前要看看其附近的一組點。假如附近的某些點屬于不同的盆,模式搜索算法本質(zhì)上時同時搜索若干個盆。

下面我就一些具體例子,來說明各種優(yōu)化方法:

  1. 先看一個求最小值的普通優(yōu)化問題
%%目標(biāo)函數(shù)
f = @(x) x.*sin(x) + x.*cos(2.*x);
%% 的取值范圍
lb = 0;
ub = 10;
%% 尋找最小值和繪圖
x0 = [0 1 3 6 8 10];
hf = figure;
for i=1:6
   x(i) = fmincon(f,x0(i),[],[],[],[],lb,ub,[],...
                  optimset('Algorithm','SQP','Disp','none'));
   subplot(2,3,i)
   ezplot(f,[lb ub]);
   hold on
   plot(x0(i),f(x0(i)),'k+')
   plot(x(i),f(x(i)),'ro')
   hold off
   title(['Starting at ',num2str(x0(i))])
   if i == 1 || i == 4
       ylabel('x sin(x) + x cos(2 x)')
   end
end

可以看出,初值x0不同,得到的結(jié)果截然不同,這說明這種求解器,能尋找局部最優(yōu),但不一定是全局最優(yōu),在起點為8時,取得全局最優(yōu)。
我們換一種求解器:fminbound,這種求解器不需要給點初值。

x2 = fminbnd(f,lb,ub);
figure
ezplot(f,[lb ub]);
hold on
plot(x2,f(x2),'ro')
hold off
ylabel('x sin(x) + x cos(2 x)')
title({'Solution using fminbnd.','Required no starting point!'})
  • 現(xiàn)在我們嘗試全局最優(yōu)的方法:GlobalSearch
% Leason Learned: Use the appropriate solver for your problem type!
%% But what if |fmincon| was the only choice?
% Use globalSearch or MultiStart
problem = createOptimProblem('fmincon','objective',f,'x0',x0(1),'lb',lb,...
            'ub',ub,'options',optimset('Algorithm','SQP','Disp','none'));
gs = GlobalSearch;
xgs = run(gs,problem);
figure
ezplot(f,[lb ub]);
hold on
plot(xgs,f(xgs),'ro')
hold off
ylabel('x sin(x) + x cos(2 x)')
title('Solution using globalSearch.')

因此全局最優(yōu)的方法能夠獲取全局最優(yōu)。

  • 再看一個線性擬合的問題:
close all, clear all, clc
%% Pharmacokinetic Data
t = [ 3.92,  7.93, 11.89, 23.90, 47.87, 71.91, 93.85, 117.84 ]              %#ok<*NOPTS>
c = [0.163, 0.679, 0.679, 0.388, 0.183, 0.125, 0.086, 0.0624 ] 
plot(t,c,'o'), xlabel('t'), ylabel('c') 
%% 3 Compartment Model
model = @(b,t) b(1)*exp(-b(4)*t) + b(2)*exp(-b(5)*t) + b(3)*exp(-b(6)*t) 
%% Define Optimization Problem 
problem = createOptimProblem('lsqcurvefit', ...
                            'objective', model, ...
                            'xdata', t, 'ydata', c, ...
                            'x0',ones(1,6),...
                            'lb', [-10 -10 -10  0   0   0 ],...
                            'ub', [ 10  10  10 0.5 0.5 0.5], ...
                            'options',optimset('OutputFcn',...
                            @curvefittingPlotIterates))
%% solve
b = lsqcurvefit(problem)  

結(jié)果:最小二乘擬合結(jié)果誤差較大

  • 現(xiàn)在我們嘗試全局最優(yōu)方法:MultiStart
%% Multistart
ms = MultiStart                                                            
[b,fval,exitflag,output,solutions] = run(ms, problem, 50)                   %#ok<*NASGU,*ASGLU> 
%%
curvefittingPlotIterates(solutions) 
%%
problem.options.OutputFcn = {};
tic, [b,fval,exitflag,output,solutions] = run(ms, problem, 100), toc  %計算算法的時間

可以看出全局優(yōu)化結(jié)果較好,誤差較小。
這種算法的運行時間:Elapsed time is 6.139324 seconds.
使用并行計算的方式解決

%% Parallel Version
matlabpool open 2 %開啟兩個matlab并行計算
ms.UseParallel = 'always' %開啟并行計算
tic, [bp,fvalp,exitflagp,outputp,solutionsp] = run(ms, problem, 100); toc
matlabpool close

結(jié)果:14 out of 100 local solver runs converged with a positive local solver exit flag.
Elapsed time is 4.358762 seconds.Sending a stop signal to all the labs ... stopped.可以看出,運行時間減少,提高了效率。

  • 再看一個尋找最小值的問題
%% Objective Function
% We wish find the minimum of the |peaks| function
clear all, close all, clc
peaks 
%% Nonlinear Constraint Function
% Subject to a nonlinear constraint defined by a circular region of radius
% three around the origin
type circularConstraint 
%% Define Optimization Problem
problem = createOptimProblem('fmincon',...
                            'objective',@(x) peaks(x(1),x(2)), ...
                            'nonlcon',@circularConstraint,...
                            'x0',[-1 -1],...
                            'lb',[-3 -3],...
                            'ub',[3 3],...
                            'options',optimset('OutputFcn',...
                                               @peaksPlotIterates))                             
%% Run the solver |fmincon| from the inital point
% We can see the solution is not the global minimum
[x,f] = fmincon(problem)    

這種方法只能尋找局部最優(yōu)。
現(xiàn)在用全局優(yōu)化算法:

%% Use |MultiStart| to Find the Global Minimum
% Define the multistart solver
close all
ms = MultiStart %這里可以換成GlobalSearch
%% Run |Multistart|
% Well use 5 starting points
[x,f,exitflag,output,solutions] = run(ms, problem, 5)
  • 再舉一個模擬退火即模式搜索的算法 :
    [x fval] = simulannealbnd(@objfun,x0,lb,ub,options)
%% Objective Function
% We wish find the minimum of the |peaks| function
clear all, close all, clc
peaks 
%% Nonlinear Constraint Function
% Subject to a nonlinear constraint defined by a circular region of radius
% three around the origin
type circularConstraint 
%% Define Optimization Problem
problem = createOptimProblem('fmincon',...
                            'objective',@(x) peaks(x(1),x(2)), ...
                            'nonlcon',@circularConstraint,...
                            'x0',[-1 -1],...
                            'lb',[-3 -3],...
                            'ub',[3 3],...
                            'options',optimset('OutputFcn',...
                                               @peaksPlotIterates))                             
%% Run the solver |fmincon| from the inital point
% We can see the solution is not the global minimum
[x,f] = fmincon(problem)     
%% Use Simmulated Annealing to Find the Global Minimum
% Solve the problem using simmulated annealing.  Note that simmulated
% annealing does not support nonlinear so we need to account for this in
% the objective function.
problem.solver  = 'simulannealbnd';
problem.objective = @(x) peaks(x(1),x(2)) + (x(1)^2 + x(2)^2 - 9);
problem.options = saoptimset('OutputFcn',@peaksPlotIterates,...
                            'Display','iter',...
                            'InitialTemperature',10,...
                            'MaxIter',300) 
[x,f] = simulannealbnd(problem)
f = peaks(x(1),x(2))  
  • Use Pattern Search to Find the Global Minimum
%% Use Pattern Search to Find the Global Minimum
% Solve the problem using pattern search.
problem.solver  = 'patternsearch';
problem.options = psoptimset('OutputFcn',@peaksPlotIterates,...
                            'Display','iter',...
                            'SearchMethod',{@searchlhs}) 
[x,f] = patternsearch(problem)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,765評論 0 33
  • Objective 你對今天學(xué)的記得什么? 婚姻生活的“九字真言” 這九個字其實是兩句話:第一句話是——“ 我錯了...
    徐偉豪閱讀 241評論 0 0
  • 玉面君書夜攔秋,輕解羅衫,拂手御紅袖。 窗前蟬燥幾時休,勸君掩目,月明聲啾啾。 華年初解鬢白眸,何日泛輕舟。 心御...
    瀟雯閱讀 173評論 0 4