예제 #1
0
파일: source.cpp 프로젝트: szarma/poprest
void Source::reproduction(const gsl_rng *rand){
        
        assign_fitness();

        gsl_ran_multinomial(rand, pop_size, pop_size, fitness, off_numbers);

        next_generation(rand);
        mutate_population(rand);
}
//~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~;~//
void Population::reproduce(Constraint constraints[], int n)
{
	assign_fitness();
	//old population
	member old_members[POP_SIZE] = members;

	member mother;
	member father;
	//The crossover between father, mother and mutate
	int threshold_1 = MUTATION_RATE/.2, threshold_2 = MUTATION_RATE * 10.;
	//for each member of the population
	for(int i = 0; i < POP_SIZE; i++)
	{
		//Selects parents
		mother = old_members[binary_search(fitness_total,
				POP_SIZE,
				rand()%int(fitness_total[POP_SIZE-1]))];
		father = old_members[binary_search(fitness_total,
				POP_SIZE,
				rand()%int(fitness_total[POP_SIZE-1]))];
		//goes through each trait and selects where it comes from
		for(int c = 0; c < n; c++)
		{
			int rnd = rand() % 10;
			if(rnd <= threshold_1)
			{
				//gets trait from mother
			    members[i].values[c] = mother.values[c];
			}
			else if(rnd <= threshold_2)
			{
			    //gets trait from father
			    members[i].values[c] = father.values[c];
			}
			else
			{
			    //mutates (completely random)
				members[i].values[c] = (rand()/double(RAND_MAX))*(constraints[c].max -
				constraints[c].min) +
				constraints[c].min;
			}
			if(!constraints[c].within_constraint(members[i].values[c]))
			{
			    members[i].values[c] = (rand()/double(RAND_MAX))*(constraints[c].max -
				constraints[c].min) +
				constraints[c].min;
			}
		}
	}
}
예제 #3
0
파일: Colony.cpp 프로젝트: szarma/poprest
void Colony::reproduction(const gsl_rng *rand){
        int k,j,new_pop_size;
        double pop_fitness;
        assign_fitness();

        pop_fitness = 0.;
        for(j=0;j<pop_size;j++)
            {
            pop_fitness += fitness[j];
            }
        pop_fitness = pop_fitness*max_fitness;
        new_pop_size = gsl_ran_poisson(rand,pop_fitness);
//	printf("fitness: %lf\tnew pop size: %i\n", pop_fitness, new_pop_size);

        if (new_pop_size==0)
            {
             printf("extinct\n");
            exit(0);
            }
        if (new_pop_size>limit)
            {
            printf("established\n");
            exit(0);
            }

        gsl_ran_multinomial(rand, pop_size, new_pop_size, fitness, off_numbers);

        next_generation(rand);
        pop_size = new_pop_size;
        mutate_population(rand);

        for(j=pop_size;j<limit;j++)
            {
            for(k=0; k<n_del; k++)
                {
                del_matrix[j][k] = 0;
                }
            for(k=0; k<n_env; k++)
                {
                env_matrix[j][k] = 0;
                }
            }
};