<commit_before>﻿using ISAAR.MSolve.Analyzers.Optimization.Commons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Troschuetz.Random;

namespace ISAAR.MSolve.Analyzers.Optimization.Algorithms.Metaheuristics.GeneticAlgorithms.Selections
{
    class UniformRandomSelection : SelectionStrategy
    {
        private readonly bool allowIdenticalParents;
        private readonly IGenerator rng;

        public UniformRandomSelection(): this(RandomNumberGenerationUtilities.troschuetzRandom)
        {
        }

        public UniformRandomSelection(IGenerator randomNumberGenerator, bool allowIdenticalParents = false)
        {
            if (randomNumberGenerator == null) throw new ArgumentException("The random number generator must not be null");
            this.allowIdenticalParents = allowIdenticalParents;
        }

        Tuple<Individual, Individual>[] SelectionStrategy.Apply(Individual[] population, int offspringsCount)
        {
            int pairsCount = (offspringsCount - 1) / 2 + 1;
            var pairs = new Tuple<Individual, Individual>[pairsCount];
            for (int i = 0; i < pairsCount; ++i)
            {
                int parent1 = rng.Next(population.Length);
                int parent2 = rng.Next(population.Length);
                if (!allowIdenticalParents)
                {
                    while (parent1 == parent2) parent2 = rng.Next(population.Length);
                }
                pairs[i] = new Tuple<Individual, Individual>(population[parent1], population[parent2]);
            }
            return pairs;
        }
    }
}
<commit_msg>Correct UniformRandomSelection constructor.<commit_after>﻿using ISAAR.MSolve.Analyzers.Optimization.Commons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Troschuetz.Random;

namespace ISAAR.MSolve.Analyzers.Optimization.Algorithms.Metaheuristics.GeneticAlgorithms.Selections
{
    class UniformRandomSelection : SelectionStrategy
    {
        private readonly bool allowIdenticalParents;
        private readonly IGenerator rng;

        public UniformRandomSelection(): this(RandomNumberGenerationUtilities.troschuetzRandom)
        {
        }

        public UniformRandomSelection(IGenerator randomNumberGenerator, bool allowIdenticalParents = false)
        {
            if (randomNumberGenerator == null) throw new ArgumentException("The random number generator must not be null");
            this.rng = randomNumberGenerator;
            this.allowIdenticalParents = allowIdenticalParents;
        }

        Tuple<Individual, Individual>[] SelectionStrategy.Apply(Individual[] population, int offspringsCount)
        {
            int pairsCount = (offspringsCount - 1) / 2 + 1;
            var pairs = new Tuple<Individual, Individual>[pairsCount];
            for (int i = 0; i < pairsCount; ++i)
            {
                int parent1 = rng.Next(population.Length);
                int parent2 = rng.Next(population.Length);
                if (!allowIdenticalParents)
                {
                    while (parent1 == parent2) parent2 = rng.Next(population.Length);
                }
                pairs[i] = new Tuple<Individual, Individual>(population[parent1], population[parent2]);
            }
            return pairs;
        }
    }
}

{'fraction_non_alphanumeric': 0.08362573099415205, 'fraction_numerical': 0.006432748538011696, 'mean_word_length': 2.5635416666666666, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4561450', 'n_tokens_mistral': 932, 'n_tokens_neox': 871, 'n_words': 194}