public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child; if (random.NextDouble() < CrossoverRate) { child = parent1.Crossover(parent2); } else { child = parent1.Crossover(); //asexual reproduction } child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(dnaSize, random, getRandomGene, fitnessFunction, crossoverType, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
List <Nematode> Reproduction() { var pool = Selection(); if (pool.Count <= 0) { Debug.LogWarning("mating pool is empty."); } var next = new List <Nematode>(); for (int i = 0, n = nematodes.Count; i < n; i++) { int m = Random.Range(0, pool.Count); int d = Random.Range(0, pool.Count); DNA mom = pool[m].DNA; DNA dad = pool[d].DNA; DNA child = mom.Crossover(dad); child.Mutate(mutationRate, mutationScale); next.Add(new Nematode(child)); } return(next); }
public void newPlasmid(DNA <T> parent1, DNA <T> parent2) { DNA <T> child = parent1.Crossover(parent2); child.Mutate(this.mutationRate); newPopulation.Add(child); }
public void SelectMateReproduct(int mutationRate) { int numOfEntries = 0; foreach (DNA element in populationDNA) { numOfEntries = System.Convert.ToInt32(System.Math.Floor(element.fitness * 100)); for (int i = 0; i < numOfEntries; i++) { matingPool.Add(element); } } if (matingPool.Count == 0) { for (int i = 0; i < populationDNA.Length; i++) { matingPool.Add(populationDNA[i]); } } for (int i = 0; i < populationDNA.Length; i++) { int randIntA = random.Next(0, General.Clamp(matingPool.Count - 1, 0, matingPool.Count - 1)); int randIntB = random.Next(0, General.Clamp(matingPool.Count - 1, 0, matingPool.Count - 1)); DNA parentA = matingPool[randIntA]; DNA parentB = matingPool[randIntB]; DNA child = parentA.Crossover(parentB); child.Mutate(mutationRate); populationDNA[i] = child; } matingPool.Clear(); }
public void NewGeneration() { if (Population.Count <= 0) { return; } CalculateFitness(); List <DNA <T> > newPopulation = new List <DNA <T> >(); for (int i = 0; i < Population.Count; i++) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } Population = newPopulation; Generation++; }
//Populate a new generation with offspring of two parents public void CreateNewGeneration() { if (population.Count <= 0) { return; } CalculateFitness(); population.Sort(CompareDNA); newPopulation.Clear(); for (int i = 0; i < population.Count; i++) { if (i < Elitism) { newPopulation.Add(population[i]); } else { DNA parent1 = ChooseParent(); DNA parent2 = ChooseParent(); DNA child = parent1.Crossover(parent2); child.Mutate(mutationRate); newPopulation.Add(child); } } List <DNA> tempList = population; population = newPopulation; newPopulation = tempList; generation++; }
public void NewGeneration() { if (Population.Count <= 0) { return; } CalculateFitness(); newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { DNA <T> parent1 = ChooseParents(); DNA <T> parent2 = ChooseParents(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(this.mutationRate); newPopulation.Add(child); } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
public List <DNA> CrossoverBothWays(DNA sourceDNA, float ratio = 0.5f) { return(new List <DNA>() { Crossover(sourceDNA, ratio), sourceDNA.Crossover(this, ratio) }); }
//set selection void Selection() { SortMatingPool(); List <Genome> newGenomeList = new List <Genome>(new Genome[currentGenomes.Count]); for (int i = 0; i < currentGenomes.Count; i++) { DNA parentA = null; DNA parentB = null; DNA child = null; if (i < 3) { child = matingGenomePool[i].dna; } else { parentA = matingGenomePool[Random.Range(0, matingGenomePool.Count)].dna; parentB = matingGenomePool[Random.Range(0, matingGenomePool.Count)].dna; child = parentA.Crossover(parentB); child.Mutate(); } Genome genome = Instantiate(newGenome, genomeSpawnLocation.position, Quaternion.identity); genome.transform.SetParent(genomeParent); SetGenomeTargetPlatform(genome); genome.SetDNA(child); newGenomeList[i] = genome; } ResetPopulation(); currentGenomes = newGenomeList; }
// Generates new population after the individuals in the first population is setup public void NewGeneration() { if (population.Count <= 0) { return; } // Calculates the total sum of the fitness in the population to be used when choosing a parent CalculatePopulationFitness(); // Sorts the population from highest fitness to lowest population.Sort(compareDNA); newPopulation.Clear(); //Debug.Log(population[0].fitness); // Iterates through the population and either adds individuals through elitism or the result of crossover for (int i = 0; i < population.Count; i++) { // Takes x amount of the fittest individuals and directly adds them to the new population if (i < elitism) { newPopulation.Add(population[i]); } else { // Chooses two parents from the population by having their fitness divided by the fitness sum to create their probability DNA parent1 = ChooseParent(); DNA parent2 = ChooseParent(); // A child of the two parents are made through crossover so there is a 50/50 split between the genes of the parents DNA child = parent1.Crossover(parent2); // Mutation is applied with a 1% to all genes to change by a small amount and is necessary to introduce variance child.Mutation(); // The fitness of the child is calculated based on the target DNA child.fitness = child.CalculateFitness(gameManager.targetDNA); // Data analyzer all new fitness // bestFitness.Add(child.fitness); // The child is added to the new generation newPopulation.Add(child); } } // New generation replaces old generation List <DNA> tempList = population; population = newPopulation; newPopulation = tempList; // Generation counter is incremented generation++; }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); //Population.Reverse(); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); // Just for visual comparisson, no effects on the algorithm // as it will be replaced below child.Fitness = (parent1.Fitness + parent2.Fitness) / 2; child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(dnaSize, random, getRandomGene, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; foreach (var dna in Population) { dna.OldFitness = dna.Fitness; dna.Fitness = 0; } Generation++; }
public void NewGeneration(T[] arr, int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); //Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(arr, dnaSize, random, fitnessFunction, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; //arr = newPopulation.ToArray(); //falta validar mutacion //for (int s = 0; s < newPopulation[s].Genes.Length; s++) //{ // arr[s] = newPopulation[s]; //} Generation++; }
private void PickParentsTopN(int index) { DNA <T> parent1 = Population[index]; index++; DNA <T> parent2 = Population[index]; DNA <T> child = parent1.Crossover(parent2); newPopulation.Add(child); }
private void BeginMating(List <DNA <T> > matingPool) { int elementA = random.Next(matingPool.Count); int elementB = random.Next(matingPool.Count); DNA <T> parentA = matingPool[elementA]; DNA <T> parentB = matingPool[elementB]; DNA <T> child = parentA.Crossover(parentB); newPopulation.Add(child); }
//old seletion algorithm static void Selection() { foreach (DNA d in Population) { d.EvaluateFitness(); } List <DNA> selectionList = Population.OrderByDescending(x => x.Fitness).Take((int)Math.Floor(Convert.ToDecimal(Population.Count * 3 / 4))).ToList(); Population.Clear(); double fitnessSum = 0; foreach (DNA d in selectionList) { fitnessSum += d.Fitness; } while (Population.Count < PopulationSize) { double fit1 = fitnessSum * rnd.NextDouble(); double fit2 = fitnessSum * rnd.NextDouble(); double sum1 = 0; double sum2 = 0; int index1 = 0; int index2 = 0; while (sum1 < fit1) { sum1 += selectionList[index1].Fitness; if (sum1 < fit1) { index1++; } } while (sum2 < fit2) { sum2 += selectionList[index2].Fitness; if (sum2 < fit2) { index2++; } } if (index1 == index2) { continue; } else { DNA child = DNA.Crossover(selectionList[index1], selectionList[index2]); Population.Add(child); } } }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < finalCount; i++) { // Debug.Log("NOVO INDIVIDUO"); if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA parent1 = ChooseParent(); DNA parent2 = ChooseParent(); DNA child = parent1.Crossover(parent2); child.Mutate(MutationRate); // for (int j = 0; j < child.Genes.Count; j++) // { // Debug.Log("Filho Mutado: {" + child.Genes[j].GetValue(0) + ", " + child.Genes[j].GetValue(1) + ", " + child.Genes[j].GetValue(2) + "}"); // } newPopulation.Add(child); } else { newPopulation.Add(new DNA(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <DNA> tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
void Reproduction() { for (int i = 0; i < population.Length; i++) { int a = (int)Random.Range(0, matingPool.Count); int b = (int)Random.Range(0, matingPool.Count); DNA partnerA = matingPool[a]; DNA partnerB = matingPool[b]; DNA child = partnerA.Crossover(partnerB); child.Mutate(mutationRate); population[i] = child; } }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { // Keep only top individuals of the previous generation if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < Population.Count; i++) { if (i < Elitism && i < Population.Count) // Elitism dodaje do nowej populacji najlepsze osobniki, if = 5 to 5 sztuk { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } else { newPopulation.Add(new DNA <T>(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <DNA <T> > tmpList = Population; Population = newPopulation; newPopulation = tmpList; Generation++; }
/// <summary> /// Next we will generate a new population based on algorithmic logic of crossover between two random DNA sequences and adding some mutation into it. /// </summary> public void Generate() { for (int i = 0; i < population.Count; i++) { { int a = RandomProvider.RND.Next(matingPool.Count); int b = RandomProvider.RND.Next(matingPool.Count); // TODO: Avoid duplicates DNA partnerA = matingPool[a]; DNA partnerB = matingPool[b]; DNA child = (DNA)partnerA.Crossover(partnerB); child.Mutate(mutationRate); child.EvalutateFitness(); population[i] = child; } } this.generations++; }
void GeneticRepopulation() { for (int i = 0; i < collectedGenomes.Count; i++) { DNA firstGenome = collectedGenomes[ChooseGenomeFromProbability()]; DNA secondGenome = collectedGenomes[ChooseGenomeFromProbability()]; DNA newGene = DNA.Crossover(firstGenome, secondGenome); creatures.Add(CreateSpecies(newGene)); } for (int i = 0; i < collectedGenomes.Count; i++) { creatures[i].genome.Mutate(mutateRate); } timer = 0; }
private DNA <T> ChooseParentRoundRobin() { DNA <T> parent1 = Population[0]; DNA <T> parent2 = Population[1]; foreach (DNA <T> Individual in Population) { if (parent1.Fitness < Individual.Fitness) { parent1 = Individual; } else if (parent2.Fitness < Individual.Fitness) { parent2 = Individual; } } return(parent1.Crossover(parent2)); }
public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = population.Count + numNewDNA; if (finalCount <= 0) { return; } if (population.Count > 0) { CalculateFitness(); population.Sort(CompareDNA); } newPopulation.Clear(); for (int i = 0; i < finalCount; ++i) { if (i < elitism && i < population.Count) { newPopulation.Add(population[i]); } else if (i < population.Count || crossoverNewDNA) { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(mutationRate); newPopulation.Add(child); } else { population.Add(new DNA <T>(dnaSize, GetRandomGene, FitnessFunction, shouldInitGenes: true)); } } List <DNA <T> > tempList = population; population = newPopulation; newPopulation = tempList; generation++; }
void Reproduction() { Car[] newPopulation = new Car[populationSize]; for (int i = 0; i < population.Length; i++) { int m = Random.Range(0, matingPool.Count - 1); int d = Random.Range(0, matingPool.Count - 1); DNA mom = matingPool[m].dna; DNA dad = matingPool[d].dna; DNA child = mom.Crossover(dad); child.Mutate(mutationRate); newPopulation[i] = InstantiateNewVehicle(vehiclePrefab, child); } for (int i = 0; i < population.Length; i++) { Destroy(population[i].gameObject); } population = newPopulation; }
// Creates the new generation public void CreateNewGeneration() { if (Population.Count <= 0) { return; } CalculateFitness(); Population.Sort(CompareDNA); newPopulation.Clear(); // Do Selection, Crossover, and Mutate to get the new population for (int i = 0; i < Population.Count; i++) { if (i < GenerSurvNum) { newPopulation.Add(Population[i]); } else { DNA <T> parent1 = ChooseParent(0); DNA <T> parent2 = ChooseParent(1); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } } List <DNA <T> > tmp = Population; Population = newPopulation; newPopulation = tmp; Generation++; }
public void NewGeneration() { if (Population.Count <= 0) { return; } CalculateFitness(); List <DNA <T> > newPopulation = new List < DNA <T>(); for (int i = 0; i < Population.Count; i++) // crossover, making a new generations with the same number of individuals as last population { DNA <T> parent1 = ChooseParent(); DNA <T> parent2 = ChooseParent(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } Population = newPopulation; Generation++; }
void Update() { // selection // calculate fitness elite.genes[0] = 0; elite.genes[1] = 0; elite.genes[2] = 0; population[0] = elite; for (int i = 0; i < population.Length; i++) { population[i].CalculateFitness(); if (population[i].fitness > bestScore) { bestScore = population[i].fitness; elite = population[i]; geneRecord.Add(elite.genes); } print("Best Fitness: " + bestScore); } // build mating pool List <DNA> matingPool = new List <DNA>(); for (int i = 0; i < population.Length; i++) { int n = (int)(population[i].fitness * 1000f); for (int j = 0; j < n; j++) { matingPool.Add(population[i]); } } // reproduction for (int i = 0; i < population.Length; i++) { int a = Random.Range(0, matingPool.Count); int b = Random.Range(0, matingPool.Count); DNA partnerA = matingPool[a]; DNA partnerB = matingPool[b]; // crossover DNA child = partnerA.Crossover(partnerB); //mutation child.Mutate(mutationRate); population[i] = child; } // display print("Generation: " + generation); if (bestScore > previousBest) { for (int i = 0; i < cylinders.Length; i++) { Vector3 start = new Vector3(elite.genes[i * 4] * 30 + offset, elite.genes[1 + (i * 4)] * 90, elite.genes[2 + (i * 4)] * 30); Vector3 end = new Vector3(elite.genes[(i * 4) + 4] * 30 + offset, elite.genes[(i * 4) + 5] * 90, elite.genes[(i * 4) + 6] * 30); float diameter = (elite.genes[(i * 4) + 3] + elite.genes[(i * 4) + 7]); RepositionCylinder(cylinders[i], start, end, diameter); // CreateCylinderBetweenPoints(start, end, diameter); // CreateSphere(start, diameter); // CreateSphere(end, diameter); } // offset += 50; } previousBest = bestScore; print("Frames Captured:" + geneRecord.Count); if (geneRecord.Count == 200) { ArrayToCSV(geneRecord, path); print("############### FILE WRITTEN ###############"); } generation++; }
public void NewGeneration(int process) { if (Population.Count <= 0) { return; } CalculateFitness(); List <DNA <T> > newPopulation = new List <DNA <T> >(); if (process == 0) { Debug.Log("RandomParent"); for (int i = 0; i < 20; i++) { DNA <T> parent1 = RouletteWheelSlection(); DNA <T> parent2 = RouletteWheelSlection(); DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } } //} else if (process == 1) { Debug.Log("MedianParent"); Population.Sort((DNA <T> a, DNA <T> b) => { return(a.Fitness > b.Fitness ? -1 : 1); }); for (int j = 0; j < 2; j++) { for (int i = 0; i < 10; i++) { DNA <T> child = HigherThanMedianSelection(i); child.Mutate(MutationRate); newPopulation.Add(child); } } } else if (process == 2) { Debug.Log("BestParent"); Population.Sort((DNA <T> a, DNA <T> b) => { return(a.Fitness > b.Fitness ? -1 : 1); }); List <DNA <T> > betterThenMedian = new List <DNA <T> >(Population); betterThenMedian.RemoveRange(Population.Count / 2, Population.Count / 2); for (int j = 0; j < 2; j++) { for (int i = 0; i < 10; i++) { DNA <T> parent1 = best; //DNA<T> parent2 = FittestParentCrossoverSelection(i); DNA <T> parent2 = betterThenMedian[i]; DNA <T> child = parent1.Crossover(parent2); child.Mutate(MutationRate); newPopulation.Add(child); } } } else if (process == 3) { Debug.Log("SinglePoint"); Population.Sort((DNA <T> a, DNA <T> b) => { return(a.Fitness > b.Fitness ? -1 : 1); }); List <DNA <T> > betterThenMedian = new List <DNA <T> >(Population); betterThenMedian.RemoveRange(Population.Count / 2, Population.Count / 2); int corssoverPoint = UnityEngine.Random.Range(1, 4); //for (int j = 0; j < 2; j++) //{ for (int i = 0; i < 10; i++) { DNA <T> parent1 = best; DNA <T> parent2 = betterThenMedian[i]; DNA <T>[] children = new DNA <T> [2]; children = parent1.SinglePointCrossover(parent2, corssoverPoint); for (int k = 0; k < 2; k++) { children[k].Mutate(MutationRate); newPopulation.Add(children[k]); } } } //}*/ else if (process == 4) { Debug.Log("MultiPoint"); Population.Sort((DNA <T> a, DNA <T> b) => { return(a.Fitness > b.Fitness ? -1 : 1); }); List <DNA <T> > betterThenMedian = new List <DNA <T> >(Population); betterThenMedian.RemoveRange(Population.Count / 2, Population.Count / 2); int corssoverPoint = UnityEngine.Random.Range(1, 5); for (int i = 0; i < 10; i++) { DNA <T> parent1 = best; DNA <T> parent2 = betterThenMedian[i]; DNA <T>[] children = new DNA <T> [2]; children = parent1.MultiplePointCrossover(parent2); for (int k = 0; k < 2; k++) { children[k].Mutate(MutationRate); newPopulation.Add(children[k]); } } } Population = newPopulation; Generation++; aboveAverageGenes.Clear(); }
void Update() { // selection // calculate fitness for (int i = 0; i < population.Length; i++) { population[i].CalculateFitness(target); } // build mating pool List <DNA> matingPool = new List <DNA>(); for (int i = 0; i < population.Length; i++) { int n = (int)(population[i].fitness * 10000f); print(n); for (int j = 0; j < n; j++) { matingPool.Add(population[i]); } } // reproduction for (int i = 0; i < population.Length; i++) { int a = Random.Range(0, matingPool.Count); int b = Random.Range(0, matingPool.Count); DNA partnerA = matingPool[a]; DNA partnerB = matingPool[b]; // crossover DNA child = partnerA.Crossover(partnerB); //mutation child.Mutate(mutationRate); population[i] = child; } // display for (int i = 0; i < population.Length; i++) { for (int j = 0; j < target.Length; j++) { Vector3 temp = new Vector3(population[i].genes[j].x + (15 * i), population[i].genes[j].y, population[i].genes[j].z); spheres[i, j].transform.position = temp; if (population[i].genes[j] == target[j]) { spheres[i, j].GetComponent <Renderer>().material.color = new Color(1, 0, 0); } else { spheres[i, j].GetComponent <Renderer>().material.color = colors[i]; } } } print(generation); generation++; }
//public void NewGeneration(int dnaTargetSize, int numNewDNA = 0, bool crossoverNewDNA = false) public void NewGeneration(int numNewDNA = 0, bool crossoverNewDNA = false) { int finalCount = Population.Count + numNewDNA; if (finalCount <= 0) { return; } if (Population.Count > 0) { CalculateFitness(); Population.Sort(CompareDNA); //testando fitness /*for (int k = 0; k < 5; k++) * { * DNA test = Population[k]; * int spykeCount = 0; * int opossumCount = 0; * int eagleCount = 0; * int lifeCount = 0; * * for (int j = 0; j < test.Genes.Count; j++) * { * switch ((int)test.Genes[j].GetValue(test.Genes[j].Length - 1)) * { * case 0: * spykeCount++; * break; * case 1: * opossumCount++; * break; * case 2: * eagleCount++; * break; * case 3: * lifeCount++; * break; * } * } * Debug.Log("Valores do K = " + k + " com FITNESS = " + test.Fitness + " : -> SPYKE: " + spykeCount + " || OPOSSUM: " + opossumCount + " || EAGLE: " + eagleCount + " || LIFE: " + lifeCount); * * }*/ } newPopulation.Clear(); for (int i = 0; i < finalCount; i++) { // Debug.Log("NOVO INDIVIDUO"); if (i < Elitism && i < Population.Count) { newPopulation.Add(Population[i]); } else if (i < Population.Count || crossoverNewDNA) { DNA parent1 = ChooseParent(); DNA parent2 = ChooseParent(); DNA child = parent1.Crossover(parent2); child.Mutate(MutationRate); // for (int j = 0; j < child.Genes.Count; j++) // { // Debug.Log("Filho Mutado: {" + child.Genes[j].GetValue(0) + ", " + child.Genes[j].GetValue(1) + ", " + child.Genes[j].GetValue(2) + "}"); // } newPopulation.Add(child); } else { /*if (dnaTargetSize > 50) * { * dnaTargetSize = 50; * }*/ //Debug.Log("DNA SIZE AQUI: " + dnaTargetSize); newPopulation.Add(new DNA(dnaSize, random, getRandomGene, fitnessFunction, shouldInitGenes: true)); } } List <DNA> tmpList = Population; /*for (int i = 0; i < tmpList.Count; i++) * { * Debug.Log("SIZE NO FOR: " + tmpList[i].size); * }*/ Population = newPopulation; newPopulation = tmpList; Generation++; }