예제 #1
0
파일: gp.cpp 프로젝트: NFGream/embeddedGPU
void next_gen()
{
	char *parent1, *parent2;
	char new_gen[POP_SIZE][MAX_IND_LEN];
	int cntr = 0;

	for(int j=0; j<POP_SIZE; j++)
		for(int k=0; k<MAX_IND_LEN; k++)
			new_gen[j][k] = '*';

	for(int i=0; i<(int)POP_SIZE * CROSSOVER_RATE; i++)
	{
		parent1 = pop[tournament(2)];
		parent2 = pop[tournament(2)];
		cross_over(parent1, parent2, new_gen[cntr++]);
	}
	for(int i=0; i<(int)POP_SIZE * MUTATION_RATE; i++)
	{
		parent1 = pop[tournament(2)];
		mutate(parent1, new_gen[cntr++]);
	}
	for(int i=0; i<(int)POP_SIZE * REPRODUCT_RATE; i++)
	{
		parent1 = pop[tournament(2)];
		reproduct(parent1, new_gen[cntr++]);
	}
	for (int i=0; i<POP_SIZE; i++)
		for (int j=0; j<MAX_IND_LEN; j++)
			pop[i][j] = new_gen[i][j];
}
예제 #2
0
QVector<Genome> GenAlg::epoch(QVector<Genome> prev_generation)
{
    m_population = prev_generation;
    reset();
    std::sort(m_population.begin(), m_population.end());

    calculate_stats();

    QVector<Genome> new_pop;

    grab_N_best(Globs::NUM_ELITES, Globs::NUM_COPIES, new_pop);

    while(new_pop.size() < m_population_size)
    {
        Genome mom = select_roulette();
        Genome dad = select_roulette();
        Genome baby = cross_over(mom, dad);
        mutate(baby);
        new_pop.append(baby);
    }
    return std::move(new_pop);
}
예제 #3
0
파일: ga.c 프로젝트: haiy/XF_PRISM
/*
psz the population size
n the generations number to run
pc the cross over probability
pm the mutation probability
*/
chro_ptr ga(mic_matrix M,int psz,int n,float pc,float pm)
{
    srand((double)time(NULL));
    population pop;
    pop.n=psz;
    pop.acu=(float*)malloc(sizeof(float)*pop.n);
    if(pop.acu==NULL)
    {
        puts("GA acu memory error!");
        exit(1);
    }
    int i=0;
    for(i=0;i<pop.n;i++)
    {
        pop.acu[i]=0;
    }
    pop.pc=pc;
    pop.pm=pm;
    ini_pop(M,&pop);
    population *S=ini_tmpop(pop);
    int T=pop.n;
    int Gn=0;
    int N=n; 
    do
    {//代数不够或者没达到最优解
//#define check_ga
#ifdef check_ga
        printf("The %d generation's best %f.\n",Gn,pop.m[0].sig);
#endif
        int n=0;
        cal_acu(&pop);
        do
        {//种群的成员数不够
            chro_ptr father=sel_one(pop);
            chro_ptr mother=sel_one(pop);
            cpy_chro(father,&(S->m[n]));
            cpy_chro(mother,&(S->m[n+1]));
            if(CY(pop))
            {
                cross_over(&(S->m[n]),&(S->m[n+1]));
            }
            if(MT(pop))
            {
                mutation(&(S->m[n]));
            }
            if(MT(pop))
            {
                mutation(&(S->m[n+1]));
            }
            n+=2;
        }while(n<T);
        update_pop(S,M);
        elitist_sel(&pop,S);
    }while(++Gn<N);
    chro_ptr bst=(chro_ptr)malloc(sizeof(chrosome));
    bst->l=pop.m[0].l;
    bst->chro=(int *)malloc(sizeof(int)*(bst->l));
    cpy_chro(&(pop.m[0]),bst);
    brk_pop(&pop);
    brk_pop(S);
    return bst;
}