AIMA.Core.Search.Local.GeneticAlgorithm.randomSelection C# (CSharp) Метод

randomSelection() приватный Метод

private randomSelection ( Set population, FitnessFunction fitnessFn ) : String
population Set
fitnessFn FitnessFunction
Результат String
        private String randomSelection(Set<String> population,
                FitnessFunction fitnessFn)
        {
            String selected = null;

            // Determine all of the fitness values
            double[] fValues = new double[population.Count];
            String[] popArray = population.toArray(new String[population.Count]);
            for (int i = 0; i < popArray.length; i++)
            {
                fValues[i] = fitnessFn.getValue(popArray[i]);
            }

            // Normalize the fitness values
            fValues = Util.normalize(fValues);
            double prob = random.nextDouble();
            double totalSoFar = 0.0;
            for (int i = 0; i < fValues.length; i++)
            {
                // Are at last element so assign by default
                // in case there are rounding issues with the normalized values
                totalSoFar += fValues[i];
                if (prob <= totalSoFar)
                {
                    selected = popArray[i];
                    break;
                }
            }

            // selected may not have been assigned
            // if there was a rounding error in the
            // addition of the normalized values (i.e. did not total to 1.0)
            if (null == selected)
            {
                // Assign the last value
                selected = popArray[popArray.length - 1];
            }

            return selected;
        }