Effacer les filtres
Effacer les filtres

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

5 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Zahraa
Zahraa le 16 Fév 2024
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 commentaires
Matt J
Matt J le 16 Fév 2024
Modifié(e) : Matt J le 16 Fév 2024
That's the same as my answer. You don't need the loop.
Torsten
Torsten le 16 Fév 2024
Why don't you simply use lower and upper bounds as 0.5e-4 and 6e-4 for the A(i) in your optimization ?

Connectez-vous pour commenter.

Plus de réponses (2)

Torsten
Torsten le 15 Fév 2024
Déplacé(e) : Torsten le 15 Fév 2024
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 commentaires
Zahraa
Zahraa le 16 Fév 2024
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 le 16 Fév 2024
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.

Connectez-vous pour commenter.


Matt J
Matt J le 16 Fév 2024
Modifié(e) : Matt J le 16 Fév 2024
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

Catégories

En savoir plus sur Problem-Based Optimization Setup dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by