Clear Filters
Clear Filters

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

2 views (last 30 days)
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.

Accepted Answer

Zahraa
Zahraa on 16 Feb 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

More Answers (2)

Torsten
Torsten on 15 Feb 2024
Moved: Torsten on 15 Feb 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 Comments
Zahraa
Zahraa on 16 Feb 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 on 16 Feb 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.

Sign in to comment.


Matt J
Matt J on 16 Feb 2024
Edited: Matt J on 16 Feb 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

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!