Clear Filters
Clear Filters

How do add constraints to an optimization problem, if the amount of constraints is changing depending of the input?

6 views (last 30 days)
Hello all together,
I have troubles by adding constraints to my optimization problem.
optproblem.Constraints.constraint1 = ...
optproblem.Constraints.constraint2 = ...
and so on.
My problem is, that the amount of constraints depands on my input. Which means, that depending on my input data, I could e.g. have either 2 or 17 constraints. My question is: How could I name them "automatically" (from either 1 - 2, or from 1 - 17)? So in case of aboves example: Is there a way to "store" the strings "constraint1" and "constraint2" so that I can use them after the point - operator?
Thanks in advance!

Accepted Answer

claudio
claudio on 22 Mar 2023
you can use a trick like this
% defining previously the number of constraints based on your imput
for kk = 1:numel(constraints)
field = sprintf('constraint%d',kk);
optproblem.Constraints.(field) = "constrain";
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!