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

2 ビュー (過去 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 Center および File ExchangeNonlinear Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by