Skip to content

Commit

Permalink
worhp update
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliang-dai committed Sep 17, 2020
1 parent c41f16a commit b623522
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions src/core/createLocalSolvers.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,15 @@
end

function res = build_local_NLP_with_worhp(f, g, h, A, lambda, rho, z, Sigma, x0, lbx, ubx, dgdx, dfdx, Hessian)
% assumption: least-square problem, i.e. without constraints
%% assumption: least-square problem, i.e. without constraints
cost = @(x)build_cost(x, f(x), lambda, A, rho, z, Sigma);
grad = @(x)build_grad(x, dfdx(x), lambda, A, rho, z, Sigma);
% unconstrained problem and Hessian is computed by hand
<<<<<<< HEAD
Hess = @(x, mu, scale)(Hessian(x,lambda) + rho*Sigma);
Hess.Func = @(x)build_hessian(x, Hessian(x,0,0), rho, Sigma);
% [cost, grad] = @(x)build_function(x, f(x), dfdx(x), [], lambda, A, rho, z, Sigma)
Nx = numel(x0);
% transform hessian matrix into hessian vector for WORHP solver
[Hess.row,Hess.col,Hess.pos] = build_Hess_indx_worhp(Hess.Func,Nx);
[xopt, multiplier] = worhp_interface(cost,grad,Hess,x0',lbx',ubx');
=======
% Sparse
Nx = numel(x0);
nr = 3;
S = zeros(Nx,Nx);
for i=1:nr
S = S + full(build_hessian(rand(Nx,1), Hessian, rho, Sigma) ~=0);
end
% get sparsity
S = S ~=0;
% convert everything to vectors for WORHP
[ pos1, pos2 ] = find(S);
posCombined = find(S);

Hess = @(x, mu, scale)build_hessian(x, Hessian, rho, Sigma, mu, scale, posCombined);

[xopt, multiplier] = worhp_interface(cost,grad,Hess,x0',lbx',ubx',pos1,pos2);
>>>>>>> 51decb027cdbf4842a4a4170f817b0edeff62a6d
res.x = xopt;
res.lam_g = [];
res.lam_x = multiplier;
Expand Down Expand Up @@ -177,34 +161,38 @@
grad = dfdx + A'*lambda + rho*Sigma*(x - z);
end

<<<<<<< HEAD
function hm = build_hessian(x, Hessian, rho, Sigma, mu, scale, posCombined)
function hm = build_hessian(x, Hessian, rho, Sigma)
hm = Hessian + rho*Sigma;
=======

function hm = build_hessian(x, Hessian, rho, Sigma, mu, scale, posCombined)
hm = Hessian(x,0,0) + rho*Sigma;
>>>>>>> 51decb027cdbf4842a4a4170f817b0edeff62a6d
if nargin > 4
hm = hm * scale;
hm = hm(:);
hm = long(hm(posCombined));
end
end

<<<<<<< HEAD
=======
% function hv = hessian_vector(x, mu, scale, posCombined, Hess)
% hm = scale * Hess()
%
% end


>>>>>>> 51decb027cdbf4842a4a4170f817b0edeff62a6d
function [ineq, eq, jac_ineq, jac_eq] = build_nonlcon(x, g, h, dgdx)
ineq = h(x);
eq = g(x);
jac_ineq = [];
jac_eq = dgdx(x)';
end

function [Hrow, Hcol, Hpos] = build_Hess_indx_worhp(H, Nx)
nr = 5;
S = zeros(Nx,Nx);
for i=1:nr
S = S + full(H(rand(Nx,1)/100) ~=0);
end
% get sparsity
S = S ~=0;
% convert everything to vectors for WORHP
[row, col] = find(S);
position_number = find(S);

diag = find(row == col);
low_triangle = find(row>col);
Hrow = [row(low_triangle);row(diag)]';
Hcol = [col(low_triangle);col(diag)]';
Hpos = [position_number(low_triangle); position_number(diag)];
end

0 comments on commit b623522

Please sign in to comment.