Вы находитесь на странице: 1из 16

УСЛОВНАЯ ОПТИМИЗАЦИЯ

Цель работы: Ознакомится с методами условной оптимизации, выполнить


поиск минимума функций различными алгоритмами.

Ход работы.

1. Воспроизвели пример 4, исследовать работу трех алгоритмов из разных


начальных точек, принадлежащих и не принадлежащих допустимой области.
1.1 Воспроизвели пример 4 с использование алгоритма interior-point (метод
внутренней точки).

Листинг программы

function history = runfmincon11(x0)


history.x = [];
history.fval = [];
A=[3 1;-1 1];b=[8;3];
[X,Y]=meshgrid(0:0.02:3.5,0:0.02:6);Z=primfun_g(X,Y);
[c,h]=contour(X,Y,Z,[0.007 0.025 0.1 0.5 2 5 10 15 ...
20 27 35 45],'blue');
clabel(c,h,[0.5 5 15 27],'FontSize',7);
hold on;plot([8/3 1.25],[0 4.25],'g-','linewidth',2.3);
plot([0 1.25],[3 4.25],'g-','linewidth',2);
text(1,1.8,'The feasible region');
xlabel('x1'); ylabel('x2'); text(x0(1),x0(2)+0.2,'x0');
title('Minimization by fmincon');
options = optimset('outputfcn',@outfun,'display',...
'off','Algorithm','interior-point');
[x,fval,exitflag,output]=fmincon(@primfun,x0,A,b,...
[],[],[],[],[],options)
y=history.x(:,1);z=history.x(:,2);
hold on;plot(y,z,'b.',y,z,'r-');
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
history.fval = [history.fval;...
optimValues.fval];
history.x = [history.x; x];
end
end
end

Результат выполнения программы решения с графиком

>> x0=[3 2];history=runfmincon11(x0)


x = 1.6049 3.1852
fval =0.0250
exitflag = 1

output =
iterations: 13
funcCount: 43
constrviolation: 0
stepsize: 5.6982e-007
algorithm: 'interior-point'
firstorderopt: 4.0000e-007
cgiterations: 0
message: [1x834 char]

Рисунок 1 - Поиск минимума методом внутренней точки


Для точки, не принадлежащей допустимой области.

>> x0=[5 10];history=runfmincon11(x0)


x=
1.6049 3.1852
fval =
0.0250
exitflag =
1
output =
iterations: 15
funcCount: 48
constrviolation: 0
stepsize: 3.7641e-004
algorithm: 'interior-point'
firstorderopt: 4.4128e-007
cgiterations: 0
message: [1x834 char]
history =
x: [16x2 double]
fval: [16x1 double]

Рисунок 2 – Поиск минимума методом внутренней точки из точки, не


принадлежащей области допустимых значений
1.2 Рассмотри данный пример с помощью Метода Active Set.

Листинг программы

function history = runfmincon111(x0)


history.x = [];
history.fval = [];
A=[3 1;-1 1];b=[8;3];
[X,Y]=meshgrid(0:0.02:3.5,0:0.02:6);Z=primfun_g(X,Y);
[c,h]=contour(X,Y,Z,[0.007 0.025 0.1 0.5 2 5 10 15 ...
20 27 35 45],'blue');
clabel(c,h,[0.5 5 15 27],'FontSize',7);
hold on;plot([8/3 1.25],[0 4.25],'g-','linewidth',2.3);
plot([0 1.25],[3 4.25],'g-','linewidth',2);
text(1,1.8,'The feasible region');
xlabel('x1'); ylabel('x2'); text(x0(1),x0(2)+0.2,'x0');
title('Minimization by fmincon');
options = optimset('outputfcn',@outfun,'display',...
'off','Algorithm','active-set');
[x,fval,exitflag,output]=fmincon(@primfun,x0,A,b,...
[],[],[],[],[],options)
y=history.x(:,1);z=history.x(:,2);
hold on;plot(y,z,'b.',y,z,'r-');
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
history.fval = [history.fval;...
optimValues.fval];
history.x = [history.x; x];
end
end
end

Результат выполнения программы решения

>> x0=[3 2];history=runfmincon111(x0)


x=
1.6049 3.1852
fval =
0.0250
exitflag =
5
output =
iterations: 7
funcCount: 21
lssteplength: 1
stepsize: 1.4155e-005
algorithm: [1x44 char]
firstorderopt: 6.9666e-005
constrviolation: 0
message: [1x843 char]
history =
x: [7x2 double]
fval: [7x1 double]

Рисунок 3 - Поиск минимума методом Active Set.

>> x0=[5 10];history=runfmincon111(x0)


x=
1.6049 3.1852
fval =
0.0250
exitflag =
5
output =
iterations: 9
funcCount: 27
lssteplength: 1
stepsize: 5.2882e-005
algorithm: [1x44 char]
firstorderopt: 2.6028e-004
constrviolation: 0
message: [1x843 char]
history =
x: [9x2 double]
fval: [9x1 double]

Рисунок 4 – Поиск минимума методом Active Set.


из точки, не принадлежащей области допустимых значений
2.1 Рассмотрим пример 5 с использование Метода внутренней точки (active-
set).

Создали m-файл:

function [c,ceq] = primcon(x)


c=[1.25*(x(1)-3.5)^2+x(2)-5;0.75/x(1)+x(2)-... 1.5*sqrt(2*x(1))];
ceq=[];
end

Листинг программы

function history = runfmincon21(x0)


history.x = [];
history.fval = [];
[X,Y]=meshgrid(0:0.02:3.5,0:0.02:6);Z=primfun_g(X,Y);
contour(X,Y,Z,[ 0.007 0.025 0.1 0.5 2 5 10 15 20 27 ...
35 45],'blue');
x=1.5:0.01:2.17; y=-1.25*(x-3.5).^2+5; hold on;
plot(x,y,'g-','linewidth',2.3); x=2.17:0.01:3.5;
y=1.5*sqrt(2*x)-0.75./x;hold on;plot(x,y,...
'g-','linewidth',2);
text(2.5,2,'The feasible region');
xlabel('x1'); ylabel('x2');
title('Minimization by fmincon');
options = optimset('outputfcn',@outfun,'display',...
'off', 'Algorithm', 'active-set');
[x,fval,exitflag,output]=fmincon(@primfun,x0,[],[],...
[],[],[],[],@primcon,options)
y=history.x(:,1);z=history.x(:,2);
hold on;plot(y,z,'b.',y,z,'r-');
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
history.fval = [history.fval;...
optimValues.fval];
history.x = [history.x; x];
end
end
end
Результат выполнения программы решения

x0=[0.5 4];history = runfmincon21(x0)


x=
2.1661 2.7758
fval =
2.4229
exitflag =
1
output =
iterations: 6
funcCount: 24
lssteplength: 1
stepsize: 3.5848e-006
algorithm: [1x44 char]
firstorderopt: 5.6352e-008
constrviolation: 1.2325e-011
message: [1x834 char]
history =
x: [7x2 double]
fval: [7x1 double]

Рисунок 5 –Поиск минимума методом внутренней точки


2.2 Рассмотрим данный пример с использованием метода активного набора
(active-set)

Результат выполнения программы решения


x0=[0.5 4];history = runfmincon22(x0)
x=
2.1661 2.7758
fval =
2.4229
exitflag =
1
output =
iterations: 10
funcCount: 35
constrviolation: 0
stepsize: 1.0756e-004
algorithm: 'interior-point'
firstorderopt: 3.5728e-006
cgiterations: 0
message: [1x834 char]
history =
x: [11x2 double]
fval: [11x1 double]

Рисунок 6 - Поиск минимума методом Active Set.


3. Воспроизвести пример 6 и исследовать работу всех алгоритмов fmincon из
нескольких начальных точек. Найти максимум этой же целевой функции, используя
разные алгоритмы.
3.1 Воспроизвести пример 6, используя алгоритм 'interior-point' (метод
внутренней точки)

Листинг программы

functionf=myfun(x)
f=3*(1-x(1))^2*exp(-x(1)^2-(x(2)+1)^2)-10*(x(1)/5-x(1)^3-x(2)^5)*exp(-x(1)^2-
x(2)^2)-1/3*exp(-(x(1)+1)^2-x(2)^2);
end

Результат выполнения программы решения

function history = runfmincon31(x0)


history.x = [];
history.fval = [];
options = optimset('outputfcn',@outfun,'display','off','Algorithm', 'interior-point');
[x,fval,exitflag,output]= fmincon(@myfun,x0,[],[],[],...
[],[-3 -3],[3 3],[],options)
[X,Y]=meshgrid(-3:0.02:3);Z=3*(1-X).^2.*exp(-X.^2- ...
(Y+1).^2)-10*(X/5 ...
-X.^3-Y.^5).*exp(-X.^2-Y.^2)-1/3*exp(-(X+1).^2-Y.^2);
meshc(X,Y,Z);
y=history.x(:,1);z=history.x(:,2);v=history.fval;
hold on;plot3(y,z,v,'black.',y,z,v,'red-');
v=-10*ones(size(z));hold on;plot3(y,z,v,'red-');
xlabel('x1'); ylabel('x2');zlabel('f');
title('Sequence of Points Computed by fmincon');
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold off
case 'iter'
% Concatenate current point and objective function
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
end end end
Результат выполнения программы решения

x0=[1 1]; history = runfmincon31(x0)


x=
0.2283 -1.6255
fval =
-6.5511
exitflag =
1
output =
iterations: 16
funcCount: 56
constrviolation: 0
stepsize: 4.8893e-008
algorithm: 'interior-point'
firstorderopt: 1.2023e-007
cgiterations: 0
message: [1x834 char]
history =
x: [17x2 double]
fval: [17x1 double]

Рисунок 7 –Вид целевой функции


Для точки[-1 2] методом внутренней точки (Interior-point):

x0=[-1 2]; history = runfmincon31(x0)


x=
-1.3474 0.2045
fval =
-3.0498
exitflag =
1
output =
iterations: 20
funcCount: 74
constrviolation: 0
stepsize: 1.1959e-006
algorithm: 'interior-point'
firstorderopt: 2.4206e-007
cgiterations: 0
message: [1x834 char]
history =
x: [21x2 double]
fval: [21x1 double]

Рисунок 8 –Вид целевой функции

3.2 Воспроизвели пример 6, используя алгоритм 'active-set' (метод активного


набора)
Результаты выполнения программы решения

x0=[1 1]; history = runfmincon32(x0)


x=
3.0000 -2.9413
fval =
-7.6347e-006
exitflag =
5
output =
iterations: 3
funcCount: 9
lssteplength: 1
stepsize: 8.6436e-005
algorithm: [1x44 char]
firstorderopt: 8.5776e-005
constrviolation: 0
message: [1x843 char]
history =
x: [3x2 double]
fval: [3x1 double]

Рисунок 9 –Вид целевой функции


Для точки[-1 2] получили:

x0=[-1 2]; history = runfmincon32(x0)


x=
-3 3
fval =
3.2235e-005
exitflag =
1
output =
iterations: 2
funcCount: 6
lssteplength: 1
stepsize: 0
algorithm: [1x44 char]
firstorderopt: 0
constrviolation: 0
message: [1x834 char]
history =
x: [2x2 double]
fval: [2x1 double]

Рисунок 10 –Вид целевой функции


3.4 Нашли максимум функции из п.3.3

Листинг программы

function history = runfmincon33(x0)


history.x = [];
history.fval = [];
options = optimset('outputfcn',@outfun,'display','off','Algorithm', 'active-set');

[x,fval,exitflag,output]= fmincon('-3*(1-x(1))^2*exp(-x(1)^2-(x(2)+1)^2)-
10*(x(1)/5-x(1)^3-x(2)^5)*exp(-x(1)^2-x(2)^2)-1/3*exp(-(x(1)+1)^2-x(2)^2)',x0,[],[],[],...
[],[-3 -3],[3 3],[],options)
[X,Y]=meshgrid(-3:0.02:3);Z=3*(1-X).^2.*exp(-X.^2- ...
(Y+1).^2)-10*(X/5 ...
-X.^3-Y.^5).*exp(-X.^2-Y.^2)-1/3*exp(-(X+1).^2-Y.^2);
meshc(X,Y,Z);
y=history.x(:,1);z=history.x(:,2);v=history.fval;
hold on;plot3(y,z,v,'black.',y,z,v,'red-');
v=-10*ones(size(z));hold on;plot3(y,z,v,'red-');
xlabel('x1'); ylabel('x2');zlabel('f');
title('Sequence of Points Computed by fmincon');
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold off
case 'iter'
% Concatenate current point and objective function
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
end
end
end

Решение программы

x0=[0 3]; history = runfmincon33(x0)


x=
0 3
fval =
0.2999
exitflag =
5
output =
iterations: 1
funcCount: 3
lssteplength: 1
stepsize: 2.1588e-004
algorithm: [1x44 char]
firstorderopt: 2.1588e-004
constrviolation: 0
message: [1x843 char]
history =
x: [0 3]
fval: 0.2999

Рисунок 11 –Вид целевой функции

Вывод: в ходе выполнения данной лабораторной работы мы ознакомились с


методами условной оптимизации, выполнить поиск минимума функций различными
алгоритмами.

Вам также может понравиться