Dear Prof.Johan Löfberg,

24 views
Skip to first unread message

yize

unread,
Jul 11, 2025, 10:52:25 PMJul 11
to YALMIP
Dear Prof.Johan Löfberg,  
I have a question regarding constraint formulation in YALMIP. In my model, I currently define the following constraints using a loop:  

for p_idx = 1:num_total_paths
    demand_idx = path_details(p_idx).demand_idx;
    Constraints = [Constraints, num_spectrum >= fsd(demand_idx, :) + num_slots_p(p_idx, :)];
end

To simplify the code, I tried writing the same logic in a matrix-style form as follows:  

Constraints = [Constraints, num_spectrum >= fsd(demand_idx, :) + num_slots_p];

However, I noticed that this vectorized version slightly increases the solution time. I would like to ask:

  1. Is there any practical advantage to using the loop form over the matrix form (or vice versa) in terms of numerical efficiency or solver interface performance?

  2. In your experience, is it recommended to avoid such vectorized forms even if they make the code cleaner?

Thank you very much for your insights and for maintaining such an excellent toolbox.

Best regards,

Yize Liu  



Johan Löfberg

unread,
Jul 12, 2025, 3:08:33 AMJul 12
to YALMIP
From the code supplied, it cannot be said anything since we don't know any dimensions, and thus have no idea if you have messed up something.

Vectorized vs non-vectorized code should make no difference when it comes to the solver, it will only affect the parsing and typically make it faster. If you get consistently different speed in the solver, you have not vectorized correctly

Just looking at it, I don't see how it can even run. fsd and num_slots_p both appears to be a matrices, thus fsd(demand_idx, :) + num_slots_p is a sum of a vector and matrix, and since YALMIP does not do singleton expansion for that case, it should give an error.

Hence, reproducible example needed
Reply all
Reply to author
Forward
0 new messages