how to use parfor for optim constraints?

3 次查看(过去 30 天)
I'm using optimisation toolbox ver 9.3 and have parallel computing toolbox 7.6
I have a lot of constraints equations to write and formulation only will take a lot of time. Many equations are in 3 nested loops .
I'm writing one of the equation.
y_uij and x_uij are binary variable already been defined. Nodes.all and flight_name_set_0 are in string format. flight_set_0 is an array of structure.
how would you suggest to write such that I can use parfor to speed up the formulation.
y1 = optimconstr(Nodes.all, flight_name_set_0, flight_name_set_0);
for n = 1:length(Nodes.all)
u = Nodes.all(n);
for f1 = 1:length(flight_set_0)
i = flight_set_0(f1).name;
for f2 = 1:length(flight_set_0)
j = flight_set_0(f2).name;
common_node = any(ismember(flight_set_0(f1).nodes,u)) & any(ismember(flight_set_0(f2).nodes,u));
if (f1 ~= f2) && common_node
y1(u,i,j) = y_uij(u,i,j) >= x_uij(u,i,j);
end
end
end
end

回答(1 个)

Alan Weiss
Alan Weiss 2022-12-22
Unfortunately, your version of Optimization Toolbox does not have the latest upgrade which uses "static analysis" to speed the creation of optimization expressions and constraints. To see the potential benefit to upgrading, see Static Analysis of Optimization Expressions, Create for Loop for Static Analysis, and Convert Constraints in for Loops for Static Analysis.
I don't mean to sound like I'm selling an upgrade, it is just that the latest software addresses exactly your issue.
Meanwhile, perhaps it is possible for you to vectorize your expressions to eliminate some for loops. I don't immediately see how to do that for your loops, but that is the thing that might speed things up enormously. For an example where I used a vectorized call to eliminate one nesting level of for loops, see Constrained Electrostatic Nonlinear Optimization Using Optimization Variables, in particular the section Define Objective Function.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
  2 个评论
Ravi Raj Saxena
Ravi Raj Saxena 2022-12-23
Thank you for the answer, I won't have problem upgrading as I'm on univerity license.
However the links you shared have fmincon as the optimisation solver problem, I have to use intlinprog. Doea this information changes anything ?
Alan Weiss
Alan Weiss 2022-12-23
No, the benefits to using external for lops in static analysis apply to all solvers, including intlinprog.
Alan Weiss
MATLAB mathematical toolbox documentation

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by