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

2 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

claudio
claudio am 22 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by