コード例 #1
0
        /// <summary>
        /// creates a new population from seralization (returned from serializePopulation())
        /// ancesstor is set to the first of the deserialized chromosomes
        /// </summary>
        /// <param name="popSerialization"></param>
        public static MultiThreadEvaluationPopulation <T> desrializePopulation(
            List <string> popSerialization,
            ChromosomeGenerator deserializer,
            IFitnessFunction fitnessFunction,
            ISelectionMethod selectionMethod,
            CustomThreadPool pool,
            bool allowDuplicateChromosomes)
        {
            IChromosome ancestor = deserializer(popSerialization[0]);
            MultiThreadEvaluationPopulation <T> newpop =
                new MultiThreadEvaluationPopulation <T>(0, (T)ancestor, fitnessFunction, selectionMethod, pool, allowDuplicateChromosomes);

            newpop.size = popSerialization.Count;
            for (int cc = 0; cc < popSerialization.Count; ++cc)
            {
                newpop.AddChromosome(deserializer(popSerialization[cc]));
            }
            return(newpop);
        }
コード例 #2
0
        public List <Chromosome> Cross(Chromosome chromosomeOne, Chromosome chromosomeTwo)
        {
            int numberOfGenes = chromosomeOne.Genes.Count;

            List <Gene> childListOne = new List <Gene>();
            List <Gene> childListTwo = new List <Gene>();

            chromosomeOne.Genes.ForEach(g => childListOne.Add(GeneGenerator.GetSpecificGene(g.Bits)));
            chromosomeTwo.Genes.ForEach(g => childListTwo.Add(GeneGenerator.GetSpecificGene(g.Bits)));

            Chromosome childChromosomeOne = ChromosomeGenerator.GetSpecificChromosome(childListOne);
            Chromosome childChromosomeTwo = ChromosomeGenerator.GetSpecificChromosome(childListTwo);

            //List<double> probabilitiesForGenes = GetProbabilitiesForGenes(numberOfGenes);
            for (int i = 0; i < numberOfGenes; i++)
            {
                int randomPlace = _rand.Next(0, childChromosomeOne.Genes[i].Bits.Length);
                for (int j = 0; j < childChromosomeOne.Genes[i].Bits.Length; j++)
                {
                    if (randomPlace > 0)
                    {
                        randomPlace -= 1;
                    }
                    else
                    {
                        int tempOne = childChromosomeOne.Genes[i].Bits[j];
                        int tempTwo = childChromosomeTwo.Genes[i].Bits[j];

                        if (tempOne != tempTwo)
                        {
                            childChromosomeOne.Genes[i].Bits[j] = tempTwo;
                            childChromosomeTwo.Genes[i].Bits[j] = tempOne;
                        }
                    }
                }
            }
            List <Chromosome> list = new List <Chromosome>();

            list.Add(childChromosomeOne);
            list.Add(childChromosomeTwo);
            return(list);
        }
コード例 #3
0
        public Chromosome Mutate(Chromosome chromosome)
        {
            List <Gene> listOfGenes = new List <Gene>();

            chromosome.Genes.ForEach(g => listOfGenes.Add(GeneGenerator.GetSpecificGene(g.Bits)));
            Chromosome mutatedChromosome = ChromosomeGenerator.GetSpecificChromosome(listOfGenes);

            int numberOfGenes = mutatedChromosome.Genes.Count;

            for (int i = 0; i < numberOfGenes; i++)
            {
                int randomPlace = _rand.Next(0, mutatedChromosome.Genes[i].Bits.Length - 1);
                if (mutatedChromosome.Genes[i].Bits[randomPlace] == 0)
                {
                    mutatedChromosome.Genes[i].Bits[randomPlace] = 1;
                }
                else
                {
                    mutatedChromosome.Genes[i].Bits[randomPlace] = 0;
                }
            }
            return(mutatedChromosome);
        }