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 CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by