Problem with Genetic algorithm fitness function

2 views (last 30 days)
Hi everybody I want to use genetic algorithm for optimizing 24 variable. The problem is this:
I implemented the code in this way:
if true
fitnessfunc=@fitness;
numberofvariables=24;
lb=zeros(1,24);
ub=[350 350 350 350 350 350 350 350 350 350 350 350 51 51 51 51 51 51 51 51 51 51 51 51];
[x,fval]=gamultiobj(@fitness,numberofvariables,[],[],[],[],lb,ub);
function C=fitness(Y)
x0=[3.2*10^5 10^10 0];
tspan=[0 350];
[t,y]=ode45(@(t,y) equations(t,y,Y),tspan,x0);
C=sum(y(:,2).^2)^2+y(350,2)^2+max(y(:,2))^2;
end
function dy = equations(t,y,glob)
%initial parameters value
dy = zeros(3,1);
s=1.3*10^4;
p=0.1245;
g=2.019*10^7;
m=3.422*10^(-10);
d=0.0412;
Mmax=50;
b=2.0*10^(-9);
a=0.18;
n=1.01*10^(-7);
Mth=10;
w=0.27;
o=0;
k=8.4*10^(-3);
time=glob(1:12);
u=glob(13:24);
delta=dirac(t-time)';
dose=u*delta;
%E=y1 , t=y2 , m=y3 landa=w
dy(1) = s+((p*y(1)*y(2))/(g+y(2)))-m*y(1)*y(2)-d*y(1)+o-k*((1-exp(-y(3)))*heaviside(y(3)-Mmax));
dy(2) = a*y(2)*(1-b*y(2))-n*y(1)*y(2)-k*((y(3)-Mth)*heaviside(y(3)-Mth));
dy(3) = dose-w*y(3);
end
end
I got the error. Is there any suggestion?
Thanks
  2 Comments
Alan Weiss
Alan Weiss on 14 Jan 2015
Please give us the MATLAB result, I mean the exact error message.
Alan Weiss
MATLAB mathematical toolbox documentation
mostafa
mostafa on 14 Jan 2015
if true
Attempted to access y(350,2); index out of bounds because size(y)=[137,3].
Error in fitness (line 5)
C=sum(y(:,2).^2)^2+y(350,2)^2+max(y(:,2))^2;
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in fcnvectorizer (line 14)
y(i,:) = feval(fun,(pop(i,:)));
Error in gamultiobjMakeState (line 52)
Score =
fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,numObj,option s.SerialUserFcn);
Error in gamultiobjsolve (line 11)
state = gamultiobjMakeState(GenomeLength,FitnessFcn,output.problemtype,options);
Error in gamultiobj (line 235)
[x,fval,exitFlag,output,population,scores] = gamultiobjsolve(FitnessFcn,nvars, ...
Error in muobga (line 6)
[x,fval]=gamultiobj(@fitness,numberofvariables,[],[],[],[],lb,ub);
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!