Global variables are forbidden in parallel multistart optimization

4 views (last 30 days)
Kun Yang
Kun Yang on 25 Oct 2021
Commented: Kun Yang on 28 Oct 2021
I have met a problem when I try to use MultiStart optimization in parallel. I would like to rise this question here, asking for help and also making the concept clearer for others.
Basic info:
I am using MultiStart optimization toolbox, my MATLAB version is R2021a. Thanks to my university.
However, my optimization ends with positive exitflag when I turn off the parallel option and always ends with exitflag=-10 when I turn on it.
As you may easily find out, -10 exitflag means user defined function has bugs. But this cannot make sense as my function goes well with single pool.
Later on, after searching in many different ways, I finally found out that the bug is the global variable. MATLAB parallel MultiStart optimization toolbox cannot support global variables in user defined functions. I have tried a demo case for example, which proves my idea.
In another word, the parallel MultiStart cannot allow functions to access global variables. The function should be totally independent. But in my case, I need to read in a large dataset in previous and I will use the dataset during my calculation. Obviously, I cannot let the objective function to read in the dataset in every trial, which may be extremely time consuming. Actually, I do not have to change my dataset in functions but the program was killed just because I try to access the global variables, which is really annoying.
Any ideas? Hope someone could save my poor codes.
Demo example:
main code:
clear all;
format long;
x0 = [2,3];
global A
ms = MultiStart('UseParallel',true,'Display','iter');
user defined objective function:
function f=usrobj(x)
global A
%A=evalin('base','A'); % evalin has the same problem

Accepted Answer

Alan Weiss
Alan Weiss on 26 Oct 2021
There are other ways to pass variables other than declariig them as global. See Passing Extra Parameters.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Kun Yang
Kun Yang on 28 Oct 2021
Dear Alan,
Thank you again! I didn't realize the usage and value of Anonymous Functions the first time I saw it. My code can run in parallel now with Anonymous Functions as objective function and constraint function. Thank you very much for your support!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!