中科大《優(yōu)化設(shè)計》課程大作業(yè)之約束優(yōu)化實(shí)驗報告(共8頁)
精選優(yōu)質(zhì)文檔-傾情為你奉上約束優(yōu)化設(shè)計實(shí)驗報告力學(xué)系型號:聯(lián)想y470CPU:i5-2450M內(nèi)存:2GB系統(tǒng):win7-64位求解問題:如上是以下三個約束方法共同需要求解的問題,預(yù)估結(jié)果:在(x1,x2,x3)(23,13,12)點(diǎn)附近存在極值。其中,每個方法對應(yīng)的初始條件分別為:(1)隨機(jī)試驗法設(shè)計變量范圍:隨機(jī)試驗點(diǎn)數(shù):N=1000精度:eps=0.001(2)隨機(jī)方向法初始點(diǎn):x0=(25,15,5)初始步長:a0=0.5精度:eps=0.001(3)線性規(guī)劃單純形法初始復(fù)合形:X=20 23 25 30;10 13 15 20;10 9 5 0頂點(diǎn)個數(shù):n=4精度:eps=0.01計算結(jié)果:約束方法所需時間迭代次數(shù)極值點(diǎn)極值隨機(jī)試驗法28.230(22.63,12.63,12.06)3445.50隨機(jī)方向法0.6549(22.67,12.67,11.98)3441.99線性規(guī)劃單純形法6.235(22.59,12.59,12.12)3445.61程序說明:主程序為main,運(yùn)行main后按提示即可得到相應(yīng)約束方法的求解結(jié)果。程序如下:1、 主程序clear;global kk;kk=0;disp(1.隨機(jī)試驗法);disp(2.隨機(jī)方向法);disp(3.線性規(guī)劃單純形法);while 1 n0=input(請輸入上面所想選擇約束優(yōu)化方法的編號(1、2、3):); if n0=1|n0=2|n0=3 break; end disp(此次輸入無效.);end disp( );disp();xx,yy=fmins(n0);fprintf(迭代次數(shù)為: %8.0fn, kk);disp(所求極值點(diǎn)的坐標(biāo)向量為:);fprintf( %16.5fn, xx);fprintf(所求函數(shù)的極值為: %16.5fn, yy);2、 調(diào)用函數(shù)function xx,yy=fmins(n0)if n0=1 tic;xx,yy=suijishiyan();toc;elseif n0=2 tic;xx,yy=suijifangxiang();toc;elseif n0=3 tic;xx,yy=danchunxing();toc;endend3、 單純形法function xx,yy=danchunxing()clear;global kk;syms a b c;f=-a*b*c;g=-a+2*b+2*c>=0;a+2*b+2*c<=72;abs(a-b-10)<=1e-3;b>=10;b<=20;X=20 23 25 30;10 13 15 20;10 9 5 0;alpha=1.3;sita=0.5;gama=1;beta=0.7;var=a;b;c;eps=0.001;N=size(X);n=N(2);FX=zeros(1,n);while 1 for i=1:n FX(i)=double(subs(f,var,X(:,i); end XS,IX=sort(FX); Xsorted=X(:,IX); px=sum(Xsorted(:,1:(n-1),2)/(n-1); Fpx=double(subs(f,var,px); SumF=0; for i=1:n SumF=SumF+(FX(IX(i)-Fpx)2; end SumF=sqrt(SumF/(n-1); if SumF<=eps xx=Xsorted(:,1); break; else bcon_1=1; cof_alpha=alpha; while bcon_1 x2=px+cof_alpha*(px-Xsorted(:,n); gx2=double(subs(g,var,x2); if min(gx2)>0 bcon_1=0; else cof_alpha=0.7*(cof_alpha); end end fx2=double(subs(f,var,x2); if fx2<XS(1) cof_gama=gama; bcon_2=1; while bcon_2 x3=x2+cof_gama*(x2-px); gx3=double(subs(g,var,x3); fx3=double(subs(f,var,x3); if min(gx3)>0 bcon_2=0; if fx3<XS(1) count=1; else count=2; end else bcon_2=0; count=3; end end if count=1 Xsorted(:,n)=x3; X=Xsorted; continue else Xsorted(:,n)=x2; X=Xsorted; continue end else if fx2<XS(n-1) Xsorted(:,n)=x2; X=Xsorted; continue else if fx2<XS(n) Xsorted(:,n)=x2; cof_beta=beta; bcon_3=1; while bcon_3<4 x4=Xsorted(:,n)+cof_beta*(px-Xsorted(:,n); gx4=double(subs(g,var,x4); if min(gx4)>0 bcon_3=5; else cof_beta=cof_beta/2; bcon_3=bcon_3+1; end end if min(gx4)>0 fx4=double(subs(f,var,x4); FNnew=double(subs(f,var,Xsorted(:,n); if fx4<FNnew Xsorted(:,n)=x4; X=Xsorted; continue else x0=Xsorted(:,1); for i=1:n Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0); end end else x0=Xsorted(:,1); for i=1:n Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0); X=Xsorted; continue end end else x0=Xsorted(:,1); for i=1:n Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0); X=Xsorted; continue end end end end end X=Xsorted; kk=kk+1;endyy=-double(subs(f,var,xx);end4、 隨機(jī)方向法function xx,yy=suijifangxiang()clear;global kk;x0 = 25;15;5;n = 3;eps = 0.001;a0 = 0.5;k = 10000;f0 = -x0(1)*x0(2)*x0(3);fl = -x0(1)*x0(2)*x0(3);i = 1;while 1 a = a0; r = -1 + 2*rand(n,k); for j = 1:k ss=(sum(abs(r(:,j)0.5; e = r(:,j)./ss; x = x0 + a*e; if (-x(1)+2*x(2)+2*x(3)>=0)&& (x(1)+2*x(2)+2*x(3)<=72)&&(abs(x(1)-x(2)-10)<=1e-3)&&(x(2)>=10)&&(x(2)<=20) f =-x(1)*x(2)*x(3); if f < fl fl = f; d = e; xl = x; end end end while 1 x = xl; a = 1.3*a; x = x+a*d; if (-x(1)+2*x(2)+2*x(3)>=0)&& (x(1)+2*x(2)+2*x(3)<=72)&&(abs(x(1)-x(2)-10)<=1e-3)&&(x(2)>=10)&&(x(2)<=20) f =-x(1)*x(2)*x(3); if f < fl fl = f; else break end else break end kk=kk+1; end for b = 1:50 x = x - a*d; a = 0.7*a; x = x + a*d; if (-x(1)+2*x(2)+2*x(3)>=0)&& (x(1)+2*x(2)+2*x(3)<=72)&&(abs(x(1)-x(2)-10)<=1e-3)&&(x(2)>=10)&&(x(2)<=20) f =-x(1)*x(2)*x(3); if f < fl fl = f; break end end end epsl = abs(f0-fl)/f0); if epsl < eps&&x(1)<23 break else i = i+1; x0 = x; f0 = f; fl = f; end kk=kk+1;endxx = x;yy =-f;end5、 隨機(jī)試驗法function xx,yy=suijishiyan()format longN=1000;x1=;x2=;x3=;zmax=-inf;x10=unifrnd(20,30,N,1);x20=unifrnd(10,20,N,1);x30=unifrnd(-10,16,N,1);for i=1:N for j=1:N for k=1:N if (-x10(i)+2*x20(j)+2*x30(k)>=0)&& (x10(i)+2*x20(j)+2*x30(k)<=72)&&(abs(x10(i)-x20(j)-10)<=1e-3) z=x10(i)*x20(j)*x30(k); if zmax<z zmax=z; x1=x10(i);x2=x20(j);x3=x30(k); end end end endendxx=x1;x2;x3;yy=zmax;end專心-專注-專業(yè)