How to plot fmincon fvalues at each iteration from parallel pools?

I'd like to plot multiple dot/line plots of the successively minimized fvals from fmincon that is being run multiple times on a parfor loop. Essentially I want the PlotFcn 'optimplotfval' that can be run on a single iteration of fmincon, but in a plot with all parallel runs of fmincon. I tried making a dataqueue and sending the information to the queue q within the outfun of the fmincon, but it seems like the queue q is inaccessible from within the outfun of fminaon.The crux of the idea is:
Main code:
q = parallel.pool.DataQueue;
q.afterEach (@Some code that will update a displayed plot);
parfor i = 1:4
[values, score] = runfmincon(...)
end
Another file runfmincon.m:
F = @(x) objfunc(...)
options = optimset('OutputFcn',{@outfun});
[values, score] = fmincon(F,.....,options)
function score = objfunc(...)
%My objective function
end
function stop = outfun(x,optimValues,state)
send(q,optimValues.fval);
end

3 Comments

See if this will work. Change
options = optimset('OutputFcn',{@outfun});
to be
optimset('OutputFcn',@(x,optimValue,state)outfun(x,optimValue,state,q));
And then change
function stop = outfun(x,optimValues,state)
to be
function stop = outfun(x,optimValues,state,q)
I believe you'll need to send back the worker ID number as well as optimValues.fval, so that afterEach will know which of the parallel plots to update.
send(q,{optimValues.fval, getCurrentTask().ID);
@Raymond Norris your suggestion worked great. Didn't think to try passing the dataqueue into the function. Thanks!

Sign in to comment.

Answers (0)

Asked:

on 25 Oct 2022

Commented:

on 26 Oct 2022

Community Treasure Hunt

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

Start Hunting!