예제 #1
0
        void Start()
        {
            //We need to set a candidate solution (feel free ti change this if you want to)
            FitnessCalc.SetSolution(new byte[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
                                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                 1, 1, 1, 1 });
            //Create initial population, a population of 50 should be fine
            Population myPop           = new Population(50, true);
            int        generationCount = 0;
            int        bestFittness    = 0;

            while (myPop.GetFittest().GetFitness() < FitnessCalc.GetMaxFitness())
            {
                generationCount++;
                if (myPop.GetFittest().GetFitness() > bestFittness)
                {
                    Console.WriteLine("Generation: " + generationCount + " Fittest: " + myPop.GetFittest().GetFitness());
                    Console.WriteLine("Fittest: ");
                    foreach (byte b in myPop.GetFittest().GetGenes())
                    {
                        Console.Write(b);
                    }
                    Console.WriteLine("");
                    bestFittness = myPop.GetFittest().GetFitness();
                }
                myPop = Algorithm.EvolvePopulation(myPop);
            }
            Console.WriteLine("Solution found!");
            Console.WriteLine("Generation: " + generationCount);
            Console.WriteLine("Genes: ");
            Console.WriteLine(myPop.GetFittest());
        }
예제 #2
0
 void CalculateFitness()
 {
     FitnessCalc?.Calculate(individuals);
 }