Exemple #1
0
        public Population(
            int length,
            int crossOverPoint,
            int initialPop,
            int popLimit,
            float mutationFreq,
            float deathParam,
            float reproductionParam,
            float[] weights)
        {
            this.m_length                 = length;
            this.m_crossoverPoint         = crossOverPoint;
            this.m_initialPopulationCount = initialPop;
            this.m_populationLimit        = popLimit;
            this.m_mutationFreq           = mutationFreq;
            this.m_deathParam             = deathParam;
            this.m_reproduceParam         = reproductionParam;

            this.m_genomes = new List <BasketListGenome>();

            for (int i = 0; i < this.m_initialPopulationCount; i++)
            {
                BasketListGenome newGenome = new BasketListGenome(this.m_length);
                newGenome.CrossoverPoint = this.m_crossoverPoint;
                newGenome.FitnessFunction();
                newGenome.SetWeights(weights);

                this.m_genomes.Add(newGenome);
            }
        }
Exemple #2
0
        private void DoCrossover(List <BasketListGenome> genes) // OREN
        {
            List <BasketListGenome> NewGeneration = new List <BasketListGenome>();
            int originalCount = genes.Count;
            int TotalScore    = 0;

            // Sum total scores
            foreach (BasketListGenome CurrElement in genes)
            {
                TotalScore += (int)CurrElement.CurrentFitness;
            }

            // Take 50% of the genes and use
            while (NewGeneration.Count != (originalCount / 2))
            {
                Random r        = new Random();
                int    rInt     = r.Next(0, TotalScore + 1);
                int    SumScore = 0;

                // Sum curr score
                foreach (BasketListGenome CurrElement in genes)
                {
                    SumScore += (int)CurrElement.CurrentFitness;

                    // Check if we can add it.
                    if ((SumScore >= rInt) && (!NewGeneration.Contains(CurrElement)))
                    {
                        NewGeneration.Add(CurrElement);
                    }

                    break;
                }
            }

            // Crossover couples
            while (NewGeneration.Count != originalCount)
            {
                // Search first
                foreach (BasketListGenome CurrElement1 in genes)
                {
                    // Check if we already have it.
                    if (!NewGeneration.Contains(CurrElement1))
                    {
                        // Search second
                        foreach (BasketListGenome CurrElement2 in genes)
                        {
                            // Check if we already have or if it is the same as the first.
                            if ((!NewGeneration.Contains(CurrElement2)) && (!CurrElement1.Equals(CurrElement2)))
                            {
                                // Crossover in order to create new genome.
                                BasketListGenome NewGenome = (BasketListGenome)CurrElement1.Crossover(CurrElement2);
                                NewGeneration.Add(NewGenome);
                            }
                        }
                    }
                }
            }
        }