コード例 #1
0
/*
 * 生成した2個体を母集団の中で適合度の悪い2個体を入れ替え
 * この時、Population配列は適合度でソートされているものとする
 */
void swap_population(void)
{
	Population[POP_SIZE - 1] = Children[0];
	calc_fitness(POP_SIZE - 1);
	Population[POP_SIZE - 2] = Children[1];
	calc_fitness(POP_SIZE - 2);
}
コード例 #2
0
ファイル: main.cpp プロジェクト: shubh24/MfoPaper
int main()
{
    srand(time(NULL));
    data dat;
    readfile(dat, "../../data/ks_19_0");
    int items = dat[0].value;
    population *pop, popalpha;
    initialize_population(popalpha, items);
    pop = &popalpha;
    //print(*pop);

    bat best = get_best(*pop);

    int iterations = 200;
    while(iterations > 0){
        new_solutions(*pop,dat);
        calc_fitness(*pop, dat);
        if(getrand()> 0.5){
            best = get_best(*pop);
            move_bat(best, rand()%5, dat); // amplitude is 2
        }

        if(fitness(best, dat) > fitness((*pop).best, dat))
            (*pop).best = best;

        iterations--;
    }
    print(*pop);
}
コード例 #3
0
ファイル: sharing_selection.c プロジェクト: hw5773/study
int selection(int *p1, int *p2)
{
	int i, j, num[2], point;
	double sum, sum_of_fitness;
	unsigned long seed = get_nano_seconds();
	srand(seed);

	sum_of_fitness = calc_fitness();

	for (i=0; i<2; i++)
	{
		point = rand() % (int)(sum_of_fitness*1000000);
		sum = 0;
		
		for (j=1; j<=N; j++)
		{
			sum += sfitness[j]*1000000;
			if ((double)point < sum)
			{
				num[i] = j;
				break;
			}
		}
	}

	*p1 = num[0];
	*p2 = num[1];			

	return 1;
}
コード例 #4
0
/*
 * 母集団内の全個体の適合度を計算
 */
void calc_fit_all(void)
{
	int i;

	for(i = 0; i < POP_SIZE; i++){
		calc_fitness(i);
	}
}
コード例 #5
0
/*
 * 母集団をランダムに初期化
 */
void init_chrom(void)
{
	int i, j;

	for(i = 0; i < POP_SIZE; i++){
		for(j = 0; j < CHROM_LEN; j++){
			// 遺伝子の各要素を0/1でランダムに初期化
			Population[i].Chromsome[j] = rand() % 2;
		}
		calc_fitness(i);
	}
}
コード例 #6
0
int main(void)
{	
	int popsize = 0, max_iter = 0;
	char target[128];
	char str_buffer[64];

	FILE *fp = fopen("conf", "r");
	fscanf(fp, "%s %d", &str_buffer, &popsize);
	fscanf(fp, "%s %s", &str_buffer, &target);
	fscanf(fp, "%s %d", &str_buffer, &max_iter);

	int target_length = strlen(target);

	srand(time(NULL));

	printf("Genetic Algorithm to find a user-specified target string\n");
	printf("Quinn Thibeault - 2015\n");
	printf("Target string: %s Length: %d\n", target, target_length);
	printf("Population Size: %d\n", popsize);
	printf("Maximum Iterations: %d\n\n", max_iter);
	
	struct organism population[POPSIZE], buffer[POPSIZE];
	
	struct organism *p_pop = population;
	struct organism *p_buf = buffer;

	init_population(p_pop, target_length);
	init_population(p_buf, target_length);

	gen_random_population(p_pop, target_length);
	
	
	for(int i=0;i<ITER; ++i){
		calc_fitness(p_pop, target, target_length);
		sort_by_fitness(p_pop);
		//print_most_fit(p_pop);
		
		if(p_pop->fitness == 0){
			printf("Number of generations: %d\n", i);
			break;
		}

		regen_population(p_pop, p_buf, target_length);
		swap(&p_pop, &p_buf);
	}
	
	print_most_fit(p_pop);	
	
	return 0;
}
コード例 #7
0
ファイル: GA.cpp プロジェクト: caomw/GeneticAlgorithm
// calculate fitness for all individuals
void GA::CalcFitness(void)
{
  for(auto &individual : this->population_) {
    calc_fitness(&individual);
  }
}