// Making the next generation public void Reproduction() { // Refill the population with children from the mating pool for (int i = 0; i < population.Length; i++) { // Destroy all rockets in population population[i].Death(); // Spin the wheel of fourtune to pick two new parents int m = Random.Range(0, matingPool.Count); int d = Random.Range(0, matingPool.Count); // Pick two parents Chapter9Fig3Rocket mom = matingPool[m]; Chapter9Fig3Rocket dad = matingPool[d]; // Get their genes Chapter9Fig3DNA momGenes = mom.DNA; Chapter9Fig3DNA dadGenes = dad.DNA; // Mate their genes Chapter9Fig3DNA child = momGenes.Crossover(dadGenes); // Mutate their genes child.Mutate(mutationRate); // Fill the new population with the new child Vector2 position = new Vector2(0, -screenSize.y); population[i] = new Chapter9Fig3Rocket(rocketObject, position, child, target, population.Length); } Generations++; }
// Initialize the population public Chapter9Fig3Population(GameObject rocketObj, Vector2 screen, float mutation, int numberOfRockets, float lifeTime, Chapter9Fig3Obstacle obTarget) { rocketObject = rocketObj; mutationRate = mutation; population = new Chapter9Fig3Rocket[numberOfRockets]; matingPool = new List <Chapter9Fig3Rocket>(); Generations = 0; screenSize = screen; target = obTarget; // Making a new set of creatures for (int i = 0; i < population.Length; i++) { Vector2 position = new Vector2(0, -screenSize.y); population[i] = new Chapter9Fig3Rocket(rocketObj, position, new Chapter9Fig3DNA(lifeTime), target, population.Length); } }