How can I fix SOLVE ignores UseParallel when derivatives calculated automatic differentiation warning for parallel computing?
2 次查看(过去 30 天)
显示 更早的评论
I coded an NLP version of the p-median problem which I am solving by fmincon. I wanted to use UseParallel to speed up the process. But I get the fallowing warning: SOLVE ignores UseParallel when derivatives calculated using Automatic Differentiation. Could you please show me how to use parallel computing with such a code?
A = readmatrix("Iris.csv");
A = A(:,1:5);
A = normalize(A);
dist_mat = pdist(A);
dist_mat = squareform(dist_mat);
x0x = zeros(150,150) + 1/150;
x0y = zeros(150,1) + 1/150;
% Create optimization variables
x = optimvar("x",150,150,"LowerBound",0,"UpperBound",1);
y = optimvar("y",150,1,"LowerBound",0,"UpperBound",1);
% Set initial starting point for the solver
initialPoint.x = x0x;
initialPoint.y = x0y;
% Create problem
problem = optimproblem;
% Define problem objective
problem.Objective = sum(dist_mat.*x,...
'all')+ sum(50.*(y.^2)./((y.^2) + 0.001));
% Define problem constraints
problem.Constraints.Xeq = sum(x,2)==1;
problem.Constraints.Yeq = sum(y)==2;
problem.Constraints.XYineq = x - repmat(y',150,1) <= 0;
% Set nondefault solver options
options = optimoptions("fmincon","Algorithm","interior-point", ...
"HessianApproximation","lbfgs","UseParallel",true,"MaxIterations",10000,"MaxFunctionEvaluations",10000,"Display","iter");
% Display problem information
%show(problem);
% Solve problem
[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint,...
"Solver","fmincon","Options",options);
0 个评论
回答(1 个)
Matt J
2022-8-21
It's not something to fix. It's what you want. Analytical differentiation is the fastest option, when it is available.
2 个评论
Matt J
2022-8-21
You might be able to do a little better if you use fmincon directly and use SpecifyObjectiveGradient=true. I wouldn't expect a huge improvement.
In either case, UseParallel is not the path to optimal speed here. Your gradients are nice and simple and can be computed analytically.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!