Ejemplo n.º 1
0
        public override Candidate Mutate(Candidate candidate)
        {
            double    chance = RandomMutator.NextDouble();
            Candidate mutant = new Candidate(candidate);

            if (chance < mutationChance)
            {
                int endIndex;
                int startIndex = RandomMutator.Next(0, candidate.chromoson.Count());
                do
                {
                    endIndex = RandomMutator.Next(0, candidate.chromoson.Count());
                } while (startIndex == endIndex);
                if (startIndex > endIndex)
                {
                    int temp = startIndex;
                    startIndex = endIndex;
                    endIndex   = temp;
                }


                mutant.chromoson.Reverse(startIndex, endIndex - startIndex);
            }
            mutant.CountFitness();
            return(mutant);
        }
Ejemplo n.º 2
0
        public override Candidate Mutate(Candidate candidate)
        {
            double chance = RandomMutator.NextDouble();

            if (chance < mutationChance)
            {
                int index1 = RandomMutator.Next(0, candidate.chromoson.Count());
                int index2;
                do
                {
                    index2 = RandomMutator.Next(0, candidate.chromoson.Count());
                } while (index1 == index2);
                Swap <int>(candidate.chromoson, index1, index2);
            }
            return(candidate);
        }