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

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 CenterFile Exchange 中查找有关 Problem-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