How to add user defined function as constraint for optimization?

How to add user defined function as constraint for optimization? Example is given below. The problem that I know is a problem is that I am parsing a constraint to a function that sees that constraint as a variable, which it is not.
x = optimvar("x",3,3);
y = const1;
z = const2;
in loop:
return_val = my_fun(x(i,j),y)
prob.Constraints.Con1 = return_val >= z;
end loop

댓글 수: 4

Thank you very much. I use R2018b version and fcn2optimexpr() has been introduced in R2019b. Is there any alternative for that?
Yes. Use the solver-based instead of the problem-based approach.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2022년 7월 6일
편집: Matt J 2022년 7월 6일
Ultimately, you must use the solver-based approach, like Torsten says. However, if you download this,
you can still use problem-based tools to set-up the linear portion of the problem, e.g.,
x = optimvar("x",3,3,'Lower',0,'Upper',1);
Constraints.rowsum=sum(x,1)<0.5;
Constraints.colsum=sum(x,2)==1;
p=prob2matrices('Constraints',Constraints);
x=fmincon(@objective,x0,p.A,p.b,p.Aeq,p.beq,p.lb,p.ub, @(x) nonlcon(x,y,z))
function [c,ceq] = nonlcon(x,y,z)
c=z-my_fun(x,y); %my_fun must be differentiable in x
ceq=[];
end

추가 답변 (0개)

질문:

2022년 7월 6일

댓글:

2022년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by