Exemple #1
0
    public List <MapIndividual> EvolveTestChunkOnRight(List <MapIndividual> pop = null)
    {
        List <MapIndividual> population;

        if (pop == null)
        {
            population = createRightPopulation();
        }
        else
        {
            population = pop;
        }

        // Evolve over generations
        int numberOfChildren = (int)((1 - elitism) * (float)population.Count);

        MapIndividual[] children = new MapIndividual[numberOfChildren];

        for (int i = 0; i < 1; i++)
        {
            // Sort population
            population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));

            // Crossover
            for (int ci = 0; ci < numberOfChildren; ci++)
            {
                // Pick parents
                var parent1 = pickParentFromPopulation(population);
                var parent2 = pickParentFromPopulation(population);
                // Crossover parents
                MapIndividual newChild = crossover(parent1, parent2);
                children[ci] = newChild;
            }

            for (int ci = 0; ci < numberOfChildren; ci++)
            {
                population[ci] = children[ci];
            }

            // Mutation
            mutatePopulation(population);
        }

        // Update world matrix with best individual
        MapIndividual bestIndividual = population[0];

        foreach (MapIndividual individual in population)
        {
            if (individual.GetFitness() > bestIndividual.GetFitness())
            {
                bestIndividual = individual;
            }
        }

        population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));

        return(population);
    }
Exemple #2
0
    public void GenerateChunksOnRight(int numChunks)
    {
        for (int chunkIndex = 0; chunkIndex < numChunks; chunkIndex++)
        {
            List <MapIndividual> population = createRightPopulation();

            // Evolve over generations
            int             numberOfChildren = (int)((1 - elitism) * (float)population.Count);
            MapIndividual[] children         = new MapIndividual[numberOfChildren];

            for (int i = 0; i < maxIterations; i++)
            {
                // Sort population
                population.Sort((x, y) => x.Fitness.CompareTo(y.Fitness));

                // Crossover
                for (int ci = 0; ci < numberOfChildren; ci++)
                {
                    // Pick parents
                    var parent1 = pickParentFromPopulation(population);
                    var parent2 = pickParentFromPopulation(population);
                    // Crossover parents
                    MapIndividual newChild = crossover(parent1, parent2);
                    children[ci] = newChild;
                }

                for (int ci = 0; ci < numberOfChildren; ci++)
                {
                    population[ci] = children[ci];
                }

                // Mutation
                mutatePopulation(population);
            }

            // Update world matrix with best individual
            MapIndividual bestIndividual = population[0];
            foreach (MapIndividual individual in population)
            {
                if (individual.GetFitness() > bestIndividual.GetFitness())
                {
                    bestIndividual = individual;
                }
            }

            // Force playability of individual
            //bestIndividual.forcePlayability();

            // Make best individual genes unmutable
            for (GeneColumn iterator = bestIndividual.GenerationHead;
                 iterator != null && iterator.IsMutable;
                 iterator = iterator.Next)
            {
                iterator.IsMutable = false;
            }

            GlobalTail.Next = bestIndividual.GenerationHead;
            bestIndividual.GenerationHead.Previous = GlobalTail;
            GlobalTail = bestIndividual.GenerationTail;
        }
    }