示例#1
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);
            }
        }
示例#2
0
        public static void RunEvolution2()
        {
            var      bmp = new System.Drawing.Bitmap(FILE_IN);
            ImageSys img = new ImageSys(bmp);
            Texture  txt = new Interpolent(img, Intpol.Nearest);

            //var fitness = BuildFitness(txt);
            Console.WriteLine("Collecting Sample Points...");
            ControlPoints.BuildData();
            Fitness <CPPN> fitness = x => ControlPoints.GradeImage(x);

            EvolSpecies <CPPN> evolver = new EvolSpecies <CPPN>(500, 1.0, fitness); //100, 0.1
            CPPN best  = new CPPN();
            CPPN proto = new CPPN();


            Renderor ren    = new Renderor();
            ImageSys output = new ImageSys(256, 256);

            //img.BMP.Save(FILE_OUT + "test.png");

            Console.WriteLine("Aproximating Image: " + FILE_IN);
            evolver.Initialise(best);

            for (int i = 0; i < MAX_GEN; i++)
            {
                Console.WriteLine();
                string index = i.ToString("0000");

                Console.WriteLine("Rendering Best Fit...");
                ren.Render(best, output);
                output.BMP.Save(FILE_OUT + "A" + index + ".png");

                Console.WriteLine("Rendering Random Species...");
                ren.Render(proto, output);
                output.BMP.Save(FILE_OUT + "B" + index + ".png");

                int    nodes = best.NumNurons;
                int    axons = best.NumAxons;
                double fit   = evolver.TopFitness;

                Console.WriteLine();
                Console.WriteLine("Generation " + i + ": " + nodes + " " + axons + " " + fit);
                Console.WriteLine("Num Species: " + evolver.NumSpecies);
                Console.WriteLine("Threshold: " + evolver.Threshold.ToString("0.00"));

                ////we cannot have more species than there are indvidual organisms
                //if (evolver.NumSpecies > 100) throw new Exception();

                evolver.Evolve();
                evolver.GetTopSpec(best);
                evolver.GetRandProto(proto);
            }
        }
示例#3
0
        public static void RunTestAdder()
        {
            TestAdder               network = new TestAdder();
            Fitness <TestAdder>     fit     = x => x.RunTests();
            EvolSpecies <TestAdder> evol    = new EvolSpecies <TestAdder>(500, 1.0, fit);

            evol.Initialise(network);

            TestAdder best = new TestAdder();

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

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

                int    nodes = best.NumNurons;
                int    axons = best.NumAxons;
                double fx    = evol.TopFitness;

                Console.WriteLine();
                Console.WriteLine("Generation " + i + ": " + nodes + " " + axons + " " + fx);
                Console.WriteLine("Num Species: " + evol.NumSpecies);
                Console.WriteLine("Threshold: " + evol.Threshold.ToString("0.00"));


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

                    best.ReduceNetwork();
                    nodes = best.NumNurons;
                    axons = best.NumAxons;
                    fx    = fit(best);

                    Console.WriteLine("Final Network: " + nodes + " " + axons + " " + fx);

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

                //Thread.Sleep(500);
            }
        }
示例#4
0
        public static void TestGennetics3()
        {
            GenString target = new GenString(
                "Twas brillig, and the slithy toves did gyer and gimble in the wabe.");
            GenString best = new GenString("Hello");

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

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

                dist = 100.0 - dist;
                if (dist < 0.1)
                {
                    dist = 0.1;
                }
                return(dist);
            };

            EvolSpecies <GenString> evol = new EvolSpecies <GenString>(1000, 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(best);

                //Thread.Sleep(500);
            }
        }