Passing a variable constraint to fmincon

Hi!,
I am using the 'fmincon' solver for optimizing a function that takes array of inputs. The optimization function is FUN = @(x)((sqrt((x(1) - a)^2 + (x(2) - b)^2) + sqrt((x(1) - c)^2 + (x(2) - d)^2))^2 - (y(nn))^2)^2. Note that y(nn) changes with 'nn'.
Currently, I am defining 'FUN' in a for loop so I can pass a scalar 'y'. Similary, the constraint function for 'FUN', i.e. c = sqrt((x(1)- h).^2 + (x(2) - g).^2)- y(nn) also change with 'nn'. However, I cannot call c(x,y(nn)) inside fimincon and neither can I define c(x) inside the for loop with a fixed value of y(nn). So, I would like to ask:
  1. Is there any efficient way to pass the vector input 'y' to fmincon without defining FUN in a for loop?
  2. How do I pass a vector argument to c(x) in addition to 'x'?
Thank you.

 Accepted Answer

Matt J
Matt J on 18 May 2023
Edited: Matt J on 18 May 2023
I think this is what you mean,
for nn=1:N
ynn=y(nn);
FUN = @(x)((sqrt((x(1) - a)^2 + (x(2) - b)^2) + sqrt((x(1) - c)^2 + (x(2) - d)^2))^2 - (ynn)^2)^2
CON=@(x) nonlcon(x,ynn,h,g);
xopt=fmincon(FUN,x0,A,b,Aeq,beq,lb,ub,con);
end
function [c,ceq]=nonlcon(x,y,h,g)
c = sqrt((x(1)- h).^2 + (x(2) - g).^2)- y;
ceq=[];
end

7 Comments

How is nn reaching nonlcon ?
I would suggest instead
for nn=1:N
FUN = @(x)((sqrt((x(1) - a)^2 + (x(2) - b)^2) + sqrt((x(1) - c)^2 + (x(2) - d)^2))^2 - (y(nn))^2)^2
CON=@(x) nonlcon(x,y(nn),h,g);
xopt=fmincon(FUN,x0,A,b,Aeq,beq,lb,ub,con);
end
function [c,ceq]=nonlcon(x,y,h,g)
c = sqrt((x(1)- h).^2 + (x(2) - g).^2)- y;
ceq=[];
end
Matt J
Matt J on 18 May 2023
Edited: Matt J on 18 May 2023
Yep, you're right. I fixed it, though I find it preferable to avoid indexing fixed parameters in anonymous function definitions.
Thanks Matt and Walter. Both solutions work. However, I would still prefer to do the optimization without a for loop. Do you recommend arrayfun?
Any given call to the function provided to fmincon() must return a scalar. Some of the other optimizer support a 'vectorized' option, but fmincon() does not.
So although you could get the entire vector y into the function, you cannot return a vectorized result.
Could you examine the values and return the "best" result? That would be discontinuous in the derivatives and would not optimize well... and you would be unable to tell which of the y elements led to that solution.
Matt J
Matt J on 18 May 2023
Edited: Matt J on 18 May 2023
Do you recommend arrayfun?
arrayfun is just different syntax for a for-loop. There's no reason to prefer it.
arrayfun() is slower then a for loop, as you have to call through an anonymous function for each iteration.
It is, however, often more convenient to write, and in easier cases can produce code that is much easier to understand than a loop. In more complicated cases, writing for arrayfun can easily produce code that is more difficut to understand than a loop would be.
Thanks guys for the insights!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 18 May 2023

Commented:

on 18 May 2023

Community Treasure Hunt

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

Start Hunting!