Essays.club - TCC, Modelos de monografias, Trabalhos de universidades, Ensaios, Bibliografias
Pesquisar

Algoritmo Genético

Por:   •  21/2/2018  •  3.009 Palavras (13 Páginas)  •  352 Visualizações

Página 1 de 13

...

apos " "não há solucção"+ i

+ " iteracoes");

System.out.println("Melhor Elemento =" + elementoMelhor);

System.out.println("Status de aceitacao =" + melhoraceitacao(elementoMelhor));

Erro = true;

}

finalIteracoes += i;

if (i > MaiorIteracoes)

totaliteracoes = i;

}

private static void criarPopulacao(Set populacao) {

while (populacao.size() < 10) {

String elementoI = gerarIndividuo(8);

println("Elemento =" + elementoI);

populacao.add(elementoI);

}

}

//Mantem a melhor populacao e devolve o melhor elemento

private static String GeneticoAlg(Set populacao,

double mutacaoP, int aceitaNew) {

String melhor = null;

Set filhosPop = new HashSet();

int sizePop = populacao.size();

while (filhosPop.size() < sizePop) {

String p1 = selecionarAleatorio(populacao, "");

String p2 = selecionarAleatorio(populacao, p1);

String filho = crossover(p1, p2);

if (randomico.nextDouble() <= mutacaoP) {

int funcaoAceitacao = aceitacao(filho);

if (funcaoAceitacao <= aceitaNew)

filho = mutate(filho);

}

filhosPop.add(filho);

}

// incrementa um par dos melhores pais

Object[] pais = populacao.toArray();

int[] f = new int[pais.length];

int melhorF = -1;

for (int i = 0; i < pais.length; i++) {

f[i] = aceitacao((String) pais[i]);

if (melhorF < f[i]) {

melhorF = f[i];

}

}

populacao.clear();

while (populacao.size() < 2) {

for (int i = 0; i < f.length; i++) {

if (f[i] == melhorF) {

populacao.add((String) pais[i]);

}

if (populacao.size() == 2)

break;

}

melhorF--;

}

filhosPop.addAll(populacao);

Object[] pop = filhosPop.toArray();

f = new int[pop.length];

melhorF = -1;

for (int i = 0; i < f.length; i++) {

f[i] = aceitacao((String) pop[i]);

if (melhorF < f[i]) {

melhorF = f[i];

melhor = (String) pop[i];

}

}

populacao.clear();

while (populacao.size() < sizePop) {

if (melhorF < 0) {

// should never happen...

System.out.println("???????");

break;

}

for (int i = 0; i < f.length; i++) {

if (f[i] == melhorF && populacao.size() < sizePop) {

populacao.add((String) pop[i]);

}

}

melhorF--;

}

return melhor;

...

Baixar como  txt (8.2 Kb)   pdf (55.4 Kb)   docx (18.2 Kb)  
Continuar por mais 12 páginas »
Disponível apenas no Essays.club