필터 지우기
필터 지우기

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