how to use parfor for optim constraints?

조회 수: 4(최근 30일)
Ravi Raj Saxena
Ravi Raj Saxena 2022년 12월 22일
댓글: Alan Weiss 2022년 12월 23일
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);


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
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

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

Community Treasure Hunt

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

Start Hunting!

Translated by