GameObject Breed(GameObject parent1, GameObject parent2) { GameObject offspring = CreateNewBot(); MovementBrain brain = offspring.GetComponent <MovementBrain>(); if (Random.Range(0, 100) == 1) { brain.Init(); brain.dna.Mutate(); } else { brain.Init(); brain.dna.Combine(parent1.GetComponent <MovementBrain>().dna, parent2.GetComponent <MovementBrain>().dna); } return(offspring); }
public void NextGeneration() { StopCoroutine("WaitTillNext"); generation++; float[][] newPop = new float[populationSize][]; List <MovementBrain> probPool = CreatePool(); for (int i = 0; i < populationSize; i++) { MovementBrain parent1 = probPool[Random.Range(0, probPool.Count)]; MovementBrain parent2 = probPool[Random.Range(0, probPool.Count)]; float[] gene = new float[numOfWB]; Debug.Log(numOfWB); for (int j = 0; j < numOfWB; j++) { if (Random.Range(0f, 1f) < mutationRate) { gene[j] = Random.Range(parent1.minNum, parent1.maxNum); } else { if (Random.Range(0, 2) == 1) { gene[j] = parent1.WandB[j]; } else { gene[j] = parent2.WandB[j]; } } } newPop[i] = gene; } for (int i = 0; i < populationSize; i++) { population[i].RemoveFromScene(); GameObject ball = (GameObject)Instantiate(redBall, transform.position, Quaternion.identity, ballParent); population[i] = ball.GetComponent <MovementBrain>(); population[i].SetWeightsandBiases(newPop[i]); population[i].ComputeOutput(); } StartCoroutine("WaitTillNext"); }