Parallel computing for Optimization Problem
4 views (last 30 days)
Show older comments
Hello all, I am using parallel computing to run globalsearch at different initial guesses (gs itself is not compatable with parallel computing, so I am using it to calculate at different initial guesses).
My problem, is that the initial evaluation is actually quite fast but the calculation of the best solution however is quite slow. I assume there is a LARGE logical error that I am commiting (as this is my first experience with parallel computing), but not really sure what it is. Specifically, my problem needs a rework of how to save the best solution and I am a little confused on how to do that. The 'fetching Outputs' section will run for a few hundred trails, and then get stuck. Here is my code.
tic
% initialize parallel pool
pool = parpool;
gs = GlobalSearch("StartPointsToRun","bounds-ineqs", "NumStageOnePoints", 100000, 'NumTrialPoints', 1000000);
% call nonlinear constraint and values for initial guess generators
nonlcon_poly_ogden = @nlcon_poly_ogden;
s = 1000; % number of loops
lowercapability = -20;
uppercapability = 20;
% by default the number of workers is max cores available
futures = cell(s,1);
for i = 1:s
% calculate a new, random initial guess
r =((lowercapability- uppercapability).*rand(6,1)+ uppercapability)';
problem = createOptimProblem('fmincon', 'x0', r, 'objective', full_model, 'lb', 100*lowercapability .*ones(6,1) ,'ub', 100 *uppercapability .*ones(6,1) ,'nonlcon', nonlcon_poly_ogden);
% run and save values into future
futures{i} = parfeval(pool, @run, 2, gs, problem);
% Counter
fprintf('Ogden PolyConvex MODEL AT: '); disp(i);
end
fprintf('Fetching Outputs ');
% let's create a solution matrix
a = zeros(s, 7);
for i = 1:s
a(i, 1:6) = futures{i}.fetchOutputs();
a(i, 7) = full_model(a(i, :));
fprintf('Soln at '); disp(i);
end
delete(gcp); % Close the parallel pool
% compute best solution
min = min(a(:, 7)); % calculate the minimum err value
[r_index,c_index] = find(a == min); % save index
soln = a(r_index, 1:6); % save soln
error = min; % save err
toc
3 Comments
Raghav
on 6 Apr 2023
Hi,
It looks like the main issue with your code is that you are not properly handling errors that might occur during the parallel execution. It is possible that some of the function evaluations fail or take longer than expected, causing your code to get stuck indefinitely in the fetchOutputs loop.
To avoid this, you can add some error handling to your code using a try-catch block, this way, if an error occurs during the execution of a particular iteration, your code will skip that iteration and move on to the next one. You can also print out an error message to help you diagnose any issues that might arise.Then we can know which iteration is causing it to stuck and debug that, also please mention the error which it presents.
Another issue with your code is that you are not properly closing the parallel pool at the end. You should use delete(pool) instead of delete(gcp) to close the pool.
Walter Roberson
on 6 Apr 2023
Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!