void RunSimulation() { float bestFitness = 0; CreatureNew.Gene bestGene = parentGene; // Find creature with best gene/fitness // then remove population foreach (GameObject c in population) { float fitness = c.GetComponent <CreatureNew>().GetFitness(); if (fitness > bestFitness) { bestFitness = fitness; bestGene = c.GetComponent <CreatureNew>().gene; } Destroy(c); } population.Clear(); Debug.Log("bestfitness: " + bestFitness); // Create new population mutated from best creature/gene from last population for (int i = 0; i < populationSize; i++) { GameObject creature = Instantiate(p_creature, start + new Vector3(4 * i, 0, 0), Quaternion.identity); creature.GetComponent <CreatureNew>().gene = bestGene.Mutate(); population.Add(creature); } }
void Start() { // Creature random parent for initial population parentGene = new CreatureNew.Gene(); parentGene.Init(); InvokeRepeating("RunSimulation", 0, simulationTime); }