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.

 採用された回答

Matt J
Matt J 2023 年 5 月 18 日
編集済み: Matt J 2023 年 5 月 18 日

0 投票

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

Walter Roberson
Walter Roberson 2023 年 5 月 18 日
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 2023 年 5 月 18 日
編集済み: Matt J 2023 年 5 月 18 日
Yep, you're right. I fixed it, though I find it preferable to avoid indexing fixed parameters in anonymous function definitions.
Rifat
Rifat 2023 年 5 月 18 日
Thanks Matt and Walter. Both solutions work. However, I would still prefer to do the optimization without a for loop. Do you recommend arrayfun?
Walter Roberson
Walter Roberson 2023 年 5 月 18 日
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 2023 年 5 月 18 日
編集済み: Matt J 2023 年 5 月 18 日
Do you recommend arrayfun?
arrayfun is just different syntax for a for-loop. There's no reason to prefer it.
Walter Roberson
Walter Roberson 2023 年 5 月 18 日
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.
Rifat
Rifat 2023 年 5 月 18 日
Thanks guys for the insights!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSystems of Nonlinear Equations についてさらに検索

質問済み:

2023 年 5 月 18 日

コメント済み:

2023 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by