Evaluate several objective functions in parallel (parfor)

5 vues (au cours des 30 derniers jours)
Maria Angela Agizza
Maria Angela Agizza le 1 Avr 2019
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.

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Avr 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
  1 commentaire
Maria Angela Agizza
Maria Angela Agizza le 1 Avr 2019
Hello, thanks a lot for the answer, it works fine!
Best regards.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by