I try to write simple analytical function Gomez and Levy with ga in optimization toolbox but it cannot run

2 ビュー (過去 30 日間)
%fitness function Gomez and Levy
function y=fitnessfunctiongl(x)
x1=x(1);
x2=x(2);
y=(4*x1.^2)-2.1*x1.^4+1/3*x1.^6+(x1*x2)-4*x2.^2+4*x2.^4;
end
¬¬¬¬¬%constraints
function [c ,ceq]=constraintgl(x)
x1=x(1);
x2=x(2);
c=-sin(4*pi*x1)+2*sin(2*pi*x2)^2-1.5;
ceq=[];
end
%main script to pass function and constraints to ga for optimization
ObjFcn=@fitnessfunctiongl;
nvars=2;
lb=[-1, -1];
ub=[0.75, 1];
nonlcon=@constraintgl;
options = optimoptions('ga','ConstraintTolerance',1e-6,'Display','iter','PlotFcn',{@gaplotrange,@gaplotbestf,@gaplotselection,@gaplotmaxconstr}); %plotting the process of finding the solution
%solution and function value
[x , fval,exitFlag,output,population,scores]= ga(ObjFcn,nvars,[],[],[],[],lb,ub,nonlcon,options);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%errors
Error in gadsplot (line 141)
[state,optimvalues] = callOnePlotFcn(fname,plotNames{i},state,options.OutputPlotFcnOptions,optimvalues,'init',args{i}{:});
Error in gacon (line 55)
state = gadsplot(options,state,'init','Genetic Algorithm');
Error in ga (line 406)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
Error in maingl (line 11)
[x , fval,exitFlag,output,population,scores]= ga(ObjFcn,nvars,[],[],[],[],lb,ub,nonlcon,options);
  2 件のコメント
sogol bandekian
sogol bandekian 2022 年 5 月 10 日
Error using makeState (line 61)
Your fitness function must return a scalar value.
Error in gacon (line 44)
state = makeState(GenomeLength,SubFitness,Iterate,subtype,options);
Error in ga (line 406)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
Error in maingl (line 11)
[x , fval,exitFlag,output,population,scores]= ga(ObjFcn,nvars,[],[],[],[],lb,ub,nonlcon,options);

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2022 年 5 月 10 日
y=(4*x1.^2)-2.1*x1.^4+1/3*x1.^6+(x1*x2)-4*x.^2+4*x2.^4;
Notice that you have 4*x.^2 . But x is a vector so that would return a vector.
  6 件のコメント
sogol bandekian
sogol bandekian 2022 年 5 月 22 日
I did this in MATLAB online but the plot are empty.how I can fix this?
Walter Roberson
Walter Roberson 2022 年 5 月 24 日
The plots are not empty. The optimization is configured for a maximum number of generations and that is the x axis used so that it does not get continually redrawn as more generations are processed. The optimization either succeeded or failed in a small number of generations, on the order of 5 generations.

サインインしてコメントする。

その他の回答 (1 件)

Bhavana Ravirala
Bhavana Ravirala 2022 年 5 月 10 日
Hi Sogol,
When I try to reproduce the issue at my end I encountered the error as Function definition not supported in this context. Create functions in a code file. If you are facing the same error refer to the below link to eliminate the error.
If not, can you send us the exact error message that you are getting.
Hope this helps!!
  1 件のコメント
sogol bandekian
sogol bandekian 2022 年 5 月 10 日
hello thank you for reply.these below errors are what I got
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Error using makeState (line 61)
Your fitness function must return a scalar value.
Error in gacon (line 44)
state = makeState(GenomeLength,SubFitness,Iterate,subtype,options);
Error in ga (line 406)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
Error in maingl (line 11)
[x , fval,exitFlag,output,population,scores]= ga(ObjFcn,nvars,[],[],[],[],lb,ub,nonlcon,options);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by