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 件のコメント

Torsten
Torsten 2022 年 7 月 6 日
Faruk Šehić
Faruk Šehić 2022 年 7 月 6 日
Thank you very much. I use R2018b version and fcn2optimexpr() has been introduced in R2019b. Is there any alternative for that?
Torsten
Torsten 2022 年 7 月 6 日
Yes. Use the solver-based instead of the problem-based approach.
Faruk Šehić
Faruk Šehić 2022 年 7 月 6 日
Thank you.

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2022 年 7 月 6 日
編集済み: Matt J 2022 年 7 月 6 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by