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 randomMutatePlace = _rand.Next(0, mutatedChromosome.Length);
            int placeHelper       = 0;

            for (int i = 0; i < mutatedChromosome.Genes.Count; i++)
            {
                for (int j = 0; j < mutatedChromosome.Genes[i].Bits.Length; j++)
                {
                    if (placeHelper == randomMutatePlace)
                    {
                        if (mutatedChromosome.Genes[i].Bits[j] == 1)
                        {
                            mutatedChromosome.Genes[i].Bits[j] = 0;
                        }
                        else
                        {
                            mutatedChromosome.Genes[i].Bits[j] = 1;
                        }
                    }
                    placeHelper++;
                }
            }
            return(mutatedChromosome);
        }
Пример #2
0
        public static Chromosome GetRandomChromosome(int numberOfGenes, int lengthOfGene)
        {
            Random     rand       = new Random();
            Chromosome chromosome = new Chromosome();

            for (int i = 0; i < numberOfGenes; i++)
            {
                Gene gen = GeneGenerator.GetRandomGene(lengthOfGene);
                chromosome.Genes.Add(gen);
            }
            return(chromosome);
        }
        public List <Chromosome> Cross(Chromosome chromosomeOne, Chromosome chromosomeTwo)
        {
            int numberOfGenes      = chromosomeOne.Genes.Count;
            int randomPlaceOfCross = _rand.Next(chromosomeOne.Length);

            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);

            for (int i = 0; i < numberOfGenes; i++)
            {
                for (int j = 0; j < childChromosomeOne.Genes[i].Bits.Length; j++)
                {
                    if (randomPlaceOfCross > 0)
                    {
                        randomPlaceOfCross -= 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);
        }
Пример #4
0
        private void SetBestActPopulationChromosome()
        {
            List <double> values = new List <double>();

            for (int i = 0; i < _actualPopulation.Count; i++)
            {
                values.Add(_fitnessOfActualPopulation.Find(c => c.Item1 == _actualPopulation[i]).Item2);
            }
            double bestValue = 0;

            if (Criterion == CriterionOfSelection.MAX)
            {
                bestValue = values.Max();
            }
            else
            {
                bestValue = values.Min();
            }
            List <Gene> genes = new List <Gene>();

            _fitnessOfActualPopulation.Find(c => c.Item2 == bestValue).Item1.Genes.ForEach(g => genes.Add(GeneGenerator.GetSpecificGene(g.Bits)));
            BestChromosome = ChromosomeGenerator.GetSpecificChromosome(genes);
        }