IA_PathFinder/Genetic.pde

55 lines
1.2 KiB
Plaintext
Raw Permalink Normal View History

2022-05-07 12:08:13 +00:00
class Genetic{
int generation=1;
boolean display=false;
Population population;
int lessSteps=0;
float lessDistance=0;
float maxFitness=0;
boolean goalReached=false;
//constructor
Genetic(boolean disp){
population=new Population(500);
display=disp;
}
//check if finished with current population
boolean isDone(){
return !population.hasDotAlive();
}
void toggleDisplay(){
display=!display;
}
//update population
void update(){
if(!isDone()){
population.update();
if(display){
population.display();
}
}
}
//generate next population
void nextGeneration(){
//claculate the fitness of points
population.calcFitness();
//get best datas
lessSteps=population.getChampion().brain.step;
lessDistance=dist(population.getChampion().pos.x, population.getChampion().pos.y, goal.x, goal.y);
maxFitness=population.getChampion().fitness;
goalReached=population.getChampion().reachedGoal;
//naturally select best dots
population.naturalSelection();
//mutate the next generation
population.mutate();
//increment counter
generation++;
}
}