Good Day Mathwork Community; I have the following code for returning the strong wolfe line search in soliving conjugate gradient method unconstrained optimization, iam find

9 Ansichten (letzte 30 Tage)
Good Day Mathwork Community;
I have the following code for returning the strong wolfe line search in soliving conjugate gradient method unconstrained optimization, iam finding it difficult to return number function evaluations (nfval) and gradient evalution (ngval). Can some one assist me with the solution of these problems
Thanks
Nasiru Salihu
function [alpha, x, f, grad, fail, nsteps] = St_Wolfe(x0,d,op)
c1 = 1e-4;
c2 = -0.00001;
%c1 = .01;
%c2 = 0.9;
%c1 = 1e-4;
%c2 = 0.001;
%c1 = 0.0001
%c2 = 0.9;
f0=test_functions(x0,op);
grad0=test_functions(x0,op,1);
g0 = grad0'*d;
if g0 >= 0
error('g0 must be negative, not a descent direction')
end
old = 0; fold = f0; gold = g0;
new = 1;
nexpand = max([50 -round(log2(norm(d)))]);
for k = 1:nexpand
xnew = x0 + new*d;
fnew=test_functions(xnew,op);
gradnew=test_functions(xnew,op,1);
gnew = gradnew'*d;
if fnew > f0 + c1*new*g0 | ((fnew >= fold) & k > 1) %
[alpha, x, f, grad, fail, nsteps] = zoom1(old, new, ...
fold, fnew, gold, gnew, f0, g0, x0, d,op, c1, c2);
return
end
if abs(gnew) <= c2*g0
alpha = new; x = xnew; f = fnew; grad = gradnew; fail = 0; nsteps = k;
return
end
if gnew >= 0
[alpha, x, f, grad, fail, nsteps] = zoom1(new, old, ...
fnew, fold, gnew, gold, f0, g0, x0, d,op, c1, c2);
return
end
old = new;
fold = fnew;
gold = gnew;
new = 2*new;
end
alpha = new;
x = xnew;
f = fnew;
grad = gradnew;
fail = -1;
nsteps = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [alpha, x, f, grad, fail, nsteps] = zoom1(...
lo, hi, flo, fhi, glo, ghi, f0, g0, x0, d,op, c1, c2)
fail = 0;
lo2 = hi;
flo2 = fhi;
glo2 = ghi;
nsteps = 0;
while lo ~= hi & nsteps < 50
nsteps = nsteps + 1;
bisect = (lo + hi)/2;
interp = cubic_interp(lo, lo2, flo, flo2, glo, glo2);
if inside(interp, lo, bisect)
atry = interp;
else
atry = bisect;
end;
xtry = x0 + atry*d;
ftry=test_functions(xtry,op);
gradtry=test_functions(xtry,op,1);
gtry = gradtry'*d;
if ftry > f0 + c1*atry*g0 | ftry >= flo
hi = atry;
lo2 = hi; flo2 = ftry; glo2 = gtry;
else
if abs(gtry) <= -c2*g0
alpha = atry; x = xtry; f = ftry; grad = gradtry;
return
end
if gtry*(hi - lo) >= 0
hi = lo;
end
lo2 = lo; flo2 = flo; glo2 = glo;
lo = atry; flo = ftry; glo = gtry;
end
end
alpha = atry; x = xtry; f = ftry; grad = gradtry; fail = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function xmin = cubic_interp(x1, x2, f1, f2, g1, g2)
eta = g1 + g2 - 3*(f1 - f2)/(x1 - x2);
gamma = sign(x2-x1)*sqrt(eta^2 - g1*g2);
xmin = x2 - (x2 - x1)*(g2 + gamma - eta)/(g2 - g1 + 2*gamma);

Antworten (1)

Umang Pandey
Umang Pandey am 31 Okt. 2023
Bearbeitet: Umang Pandey am 31 Okt. 2023
Hi Nasiru,
I understand that you want to count the number of function evaluations (nfval) and number of gradient evaluations (ngval) in your code.
This can be achieved by modifying the function definitions to include the "ngval" and "nfval" variables, in the following manner:
1. Initialize the "nfval" and "ngval" variables in the "St_Wolfe" function:
function [alpha, x, f, grad, fail, nsteps, nfval, ngval] = St_Wolfe(x0,d,op)
nfval = 0;
ngval = 0;
% Your custom code...
2. Modify the "test_functions", "zoom1" and "cubic_interp" functions to pass and update the "nfval" and "ngval" variables within these functions whenever they are called and gradients are calculated:
function [xmin, nfval, ngval] = cubic_interp(x1, x2, f1, f2, g1, g2, nfval, ngval)
nfval = nfval + 1;
% Your custom code...
function [alpha, x, f, grad, fail, nsteps, nfval, ngval] = zoom1(...
lo, hi, flo, fhi, glo, ghi, f0, g0, x0, d,op, c1, c2, nfval, ngval)
nfval = nfval + 1;
% Your custom code...
Best,
Umang

Kategorien

Mehr zu FPGA, ASIC, and SoC Development finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by