// 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 Chapter9Fig2Rocket mom = matingPool[m]; Chapter9Fig2Rocket dad = matingPool[d]; // Get their genes Chapter9Fig2DNA momGenes = mom.DNA; Chapter9Fig2DNA dadGenes = dad.DNA; // Mate their genes Chapter9Fig2DNA 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 Chapter9Fig2Rocket(rocketObject, position, child, targetPosition); } Generations++; }
// Initialize the population public Chapter9Fig2Population(GameObject rocketObj, Vector2 screen, float mutation, int numberOfRockets, float lifeTime, Vector2 targetPos) { rocketObject = rocketObj; mutationRate = mutation; population = new Chapter9Fig2Rocket[numberOfRockets]; matingPool = new List <Chapter9Fig2Rocket>(); Generations = 0; screenSize = screen; targetPosition = targetPos; // Making a new set of creatures for (int i = 0; i < population.Length; i++) { Vector2 position = new Vector2(0, -screenSize.y); population[i] = new Chapter9Fig2Rocket(rocketObj, position, new Chapter9Fig2DNA(lifeTime), targetPos); } }