How to pass more parameters than the existing ones to output function of fmincon?

The above link is to the documentation of Output Functions, a function that is evaluated at each individual iteration of fmincon. The parameters passed to it by default are x, optimValues and state.
1. How to pass extra parameters to this function?
2. Is there any way to compute a partial stepsize? Say numel(x) = 60 and I want to calculate stepsize norm(x(1:10)-xk(1:10)), is it possible? Because optimValues.stepsize calculates norm(x-xk) which is taking into account the entire vector.

 Accepted Answer

To pass external parameters, use the documented ways. You can also use persistent variables within an output function.
Alan Weiss
MATLAB mathematical toolbox documentation

1 Comment

I'm thinking I'll persistent variables to compute partial step size within outputfunction. But I'm using this within parfor. Will this still work? If not, how do I do it?

Sign in to comment.

More Answers (1)

Is there any way to compute a partial stepsize?
One way is to modify the example at the link you posted:
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.x = [history.x; x(1:10)]; %only save 10 elements
history.x(1:end-2,:)=[]; %only save the last 2 iterations
if size(history,1)>1
partial_stepsize=norm( history.x(2,:) - history.x(1,:) );
end

6 Comments

The previous iteration's partial vector never got retained at the end of each call. According to my code below, tempx0 is what is retained as the first row of history.x . How do I fix this?
epsilon = gamma*0.01*min(0.03,1/mag);
history = struct;
history.x = tempx0';
%Function Definition and Solver
fun = @(x)tempx0'*H*tempx0 + h'*tempx0 + const + (H(x_i,x_i)*tempx0(x_i) + h(x_i))'*(x(x_i) - tempx0(x_i));
term_func = @(x,optimValues,state)inexact_gja_outfun(x,optimValues,state,epsilon,history,x_i)
options = optimoptions(@fmincon,'OutputFcn', term_func,'Algorithm','sqp','MaxIterations',1e6,'MaxFunctionEvaluations',1e6,'StepTolerance',1e-10);
[x,fval,exitflag,output{j}] = fmincon(fun,tempx0,A,b,Aeq,beq,lb,ub,@(x)nonl_gja_constraints(x,j),options);
The above code is from where I call the outputfunction. The code below is the outputfunction.
function stop = inexact_gja_outfun(x,optimValues,state, epsilon, history, x_i)
history.x = [history.x(x_i); x(x_i)'];
history.x(1:end-2,:)=[];
stop = false;
if size(history.x,1)>1 && optimValues.iteration > 0
partial_stepsize=norm( history.x(2,:) - history.x(1,:) );
if partial_stepsize <= epsilon
stop = true;
end
end
end
Please show the code with all relevant nesting. Is inexact_gja_outfun nested in the workspace of your main function?
inexact_gja_outfun is passed through optimoptions. I defined the function in a separate file.
No, it must be nested inside your runfmincon() function, like here.
Accordingly also, your options should be
options = optimoptions(@fmincon,'OutputFcn', @inexact_gja_outfun,____);
and let history and x_i be externally scoped variables, not arguments to the output function:
function stop = inexact_gja_outfun(x,optimValues,state)
.....
end
Viswanath, I fixed that by making 'history' a global variable.
Apparently, passing it to the output function that way you've done restricts its size. As a result, concatenate doesn't work as it can't add more rows -- row 1 stays at initial value and row 2 gets updated with current value.
Making it global removes this restriction. Hope this helps.
Making it global removes this restriction.
That sounds doubtful to me. There shouldn't be a difference in memory limitations between global variables and other variable types.

Sign in to comment.

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!