Beispiel #1
0
        public static void TestGennetics2()
        {
            GenString2 target = new GenString2(
                "Twas brillig, and the slithy toves did gyer and gimble in the wabe.");
            GenString2 best = new GenString2("Hello");

            Console.WriteLine("Target: " + target);

            Fitness <GenString2> fit = delegate(GenString2 gs)
            {
                double dist = gs.Distance(target);
                //return 1.0 / (dist + 1.0);
                return(-dist);
            };

            EvolMonogen <GenString2> evol = new EvolMonogen <GenString2>(100, 0.1, fit);

            evol.Initialise(best);

            for (int i = 0; i < MAX_GEN; i++)
            {
                evol.Evolve();
                evol.GetTopSpec(best);

                double fitness = evol.TopFitness;

                Console.WriteLine();
                Console.WriteLine("Generation " + i + ": " + fitness);
                Console.WriteLine(best);

                //Thread.Sleep(500);
            }
        }
Beispiel #2
0
        public static void TestGennetics4()
        {
            GenString2 target = new GenString2(
                "Twas brillig, and the slithy toves did gyer and gimble in the wabe.");
            GenString2 best = new GenString2("Hello");

            Console.WriteLine("Target: " + target);

            Fitness <GenString2> fit = delegate(GenString2 gs)
            {
                double dist = gs.Distance(target);
                //return 1.0 / (dist + 1.0);
                //return -dist;

                dist = (5000.0 - dist) / 5000.0;
                if (dist < 0.0001)
                {
                    dist = 0.0001;
                }
                return(dist);
            };

            EvolSpecies <GenString2> evol = new EvolSpecies <GenString2>(500, 0.1, fit);

            evol.Initialise(best);

            for (int i = 0; i < MAX_GEN; i++)
            {
                evol.Evolve();
                evol.GetTopSpec(best);

                double fitness = evol.TopFitness;
                int    species = evol.NumSpecies;

                Console.WriteLine();
                Console.WriteLine("Generation " + i + ": " + fitness);
                Console.WriteLine("Num Species: " + species);
                Console.WriteLine("Threshold: " + evol.Threshold);
                Console.WriteLine(best);


                if (fitness >= 0.996)
                {
                    Console.WriteLine();
                    Console.WriteLine("#############################################################");
                    Console.WriteLine("TARGET FITNESS OF > 0.996 REACHED!!!");
                    Console.WriteLine("#############################################################");
                    Console.WriteLine();

                    Console.WriteLine("Press any key...");
                    Console.ReadKey(true);
                    break;
                }

                //Thread.Sleep(500);
            }
        }