use ga optimization but got error for ga process

%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);
----------------------------------but I got these 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);

 Accepted Answer

syms x [1 2]
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;
c=-sin(4*pi*x1)+2*sin(2*pi*x2)^2-1.5;
sol1 = solve(c, x1)
sol1 = 
eqn2 = subs(y, x1, sol1)
eqn2 = 
sol2a = vpasolve(eqn2(1), x2)
sol2a = 
sol2b = vpasolve(eqn2(2), x2)
sol2b = 
0.087919847529159662140915717986356
back1a = vpa(subs(eqn2(1), x2, sol2a))
back1a = 
back1b = vpa(subs(eqn2(2), x2, sol2b))
back1b = 
ya = vpa(subs(y, {x1, x2}, {back1a, sol2a}))
ya = 
yb = vpa(subs(y, {x1, x2}, {back1b, sol2b}))
yb = 
Beyond this, you need to test at least one c value less than that equality, such as c=-sin(4*pi*x1)+2*sin(2*pi*x2)^2-1.6; in case the minima is not right at the constraint bound.

More Answers (1)

Matt J
Matt J on 10 May 2022
Edited: Matt J on 10 May 2022
Perhaps you meant to have,
y=(4*x1.^2)-2.1*x1.^4+1/3*x1.^6+(x1*x2)-4*x1.^2+4*x2.^4;
or
y=(4*x1.^2)-2.1*x1.^4+1/3*x1.^6+(x1*x2)-4*x2.^2+4*x2.^4;

6 Comments

I did what you said but I have 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);
Here's what I'm seeing.
%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);
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
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
how I can fix the plots ,sice they are empty of data
They don't look empty to me. Zoom in on the early part of the plots.
yes,you are right,thank you so much,but is there a way to increase the data on plots?
You have nonlinear constraints. Therefore, the algorithm has few iterations, and plot functions are called just once per iteration. For an explanation of what nonlinear constraints do to the ga algorithm, see Nonlinear Constraint Solver Algorithms. For an example showing this behavior, see Constrained Minimization Using the Genetic Algorithm.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!