Example #1
0
        private String retrieveBestIndividual(Set <String> population,
                                              FitnessFunction fitnessFn)
        {
            String bestIndividual  = null;
            double bestSoFarFValue = Double.NEGATIVE_INFINITY;

            foreach (String individual in population)
            {
                double fValue = fitnessFn.getValue(individual);
                if (fValue > bestSoFarFValue)
                {
                    bestIndividual  = individual;
                    bestSoFarFValue = fValue;
                }
            }

            return(bestIndividual);
        }
Example #2
0
        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);
        }
Example #3
0
        private String retrieveBestIndividual(Set<String> population,
                FitnessFunction fitnessFn)
        {
            String bestIndividual = null;
            double bestSoFarFValue = Double.NEGATIVE_INFINITY;

            foreach (String individual in population)
            {
                double fValue = fitnessFn.getValue(individual);
                if (fValue > bestSoFarFValue)
                {
                    bestIndividual = individual;
                    bestSoFarFValue = fValue;
                }
            }

            return bestIndividual;
        }
Example #4
0
        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;
        }