示例#1
0
        static void Main(string[] args)
        {
            var pop = new List <IIndividual>(32);

            Console.WriteLine("Read from file individuals?(y/n)");
            var yayornay = Console.ReadLine();

            if (yayornay == "y")
            {
                Console.WriteLine("File path");
                var       path   = Console.ReadLine();
                string [] strArr = null;
                try{
                    strArr = File.ReadAllLines(path);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.ReadKey(false);
                    Environment.Exit(1);
                }
                foreach (var str in strArr)
                {
                    Console.WriteLine("Read " + str);
                    if (str.Trim() == string.Empty)
                    {
                        continue;
                    }
                    pop.Add(new GotchaIndividual(str));
                }
            }
            else
            {
                Console.WriteLine("Creating individuals");
                for (var ii = 0; ii < 32; ++ii)
                {
                    pop.Add(new GotchaIndividual());
                }
            }

            Ages myGA = new Ages(
                1000,
                new CompareEvaluate(Evaluate),
                GotchaIndividual.GarboCrossoverOperator,
                (r) => new GotchaIndividual(),
                pop);

            myGA.SetRandomSeed(0);

            myGA.GoThroughGenerations();

            Console.ReadKey();
        }
示例#2
0
文件: GA.cs 项目: GKBelmonte/Core
        private void Test_PolynomialGA(Func <double, double> func)
        {
            //Settings
            bool adaptive = false;
            int  seed     = 0;

            Utils.SetRandomSeed(seed);
            var rng = new Random(seed);

            PolynomialOrder = 5;
            int populationSize = 100;
            //number of samples to use the in the polynomial approx
            // to test the differnce
            double start = -10, end = 10;
            //Step between the samples
            double step = 0.02;
            //Number of generations to bundle
            int genBundleCount = 10;

            //Stop at Gen
            GenerationStop = 500;

            //Get Data for tests
            double[] xRange, expectedValues;
            GetFofXForRange(func, start, step, end, out xRange, out expectedValues);

            double[][] allPowsOfX = GetPowersOfX(xRange, PolynomialOrder);

            ExpectedValsX  = xRange;
            ExpectedValsY  = expectedValues;
            PowsOfXForAllX = allPowsOfX;

            //Create Delegates
            Evaluate eval = (i) =>
                            ((CartesianIndividual)i).PolynomialEval(allPowsOfX, expectedValues);

            CrossOver crossOver = adaptive
                ? AdaptiveCartesianIndividual.CrossOver
                : (CrossOver)CartesianIndividual.CrossOver;

            Generate generate = adaptive
                ? (Generate)((r) => new AdaptiveCartesianIndividual(PolynomialOrder, 1, r: r, emptySigma: true))
                : (r) => new CartesianIndividual(PolynomialOrder, 1, r);

            // Generate Population
            List <IIndividual> pop;

            pop = Enumerable
                  .Range(0, populationSize)
                  .Select(i => generate(rng))
                  .Cast <IIndividual>()
                  .ToList();

            Ages = new Ages(genBundleCount,
                            eval,
                            crossOver,
                            generate,
                            pop);

            Ages.SetRandomSeed(seed);

            Ages.NicheStrat = new NicheDensityStrategy(
                Ages,
                (l, r) => CartesianIndividual.Distance((CartesianIndividual)l, (CartesianIndividual)r));
        }