コード例 #1
0
 void MainLoop()
 {
     while (!population.IsFinished())
     {
         population.NaturalSelection();
         population.Generate();
         population.CalcFitness();
         string result = population.GetBest();
         Console.WriteLine("Current best: " + result);
         Console.WriteLine("Average fitness: " + (int)(population.AverageFitness() * 100) + "%");
         Console.WriteLine("Generations: " + population.generations);
         Console.WriteLine("Mutation rate: " + mutationRate * 100 + "%");
         Console.WriteLine();
     }
     Console.ReadKey();
 }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            Console.Write("Welcome to LeanZo's Genetic Algorithm!\nThis program is inspired by the proccess of natural selection.\nGenetic algorithms are used to generate high-quality and optimized solution using principles as mutation and natural selection.\n");
            Console.Write("This program operates on the following manner: There is a 'population', filled with 'beings', each of these having traits represented by the old eight deadly sins.\nThese traits have a range of 0 to 100. The user sets a goal.");
            Console.Write("The program runs.\nFor each 'being' of the first generation it sets random traits values, then the program calculates the average of these traits.\nThe 'being' which average is closer to the goal lives on and a new generation is created, ");
            Console.Write("filled with clones of this 'being'.\nThe clones are mutated randomily. The once again the program search for the 'being' closer to the goal.\nThis proccess goes on until a 'being' with average equal to the goal is found.\n");

            Console.ForegroundColor = ConsoleColor.Red;

            Console.Write("Enter the goal(0 - 100): ");
            Goal = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the population size: ");
            Population population = new Population(Convert.ToUInt32(Console.ReadLine()));

            Console.ForegroundColor = ConsoleColor.Green;

            //Shows the best being of each generation while they do not reach the goal
            while (!population.SearchForFittest())
            {
                Console.WriteLine("\nGEN: {0}\nBeing: {1}/{2}\nID: {3}\n---\nLust: {4}\nGluttony: {5}\nGreed: {6}\nSloth: {7}\nWrath: {8}\nEnvy: {9}\nPride: {10}\nVainglory: {11}\n---\nAverage: {12}\n",
                                  population.gen, population.bestBeing + 1, population.beings.Length, population.beings[population.bestBeing].id, population.beings[population.bestBeing].Lust, population.beings[population.bestBeing].Gluttony,
                                  population.beings[population.bestBeing].Greed, population.beings[population.bestBeing].Sloth, population.beings[population.bestBeing].Wrath,
                                  population.beings[population.bestBeing].Envy, population.beings[population.bestBeing].Pride, population.beings[population.bestBeing].Vainglory,
                                  population.Average(population.beings[population.bestBeing]));

                population.NaturalSelection();
            }

            Console.WriteLine("\nPERFECT!\nGEN: {0}\nBeing: {1}/{2}\nID: {3}\n---\nLust: {4}\nGluttony: {5}\nGreed: {6}\nSloth: {7}\nWrath: {8}\nEnvy: {9}\nPride: {10}\nVainglory: {11}\n---\nAverage: {12}\n",
                              population.gen, population.bestBeing + 1, population.beings.Length, population.beings[population.bestBeing].id, population.beings[population.bestBeing].Lust, population.beings[population.bestBeing].Gluttony,
                              population.beings[population.bestBeing].Greed, population.beings[population.bestBeing].Sloth, population.beings[population.bestBeing].Wrath,
                              population.beings[population.bestBeing].Envy, population.beings[population.bestBeing].Pride, population.beings[population.bestBeing].Vainglory,
                              population.Average(population.beings[population.bestBeing]));
            Console.Beep();

            Console.ReadLine();
        }