Exemplo n.º 1
0
    public Move[] CrossOver(AIIndividual otherParent)
    {
        Move[] childGenes = new Move[Genes.Length];
        for (int i = 0; i < Genes.Length; i++)
        {
            childGenes[i] = Random.value < 0.5 ? Genes[i] : otherParent.Genes[i];
        }

        return(childGenes);
    }
Exemplo n.º 2
0
    private void NewGeneration()
    {
        // Go calculate the fitness
        CalculateFitness();

        //Order population based on fitness
        IOrderedEnumerable <AIIndividual> orderedPopulation = population.Select(value => value).OrderByDescending(individual => individual.Fitness);
        // Find the fittest individual
        AIIndividual fittest = orderedPopulation.ElementAt(0);
        // Find the second fittest individual
        AIIndividual secondFittest = orderedPopulation.ElementAt(1);

        if (bestGenes == null || bestFitness < fittest.Fitness)
        {
            bestGenes      = fittest.Genes;
            bestFitness    = fittest.Fitness;
            bestGeneration = generation;
            bestCleaned    = fittest.cleaned;
        }

        // Create new population
        List <AIIndividual> newPopulation = new List <AIIndividual>();

        for (int i = 0; i < population.Count; i++)
        {
            GameObject   initializedObject = Instantiate(individualObject, spawnLocation, Quaternion.identity);
            AIIndividual child             = initializedObject.GetComponent <AIIndividual>();

            initializedObject.transform.name = GetIndividualName(newPopulation);

            // Crossover fittest with second fittest
            child.Genes = bestGenes.Equals(fittest.Genes) ? fittest.CrossOver(secondFittest) : fittest.CrossOver(bestGenes);

            // Mutate
            child.Mutate(mutationRate);

            // Give copy of the room because they can not clean the same room
            child.RoomClone = CloneRoom();

            // Child is ready!
            child.Initialize();

            // Add child to new population
            newPopulation.Add(child);
        }

        foreach (AIIndividual individual in population)
        {
            Destroy(individual.gameObject);
        }
        population.Clear();
        population = newPopulation;

        generation++;
    }
Exemplo n.º 3
0
    private void StartPopulation()
    {
        for (int i = 0; i < initalPopulationCount; i++)
        {
            GameObject   initializedObject = Instantiate(individualObject, spawnLocation, Quaternion.identity);
            AIIndividual individual        = initializedObject.GetComponent <AIIndividual>();

            initializedObject.transform.name = GetIndividualName(population);

            // Random genes because we have no clue about the room yet
            individual.Genes = CreateRandomGenes();

            // Give copy of the room because they can not clean the same room
            individual.RoomClone = CloneRoom();

            // Individual is ready!
            individual.Initialize();

            population.Add(individual);
        }
    }