void cria_nova_geracao(){ int i; for(i=0;i<TAMANHO_POPULACAO-1;i++) { //Seleção torneio(i, populacao, &pai1); torneio(i+1, populacao, &pai2); //Recombinação recombinacao(&pai1, &pai2, &filho1, &filho2, TAXA_DE_RECOMBINACAO); //Mutação mutacao(&filho1, TAXA_DE_MUTACAO); mutacao(&filho2, TAXA_DE_MUTACAO); filho1.aptidao = funcao_de_avaliacao(&filho1); filho2.aptidao = funcao_de_avaliacao(&filho2); adiciona_individuo(&filho1,i); adiciona_individuo(&filho2,++i); } //Ordena a geração atual qsort(populacao, TAMANHO_POPULACAO, sizeof(individuo), (int(*)(const void*, const void*))compara_individuo); //Ordena a nova geração qsort(copia_populacao, TAMANHO_POPULACAO, sizeof(individuo), (int(*)(const void*, const void*))compara_individuo); //Mantém a elite e substitui o restante pelos melhores da nova geração int j = 0, l; for(i = ELITE; i < TAMANHO_POPULACAO;i++,j++){ for(l=0;l<TAMANHO_INDIVIDUO;l++){ populacao[i].genotipo[l] = copia_populacao[j].genotipo[l]; } } }
int main(int argc, char **argv) { int next_option, verbose = 0; int i,r; Populacao *pop; Individuo *rind1; Individuo *rind2; int numtorn = 1000000; int iniind = 40; int tampop = 100; double rcrossover = 0.9; double rmutation = 0.01; int useexternal = 0; strcpy(programname, "fitextern"); tammaxestimado = 50; fitness = fitnessmult; //fitness = fitnessinst; const char *short_options = "hvt:n:c:m:i:ep:H:"; const struct option long_options[] = { {"help", 0, NULL, 'h'}, {"tournament", 1, NULL, 't'}, {"population", 1, NULL, 'n'}, {"iniind", 1, NULL, 'i'}, {"tammaxestimado", 1, NULL, 'H'}, {"crossover", 1, NULL, 'c'}, {"mutation", 1, NULL, 'm'}, {"extern", 0, NULL, 'e'}, {"program", 1, NULL, 'p'}, {"verbose", 0, NULL, 'v'}, {NULL, 0, NULL, 0} }; program_name = argv[0]; do { next_option = getopt_long(argc, argv, short_options, long_options, NULL); switch (next_option) { case 'h': print_usage(stdout, 0); break; case 'v': verbose = 1; break; case '?': /* opcao invalida*/ print_usage(stderr, 1); break; case 't': numtorn = atoi(optarg); break; case 'H': tammaxestimado = atoi(optarg); break; case 'n': tampop = atoi(optarg); break; case 'i': iniind = atoi(optarg); break; case 'c': rcrossover = atof(optarg); break; case 'm': rmutation = atof(optarg); break; case 'e': useexternal = 1; break; case 'p': strncpy(programname, optarg, 1024); break; case -1: break; default: abort(); } } while (next_option != -1); if (useexternal) fitness = fitexternal; initGP(); pop = makePopulacaoRand(tampop, iniind, rcrossover, rmutation); for(i = 0; i < numtorn; i++) r = torneio(pop, fitness, verbose); //printPopulacao(stdout, pop); freePopulacao(pop); return 0; }