Esempio n. 1
0
        private List <PopulationMember> addCrossoverMembers(List <PopulationMember> newPop, int numberToAdd)
        {
            //Console.WriteLine ("Beginning Crossover");
            // The fittest members have are more likely to be crossed (likelihood is proportional to their fitness)
            List <int> probabilityArray = new List <int> ();

            //Console.WriteLine ("Caluclating Probability Array...");

            for (int x = 0; x < newPop.Count; x++)
            {
                for (int i = 0; i < newPop[x].lastFitness; i++)
                {
                    probabilityArray.Add(x);
                }
            }

            for (int i = 0; i < numberToAdd; i++)
            {
                PopulationMember[] parents = new PopulationMember[Configuration.CROSSOVER_PARENT_COUNT];
                for (int x = 0; x < parents.Length; x++)
                {
                    parents [x] = newPop[probabilityArray [generator.next(0, probabilityArray.Count)]];
                }

                //Console.WriteLine ("{4}/{5}: Crossing {0} (fitness {1}) and {2} (fitness {3})", first, newPop[first].lastFitness, second, newPop[second].lastFitness, i, numberToAdd);

                // Mutate crossover members
                PopulationMember newMember = crosser.crossover(parents);
                newMember = mutator.mutate(newMember);

                // With a certain probability, the crossover will be dismissed and a brand new population member introduced
                if (generator.next(0, Configuration.NEW_DURING_MUTATION_INVERSE) == 0)
                {
                    PopulationMember p = new PopulationMember();
                    while (p.calculateFitness() < 1)
                    {
                        p = new PopulationMember();
                    }
                    newPop.Add(p);
                }
                else
                {
                    newPop.Add(newMember);
                }

                //newPop.Add(crosser.crossover (newPop[first], newPop[second]));
            }

            return(newPop);
        }