Evaluate several objective functions in parallel (parfor)

조회 수: 5 (최근 30일)
Maria Angela Agizza
Maria Angela Agizza 2019년 4월 1일
댓글: Maria Angela Agizza 2019년 4월 1일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by