I have a very big code with multiple classes which creates a simulation of artificial creatures. It contains an evolution algorithm which at the end of each generation saves several copies of the best individuals and then recreates an entirely new generation using their neural networks with added mutations. On the creation of a new generation, the old one is wiped. However, with each generation, it loads the next generation slower practically stopping on gen 4 or 5 (the number of agents doesn't change from generation to generation). At that point, it just basically freezes after I call the "evolveWorkers()" function. Is there any tool which I can use in order to find the function on which it gets stuck?
Thanks in advance to anyone who replies.
And in case you can help me, here are some bits of code which I think might be creating a problem, if you want a function of something in particular I am happy to share it too.:
void evolveWorkers() {
int j = 0;
ArrayList <Alien> elites = new ArrayList <Alien>(); //creating an array list where to store the best fit
elites = returnFit();
for ( int i = 0; i< aliens.size(); i++) {
if ( aliens.get(i) instanceof Worker) {
aliens.get(i).dead = true; //marks the old aliens dead so they get removed
}
}
for ( int i = 0; i< 20; i++) {
aliens.add(new Worker(width/2, height/2, elites.get(j).net));
j++;
if (j >= elites.size()) {
j = 0;
}
}
}
}
ArrayList <Alien> returnFit() {
ArrayList <Alien> workers; //array to pick the workers out of all aliens
ArrayList <Alien> elites;
elites = new ArrayList<Alien>();
workers = new ArrayList <Alien>();
for ( int i = 0; i < aliens.size(); i++) {
Alien m = aliens.get(i);
if ( m instanceof Worker) {
workers.add(m);
}
Collections.sort(workers, new Sortbyfitness());
}
for (int i = workers.size()-1; i >= workers.size()-1 - int(workers.size()* elitesPercent/100); i --) {
elites.add(workers.get(i));
}
return elites;
}
//remove the dead aliens
void clearDead() {
for (int i = 0; i< aliens.size(); i++) {
Alien a = aliens.get(i);
if (a.dead) {
aliens.remove(a);
a.removeObserver(tutorial); //important to remove the observers in order to preserve framerate
}
}
}
I suspect that the agents are not really wiped at the new gen so the program somehow needs to go through all of them and the data gets accumulated somewhere... I am not sure.