Genetic Algorithm: Failure in initial supplied Fitness Function

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!

5 comentarios

Please attach all files and all code needed to reproduce the error. Without we can not help.
Hey Stephan, although this is my first question on matlab answers, i get this error that says i cannot attach any files because i exceeded the daily upload limit. I am writing the files as code snippets here.
first some explanations:
  • in this GA application, I am trying to run an abaqus job, in which i analyse an actuator's ability to curve with a given force. From abaqus, I obtain the displacement of the actuator and the with a circle approximation i find the radius of the circle, which corresponds to the displacement. Additionally, i obtain the current time of the simulation, which tells me how far the actuator has bent with the supplied force.
  • Curvature and Time are the components that comprise the fitness function. However, my variables within the GA are not these components. My variables are the chamfer radius and the constriction height of a certain part within the abaqus model. I have isolated these models within the variables.py file.
  • Here is a quick run-down as to how the whole code works. ps: i am running this code on my university's cluster computing system, hence the parfors.
  • first, geneticalgorithm.m is called.
  • within geneticalgorithm.m, the GA is set up with the fitness function.
  • fitness function calls Main()
  • Main sets up an abaqus job, for which the parameters are defined within Main()
  • Following this, we have a for loop in which the abaqus job is being assessed for its stress values. after the assessment is done and the job is either successfully finised or stopped due to the fact that a stress value has been exceeded, we call Auwertung(), which now produces the curvature and time values i previously discussed. These values are written on txt files.
  • Finally, the values that have been read from curvature and time files are used for the fitness function to determine the fitness value.
  • PS: for one generation, all the files work normally, I can obtain values without any syntax problems.
Genetic Algorithm is as follows:
%% sets up the genetic algorithm function handle for the real fitnessfunction
clear
rng(now)
parpool(4);
options = optimoptions('ga');
options = optimoptions(options,'MaxGenerations',2);
options = optimoptions(options,'MaxStallGenerations',5);
options = optimoptions(options,'PopulationSize',2);
[v,fval,exitflag,output,population,scores]=ga(@FitnessFunktion,2,[],[],[],[],[0.5 2],[2.5 4])
Fitness Function is as follows:
function Fitness=FitnessFunktion(x)
psize=size(x);
popsize=psize(1);
x;
if numel(x)==1
Fitness=zeros(popsize,1);
return
else
y = zeros(popsize,2);
end
for ii=1:popsize
cd('/bigwork/nhkwkara/GA_DEBUGGER');
folder=strcat('abaqus',num2str(ii));
mkdir(folder)
copyfile('Auswertung.py',[folder]);
copyfile('modellaufbau.py',[folder]);
copyfile('ParameterDefinition.m',folder);
copyfile('Main.m',folder);
copyfile('A_circFit.m',folder);
copyfile('Auswertung.m',[folder]);
copyfile('stressfinder.py',[folder]);
cd(folder);
Main(x(ii,:));
end
parfor ii=1:popsize
cd('/bigwork/nhkwkara/GA_DEBUGGER');
folder=strcat('abaqus',num2str(ii));
cd(folder);
mypath =pwd
cd(mypath);
system(['abaqus analysis noGUI input=actuator.inp job=actuator cpus=4 background']);
pause(360)
while true
cd(mypath);
system(['abaqus job=actuator suspend']);
pause(3)
system(['abaqus cae noGUI=stressfinder.py']);
pause(30)
if isfile('flag.txt')
system(['abaqus job=actuator terminate']);
break;
end
system(['abaqus job=actuator resume'])
pause(180)
end
Auswertung(ii,x(ii,:));
end
cd('/bigwork/nhkwkara/GA_DEBUGGER');
for i=1:popsize
direc=strcat('abaqus',num2str(i));
imported=importdata([direc,'/results.txt']);
fid = fopen('generationresults.txt', 'a');
%write the row of results
fprintf(fid,'%0.12f ',imported(1,:));
fprintf(fid,'\r\n');
fclose(fid);
fid2 = fopen('extractedresults.txt', 'a');
%write the row of results
fprintf(fid2,'%0.12f ',imported(1,:));
fprintf(fid2,'\r\n');
fclose(fid2);
%erase procedure
direc=strcat('abaqus',num2str(i));
cd('/bigwork/nhkwkara/GA_DEBUGGER');
rmdir(['/bigwork/nhkwkara/GA_DEBUGGER/',direc],'s')
end
%determine the fitness value
fid = fopen('extractedresults.txt', 'a');
fprintf(fid,'\r\n');
fprintf(fid,'\r\n');
fclose(fid);
fitnessresults=importdata('generationresults.txt');
curve =fitnessresults(:,1);
ttime =fitnessresults(:,2);
Fitness=((curve/sum(curve))+ttime/sum(ttime))/sum(curve/sum(curve)+ttime/sum(ttime));
delete('generationresults.txt');
%record the fitness values
fitnesslog= fopen('fitness.txt');
fprintf(fitnesslog,'%0.12f',Fitness);
fprintf(fitnesslog,'\n');
fclose(fitnesslog);
end
Auswertung (evaluation) is as follows:
function [currenttime,curvature]=Auswertung(i,x)
system(['abaqus cae noGUI=Auswertung.py']);
y_disp_data=zeros(41,3);
%dummy x values to test the code
%x=[1 2];
if exist(['displacement-x.txt'],'file')==2
displacement_y = strcat('displacement-x.txt');
B=importdata(displacement_y);
%the first column shows the true displacement
%thats why we access the second column
y_disp_data(:,1)=B.data(:,2);
x1=1:1:40;
x2=x1';
%transpose step, the columns are exactly the same as the upper for-loop
column_y=y_disp_data(x2,1);
truedistancey=B.data(x2,1);
coordsy=truedistancey+column_y;
[~,~,radius,~] = A_circFit(coordsy,column_y);
curvature = 1/radius;
conc=strcat('Time.txt');
currenttime=importdata(conc);
end
fid = fopen('results.txt', 'a');
fprintf(fid,'%0.12f ',curvature);
fprintf(fid,'%0.12f\r\n',currenttime);
fclose(fid);
end
If you'd like to have them as real files, i can wait one day to see if the daily upload limit error vanishes and try to upload them tomorrow.
Many thanks in advance!
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
Stephan el 4 de Jun. de 2020
Editada: Stephan el 4 de Jun. de 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.
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.

Iniciar sesión para comentar.

Respuestas (0)

Productos

Versión

R2020a

Preguntada:

el 3 de Jun. de 2020

Comentada:

el 4 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by