Ejemplo n.º 1
0
        public static Tuple <Individual, Individual> Crossover(Tuple <Individual, Individual> parents)
        {
            Individual parent1 = parents.Item1;
            Individual parent2 = parents.Item2;

            Individual[] childrenArray = new Individual[2];

            int counter = 0;

            while (counter < 2)
            {
                int[] childgenes  = new int[parent1.Length()];
                int   randomIndex = r.Next(parent1.Length());
                for (int i = 0; i < randomIndex; i++)
                {
                    childgenes[i] = parent1.genes[i];
                }
                for (int i = randomIndex; i < parent1.Length(); i++)
                {
                    childgenes[i] = parent2.genes[i];
                }

                childrenArray[counter] = new Individual(childgenes);
                counter++;
            }

            return(new Tuple <Individual, Individual>(childrenArray[0], childrenArray[1]));
        }
Ejemplo n.º 2
0
        public static Individual Mutation(Individual ind, double mutationRate)
        {
            double random = r.NextDouble();

            if (random < mutationRate)
            {
                ind.ModifyGene(r.Next(ind.Length()));
            }

            return(ind);
        }