public void TestGPTemplate()
        {
            GPSolutionDefinition holder = new GPSolutionDefinition(config);

            /*
             * vou simular um template com 4 formulas(nodes) e 2 variaveis numéricas
             * f1(NODE,NUMBER)
             * F2(NODE,NODE)
             * F3
             * F4(NUMBER)
             */
            SemanticaList listaFormulas = CriaListaDefault(holder);

            //Lista Numerica
            SemanticaList listaNumerica = CriaListaNumerica(holder);

            //Templates: possuem propriedades evolutivas, cada uma apontando para uma lista de semantica
            GPTemplate template = CreateTestTemplate(config, listaFormulas, listaNumerica);

            GPSolution solution = template.CreateRandomSolution();

            Assert.IsNotNull(solution);
            GPAbstractNode p1 = solution.GetValue("prop1");

            Assert.IsNotNull(p1);
            GPAbstractNode p2 = solution.GetValue("prop2");

            Assert.IsNotNull(p2);
            GPAbstractNode p3 = solution.GetValue("prop3");

            Assert.IsNotNull(p3);
            GPAbstractNode p4 = solution.GetValue("prop4");

            Assert.IsNotNull(p4);

            GPAbstractNode n1 = solution.GetValue("number1");

            Assert.IsNotNull(n1);
            GPAbstractNode n2 = solution.GetValue("number2");

            Assert.IsNotNull(n2);


            GPAbstractNode clonep1 = p1.Clone();

            Assert.IsNotNull(clonep1);
            Assert.IsFalse(clonep1 == p1);
            Assert.IsTrue(clonep1.ToString() == p1.ToString());

            GPSolution cloneSolution = solution.Clone();
            GPSolution solution2     = template.CreateRandomSolution();

            GPSolution childSolution2 = null;
            GPSolution childSolution  = solution.CreateChildWith(solution2, out childSolution2);

            Assert.IsNotNull(childSolution);
            Assert.IsNotNull(childSolution2);
        }
Beispiel #2
0
        public void Mutate()
        {
            IList <GPSolution> nextSolutions = new List <GPSolution>();

            for (int i = 0; i < solutions.Count; i++)
            {
                if (i <= config.elitismRange * solutions.Count / 100)
                {
                    nextSolutions.Add(solutions[i]);
                }
                else if (i <= config.mutationRange * solutions.Count / 100)
                {
                    GPSolution solution = solutions[i];
                    solution.Mutate(20);
                    nextSolutions.Add(solutions[i]);
                }
                else if (i <= config.generateChildRange * solutions.Count / 100)
                {
                    GPSolution solution1 = solutions[Utils.RandomInt(0, config.generateChildRange / 100f * solutions.Count)];
                    GPSolution solution2 = solutions[Utils.RandomInt(0, config.generateChildRange / 100f * solutions.Count)];
                    GPSolution child2    = null;
                    GPSolution child     = solution1.CreateChildWith(solution2, out child2);
                    nextSolutions.Add(child);
                    child.Mutate(20);
                    nextSolutions.Add(child2);
                    child2.Mutate(20);
                    i += 1;
                }
                else
                {
                    nextSolutions.Add(template.CreateRandomSolution());
                }
            }
            solutions.Clear();
            solutions = nextSolutions;

            /*
             *
             * for (int i = config.elitismRange * solutions.Count/100; i < solutions.Count; i++)
             * {
             *  GPSolution solution = solutions[i];
             *
             *  if (Utils.Random(0, 100) < config.spliceChancePerc)
             *  {
             *      //Pego um dos top 85%
             *      GPSolution mateWith = solutions[Utils.Random(0, 85) / 100 * solutions.Count];
             *      solution.SpliceWith(mateWith);
             *  }
             *  if (Utils.Random(0, 100) <= config.mutationRatePerc)
             *  {
             *      solution.Mutate();
             *  }
             *  //TODO: mutate, splice, etc
             * }
             *
             * for (int i = solutions.Count-1;i>=config.unfitRemovalPercent * config.poolSize / 100; i--)
             * {
             *  solutions.RemoveAt(i);
             * }
             * for (int i = config.unfitRemovalPercent * config.poolSize / 100; i < config.poolSize; i++)
             * {
             *  solutions.Add(template.CreateRandomSolution());
             * }*/
        }