public Gene(Gene mother, Gene father, bool mutate) { // Inherit chromosomes from "parents". chromosomes = new Chromosome[MAX_CHROMOSOMES]; chromosomes[0] = mother.chromosomes[0]; chromosomes[1] = mother.chromosomes[1]; chromosomes[2] = Genetic.Random.NextBool() ? mother.chromosomes[2] : father.chromosomes[2]; chromosomes[3] = father.chromosomes[3]; chromosomes[4] = father.chromosomes[4]; if (mutate) { int firstToMutate = Genetic.Random.Next(0, MAX_CHROMOSOMES); int secondToMutate = Genetic.Random.Next(0, MAX_CHROMOSOMES); chromosomes[firstToMutate] = PredefinedChromosomes[Genetic.Random.Next(0, PredefinedChromosomes.Length)]; chromosomes[secondToMutate] = PredefinedChromosomes[Genetic.Random.Next(0, PredefinedChromosomes.Length)]; } ComputeWeight(); ComputeValue(); CheckIfIsDesirable(); }
// gera primeira população randomica, está gerando número randomico para cada vértice. Esse número randomico varia conforme o número de vértices disponíveis. public void GenerateRandomPopulation() { for (int k = 0; k < this.tamanho_populacao; k++) // tamanho da população --> rever { Gene newInd = new Gene(this.grafo.GetNumNodes()); newInd.InitializeIndividuo(); newInd.ShuffleIndividuo(); population.Add(k, newInd); } }
private void GenerateNextGeneration(ref List <Gene> genes) { if (genes.Count == 0) { while (genes.Count < GenerationSize) { genes.Add(new Gene()); } return; } List <Gene> newGenes = new List <Gene>(); for (int i = 0; i + 1 < genes.Count; i += 2) { ComputeNextMutation(); Gene mother = genes[i]; Gene father = genes[i + 1]; Gene siblingA = new Gene(mother, father, mutate); Gene siblingB = new Gene(father, mother, mutate); if (siblingA.IsDesirableGene) { newGenes.Add(siblingA); } if (siblingB.IsDesirableGene) { newGenes.Add(siblingB); } if (mutate) { mutate = false; } mutationCounter++; } while (newGenes.Count < GenerationSize) { Gene gene = new Gene(); if (gene.IsDesirableGene) { newGenes.Add(gene); } } genes = newGenes; }
public bool AreGenesAttempted(List <Gene> listGenes) { bool areAttempted = false; int countListGenes = listGenes.Count; if (countListGenes == 0) { return(true); } foreach (var list in _listOfListsGenesAttempted) { if (countListGenes != list.Count) { continue; } for (int i = 0; i < list.Count; i++) { Gene attemptedGene = list[i]; bool isFound = false; foreach (var gene in listGenes) { if (gene.GetType() == attemptedGene.GetType()) { isFound = true; break; } } if (!isFound) { break; } if (i == list.Count - 1) { areAttempted = true; } } if (areAttempted) { break; } } return(areAttempted); }
private void CopyBestOne(Gene ind1, Gene ind2) { if (ind1.avaliationValue > ind2.avaliationValue) { Gene novoGene = new Gene(ind2.individuo.Length); ind2.individuo.CopyTo(novoGene.individuo, 0); novoGene.avaliationValue = ind2.avaliationValue; populationBuff.Add(populationBuff.Count, novoGene); } else { Gene novoGene = new Gene(ind1.individuo.Length); ind1.individuo.CopyTo(novoGene.individuo, 0); novoGene.avaliationValue = ind1.avaliationValue; populationBuff.Add(populationBuff.Count, novoGene); } }
//Avalia toda a população public void AvaliateFunction() { foreach (DictionaryEntry gene in this.population) { ((Gene)gene.Value).AvaliateGene(this.grafo); if (isFirst == false) // seta solução arbitraria { Gene novaSolucao = ((Gene)gene.Value); solution = new Gene(novaSolucao.individuo.Length); novaSolucao.individuo.CopyTo(solution.individuo, 0); solution.avaliationValue = novaSolucao.avaliationValue; isFirst = true; } if (((Gene)gene.Value).avaliationValue < solution.avaliationValue) // guarda o melhor gene { Gene novaSolucao = ((Gene)gene.Value); solution = new Gene(novaSolucao.individuo.Length); novaSolucao.individuo.CopyTo(solution.individuo, 0); solution.avaliationValue = novaSolucao.avaliationValue; EhDiferente = true; } } }
//http://creationwiki.org/pt/Algoritmo_gen%C3%A9tico#Order_crossover_.28OX.29 // me basiei no crossOver OX qe é o q o guilherme implementou. private void CrossOver(Gene ind1, Gene ind2) { Gene son = new Gene(this.grafo.GetNumNodes()); Gene son2 = new Gene(this.grafo.GetNumNodes()); int cut = staticRandom.Next(grafo.GetNumNodes()); int cut2 = staticRandom.Next(grafo.GetNumNodes()); while (cut >= cut2) // faz com que os cortes não sejam no msm ponto { cut = staticRandom.Next(grafo.GetNumNodes()); cut2 = staticRandom.Next(grafo.GetNumNodes()); } //atribui os valores de cada um dos filhos na faixa. if (cut2 > cut) { int[] buff1 = new int[this.grafo.GetNumNodes()]; int[] buff2 = new int[this.grafo.GetNumNodes()]; for (int i = cut; i < cut2; i++) { son.individuo[i] = ind1.individuo[i]; son2.individuo[i] = ind2.individuo[i]; } int coun = 0; for (int k = cut2; k < this.grafo.GetNumNodes(); k++) // registra a lista auxiliar para completar o filho , depois do corte { buff1[coun] = ind1.individuo[k]; buff2[coun] = ind2.individuo[k]; coun++; } for (int k = 0; k < cut2; k++) // antes do primeiro corte { buff1[coun] = ind1.individuo[k]; buff2[coun] = ind2.individuo[k]; coun++; } //completa os filhos int coun2 = cut2; int coun3 = cut2; for (int k = 0; k < buff2.Length; k++) { if (son.individuo.Contains(buff2[k]) == false) { son.individuo[coun2] = buff2[k]; coun2++; } if (son2.individuo.Contains(buff1[k]) == false) { son2.individuo[coun3] = buff1[k]; coun3++; } if (coun2 >= buff2.Length) { coun2 = 0; } if (coun3 >= buff2.Length) { coun3 = 0; } } } //adiciona elementos na população populationBuff.Add(populationBuff.Count, son); populationBuff.Add(populationBuff.Count, son2); }