# how to use parfor for optim constraints?

2 views (last 30 days)
Ravi Raj Saxena on 22 Dec 2022
Commented: Alan Weiss on 23 Dec 2022
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

Alan Weiss on 22 Dec 2022
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
Ravi Raj Saxena on 23 Dec 2022
However the links you shared have fmincon as the optimisation solver problem, I have to use intlinprog. Doea this information changes anything ?
Alan Weiss on 23 Dec 2022
No, the benefits to using external for lops in static analysis apply to all solvers, including intlinprog.
Alan Weiss
MATLAB mathematical toolbox documentation

### Categories

Find more on Get Started with Problem-Based Optimization and Equations in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!