필터 지우기
필터 지우기

How to set a target in the Optimisation Toolbox?

조회 수: 3 (최근 30일)
BioZ
BioZ 2021년 11월 18일
편집: Matt J 2021년 11월 18일
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

채택된 답변

Matt J
Matt J 2021년 11월 18일
편집: Matt J 2021년 11월 18일
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
Matt J
Matt J 2021년 11월 18일
편집: Matt J 2021년 11월 18일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2021년 11월 18일
  댓글 수: 1
BioZ
BioZ 2021년 11월 18일
편집: BioZ 2021년 11월 18일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by