フィルターのクリア

Modifying the objective function in order to meet the optimization variable boundaries

2 ビュー (過去 30 日間)
First Try
if (A <= 1e-04) & (A >= 4e-04)
fun = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2;
else
fun = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2 + 1e30 * ( (A - 4e-04).^2 + (A - 1e-04).^2 );
or second try
for i = 1:size(A,2)
if (A(i) >= 1e-04) & (A(i) <= 4e-04)
fun(i) = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2;
else
fun(i) = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2 - 1e30 * ( (A(i) - 4e-04).^2 + (A(i) - 1e-04).^2 );
end
end
Hello,
I wrote above a piece of a code where fun is an objective function that I aim to minimize. And A, an array of size (1,13), is the optimization variable. f is also an array of size(1,13) and my aim is to tune its first two values f(1) and f(2) where the values of f depend on A. The ultimate goal is to optimize A such that f(1) and f(2) obtain the specific values 500 and 1800 respectively, and this has to be done by minimizing the value of objective fucntion where its return value is a scalar.
I needed to impose my conditions that if A lies outisde the specified boundaries then the value of the objective function function will decrease rapidly depending on the distance of this variable from the boundaries.
The displayed error in Matlab is "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-13." of the first try.
The second gave unlogical result.
I'm not knowing how to fix it.
Thank you.

採用された回答

Zahraa
Zahraa 2024 年 2 月 16 日
My professor at university helped, and this is how it should work:
fun = (1- (f(1))/500)^2 + (1- (f(2))/1000)^2;
for i = 1:length(A)
if (A(i) <= 0.5e-04)
fun = fun + 1e10 * (A(i) - 0.5e-04).^2 ;
elseif (A(i) >= 6e-04)
fun = fun + 1e10 * (A(i) - 6e-04).^2 ;
end
end
  2 件のコメント
Matt J
Matt J 2024 年 2 月 16 日
編集済み: Matt J 2024 年 2 月 16 日
That's the same as my answer. You don't need the loop.
Torsten
Torsten 2024 年 2 月 16 日
Why don't you simply use lower and upper bounds as 0.5e-4 and 6e-4 for the A(i) in your optimization ?

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

その他の回答 (2 件)

Torsten
Torsten 2024 年 2 月 15 日
移動済み: Torsten 2024 年 2 月 15 日
if (A <= 1e-04) & (A >= 4e-04)
How can A be <= 1e-4 and >= 4e-4 at the same time ?
And if-statements have to be done elementwise:
for i = 1:size(A,2)
if A(i) ...
f(i) = ...
else
f(i) = ...
end
end
  5 件のコメント
Zahraa
Zahraa 2024 年 2 月 16 日
Also this didn't work
fun = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2;
for i = 1:length(A)
if (A(i) <= 1e-04)
fun = fun + 1e10 * (A(i) - 1e-04).^2 ;
elseif (A(i) >= 4e-04)
fun = fun + 1e10 * (A(i) - 1e-04).^2 ;
end
end
Torsten
Torsten 2024 年 2 月 16 日
f is also an array of size(1,13) and my aim is to tune its first two values f(1) and f(2) where the values of f depend on A.
In your objective function, compute f(1) and f(2) from your optimization variables A and return (f(1)-500)^2 + (f(2)-1800)^2.

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


Matt J
Matt J 2024 年 2 月 16 日
編集済み: Matt J 2024 年 2 月 16 日
If the boundaries are given by simple lower and upper bound vectors lb and ub, you can do,
function fun=objective(A,lb,ub)
arguments
A (1,:)
lb (1,:)
ub (1,:)
end
Athresh=max(min(A,ub),lb);
distance=norm(A-Athresh);
f=...some function of A....
fun = (1- (f(1))/500)^2 + (1- (f(2))/1800)^2 + 1e30*distance^2;
end

カテゴリ

Help Center および File ExchangeProblem-Based Optimization Setup についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by