Exemplo n.º 1
0
 private void LogIndividual(Chromosome chrome, int index)
 {
     Console.WriteLine("--- Individual " + index.ToString() + " ---");
     Console.WriteLine("Name: " + chrome.Name);
     Console.WriteLine("Parents: " + string.Join(", ", chrome.Parents.ToArray()));
     Console.WriteLine("Chromosome: " + chrome.ChromeCode);
     Console.WriteLine("Is Mutant: " + chrome.IsMutant.ToString());
     Console.WriteLine("---/ END Individual " + index.ToString() + " /---");
 }
Exemplo n.º 2
0
        public Chromosome Mate(Chromosome mum, Chromosome dad)
        {
            //How many children (Random litter size between 1 and 10)

            List<Chromosome> children = new List<Chromosome>();

            //Where in the chromosome are we splicing from
            //Note: Pig and Elephant DNA just won't splice - Google Loverboy Pig and Elephant DNA for details
            int pivot = RandomGen.Next(0, PopulationSize);
            int chromeLength = mum.GeneList.Count;

            //Get Genome from Mother up to pivot, get rest from father from pivot

            List<int> childCode = new List<int>();
            childCode.AddRange(mum.GeneList.GetRange(0, pivot));
            childCode.AddRange(dad.GeneList.GetRange(pivot, (dad.GeneList.Count) - pivot));

            Chromosome child;

            double mutationChance = RandomGen.NextDouble();
            if (MutationProbabilty > mutationChance)
            {
                //Mutate - same code at pivot but we dont necessarily want the same point in the gene.
                int mutantLocus = RandomGen.Next(0, chromeLength);
                int mutantGene = 1 - childCode[mutantLocus];

                childCode[mutantLocus] = mutantGene;

                child = new Chromosome(RandomGen, childCode);
                child.IsMutant = true;
            }
            else
            {
                child = new Chromosome(RandomGen, childCode);
            }

            child.Parents.Add(mum.Name);
            child.Parents.Add(dad.Name);

            Console.WriteLine("Child Created...");

            return child;
        }