// Create a new face public Face(Dnaface dna_, float x_, float y_) { dna = dna_; x = x_; y = y_; fitness = 1; r = new Rect((int)(x - wh / 2), (int)(y - wh / 2), (int)(wh), (int)(wh)); }
// Crossover // Creates new DNA sequence from two (this & public Dnaface crossover(Dnaface partner) { float[] child = new float[genes.Length]; int crossover = (int)(Random.Range(0, genes.Length)); for (int i = 0; i < genes.Length; i++) { if (i > crossover) { child[i] = genes[i]; } else { child[i] = partner.genes[i]; } } Dnaface newgenes = new Dnaface(child); return(newgenes); }
// Making the next generation public 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.Range(0, matingPool.Count)); int d = (int)(Random.Range(0, matingPool.Count)); // Pick two parents Face mom = matingPool[m]; Face dad = matingPool[d]; // Get their genes Dnaface momgenes = mom.getDNA(); Dnaface dadgenes = dad.getDNA(); // Mate their genes Dnaface child = momgenes.crossover(dadgenes); // Mutate their genes child.mutate(mutationRate); // Fill the new population with the new child population[i] = new Face(child, 50 + i * 75, 60); } generations++; }