




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、優(yōu)化方法上機(jī)大作業(yè)上機(jī)大作業(yè):編寫程序求解下述問題 min x f(x) = (1x1)2 + 100(x2 x12)2. 初始點取 x0 = 0, 精度取=1e4,步長由 Armijo 線搜索生成, 方向分別由下列方法生成: 1 最速下降法2 牛頓法3 BFGS 方法 4 共軛梯度法1. 最速下降法源程序如下:function x_star = steepest(x0,eps) gk = grad(x0); res = norm(gk); k = 0; while res > eps && k<=1000 dk = -gk; ak =1; f0 = fun(x0)
2、; f1 = fun(x0+ak*dk); slope = dot(gk,dk); while f1 > f0 + 0.1*ak*slope ak = ak/2; xk = x0 + ak*dk; f1 = fun(xk); end k = k+1; x0 = xk; gk = grad(xk);res = norm(gk); fprintf('-The %d-th iter, the residual is %fn',k,res); end x_star = xk; end function f = fun(x) f = (1-x(1)2 + 100*(x(2)-x(1
3、)2)2; endfunction g = grad(x) g = zeros(2,1); g(1)=2*(x(1)-1)+400*x(1)*(x(1)2-x(2); g(2) = 200*(x(2)-x(1)2); end 運行結(jié)果:>> x0=0,0'eps=1e-4eps = 1.0000e-004>> xk=steepest(x0,eps)-The 1-th iter, the residual is 3.271712-The 2-th iter, the residual is 2.504194-The 3-th iter, the residual
4、is 2.073282-The 998-th iter, the residual is 0.449447-The 999-th iter, the residual is 0.449447-The 1000-th iter, the residual is 0.449447-The 1001-th iter, the residual is 0.449447xk = 0.63690.40382. 牛頓法源程序如下:function x_star = newton(x0,eps) gk = grad(x0); bk = grad2(x0)(-1); res = norm(gk); k = 0;
5、 while res > eps && k<=1000 dk=-bk*gk; xk=x0+dk; k = k+1; x0 = xk; gk = grad(xk); bk = grad2(xk)(-1); res = norm(gk); fprintf('-The %d-th iter, the residual is %fn',k,res); end x_star = xk; end function f = fun(x) f = (1-x(1)2 + 100*(x(2)-x(1)2)2; endfunction g = grad2(x) g = z
6、eros(2,2); g(1,1)=2+400*(3*x(1)2-x(2); g(1,2)=-400*x(1); g(2,1)=-400*x(1); g(2,2)=200; end function g = grad(x) g = zeros(2,1); g(1)=2*(x(1)-1)+400*x(1)*(x(1)2-x(2); g(2) = 200*(x(2)-x(1)2); end 運行結(jié)果:>> x0=0,0'eps=1e-4;>> xk=newton(x0,eps)-The 1-th iter, the residual is 447.213595-Th
7、e 2-th iter, the residual is 0.000000xk = 1.0000 1.00003. BFGS方法源程序如下:function x_star = bfgs(x0,eps) g0 = grad(x0); gk=g0; res = norm(gk); Hk=eye(2); k = 0; while res > eps && k<=1000 dk = -Hk*gk; ak =1; f0 = fun(x0); f1 = fun(x0+ak*dk); slope = dot(gk,dk); while f1 > f0 + 0.1*ak*sl
8、ope ak = ak/2; xk = x0 + ak*dk; f1 = fun(xk); end k = k+1; fa0=xk-x0; x0 = xk; g0=gk;gk = grad(xk);y0=gk-g0;Hk=(eye(2)-fa0*(y0)')/(fa0)'*(y0)*(eye(2)-(y0)*(fa0)')/(fa0)'*(y0)+(fa0*(fa0)')/(fa0)'*(y0);res = norm(gk); fprintf('-The %d-th iter, the residual is %fn',k,res
9、); end x_star = xk; endfunction f=fun(x) f=(1-x(1)2 + 100*(x(2)-x(1)2)2; endfunction g = grad(x) g = zeros(2,1); g(1)=2*(x(1)-1)+400*x(1)*(x(1)2-x(2); g(2) = 200*(x(2)-x(1)2); end 運行結(jié)果:>> x0=0,0'>> eps=1e-4;>> xk=bfgs(x0,eps)-The 1-th iter, the residual is 3.271712-The 2-th ite
10、r, the residual is 2.381565-The 3-th iter, the residual is 3.448742-The 998-th iter, the residual is 0.004690-The 999-th iter, the residual is 0.008407-The 1000-th iter, the residual is 0.005314-The 1001-th iter, the residual is 0.010880xk = 0.99550.99114. 共軛梯度法源程序如下:function x_star =conj(x0,eps) gk
11、 = grad(x0);res = norm(gk); k = 0; dk = -gk; while res > eps && k<=1000 ak =1; f0 = fun(x0);f1 = fun(x0+ak*dk);slope = dot(gk,dk); while f1 > f0 + 0.1*ak*slope ak = ak/2; xk = x0 + ak*dk; f1 = fun(xk); end d0=dk;g0=gk; k=k+1; x0=xk;gk=grad(xk);f=(norm(gk)/norm(g0)2; res=norm(gk);dk=
12、-gk+f*d0;fprintf('-The %d-th iter, the residual is %fn',k,res); end x_star = xk; end function f=fun(x)f=(1-x(1)2+100*(x(2)-x(1)2)2;endfunction g=grad(x)g=zeros(2,1);g(1)=400*x(1)3-400*x(1)*x(2)+2*x(1)-2;g(2)=-200*x(1)2+200*x(2);end 運行結(jié)果:>> x0=0,0'>> eps=1e-4;>> xk=Conj(
13、x0,eps)-The 1-th iter, the residual is 3.271712-The 2-th iter, the residual is 1.380542-The 3-th iter, the residual is 4.527780-The 4-th iter, the residual is 0.850596-The 73-th iter, the residual is 0.001532-The 74-th iter, the residual is 0.000402-The 75-th iter, the residual is 0.000134-The 76-th
14、 iter, the residual is 0.000057xk = 0.9999 0.9999上機(jī)大作業(yè):編寫程序利用增廣拉格朗日方法求解下述問題 min 4x1 x2 2 12s.t. 25x12 x22 = 0 10x1 x12 + 10x2 x22 34 0x 1,x2 0初始點取 x0 = 0, 精度取= 1e4. 主程序:function x,mu,lamda,output=main(fun,hf,gf,dfun,dhf,dgf,x0)maxk=2000;theta=0.8; eta=2.0;k=0; ink=0;ep=1e-4; sigma=0.4;x=x0; he=feval
15、(hf,x); gi=feval(gf,x);n=length(x); l=length(he); m=length(gi);mu=0.1*ones(l,1); lamda=0.1*ones(m,1);betak=10; betaold=10; while(betak>ep && k<maxk)ik,x,val=bfgs('lagrang','dlagrang',x0,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma);ink=ink+ik;he=feval(hf,x); gi=feval(gf,x);bet
16、ak=sqrt(norm(he,2)2+norm(min(gi,lamda/sigma),2)2);if betak>epmu=mu-sigma*he;lamda=max(0.0,lamda-sigma*gi);if(k>=2 && betak>theta*betaold)sigma=eta*sigma;endendk=k+1;betaold=betak;x0=x;endf=feval(fun,x);output.fval=f;output.iter=k;output.inner_iter=ink;output.beta=betak;增廣拉格朗日函數(shù)funct
17、ion lag=lagrang(x,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma)f=feval(fun,x); he=feval(hf,x); gi=feval(gf,x);l=length(he); m=length(gi);lag=f; s1=0.0;for(i=1:l)lag=lag-he(i)*mu(i);s1=s1+he(i)2;endlag=lag+0.5*sigma*s1;s2=0.0;for(i=1:m)s3=max(0.0,lamda(i)-sigma*gi(i);s2=s2+s32-lamda(i)2;endlag=lag+s2/(2.0*s
18、igma);增廣拉格朗日梯度函數(shù)function dlag=dlagrang(x,fun,hf,gf,dfun,dhf,dgf,mu,lamda,sigma)dlag=feval(dfun,x);he=feval(hf,x); gi=feval(gf,x);dhe=feval(dhf,x); dgi=feval(dgf,x);l=length(he); m=length(gi);for(i=1:l)dlag=dlag+(sigma*he(i)-mu(i)*dhe(:,i);endfor(i=1:m)dlag=dlag+(sigma*gi(i)-lamda(i)*dgi(:,i);end線搜索程
19、序 基于BFGS方法function k,x,val=bfgs(fun,gfun,x0,varargin)Max=1000;ep=1.e-4;beta=0.55; sigma1=0.4;n=length(x0); Bk=eye(n);k=0;while(k<Max)gk=feval(gfun,x0,varargin:);if(norm(gk)<ep), break; enddk=-Bkgk; m=0; mk=0;while(m<20) newf=feval(fun,x0+betam*dk,varargin:);oldf=feval(fun,x0,varargin:);if(n
20、ewf<=oldf+sigma1*betam*gk'*dk)mk=m; break;endm=m+1;endx=x0+betamk*dk;sk=x-x0;yk=feval(gfun,x,varargin:)-gk;if(yk'*sk>0)Bk=Bk-(Bk*sk*sk'*Bk)/(sk'*Bk*sk)+(yk*yk')/(yk'*sk);endk=k+1;x0=x;endval=feval(fun,x0,varargin:);目標(biāo)函數(shù)文件function f=f1(x)f=4*x(1)-x(2)2-12;等式約束文件function
21、he=h1(x)he=25-x(1)2-x(2)2;不等式約束function gi=g1(x)gi=zeros(3,1);gi(1)=10*x(1)-x(1)2+10*x(2)-x(2)2-34;gi(2)=x(1);gi(3)=x(2);目標(biāo)函數(shù)梯度文件function g=df1(x)g=4;-2*x(1);等式函數(shù)梯度文件function dhe=dh1(x)dhe=-2*x(1);-2*x(2);不等式函數(shù)梯度文件function dgi=dg1(x)dgi=10-2*x(1),1,0;10-2*x(2),0,1;輸入數(shù)據(jù)X0=0;0x,mu,lamda,output=main(
22、39;f1','h1','g1','df1','dh1','dg1',x0)輸出數(shù)據(jù)x = 1.0013 4.8987mu = 0.0158lamda = 0.5542 0 0output = fval: -31.9924 iter: 5 inner_iter: 33 beta: 8.4937e-005上機(jī)大作業(yè):1.解:將目標(biāo)函數(shù)改寫為向量形式:x'*a*x-b*x程序代碼:n=2;a=0.5,0;0,1;b=2 4;c=1 1;cvx_beginvariable x(n)minimize( x&
23、#39;*a*x-b*x)subject toc * x <= 1x>=0cvx_end運算結(jié)果:Calling SDPT3 4.0: 7 variables, 3 equality constraints For improved efficiency, SDPT3 is solving the dual problem.- num. of constraints = 3 dim. of socp var = 4, num. of socp blk = 1 dim. of linear var = 3* SDPT3: Infeasible path-following algor
24、ithms* version predcorr gam expon scale_data NT 1 0.000 1 0 it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime- 0|0.000|0.000|8.0e-001|6.5e+000|3.1e+002| 1.000000e+001 0.000000e+000| 0:0:00| chol 1 1 1|1.000|0.987|4.3e-007|1.5e-001|1.6e+001| 9.043148e+000 -2.714056e-001| 0:0:01| chol 1 1 2
25、|1.000|1.000|2.6e-007|7.6e-003|1.4e+000| 1.234938e+000 -5.011630e-002| 0:0:01| chol 1 1 3|1.000|1.000|2.4e-007|7.6e-004|3.0e-001| 4.166959e-001 1.181563e-001| 0:0:01| chol 1 1 4|0.892|0.877|6.4e-008|1.6e-004|5.2e-002| 2.773022e-001 2.265122e-001| 0:0:01| chol 1 1 5|1.000|1.000|1.0e-008|7.6e-006|1.5e
26、-002| 2.579468e-001 2.427203e-001| 0:0:01| chol 1 1 6|0.905|0.904|3.1e-009|1.4e-006|2.3e-003| 2.511936e-001 2.488619e-001| 0:0:01| chol 1 1 7|1.000|1.000|6.1e-009|7.7e-008|6.6e-004| 2.503336e-001 2.496718e-001| 0:0:01| chol 1 1 8|0.903|0.903|1.8e-009|1.5e-008|1.0e-004| 2.500507e-001 2.499497e-001| 0
27、:0:01| chol 1 1 9|1.000|1.000|4.9e-010|3.5e-010|2.9e-005| 2.500143e-001 2.499857e-001| 0:0:01| chol 1 1 10|0.904|0.904|5.7e-011|1.3e-010|4.4e-006| 2.500022e-001 2.499978e-001| 0:0:01| chol 2 2 11|1.000|1.000|5.2e-013|1.1e-011|1.2e-006| 2.500006e-001 2.499994e-001| 0:0:01| chol 2 2 12|1.000|1.000|5.9
28、e-013|1.0e-012|1.8e-007| 2.500001e-001 2.499999e-001| 0:0:01| chol 2 2 13|1.000|1.000|1.7e-012|1.0e-012|4.2e-008| 2.500000e-001 2.500000e-001| 0:0:01| chol 2 2 14|1.000|1.000|2.3e-012|1.0e-012|7.3e-009| 2.500000e-001 2.500000e-001| 0:0:01| stop: max(relative gap, infeasibilities) < 1.49e-008- num
29、ber of iterations = 14 primal objective value = 2.50000004e-001 dual objective value = 2.49999996e-001 gap := trace(XZ) = 7.29e-009 relative gap = 4.86e-009 actual relative gap = 4.86e-009 rel. primal infeas (scaled problem) = 2.33e-012 rel. dual " " " = 1.00e-012 rel. primal infeas (
30、unscaled problem) = 0.00e+000 rel. dual " " " = 0.00e+000 norm(X), norm(y), norm(Z) = 3.2e+000, 1.5e+000, 1.9e+000 norm(A), norm(b), norm(C) = 3.9e+000, 4.2e+000, 2.6e+000 Total CPU time (secs) = 0.99 CPU time per iteration = 0.07 termination code = 0 DIMACS: 3.3e-012 0.0e+000 1.3e-01
31、2 0.0e+000 4.9e-009 4.9e-009- -Status: SolvedOptimal value (cvx_optval): -32. 解:將目標(biāo)函數(shù)改寫為向量形式:程序代碼:n=3;a=-3 -1 -3;b=2;5;6;C=2 1 1;1 2 3;2 2 1;cvx_begin variable x(n) minimize( a*x) subject to C * x <= b x>=0cvx_end運行結(jié)果:Calling SDPT3 4.0: 6 variables, 3 equality constraints- num. of constraints
32、= 3 dim. of linear var = 6* SDPT3: Infeasible path-following algorithms* version predcorr gam expon scale_data NT 1 0.000 1 0 it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime- 0|0.000|0.000|1.1e+001|5.1e+000|6.0e+002|-7.000000e+001 0.000000e+000| 0:0:00| chol 1 1 1|0.912|1.000|9.4e-001|4
33、.6e-002|6.5e+001|-5.606627e+000 -2.967567e+001| 0:0:00| chol 1 1 2|1.000|1.000|1.3e-007|4.6e-003|8.5e+000|-2.723981e+000 -1.113509e+001| 0:0:00| chol 1 1 3|1.000|0.961|2.3e-008|6.2e-004|1.8e+000|-4.348354e+000 -6.122853e+000| 0:0:00| chol 1 1 4|0.881|1.000|2.2e-008|4.6e-005|3.7e-001|-5.255152e+000 -5.622375e+000| 0
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 3957-2020化工企業(yè)安全生產(chǎn)信息化管理平臺數(shù)據(jù)規(guī)范
- DB32/T 3761.4-2020新型冠狀病毒肺炎疫情防控技術(shù)規(guī)范第4部分:工業(yè)企業(yè)
- DB32/T 3711-2020內(nèi)河低壓小容量船舶岸電連接系統(tǒng)技術(shù)規(guī)范
- DB32/T 3558-2019生活垃圾焚燒飛灰熔融處理技術(shù)規(guī)范
- DB31/T 858-2015鋼渣粉混凝土砌塊應(yīng)用技術(shù)規(guī)程
- DB31/T 677-2021木制品制造業(yè)職業(yè)病危害預(yù)防控制規(guī)范
- DB31/T 668.1-2012節(jié)能技術(shù)改造及合同能源管理項目節(jié)能量審核與計算方法第1部分:總則
- DB31/T 631-2012公共汽車燃油消耗定額
- DB31/T 601-2019地理標(biāo)志產(chǎn)品金山蟠桃
- DB31/T 329.1-2019重點單位重要部位安全技術(shù)防范系統(tǒng)要求第1部分:展覽館、博物館
- 馬拉松賽事策劃方案
- 2.3第1.2課時物質(zhì)的量課件高一上學(xué)期化學(xué)人教版
- 景觀照明項目評估報告
- 電影你的名字課件
- (小學(xué))語文教師書寫《寫字教學(xué)講座》教育教研講座教學(xué)培訓(xùn)課件
- 設(shè)備清潔安全保養(yǎng)培訓(xùn)課件
- 心理危機(jī)評估中的量表和工具
- plc課程設(shè)計模壓機(jī)控制
- 中國大學(xué)生積極心理品質(zhì)量表
- 2023充電樁停車場租賃合同 充電樁租地合同正規(guī)范本(通用版)
- JCT908-2013 人造石的標(biāo)準(zhǔn)
評論
0/150
提交評論