Example #1
0
    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);
        }
    }
Example #2
0
    void Start()
    {
        // Creature random parent for initial population
        parentGene = new CreatureNew.Gene();
        parentGene.Init();

        InvokeRepeating("RunSimulation", 0, simulationTime);
    }