Sei sulla pagina 1di 8

Para baixar o código acesse: https://goo.

gl/znCEkr

Escola Politécnica da Universidade de São Paulo

Diego Varalda de Almeida, 10580379

Resolução da 7a lista de exercícios

PMR5215 - Otimização aplicada ao projeto


de sistemas mecânicos

Orientadores: Prof. Dr. Emílio Carlos Nelli


Silva ; Prof. Dr. Thiago Martins

Universidade de São Paulo


Programa de Pós-graduação

São Paulo
2018
A implementação em Matlab desta lista está disponível para download em https://goo.gl/znCEkr

Item [a]
Uma função foi implementada em Matlab, com o intuito de chegar se determinada solução para um
problema satisfazem as condições de KKT, dados os valores de e a função satisfyKKT() retorna lógico
TRUE se todas as três condições forem satisfeitas. A solução com multiplicadores de
Lagrange satisfaz as condições de KKT visto que e

Desta forma, o ponto fornecido resulta em uma condição de estacionariedade. Para determinar se é ponto
de mínimo, devemos calcular a matriz Hessiana

Cujo autovalores são , logo a matriz Hessiana é positiva-definida, sendo portanto, um ponto de
mínimo.

Item [b]
A figura abaixo mostra as curvas de nível do problema além de evidenciar o domínio viável dado pelas
restrições. O ponto em vermelho é a solução do problema .
Item [c]
O resultado é mostrado na Tabela 1, na última página

% Solution to problem 1 list 7

clc
close all

% Add OMT folder to the path.


folder = fileparts(which(mfilename));
addpath(genpath(folder));

syms x_1 x_2

f = x_1^2 + x_2^2 - 16*x_1 - 2*x_2;


g = [sin(2*x_1)+(2*pi-x_1)/8 - x_2 >=0;
-sin(2*x_1)+(2*pi-x_1)/8 + x_2 >=0];

fvars =[x_1, x_2];

xlim = [0, 7, -3, 3, 0.3];


fighandle = figure;
% Plotting Surface
plotSurface(f, xlim, fighandle)

% Plotting Contour and constraint


nfig = figure;
xlim = [0, 7, -3, 3, 0.01];
plotGradient(f, xlim, fvars, nfig);
plotConstraint(g, xlim, fvars, nfig);

% Plotting optimal solution


xsol = [2*pi, 0];
lambda = [81-16*pi, 79-16*pi];
plotOptimSol(xsol, nfig);

% Check KKT conditions


isKKT = satisfyKKT(f, g, fvars, xsol, lambda);

%Check Hessian
H = hessian( lagrangian(f,g), fvars );

function [xsol, T] = slpopt(f, g, b, geq, beq, x0, delta)


% SLPOPT Successive Linear Programming optimization

syms x2 x1
f = 64 - 16*x2 - 32*x1;

g = [4*x1+2*x2-30;
x1+2*x2-6];

fvars = [x1, x2];

x0 = [0; 0];

delta = 2;

% Compute the gradient of the objective function and constraints


fgrad = gradient_f(f, fvars);

ggrad = gradient_f(g, fvars);

ffun = matlabFunction(f, fvars);


x_old = x0;
x_i = x0;

for i = 1 : 15
f_lin = linearize_fun(ffun, fgrad, fvars, x_i);

g_lin = linearize_fun(gfun, ggrad, fvars, x_i);

[fcoefs, ~] = coeffs_fun(f_lin, fvars);


[A, b] = coeffs_fun(g_lin, fvars);
% [Aeq, beq] = coeffs_fun(geq_lin, fvars);
Aeq = []; beq = [];

lb = x_i - ones(size(x_i)).*delta;
ub = x_i + ones(size(x_i)).*delta;

% Solve the linear optimization problem


[x_i,~,~,~,LAMBDA] = linprog(fcoefs, A, b, Aeq, beq, lb, ub);
lambda = LAMBDA.ineqlin;

% Computes the Lagrangian


Lval = numLagrangian(f, g, lambda);

% evaluate original objective function using the solution


fval = ffun(x_i);

% evaluate the constraints in the point


gval = gfun(x_i);

% check if x_i is a viable solution


if (ffun(x_i) < ffun(x_old)) && all(violated_g(x_i) > violated_glin(x_i))

flag = true; %accept

elseif (ffun(x_i) > ffun(x_old)) && all( violated_g(x_i) < violated_glin(x_i) )


flag = false; %reject

elseif ( L(x_i) < L(x_old) )


flag = true;

else
flag = false;
end

if ~flag % if rejected
delta = delta/2;
x_i = x_old;
end

end
end

%%
function f_lin = linearize_fun(fun, grad_fun, Xvars, x_i)
% Linearize nonlinear function fun at point x_i using the function's
% gradient (grad_fun)

fi = fun(x_i);
grad_fi = grad_fun(x_i);
f_lin = fi + transpose(grad_fi)*(Xvars - x_i);

end

%%
function grad = gradient_f(f, fvars)
grad = [];
for i = 1:length(f)
grad = [grad; gradient(f(i))];
end

grad = matlabFunction(grad, fvars);

end

%%
function L = numLagrangian(fval, gval, lambda)

L = fval + dot(-gval,lambda);
end
%%
function [cf, cr] = coeffs_fun(f, fvars)
% Return the coefficients of a symbolic function Example: if f = 3*x^2 +
% pi*y + z + 2 , the function will return A = [3, pi, 1] (Note that the
% function will try to preserve the order of the coefficients). The last
% coefficient (2) will not be inserted in the output since it is not
% multiplied by any variable. If f is an array of symbolic expressions, the
% output will be a matriz, in wich each row will correspond to the
% coefficients of an expression.

% get variables from expression if fvars is empty


if isempty(fvars), fvars = symvar(f); end

cf = []; cr = [];
for i = 1 : length(f)
children_f = children(f(i));
cf_i = [];
cr_i = [];
for j = 1: length(children_f)
coef_f = coeffs(children_f(j), fvars);

if isempty(symvar(children_f(j)))
cr_i = [cr_i, -coef_f];
elseif ~isempty(symvar(children_f(j)))
cf_i = [cf_i, coef_f];
end
end

cf = [cf; double(cf_i)];
cr = [cr; double(cr_i)];

end
end

Item [d]

A tabela abaixo compara os resultados obtidos nos itens e .


iter (x_1 ; x_2) limites Problema linear (x_1; x_2) (Lambdas) F g_1 ; g_2 L
0 0.000000 s
2.0 min -16.000000 x_1 -2.000000 x_2 1.310776 0.000000 -19.254277 -0.881533 -35.251530
0.000000 tal que -1.875000 x_1 +1.000000 x_2 <= +0.785398 2.000000 7.529412 2.124635
+2.125000 x_1 -1.000000 x_2 <= +0.785398
-2.000000 < = x_1 < = +2.000000
-2.000000 < = x_2 < = +2.000000
1 1.310776 2.0 min -13.378449 x_1 +2.000000 x_2 1.911909 7.190406 -26.935149 -0.084112 -26.330346
2.000000 tal que +1.860597 x_1 +1.000000 x_2 <= +3.557293 0.000000 0.000000 1.176932
-1.610597 x_1 -1.000000 x_2 <= -1.986496
-0.689224 < = x_1 < = +3.310776
-0.000000 < = x_2 < = +4.000000
2 1.911909 2.0 min -12.176182 x_1 -2.000000 x_2 3.054125 7.259209 -31.538320 2.229588 -47.723367
0.000000 tal que +1.677343 x_1 +1.000000 x_2 <= +3.122814 -2.000000 0.000000 -1.422323
-1.427343 x_1 -1.000000 x_2 <= -1.552018
-0.088091 < = x_1 < = +3.911909
-2.000000 < = x_2 < = +2.000000
3 3.054125 2.0 min -9.891750 x_1 -6.000000 x_2 3.329935 0.000000 -42.190491 0.736996 -42.196711
-2.000000 tal que -1.844476 x_1 +1.000000 x_2 <= -5.403671 0.000000 4.722781 0.001317
+2.094476 x_1 -1.000000 x_2 <= +6.974467
+1.054125 < = x_1 < = +5.054125
-4.000000 < = x_2 < = +0.000000
4 3.329935 2.0 min -9.340130 x_1 -2.000000 x_2 4.338267 0.000000 -50.591714 -1.076533 -57.945887
0.000000 tal que -1.734779 x_1 +1.000000 x_2 <= -5.039704 2.000000 4.705880 1.562762
+1.984779 x_1 -1.000000 x_2 <= +6.610501
+1.329935 < = x_1 < = +5.329935
-2.000000 < = x_2 < = +2.000000
5 4.338267 2.0 min -7.323465 x_1 +2.000000 x_2 4.918783 4.603722 -54.506101 -0.230614 -53.444417
2.000000 tal que +1.590771 x_1 +1.000000 x_2 <= +7.824655 0.000000 0.000000 0.571715
-1.340771 x_1 -1.000000 x_2 <= -6.253858
+2338267 < = x_1 < = +6.338267
+0.000000 < = x_2 < = +4.000000
6 4.918783 2.0 min -6.162434 x_1 -2.000000 x_2 5.822909 3.148900 -51.260275 1.261598 -55.232922
0.000000 tal que +1.957012 x_1 +1.000000 x_2 <= +9.395503 -2.000000 0.000000 -1.146529
-1.707012 x_1 -1.000000 x_2 <= -7.824706
+2.918783 < = x_1 < = +6.918783
-2.000000 < = x_2 < = +2.000000
7 4.918783 I.0 min -6.162434 x_1 -2.000000 x_2 5.311926 3.148900 -53.774258 0.189704 -54.371618
0.000000 tal que +1.957012 x_1 +1.000000 x_2 <= +9.395503 -1.000000 0.000000 0.053111
-1.707012 x_1 -1.000000 x_2 <= -7.824706
+3.918783 < = x_1 < = +5.918783
-1.000000 < = x_2 < = +1.000000
8 5.311926 1.0 min -5.376148 x_1 -4.000000 x_2 6.283185 11.881531 -55.097375 1.637266 -61.646439
-1.000000 tal que +0.851441 x_1 +1.000000 x_2 <= +3.712497 -1.637266 7.881531 -1.637266
-0.601441 x_1 -1.000000 x_2 e -2.141701
+4.311926 < = x_1 < = +6.311926
-2.000000 < = x_2 < = +0.000000
9 6.283185 1.0 min -3.433629 x_1 -5.274532 x_2 5.983295 0.000000 -58.252263 0.110292 -58.195193
-1.637266 tal que -1.875000 x_1 +1.000000 x_2 <= -11.780972 -0.637266 1.615826 -0.035319
+2.125000 x_1 -1.000000 x_2 <= +13.351769
+5.283185 < = x_1 < = +7.283185
-2.637266 < = x_2 < = -0.637266
10 5.983295 1.0 min -4.033409 x_1 -3.274532 x_2 6.283185 39.394863 -60.909003 0.069367 -61.136145
-0.637266 tal que -1.525920 x_1 +1.000000 x_2 <= -9.657004 -0.069367 36.120331 -0.069367
+1.775920 x_1 -1.000000 x_2 <= +11.227800
+4.983295 < = x_1 < = +6.983295
-1.637266 < = x_2 < = +0.362734
11 6.283185 1.0 min -3.433629 x_1 -2.138733 x_2 6.283185 31.913748 -61.052547 0.000000 -61.052547
-0.069367 tal que -1.875000 x_1 +1.000000 x_2 <= -11.780972 0.000000 29.775015 0.000000
+2.125000 x_1 -1.000000 x_2 <= +13.351769
+5.283185 < = x_1 < = +7.283185
-1.069367 < = x_2 < = +0.930633
12 6.283185 1.0 min -3.433629 x_1 -2.000000 x_2 6.283185 30.734518 -61.052547 0.000000 -61.052547
0.000000 tal que -1.875000 x_1 +1.000000 x_2 <= -11.780972 0.000000 28.734518 0.000000
+2.125000 x_1 -1.000000 x_2 <= +13.351769
+5.283185 < = x_1 < = +7.283185
-1.000000 < = x_2 < = +1.000000
13 6.283185 0.5 min -3.433629 x_1 -2.000000 x_2 6.283185 30.734518 -61.052547 0.000000 -61.052547
0.000000 tal que -1.875000 x_1 +1.000000 x_2 <= -11.780972 0.000000 28.734518 0.000000
+2.125000 x_1 -1.000000 x_2 <= +13.351769
+5/83185 < = x_1 < = +6.783185
-0.500000 < = x_2 < = +0.500000
14 6.283185
0.000000

Potrebbero piacerti anche