bool DoGAIteration() { bool foundBest = false; uint numberOfReplacements = (uint)Mathf.RoundToInt(replacementFactor * populationSize); if (bestIndividual == null) { bestIndividual = population.BestIndividual; } else if (optimizationFunction.getFitnessOfIndividual(population.BestIndividual) < optimizationFunction.getFitnessOfIndividual(bestIndividual)) { bestIndividual = population.BestIndividual; } bestSolution = optimizationFunction.getFitnessOfIndividual(bestIndividual); if (bestSolution == optimizationFunction.MinVal && goal == OptimizationType.MINIMIZE) { return(foundBest = true); } //Selection: devuelve un conjunto de padres para la creación de nuevos individuos HashSet <Individual> nonRepParents; List <Individual> nextGenParents = population.RouletteSelection((uint)numParentsPerGen, out nonRepParents); Debug.Log("<b><color=teal>[3] SELECTION DONE </color></b>"); Population.PrintSelectedChromosomes(nextGenParents); //Crossover: creación de descendencia List <Individual> nextGenChildren = population.UniformCrossOver(nextGenParents, numberOfReplacements); Debug.Log("<b><color=teal>[4] CROSSOVER DONE </color></b>"); Population.PrintSelectedChromosomes(nextGenChildren); //Mutation: mutación de algunos bits population.Mutation(nextGenChildren); Debug.Log("<b><color=teal>[5] MUTATION DONE </color></b>"); Population.PrintSelectedChromosomes(nextGenChildren); if (numberOfReplacements != nextGenChildren.Count) { Debug.LogException(new System.Exception("TO REPLACE NOT THE SAME AS DELETED ELEMENTS")); } Debug.Log("NUMBER OF REPLACEMENTS TO PERFORM " + numberOfReplacements); Debug.Log("NUMBER OF CHILDREN BEING GENERATED FOR NEXT ITERATION" + nextGenChildren.Count); //Reemplazo y nueva iteración population.ReplaceN_Worst((int)numberOfReplacements, nextGenChildren); iteration++; return(foundBest); }
//Step 1: Initialization of chromosomes (each gene is a 0 or a 1; a bit) public bool InitPopulation(int popSize, float mutationFactor, int rndSeed = 2) { if (popSize > optimizationFunction.Width * optimizationFunction.Height || popSize <= 0) { PopulationSize = 0; return(false); } if (mutationFactor < 0 || mutationFactor > 1) { Debug.LogException(new System.Exception("Invalid Mutation Factor")); return(false); } this.mutationFactor = mutationFactor; PopulationSize = popSize; //Creación de individuos de forma aleatoria dentro del Grid del problema popIndividuals = new Individual[PopulationSize]; IndivRanking = new SortedList <float, List <Individual> >(); for (int i = 0; i < PopulationSize; i++) { popIndividuals[i] = new Individual("i" + (i + 1), optimizationFunction.Width, optimizationFunction.Height); if (!IndivRanking.ContainsKey(optimizationFunction.getFitnessOfIndividual(popIndividuals[i]))) { IndivRanking.Add(optimizationFunction.getFitnessOfIndividual(popIndividuals[i]), new List <Individual> { popIndividuals[i] }); } else { List <Individual> inds; IndivRanking.TryGetValue(optimizationFunction.getFitnessOfIndividual(popIndividuals[i]), out inds); inds.Add(popIndividuals[i]); } } return(true); }