Ejemplo n.º 1
0
        public List <IntChromosome> Selection(List <IntChromosome> chromosomes, FitnessFuncGoal goal)
        {
            List <IntChromosome> bestChromosomes = new List <IntChromosome>();

            while (bestChromosomes.Count < chromosomes.Count)
            {
                chromosomeIndexes.AsParallel().ForAll(val => val = random.Next(chromosomes.Count));
                bestChromosomes.Add(FindBest(chromosomes, goal));
            }
            return(bestChromosomes);
        }
Ejemplo n.º 2
0
        private IntChromosome FindBest(List <IntChromosome> chromosomes, FitnessFuncGoal goal)
        {
            IntChromosome bestChromosome = null;

            foreach (int index in chromosomeIndexes)
            {
                if (bestChromosome == null || comparator.Compare(bestChromosome, chromosomes.ElementAt(index)) > 0)
                {
                    bestChromosome = chromosomes.ElementAt(index);
                }
            }
            return(bestChromosome);
        }
Ejemplo n.º 3
0
 public TournamentSelection(int populationSize, int tournamentSize, FitnessFuncGoal goal)
 {
     if (tournamentSize >= 2 && tournamentSize < populationSize)
     {
         chromosomeIndexes = new List <int>(tournamentSize);
         comparator        = initComparator(goal);
     }
     else
     {
         throw new ArgumentException
                   ("Tournament size must be between 2 and population size minus 1.");
     }
 }
Ejemplo n.º 4
0
        public IComparer <IntChromosome> initComparator(FitnessFuncGoal goal)
        {
            switch (goal)
            {
            case FitnessFuncGoal.Min:
                return(new MinComparator());

            case FitnessFuncGoal.Max:
                return(new MaxComparator());

            default:
                throw new Exception("Fitness function type must be initialized!");
            }
        }
Ejemplo n.º 5
0
 public SphericalFunction(FitnessFuncGoal goal)
 {
     this.goal = goal;
 }