Thứ Năm, 20 tháng 2, 2014

Tài liệu CHƯƠNG 8: TỐI ƯU HOÁ pptx


374
ymin=yp;

Đểtìmcựctiểucủahàmtadùngchươngtrình
ctgolden.m:

clearall,clc
f=inline(ʹ1.6*x^3+3*x^2‐2*xʹ);
x=0;
delta=1e‐8;
epsilon=1e‐10;
[a,b]=goldbracket(f,x,0.2);
[xmin,ymin]=golden(f,a,b,delta,epsilon)


§3.PHƯƠNGPHÁPXẤPXỈBẬCHAI
 Ýtưởngcủaphươngphápnàylà:
‐xấpxỉhàmđốitượngf(x)bằngmộthàmbậc2p
2(x)qua3điểmcho
trước
‐cậpnhật3điểmnàybằngcáchthaymộttrong3điểmbằngcựctiểu
củahàmp
2(x)
 Qua3điểm:
[]
[]
[
]
{
}
00 11 22
(x ,f(x ) , (x ,f(x ) , (x , f(x ) x0<x1<x2
tatìmđượcđathứcnộisuyp
2(x)vàđiểmcóđạohàmbằngzero:

−+ −+ −
==
⎡⎤
−+ −+ −
⎣⎦
22 22 22
01 2 12 0 20 1
3
01 2 12 0 20 1
f(x x) f(x x) f(x x)
xx
2 f (x x ) f (x x ) f (x x )
(1)
Đặcbiệt,nếu cácđiểmtìmđượctrướcđâyphânbốđềuvớikhoảngcáchh(
nghĩalàx
2‐x1=x1‐x0=h)thì(1)trởthành:

−+ −+ − −+
==+

+−
⎡⎤
−+ −+ −
⎣⎦
22 22 22
01 2 12 0 20 1 0 1 2
30
012
01 2 12 0 20 1
f(x x) f(x x) f(x x) 3f 4f f
xxh
2( f 2f f )
2 f (x x ) f (x x ) f (x x )
 (2)
Ta cập nhật 3điểm theo cách này chođến khi
−≈
20
xx 0 hay
−≈
20
f(x ) f(x ) 0
vàcựctiểulàx3.Quytắccậpnhật3điểmlà:
‐Trongtrườnghợp
<
<
031
xxxtadùng
031
(x ,x ,x)
hay
312
(x ,x,x )
làm3
điểmmớituỳtheof(x
3)<f(x1)haykhông
‐Trongtrườnghợp
<
<
132
xxxtadùng
132
(x ,x ,x )hay
013
(x ,x,x )làm3
điểmmớituỳtheof(x
3)≤f(x1)haykhông.
Quátrìnhtìmcựctiểuđượcmôtảtrênhìnhsau:

375

Taxâydựnghàm
optquad()đểthựchiệnthuậttoánnày.

function[xo,fo]=optquad(f,x0,tolx,tolfun,maxiter)
%Timcuctieucuaf(x)bangphuongphapxapxibac2
iflength(x0)>2
x012=x0(1:3);
else
iflength(x0)==2
a=x0(1);
b=x0(2);
else
a=x0‐10;
b=x0
+10;
end
x012=[a(a+b)/2b];
end
f012=f(x012);
[xo,fo]=optquad0(f,x012,f012,tolx,tolfun,maxiter);

function[xo,fo]=optquad0(f,x012,f012,tolx,tolfun,k)
x0=x012(1);
x1=x012(2);
x2=x012(3);

376
f0=f012(1);
f1=f012(2);
f2=f012(3);
nd=[f0‐f2f1‐f0f2‐f1]*[x1*x1x2*x2x0*x0;x1x2x0]ʹ;
x3=nd(1)/2/nd(2);
f3=feval(f,x3);%Pt.(1)
ifk<=0|abs(x3‐x1)<tolx|abs(f3‐f1)<tolfun
xo=x3;
fo=
f3;
ifk==0
fprintf(ʹDaycothexemladiemcuctieuʹ)
end
else
ifx3<x1
iff3<f1
x012=[x0x3x1];
f012=[f0f3f1];
else
x012=[x3x1x2];
f012=[f3f1
f2];
end
else
iff3<=f1
x012=[x1x3x2];
f012=[f1f3f2];
else
x012=[x0x1x3];
f012=[f0f1f3];
end
end
[xo,fo]=optquad0(f,x012,f012,tolx,tolfun,k‐1);
end


Đểtìmđiểmcựctiểutadùngchươngtrình
ctoptquad.m:

clearall,clc

377
%f=inline(ʹ(x.^2‐4).^2/8‐1ʹ);
a=0;
b=3;
delta=1e‐8;
epsilon=1e‐10;
maxiter=100;
[xmin,ymin]=optquad(f,[ab],delta,epsilon,maxiter)


§4.PHƯƠNGPHÁPNELDER‐MEAD
 PhươngphápNelder‐Meadcóthểdùngđểtìmcựctiểucủahàmnhiều
biếnmàphươngphápti ếtdiệnvànghayphươngphápxấpxỉbậc2không
dùngđược.ThuậttoánNelder‐Meadgồmcác
bướcnhưsau:
Bước1:Cho3điểmđầutiêna,b,cvớif(a)<f(b)<f(c)
Bước2:Nếu3điểmvàcácgiátrịtươngứngcủahàmđủgầnnhauthìta
coialàđiểmcựctiểuvàkếtthúcquátrìnhtính
Bước 3: Nếu không ta coi
điểm cực tiểu nằmđối diện với
điểm c trênđường ab(xem hình
vẽ)vàlấy:
 e=m+2(m‐c)
với
m=(a+b)/2
vànếuf(e)<
f(b)thìlấy:
 r=(m+e)/2=2m‐c
vànếuf(r)<f(c)thìlấyrlàmgiá
trị mới của c; nếu f(r)
≥ f(b) thì
lấy:
 s=(c+m)/2
vànếuf(s)<f(c)thìlấyslàmgiátrịmớicủa c;nếukhôngbỏcácđiểmb,cvà
dùngmvàc
1=(a+c)/2làmđiểmbvàcmớivàchorằngcựctiểunằmquanh
điểma.

Bước4:Trởvềbước1
Taxâydựnghàm
neldermead()đểthựchiệnthuậttoánnày:

function[xo,fo]=neldermead(f,x0,tolx,tolfun,maxiter)
n=length(x0);
e
c1
a
m
r
c
b
s2
s1
c2
m=(a+b)/2 r=m+(m‐c)
e=m+2(m‐c) s
1=(c+m)/2
s
2=(m+r)/2 c1=(c+a)/2
c
2=(r+a)/2

378
ifn==1%truonghopham1bien
[xo,fo]=optquad(f,x0,tolx,tolfun);
return
end
S=eye(n);
fori=1:n
i1=i+1;
ifi1>n
i1=1;
end
abc=[x0;x0+S(i,:);x0+S(i1,:)];
fabc
=[feval(f,abc(1,:));feval(f,abc(2,:));feval(f,abc(3,:))];
[x0,fo]=neldermead0(f,abc,fabc,tolx,tolfun,maxiter);
ifn<3,
break;
end
end
xo=x0;


function[xo,fo]=neldermead0(f,abc,fabc,tolx,tolfun,k)
[fabc,I]=sort(fabc);
a=abc(I(1),:);
b=abc(I(2),:);
c=abc(I(3),:);
fa=fabc(1);
fb=fabc(2);
fc=fabc(3);
fba=fb‐fa;
fcb=fc‐fb;
ifk<=0|abs(fba)+abs(fcb)
<tolfun|abs(b‐a)+abs(c‐b)<tolx
xo=a;
fo=fa;
ifk==0
fprintf(ʹXemdayladiemcuctieuʹ)
end
else

379
m=(a+b)/2;
e=3*m‐2*c;
fe=feval(f,e);
iffe<fb
c=e;
fc=fe;
else
r=(m+e)/2;
fr=feval(f,r);
iffr<fc
c=r;
fc=fr;
end
iffr>=fb
s=(c+m)/2;
fs=feval(f,s);
iffs<fc
c=s;
fc=fs;
else
b=m;
c=(a+c)/2;
fb=feval(f,b);
fc=feval(f,c);
end
end
end

[xo,fo]=neldermead0(f,[a;b;c],[fafbfc],tolx,tolfun,k‐1);
end

Để tìm cực tiểu của hàm
=
=− − +−
22
112 122
zf(x,y)x xx 4x x x lân cận [0 0] ta
dùngchươngtrình
ctneldermead.m:

clearall,clc
f=inline(ʹx(1)*x(1)‐x(1)*x(2)‐4*x(1)+x(2)*x(2)‐x(2)ʹ);
x0=[00];

380
b=1;
delta=1e‐8;
epsilon=1e‐10;
maxiter=100;
[xmin,ymin]=neldermead(f,x0,delta,epsilon,maxiter)


§5.PHƯƠNGPHÁPĐỘDỐCLỚNNHẤT
 Phương pháp này tìmđiểm cực tiểu của hàm n biến theo hướng
gradientâm:

[] []
⎡⎤
∂∂ ∂
−=−∇=−
⎢⎥
∂∂ ∂
⎣⎦
L
12 n
f(x) f(x) f(x)
g( x ) f ( x )
xx x

vớikíchth ướcbướctínhα
ktạilầnlặpthứkđượchiệuchỉnhsaochogiátrị
hàmcựctiểutheohướngtìm.Thuậttoángồmcácbướcsau:
 •Tạilầnlặpthứk=0,tìmgiátrịhàm
f(x0)vớiđiểmkhởiđầux0
•Tạilầnlặpthứk,tìmα
ktheohướng‐g(x)

(
)
−−−−
α= −α
k1 k1 k1 k1
fx g /g (1)
•Tínhgiátrịx
k:

−−−−
=−α
kk1k1k1k1
xx g/g(2)
•Nếux
k≈xk‐1vàf(xk)≈f(xk‐1)thìcoilàcựctiểu,nếukhông thìquayvề
bước2.

function[xo,fo]=steepest(f,x0,tolx,tolfun,alpha0,maxiter)
ifnargin<6
maxiter=100;
end
ifnargin<5
alpha0=10;%kichthuockhoigan
end
ifnargin<4
tolfun=1e‐8;
end%|f(x)|<tolfunmongmuon
ifnargin<3
tolx=
1e‐6;
end%|x(k)‐x(k‐1)|<tolxmongmuon
x=x0;

381
fx0=feval(f,x0);
fx=fx0;
alpha=alpha0;
kmax1=25;
warning=0;
fork=1:maxiter
g=grad(f,x);
g=g/norm(g);%gradientlavectohang
alpha=alpha*2;%dethuditheohuonggradientam
fx1=feval(f,x‐alpha*2*g);
for
k1=1:kmax1%timbuoctoiuu
fx2=fx1;
fx1=feval(f,x‐alpha*g);
iffx0>fx1+tolfun&fx1<fx2‐tolfun%fx0>fx1<fx2
den=4*fx1‐2*fx0‐2*fx2;
num=den‐fx0+fx2;%
alpha=alpha*num/den;Pt.(1)
x=
x‐alpha*g;
fx=feval(f,x);%Pt.(2)
break;
else
alpha=alpha/2;
end
end
ifk1>=kmax1
warning=warning+1;%kgtimduocbuoctoiuu
else
warning=0;
end
ifwarning>=2|(norm(x‐x0)<tolx&abs(fx‐fx0)<tolfun)
break;
end
x0=x;
fx0=fx;
end
xo=x;fo=fx;

382
ifk==maxiter
fprintf(ʹDaylaketquatotnhatsau%dlanlapʹ,maxiter)
end


functiong=grad(func,x)
%Tinhgradientcuahamf(x).
h=1.0e‐6;
n=length(x);
g=zeros(1,n);
f0=feval(func,x);
fori=1:n
temp=x(i);
x(i)=temp+h;
f1=feval(func,x);
x(i)=temp;
g(1,i)=
(f1‐f0)/h;
end


Đểtìmcựctiểucủahàmtadùngchươngtrình
ctsteepest.m:

clearall,clc
f=inline(ʹx(1)*x(1)‐x(1)*x(2)‐4*x(1)+x(2)*x(2)‐x(2)ʹ);
x0=[0.50.5];
tolx=1e‐4;
tolfun=1e‐9;
alpha0=1;
maxiter=100;
[xo,fo]=steepest(f,x0,tolx,tolfun,alpha0,maxiter)


§6.PHƯƠNGPHÁPNEWTON
 Việctìmđiểmcựctiểucủahàmf(x)tươngđươngvớiviệcxácđịnhxđể
chogradientg(x)củahàmf(x)bằngzero.Nghiệmcủag(x)=0cóth ểtìmđược
bằngcáchdùngphương
phápNewtonchohệphươngtrình phituyến.Hàm
newtons(x)dùngđểtìmnghiệmcủaphươngtrìnhg(x)=0là:

function[x,fx,xx]=newtons(f,x0,tolx,maxiter)

383
h=1e‐4;
tolfun=eps;
EPS=1e‐6;
fx=feval(f,x0);
nf=length(fx);
nx=length(x0);
ifnf~=nx
error(ʹKichthuoccuagvax0khongtuongthich!ʹ);
end
ifnargin<4
maxiter=100;
end
ifnargin<3

tolx=EPS;
end
xx(1,:)=x0(:).ʹ;
fork=1:maxiter
dx=‐jacob(f,xx(k,:),h)\fx(:);%‐[dfdx]ˆ‐1*fx
xx(k+1,:)=xx(k,:)+dx.ʹ;
fx=feval(f,xx(k+1,:));
fxn=norm(fx);
iffxn<tolfun|norm(dx)
<tolx
break;
end
end
x=xx(k+1,:);
ifk==maxiter
fprintf(ʹKetquatotnhatsau%dlanlap\nʹ,maxiter)
end


functiong=jacob(f,x,h)%Jacobiancuaf(x)
ifnargin<3
h=1e‐4;
end
h2=2*h;
n=length(x);

Không có nhận xét nào:

Đăng nhận xét