Skip to content

Instantly share code, notes, and snippets.

@MariaLavrovskaya
Last active October 18, 2021 10:11
Show Gist options
  • Save MariaLavrovskaya/219ddffddc7a9dcb2ec8d377a8f94af0 to your computer and use it in GitHub Desktop.
Save MariaLavrovskaya/219ddffddc7a9dcb2ec8d377a8f94af0 to your computer and use it in GitHub Desktop.
function [xMin, fMin, t, nIter, infoPD] = PrimalDual(F, ineqConstraint, eqConstraint, x0, lambda0, nu0, mu, tol, tolFeas, maxIter, opts)
% Initilize the structures
nIter = 0;
stopCond = false;
x_k = x0; %k stands for current iteration
lambda_k = lambda0; %k stands for current iteration
nu_k = nu0; %k stands for current iteration
infoPD.xs = x_k; %all the interations
infoPD.lambdas = lambda0; %all the iterations of lambdas
infoPD.nus = nu0; %all iterations of nus
infoPD.r_dual = [];
infoPD.r_cent = [];
infoPD.r_prim = [];
infoPD.surgap = [];
% Define residual function
r_dual = @(t, x, l, n) F.df(x) + (ineqConstraint.df(x))'*l + eqConstraint.A'*n;
r_cent = @(t, x, l, n) -diag(l)*ineqConstraint.f(x) - ones(m, 1)/t;
r_prim = @(t, x, l, n) eqConstraint.A*x - eqConstraint.b;
res = @(t, x, l, n) [r_dual(t, x, l, n); r_cent(t, x, l, n); r_prim(t, x, l, n)];
% Surrogate gap to track how much we need to step
eta = - (ineqConstraint.f(x0))'*lambda0;
t = mu*m/eta;
% Find direction we need to go
while (~stopCond && nIter < maxIter) %check if stopping criteria are not violated
% Compute residual at current iteration
res_k = res(t, x_k, lambda_k, nu_k);
disp(size(res_k))
% Find direction (inverted r_t for deltaY)
HessianIneqConstraints_x_k = zeros(length(x_k),length(x_k));
ineqConstraints_x_k = ineqConstraint.d2f(x_k);
% Compute the Hessian of the current iteration for x_k
HessianIneqConstraints_x_k = zeros(length(x_k),length(x_k));
inequalityConstraints_x_k = ineqConstraint.d2f(x_k);
for j = 1:length(l_k)
HessianIneqConstraints_x_k = HessianIneqConstraints_x_k + lambda_k(j)*inequalityConstraints_x_k(:,:,j);
end
deltaY = -[F.d2f(x_k) + HessianIneqConstraints_x_k, ineqConstraint.df(x_k)' , eqConstraint.A'; ...
-diag(l_k)*ineqConstraint.df(x_k) , -diag(ineqConstraint.f(x_k)), zeros(m, nEq) ; ...
eqConstraint.A , zeros(nEq, m) , zeros(nEq, nEq)]\res_k;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment