I want to create optimization constraints using Problem-based Optimization in MATLAB.
However, in many optimization problems, vectorization is not possible due to the problem nature, therefore loops are needed.
I have noticed that the loops involving optimization variables are too slow.
In the example below, a loop with 1000 iterations that includes a single optimization variable consumes around 7 seconds.
Is this the expected behaviour for generating optimization constraints over loops?
I understand that MATLAB is built on vectorization principles, however 7 seconds for a 1000 iteration loop is extremely slow.
I = "i" + (1:1000)' ;
k = optimvar ( 'k' , I , 'LowerBound' , 0 ) ;
Constraint_test = optimconstr ( I ) ;
for i = 1 : length(I)
Constraint_test(i) = 2 * k(i) + 3 <= 10 ;
Constraint_test_vec = 2 * k + 3 <= 10 ;