public void TestGPInicial()
        {
            GPSolutionDefinition sh            = new GPSolutionDefinition(config);
            SemanticaList        listaFormulas = CriaListaDefault(sh);
            SemanticaList        listaNumerica = CriaListaNumerica(sh);

            GPTemplate template = CreateTestTemplate(config, listaFormulas, listaNumerica);
            GPPool     pool     = new GPPool(config);

            Assert.IsTrue(config.poolSize > 0);
            Assert.IsTrue(config.elitismRange > 0);
            Assert.IsTrue(config.mutationRatePerc > 0);
            Assert.IsTrue(config.mutationRange > 0);
            Assert.IsTrue(config.poolSize > 0);

            pool.InitPool(template);


            Assert.IsTrue(pool.solutions.Count > 0);

            //randomizo valores iniciais para fazer ordenação
            Random rnd = new Random();

            for (int i = 0; i < pool.solutions.Count; i++)
            {
                GPSolution solution = pool.solutions[i];
                solution.fitnessResult = rnd.Next(1, 10000);
            }


            pool.SortFitness();
            for (int i = 1; i < pool.solutions.Count; i++)
            {
                Assert.IsTrue(pool.solutions[i - 1].fitnessResult >= pool.solutions[i].fitnessResult);
            }

            GPSolution sol1 = pool.solutions[1];
            GPSolution sol2 = pool.solutions[2];

            string splicedKey = sol1.SpliceWith(sol2);

            Assert.IsNotNull(splicedKey);

            string strOriginal = sol1.GetValue(splicedKey).ToString();

            sol1.Mutate(sol1.GetValue(splicedKey), splicedKey);
            string strMutated = sol1.GetValue(splicedKey).ToString();
            //Assert.IsFalse(strOriginal == strMutated,"Não mutou: "+strOriginal);

            GPSolution badSolution = pool.solutions[pool.solutions.Count - 1];

            pool.Mutate();
            Assert.IsFalse(pool.solutions.Contains(badSolution));
            Assert.IsTrue(pool.solutions.Count == config.poolSize, pool.solutions.Count + "<>" + config.poolSize);
        }
Пример #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());
             * }*/
        }