Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (90 sloc) 3.05 KB
class Population {
float mutationRate; // Mutation rate
LetterPoints[] population;
ArrayList<LetterPoints> matingPool; // ArrayList which we will use for our "mating pool"
int generations; // Number of generations
boolean clicked;
Population(float m, int num) {
mutationRate = m;
population = new LetterPoints[num];
matingPool = new ArrayList<LetterPoints>();
generations = 0;
for (int i = 0; i < population.length; i++) {
population[i] = new LetterPoints(new DNA(), 250*i + 30, 400);
}
}
void display() {
for (int i = 0; i < population.length; i++) {
//translate(200, 0);
population[i].display();
}
}
// // Are we rolling over any of the faces?
// void clicked(int mx, int my) {
// for (int i = 0; i < population.length; i++) {
// population[i].clicked(mx, my);
// }
// }
// Are we rolling over any of the faces?
void rollover(int mx, int my) {
for (int i = 0; i < population.length; i++) {
population[i].rollover(mx, my);
}
}
// Generate a mating pool
void selection() {
// Clear the ArrayList
matingPool.clear();
// Calculate total fitness of whole population
float maxFitness = getMaxFitness();
// Calculate fitness for each member of the population (scaled to value between 0 and 1)
// Based on fitness, each member will get added to the mating pool a certain number of times
// A higher fitness = more entries to mating pool = more likely to be picked as a parent
// A lower fitness = fewer entries to mating pool = less likely to be picked as a parent
for (int i = 0; i < population.length; i++) {
float fitnessNormal = map(population[i].getFitness(), 0, maxFitness, 0, 1);
int n = (int) (fitnessNormal * 100); // Arbitrary multiplier
for (int j = 0; j < n; j++) {
matingPool.add(population[i]);
}
}
}
// Making the next generation
void reproduction() {
// Refill the population with children from the mating pool
for (int i = 0; i < population.length; i++) {
// Sping the wheel of fortune to pick two parents
int m = int(random(matingPool.size()));
int d = int(random(matingPool.size()));
// Pick two parents
LetterPoints mom = matingPool.get(m);
LetterPoints dad = matingPool.get(d);
// Get their genes
DNA momgenes = mom.getDNA();
DNA dadgenes = dad.getDNA();
// Mate their genes
DNA child = momgenes.crossover(dadgenes);
// Mutate their genes
child.mutate(mutationRate);
// Fill the new population with the new child
population[i] = new LetterPoints(child, 250*i+ 30, 400);
}
generations++;
}
int getGenerations() {
return generations;
}
// Find highest fintess for the population
float getMaxFitness() {
float record = 0;
for (int i = 0; i < population.length; i++) {
if (population[i].getFitness() > record) {
record = population[i].getFitness();
}
}
return record;
}
// void released() {
// clicked = false;
// }
}