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

6 次查看(过去 30 天)
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!

采纳的回答

claudio
claudio 2023-3-22
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by