Error in the codes. Please help

Error: File: Fitnessfunc_g.m Line: 17 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function
"Fitnessfunc_g".)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore =
FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 18)
state =
makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] =
galincon(FitnessFcn,nvars, ...
Error in GAgear (line 12)
[x,fval,exitflag,output]= ga(ObjectiveFunction,nvars,[],[],LB,UB);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA
cannot continue.
The codes wriiten are as follows
We=(3.14*rho)*{x*m^2*z1*(1+a^2)-(D1^2-d0^2)*(l-bw)-n*dp^2*bw-(d1^2+d2^2)*x}/4000;\\objective function
function GAgear
clc
D=Data_g;
nvars=1;
LB=20;
UB=40;
ObjectiveFunction=@Fitnessfunc_g;
%ConstraintFunction=@Constraintfunc;
options=gaoptimset('StallGenLimit',4,'PopulationSize',10);
disp('Optimization in progress.....');
% Run GA
[x,fval,exitflag,output]= ga(ObjectiveFunction,nvars,[],[],LB,UB);
% Display optimization results
disp('Optimization results:')
fprintf('Weight of best solution is: %6.8g\n',fval)
%fprintf('Number of function evaluations: %d\n',Z)
disp('Best solution:')
disp(x')

6 comentarios

Please show the code for Fitnessfunc_g
Note that your code
We=(3.14*rho)*{x*m^2*z1*(1+a^2)-(D1^2-d0^2)*(l-bw)-n*dp^2*bw-(d1^2+d2^2)*x}/4000;\\objective function
attempts to multiply a cell array by a value. {} in MATLAB creates a cell array rather than acting as a layer of (). Cell arrays cannot be multiplied.
Paridhi Rai
Paridhi Rai el 30 de Sept. de 2015
Editada: Walter Roberson el 30 de Sept. de 2015
Sir, thanks for your reply. But still I am struggling.
My full code for Fitnessfunc_g is
function y = Fitnessfunc_g(x)
function y = Fitnessfunc_g(x)
We=0;
lw=2.5*m;
Dr = m*(a*z1 - 2.5);
Di= Dr-2*lw;
bw=3.5*m;
d0=d2+25;
dp=0.25*(Di-d0);
z2=(z1*D2)/D1;
D1=m*z1;
D2=a*m*z1;
N2=N1/a;
z2=(z1*D2)/D1;
b3=(4.97*(10^6)*H)/(N1*gamma);
b4=(4.97*(10^6)*H)/(N2*gamma);
We=(3.14*rho)*(x*m^2*z1*(1+a^2)-(D1^2-d0^2)*(l-bw)-n*dp^2*bw-(d1^2+d2^2)*x)/4000;
end
y=We;
Please help me.
the error given is
Error: File: Fitnessfunc_g.m Line: 19 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function
"Fitnessfunc_g".)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore =
FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 18)
state =
makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] =
galincon(FitnessFcn,nvars, ...
Error in GAgear (line 12)
[x,fval,exitflag,output,scores]=
ga(ObjectiveFunction,nvars,[],[],LB,UB);
Paridhi Rai
Paridhi Rai el 30 de Sept. de 2015
Editada: Walter Roberson el 30 de Sept. de 2015
Now I have made some changes in my codes, the error is
Error using ga (line 316)
GA requires the following inputs to be of data type double: 'lb'.
Error in GAgear (line 12)
[x,fval]= ga(ObjectiveFunction,nvars,[],[],[],[],options)
I can not understand the values given for lb? Help...
Walter Roberson
Walter Roberson el 30 de Sept. de 2015
It looks to me as if your "options" is not a structure and so is not being recognized as options. You did not happen to show the code that constructed your options.
Paridhi Rai
Paridhi Rai el 1 de Oct. de 2015
Sir, I didnot get it. Please help me with this. If I didnt put the options then the error will be like the previous one.
Walter Roberson
Walter Roberson el 1 de Oct. de 2015
I was mistaken about the problem possibly being options not being a structure.
I do not know what the problem is.

Iniciar sesión para comentar.

Respuestas (3)

Aliyu Bagudu
Aliyu Bagudu el 5 de Abr. de 2016

1 voto

I had the same problem however, I was able figure out where the problem actually comes from. The problem comes from the formula GA uses to calculate the number children of the next generation and thus the number of selected parents. There are 3 types of children in GA: Elite children(specified by EliteCount), Crossover children(specified by crossoverFraction) and calculated as crossoverFraction*(populationSize - EliteCount), and mutation children (calculated using populationSize - EliteCount - crossover children). You don't specify number of mutation children, it is calculated.
So the problem comes from rounding when calculation crossover fraction. The rounding is done to the nearest integer. So for some combination of populationSize, crossoverFraction, and EiltCount, this can result in total number of children that is not equal to populationSize.
Number of parents is 2 times number of children for crossover children. For mutation number of parents is equal to number of children.
So do your calculations and make sure populationSize stay fixed.
Jan
Jan el 1 de Oct. de 2015
Editada: Jan el 1 de Oct. de 2015

0 votos

The command ga: input argument LB expects the 7.th input argument to be a double vector. But in your case it is a struct.
Sanabil
Sanabil el 25 de Mayo de 2023

0 votos

child=mutate(child);
i got an error in this can u tell why

Preguntada:

el 30 de Sept. de 2015

Respondida:

el 25 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by