How can I use fmincon with general (user defined) nonlinear constraints?

2 views (last 30 days)
Hello everyone, I am trying to implement an optimization algorithm that as a subroutine solves another nonlinear optmization problem. To make things more concerete the function I am implementing is something like this:
function [x_opt, y_opt, lambda_opt, obj_fun, status] = bpTrustRegion(nx,ny,fl,...,gl,...,gf,...)
where:
nx ... size of input vector x
ny ... size of input vector y
fl = @(x,y) (leader objective function)
gl = @(x,y) [ ... ] ... leader nonlinear constraints
gf = @(x,y) [ ... ] ... follower nonlinear constraints
Now I want to solve so-called high point relaxation which means solving
min_{x,y} f(x,y)
s.t. gl(x,y) <= 0
gf(x,y) <= 0
So to do that I am trying to call a subroutine solveHighPointRelaxation:
function [x0,y0,nx,ny] = solveHighPointRelaxation(nx,ny,fl,gl,gf)
x0 = zeros(nx+ny,1);
f = @(x)(fl(x(1:nx),x(nx+1:nx+ny)));
g = @(x)[gl(x(1:nx),x(nx+1:nx+ny));gf(x(1:nx),x(nx+1:nx+ny))];
[x,fval,exitflag,output] = fmincon(f,x0,[],[],[],[],[],[],g);
But! I am getting an error "Insufficient number of outputs from right hand side of equal sign to satisfy assignment." caused by not returning an equality nonlinear constraints. And here I get into trouble. I tried to pass the following:
[[],g] ... does not see the square brackets as an output, i.e. the same error is thrown
[0,g] ... cannot concatenate (obviously as g(x) is column vector), i.e. "Error using horzcat"
[0;g] ... understands as single output, i.e. the first error occure
@(x)[0;g(x)] ... again understands as single output, i.e. the first error occure
I know I could write the functions into a functions into a function
function [c,ceq] = constreval(x)
... gl(x,y) rewriten "by hand" returning the evaluated functions
end
But this defines the whole point of using a function in the first place. I know I could pass this function in the bpTrustRegion function in the first place (something like highPointRelaxationNonlcon(x)) which is really user-UNfriendly approach. Will be glad for any input on this matter.
Thanks in advance, community! <3

Accepted Answer

Torsten
Torsten on 4 Feb 2025
Moved: Torsten on 4 Feb 2025
g = @(x)deal([gl(x(1:nx),x(nx+1:nx+ny));gf(x(1:nx),x(nx+1:nx+ny))],[])

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!