# Genetic Algorithm: Failure in initial supplied Fitness Function

6 views (last 30 days)
Ozgun Karatas on 3 Jun 2020
Commented: Ozgun Karatas on 4 Jun 2020
Hello everybody!
I am currently having a problem with a genetic algorithm code i am trying to run.
The following is my genetic algorithm, which calls a specific fitness function. The fitness function determines the fitness value according to the variables.
%% sets up the genetic algorithm function handle for the real fitnessfunction
[v,fval,exitflag,output,population,scores]=ga(@FitnessFunktion,2,[],[],[],[],[0.5 2],[2.5 4],[],options)
The fitness function is as follows: (i have taken out the parts irrelevant to the code) From generationsresults.txt we get the curve and time variables, which are then supplied to the Fitness Function.
function Fitness=FitnessFunktion(x)
%%code snippets to obtain the results have been omitted
fitnessresults=importdata('generationresults.txt');
curve =fitnessresults(:,1);
ttime =fitnessresults(:,2);
Fitness=((curve/sum(curve))+ttime/sum(ttime))/sum(curve/sum(curve)+ttime/sum(ttime));
end
I am able to produce the first generation without any problems but when the GA wants to move on forward with the second generation, i get the following error: I do not know why I cannot create further generations. Any help would be much appreciated, thank you in advance!

Ozgun Karatas on 4 Jun 2020
sorry, i forgot to add the other files! they are attached here!
Main is as follows:
function Main(x)
ParameterDefinition(x);
delete('actuator.*');
system(['abaqus cae noGUI=modellaufbau.py']);
end
ParameterDefinition is as follows:
function ParameterDefinition(x)
%x(1) = inner chamfer
%x(2) = constriction height: engung
if exist(['Variables.py'],'file')==2
delete('Variables.py');
end
file_id = fopen('Variables.py', 'w');
fprintf(file_id,'chamfer = %0.12f\n',x(1));
fprintf(file_id,'engung = %0.12f\n',x(2));
fclose(file_id);
end
A_circFit is as follows:
function [xc,yc,R,a] = A_circFit(x,y)
x=x(:); y=y(:);
a=[x y ones(size(x))]\[-(x.^2+y.^2)];
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
edit: on the first comment regarding fitnesslog = fopen('fitness.txt') i forgot to add an append, it should look like the following;
fitnesslog=fopen('fitness.txt','a');
Stephan on 4 Jun 2020
For me there are a lot of open questions, directly starting at the beginning of FitnessFunktion. It doesnt make sense for me. The size of x will always be 2 - why do you ask for it inside an if-statement?
you might want to send me an email, maybe we can discuss directly in german language.
Ozgun Karatas on 4 Jun 2020
Hey Stephan!
I have sent you an email regarding the question, i can continue answering on the forum if you'd prefer & for other peers' understanding.