Evaluate several objective functions in parallel (parfor)

5 Ansichten (letzte 30 Tage)
Hello Matlab community,
I am looking for help because I have an issue with parfor and optimization algorithms.
I have an optimization written as follows:
[x,fval] = patternsearch(@ObjFunc,start_par,[],[],[],[],min_par,max_par,@constraints,opts);
I need to optimize the parameters to separately fit an ensemble of curves, stored in an array. To speed the calculation up, I would like to use "parfor".
In my ObjFunc, I have the following code:
load('file.mat');
exp_curve = file(:,li);
where "li" is the loop variable.
Working with a normal "for cycle", I would pass "li" as global. Global variables do not work while using "parfor", and I haven't yet found a way to pass the loop variable to my ObjFunc.
Do you have suggestion that can help me?
Best regards.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 1 Apr. 2019
You should avoid loading data inside an objective function, as that function will be called many times. You should instead http://www.mathworks.com/help/matlab/math/parameterizing-functions.html parameterize
That could include,
filestruct = load('file.mat', 'file');
file = filestruct.file;
for li = 1 : whatever
func{i} = @(x) Objfunc(x, file(:,li));
end
parfor li = 1 : whatever
patternsearch(func{i}, ....)
end
or you could
filestruct = load('file.mat', 'file');
file = filestruct.file;
parfor li = 1 : whatever
patternsearch( @(x) Objfunc(x, file(:,li)), ...)
end

Weitere Antworten (0)

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by