Beispiel #1
0
        private void Test_PolynomialGA(Func <double, double> func)
        {
            int    polynomialOrder = 5;
            int    populationSize  = 500;
            int    sampleCount     = 100;
            int    genCount        = 5000;
            Random rng             = new Random(0);
            var    pop             = Enumerable
                                     .Range(0, populationSize)
                                     .Select(i => new CartesianIndividual(polynomialOrder, rng))
                                     .Cast <IIndividual>()
                                     .ToList();

            double[] xRange = Enumerable
                              .Range(0, sampleCount)
                              .Select(i => (double)i / 100)
                              .ToArray();

            double[][] allPowsOfX = new double[sampleCount][];
            for (int i = 0; i < allPowsOfX.Length; ++i)
            {
                allPowsOfX[i] = new double[polynomialOrder];

                double powX = 1;
                for (int j = 0; j < allPowsOfX[i].Length; ++j)
                {
                    allPowsOfX[i][j] = powX;
                    powX            *= xRange[i];
                }
            }

            double[] expectedValues = xRange
                                      .Select(i => func(i))
                                      .ToArray();

            Utils.SetRandomSeed(0);

            var ages = new Ages(genCount,
                                new Evaluate((i) => ((CartesianIndividual)i).PolynomialEval(allPowsOfX, expectedValues)),
                                CartesianIndividual.CrossOver,
                                new Generate((r) => new CartesianIndividual(polynomialOrder, r)),
                                pop);

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

            ages.GoThroughGenerations();

            for (int i = 0; i < 4; ++i)
            {
                int ix = (ages.Champions.Count / 4) * i;
                Log.Info(ages.Champions[ix]);
            }
            Log.Info(ages.Champions.Last());
        }
Beispiel #2
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();
        }
Beispiel #3
0
 public Ages.EvaluatedIndividual Generation()
 {
     Ages.GoThroughGenerations();
     Ages.EvaluatedIndividual champ = Ages.Champions.Last();
     return(champ);
 }