Clear Filters
Clear Filters

How to set a target in the Optimisation Toolbox?

5 views (last 30 days)
Greetings, I have the following code which simply finds a value which maximises the area of a square.
Lets say I want the Area to be not maximum but lets say 23. (or any number between 0 and max value). How can I set such a goal ?
Any help would be much appreciated.
Optim.m
clc; clear all; close all
FitnessFunction = @SquareF;
numberOfVariables = 1;
A = []; b = [];
Aeq = []; beq = [];
lb = 0;
ub = 1;
options = optimoptions(@gamultiobj,'PlotFcn',{@gaplotscorediversity,@gaplotstopping,@gaplotspread});
[x,Volume] = gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,ub,options);
formatSpec = 'Max Area is %5f\n';
fprintf(formatSpec,Volume)
and
SquareF.m
function y = SquareF(x)
%y(1) = (x+2)^2 - 10;
Lt = 10; % Total Perimeter Length Available
A = Lt*x(1); % Side A
B = Lt-A; % Side B
y(1) = -A*B; %Area m^2
rL = A+B;
end

Accepted Answer

Matt J
Matt J on 18 Nov 2021
Edited: Matt J on 18 Nov 2021
Lets say I want the Area to be not maximum but lets say 23. (or any number between 0 and max value)
It sounds like you are saying you want to maximize area but with the constraint that the area can be no larger than 23 and the perimeter no greater than Lt. For a square, that's the same as saying that the length of the side x is bounded above by min( sqrt(23), Lt/4).
Lt=15;
lb=0;
ub=min( sqrt(23), Lt/4);
fun=@(x) -x^2; %objective
[x,negativeArea]=fmincon(fun,sqrt(23),[],[],[],[],lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 3.7500
negativeArea = -14.0625
  1 Comment
Matt J
Matt J on 18 Nov 2021
Edited: Matt J on 18 Nov 2021
Alternatively, you might be saying you want to bring the area as close to 23 as possible, but still with the constraint that the perimeter is <=Lt, If so, then that would be as below, but happens to give the same result.
Lt=15;
lb=0;
ub=Lt/4;
fun=@(x) (23-x^2).^2; %objective
x=fmincon(fun,sqrt(23),[],[],[],[],lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 3.7500
Area=x.^2
Area = 14.0625

Sign in to comment.

More Answers (1)

Matt J
Matt J on 18 Nov 2021
  1 Comment
BioZ
BioZ on 18 Nov 2021
Edited: BioZ on 18 Nov 2021
This unfortunately doesnt seem to work. Lets say I put target as 18 then the optimiser does produce an Area of 18 yes, but with this condition my input X is simply 0. Basically my optimiser finds 0 as optimal value X and then adds 18.
I was looking more for a target which would give me a Value of X that would result in Area of for example 18 or as close to it as possible.
clc; clear all; close all
FitnessFunction = @SquareF;
numberOfVariables = 1;
A = []; b = [];
Aeq = []; beq = [];
lb = 0;
ub = 1;
options = optimoptions(@gamultiobj,'PlotFcn',{@gaplotscorediversity,@gaplotstopping,@gaplotspread});
target_fun = @(x) abs(FitnessFunction(x)-18)
[x,Area] = gamultiobj(target_fun,numberOfVariables,[],[],[],[],lb,ub,options);
formatSpec = 'Max Area is %5f\n';
fprintf(formatSpec,Area)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!