public void test_getValue()
 {
     Assert.IsTrue(0.0 == fitnessFunction.apply(
                       new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 0, 0, 0, 0, 0, 0, 0, 0 }))));
     Assert.IsTrue(0.0 == fitnessFunction.apply(
                       new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 0, 1, 2, 3, 4, 5, 6, 7 }))));
     Assert.IsTrue(0.0 == fitnessFunction.apply(
                       new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 7, 6, 5, 4, 3, 2, 1, 0 }))));
     Assert.IsTrue(23.0 == fitnessFunction.apply(
                       new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 5, 6, 1, 3, 6, 4, 7, 7 }))));
     Assert.IsTrue(28.0 == fitnessFunction.apply(
                       new Individual <int>(CollectionFactory.CreateQueue <int>(new[] { 0, 4, 7, 5, 2, 6, 1, 3 }))));
 }
Esempio n. 2
0
        static void nQueensGeneticAlgorithmSearch()
        {
            System.Console.WriteLine("\nNQueensDemo GeneticAlgorithm  -->");
            try
            {
                IFitnessFunction <int>       fitnessFunction = NQueensGenAlgoUtil.getFitnessFunction();
                GoalTest <Individual <int> > goalTest        = NQueensGenAlgoUtil.getGoalTest();
                // Generate an initial population
                ISet <Individual <int> > population = CollectionFactory.CreateSet <Individual <int> >();
                for (int i = 0; i < 50; ++i)
                {
                    population.Add(NQueensGenAlgoUtil.generateRandomIndividual(boardSize));
                }

                GeneticAlgorithm <int> ga = new GeneticAlgorithm <int>(boardSize,
                                                                       NQueensGenAlgoUtil.getFiniteAlphabetForBoardOfSize(boardSize), 0.15);

                // Run for a set amount of time
                Individual <int> bestIndividual = ga.geneticAlgorithm(population, fitnessFunction, goalTest, 1000L);

                System.Console.WriteLine("Max Time (1 second) Best Individual=\n"
                                         + NQueensGenAlgoUtil.getBoardForIndividual(bestIndividual));
                System.Console.WriteLine("Board Size      = " + boardSize);
                //System.Console.WriteLine("# Board Layouts = " + (new BigDecimal(boardSize)).pow(boardSize)/*);*/
                System.Console.WriteLine("Fitness         = " + fitnessFunction.apply(bestIndividual));
                System.Console.WriteLine("Is Goal         = " + goalTest(bestIndividual));
                System.Console.WriteLine("Population Size = " + ga.getPopulationSize());
                System.Console.WriteLine("Iterations      = " + ga.getIterations());
                System.Console.WriteLine("Took            = " + ga.getTimeInMilliseconds() + "ms.");

                // Run till goal is achieved
                bestIndividual = ga.geneticAlgorithm(population, fitnessFunction, goalTest, 0L);

                System.Console.WriteLine("");
                System.Console
                .WriteLine("Goal Test Best Individual=\n" + NQueensGenAlgoUtil.getBoardForIndividual(bestIndividual));
                System.Console.WriteLine("Board Size      = " + boardSize);
                //System.Console.WriteLine("# Board Layouts = " + (new BigDecimal(boardSize)).pow(boardSize)/*);*/
                System.Console.WriteLine("Fitness         = " + fitnessFunction.apply(bestIndividual));
                System.Console.WriteLine("Is Goal         = " + goalTest(bestIndividual));
                System.Console.WriteLine("Population Size = " + ga.getPopulationSize());
                System.Console.WriteLine("Itertions       = " + ga.getIterations());
                System.Console.WriteLine("Took            = " + ga.getTimeInMilliseconds() + "ms.");
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        // RANDOM-SELECTION(population, FITNESS-FN)
        protected virtual Individual <A> randomSelection(ICollection <Individual <A> > population, IFitnessFunction <A> fitnessFn)
        {
            // Default result is last individual
            // (just to avoid problems with rounding errors)
            Individual <A> selected = population.Get(population.Size() - 1);

            // Determine all of the fitness values
            double[] fValues = new double[population.Size()];
            for (int i = 0; i < population.Size(); ++i)
            {
                fValues[i] = fitnessFn.apply(population.Get(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 = population.Get(i);
                    break;
                }
            }

            selected.incDescendants();
            return(selected);
        }
Esempio n. 4
0
        public virtual Individual <A> retrieveBestIndividual(ICollection <Individual <A> > population, IFitnessFunction <A> fitnessFn)
        {
            Individual <A> bestIndividual  = null;
            double         bestSoFarFValue = double.NegativeInfinity;

            foreach (Individual <A> individual in population)
            {
                double fValue = fitnessFn.apply(individual);
                if (fValue > bestSoFarFValue)
                {
                    bestIndividual  = individual;
                    bestSoFarFValue = fValue;
                }
            }

            return(bestIndividual);
        }