55 lines
1.2 KiB
Plaintext
55 lines
1.2 KiB
Plaintext
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++;
|
|
}
|
|
} |