Exemplo n.º 1
0
/* Create new metapopulation */
struct metapopulation * create_metapopulation(struct param *par){
	int i, npop, nsus, maxnpat, nini ;
	npop = par->npop;
	nsus = par->nsus;
	maxnpat = npop*nsus;
	nini = par->nstart;

	/* create pointer to metapopulation */
	struct metapopulation *out;
	out = (struct metapopulation *) calloc(1, sizeof(struct metapopulation));
	if(out == NULL){
		fprintf(stderr, "\n[in: population.c->create_metapopulation]\nNo memory left for creating new metapopulation. Exiting.\n");
		exit(1);
	}

	/* set maxnpat and npop */
	out->maxnpat = maxnpat;
	out->npop = npop;

	/* allocate pathogen array */
	out->pathogens = (struct pathogen **) calloc(maxnpat, sizeof(struct pathogen *));
	if(out->pathogens == NULL){
		fprintf(stderr, "\n[in: population.c->create_metapopulation]\nNo memory left for creating pathogen array in the metapopulation. Exiting.\n");
		exit(1);
	}

	/* allocate population array */
	out->populations = (struct population **) calloc(npop, sizeof(struct population *));
	if(out->populations == NULL){
		fprintf(stderr, "\n[in: population.c->create_metapopulation]\nNo memory left for creating populations array in the metapopulation. Exiting.\n");
		exit(1);
	}


	/* fill in the pathogens and popid arrays */
	for(i=0;i<maxnpat;i++){
		(out->pathogens)[i] = create_pathogen();
		if(i<nini){ /* there are nini intial pathogens in the metapopulation */
			(out->pathogens[i])->age = 1; /* 'active' pathogen */
			(out->pathogens[i])->popid = 0;

		} else {
			(out->pathogens[i])->age = -1; /* 'neutralised' pathogen */
			(out->pathogens[i])->popid = -1;
		}
	}

	/* fill in the populations arrays */
	out->populations[0] = create_population(nsus, nini, 0);
	for(i=1;i<npop;i++) {
		out->populations[i] = create_population(nsus, 0, 0);
	}

	return out;
}
Exemplo n.º 2
0
int main()
{
	int x, y;
	int check = 0;
	int pop_size = 1;
	int bit_count = 2;
	int data_count = 2;
	
	population *pop = create_population(pop_size, bit_count, data_count);
	
	while(1)
	{			 	
		printf("x : ");
		scanf("%d", &x);
		printf("y : ");
		scanf("%d", &y);
	
	    if(x<0 || x>14)// 오목판을 넘어가는 숫자를 입력시 다시 입력 
	    {
			check = 1;
		}
		
		else if(y<0 || y>14)	
		{
			check = 1;
		}
		
		if(check == 1)
		{
			printf("다시 입력 하세요\n");
			check = 0;
			continue;
		}
		
		if(board[x][y] == 1)// 중복을막음 
		{
		printf("중복된 곳입니다.\n");
		continue;
  		}
    	board[x][y] = 1;
	
		rand_population(pop);//랜덤수 추출 

		for(int i = 0; i < pop->pop_size; i++){
			printf("컴퓨터의 출력\n");
		 for(int j = 0; j < bit_count; j++){
			printf("%d ", pop->ivd[i].crms[j]);//랜덤한 좌표출력 
			}
		printf("\n");
		}
		
	}	
	
	free_population(pop);
	printf("아무키나 누르십시오...");getchar();
	return 0; 
}
Exemplo n.º 3
0
int main(int argc, char*argv[])
{
   init(argc, argv);
   
   create_population();
   
   while(1)
     {
	eva_step();
     }
}
Exemplo n.º 4
0
/*
 * Create the universe from the properties that are
 * contained in the 'nuo' object.
 *
 * If only 1 strain was specified by the user, then
 * that starting organism goes right smack into the middle
 *
 * Otherwise, each strain is positioned around the place.
 *
 */
UNIVERSE *CreateUniverse(NewUniverseOptions *nuo, char *errbuf)
{
	int i, xpos, ypos;
	ORGANISM *o[8];
	UNIVERSE *u;
	int x[8], y[8];
	int wa, wb, wc;
	int ha, hb, hc;
	char buf[3000];
	CString source_code;
	FILE *fp;
	bool failed;
	int posi;
	size_t len;

	ASSERT( nuo != NULL );
	ASSERT( errbuf != NULL );

	if( nuo->num_strains == 0 ) {
		sprintf(errbuf, "No starting population was specified.");
		return NULL;
	}

	/*
 	 * Compute starting positions for each possible strain
	 */
	wa = nuo->width/4;
	wb = nuo->width/2;
	wc = nuo->width - nuo->width/4;

	ha = nuo->height/4;
	hb = nuo->height/2;
	hc = nuo->height - nuo->height/4;

	if( nuo->num_strains > 1 ) {
		x[0] = wb;
		y[0] = ha;

		x[1] = wb;
		y[1] = hc;

		x[2] = wa;
		y[2] = hb;

		x[3] = wc;
		y[3] = hb;

		x[4] = wa;
		y[4] = ha;

		x[5] = wc;
		y[5] = ha;

		x[6] = wa;
		y[6] = hc;

		x[7] = wc;
		y[7] = hc;
	} else {
		/*
		 * If just 1 strain in sim, then put it in the middle.
		 */
		for(i=0; i<8; i++) {
			x[i] = wb;
			y[i] = hb;
		}
	}

	for(i=0; i<8; i++) {
		o[i] = NULL;
	}

	posi = 0;
	failed = FALSE;
	for(i=0; i<8; i++) {
		if( ! nuo->has_strain[i] )
			continue;

		xpos = x[posi];
		ypos = y[posi];

		fp = fopen(nuo->filename[i], "r");
		if( fp == NULL ) {
			sprintf(errbuf, "%s: %s", nuo->filename[i], strerror(errno));
			failed = TRUE;
			break;
		}

		source_code = "";
		while( fgets(buf, sizeof(buf), fp) != NULL ) {
			len = strlen(buf)-1;
			if( buf[len-1] == '\n' )
				buf[ strlen(buf)-1 ] = '\0';
			strcat(buf, "\r\n");
			source_code += buf;
		}
		fclose(fp);

		o[i] = Organism_Make(xpos, ypos, i, nuo->energy[i], source_code, errbuf);
		if( o[i] == NULL ) {
			failed = TRUE;
			break;
		}

		if( nuo->sexonly[i] ) {
			o[i]->oflags |= ORGANISM_FLAG_SEXONLY;
		}

		posi++;
	}

	if( failed ) {
		for(i=0; i<8; i++) {
			if( o[i] == NULL ) {
				continue;
			}
			Organism_delete(o[i]);
		}
		return NULL;
	}

	u = Universe_Make(nuo->seed, nuo->width, nuo->height, &nuo->kfmo);
	ASSERT( u != NULL );

	if( nuo->want_barrier ) {
		create_barrier_ellipse(u);
	}

	for(i=0; i<8; i++) {
		if( o[i] == NULL )
			continue;

		create_population(u, nuo->population[i], nuo->energy[i], o[i]);
	}

	return u;
}
Exemplo n.º 5
0
Arquivo: game.c Projeto: phoboz/edom
void play(int start_level)
{
  coord opx, opy;
  BOOL quit = 0;
  
  /*
   * Build the current level.
   *
   * XXXX: Once it is possible to save/restore the map this no longer
   *       must be done if the game was started by restoring a save file.
   */
  d.dl = start_level;
  build_map();
  create_population();
  build_monster_map();
  d.visited[0] = TRUE;
  
  /* Initial player position. */
  place_player(d.stxu[d.dl], d.styu[d.dl]);

  /* Initial panel position. */
  d.psx = d.psy = 0;

  /*
   * Standard stuff.
   */
  
  do
  {
    /* Print all the new things. */
    update_screen(d.px, d.py);

    /* The message line should be cleared in any case. */
    clear_messages();
    
    /* Memorize the old PC position. */
    opx = d.px;
    opy = d.py;

    SDL_Event event;

    while (SDL_PollEvent(&event))
    {
      if (event.type == SDL_QUIT)
        quit = TRUE;

      if (event.type == SDL_KEYDOWN)
        game_keydown(event.key.keysym.sym);
    }

    int input = get_input();

    if (input & PRESS_ESC)
      quit = TRUE;

    BOOL player_turn = move_monsters();

    if (player_turn && d.pa.act == IDLE)
    {
      if (input & PRESS_LEFT)
      {
        set_dir_actor(&d.pa, LEFT);
        if (is_open(d.px - 1, d.py) &&
            !is_monster_at(d.px - 1, d.py))
          move_player(LEFT);
      }
      else if (input & PRESS_RIGHT)
      {
        set_dir_actor(&d.pa, RIGHT);
        if (is_open(d.px + 1, d.py) &&
            !is_monster_at(d.px + 1, d.py))
          move_player(RIGHT);
      }
      else if (input & PRESS_UP)
      {
        set_dir_actor(&d.pa, UP);
        if (is_open(d.px, d.py - 1) &&
            !is_monster_at(d.px, d.py - 1))
          move_player(UP);
      }
      else if (input & PRESS_DOWN)
      {
        set_dir_actor(&d.pa, DOWN);
        if (is_open(d.px, d.py + 1) &&
            !is_monster_at(d.px, d.py + 1))
          move_player(DOWN);
      }
    }
    else if (d.pa.act == MOVE)
    {
      animate_move_actor(&d.pa);
    }
    else if (d.pa.act == CHARGE)
    {
      animate_charge_actor(&d.pa);
    }
    else if (d.pa.act == ATTACK)
    {
      if (animate_attack_actor(&d.pa))
        attack_monster_at(d.pa.tx, d.pa.ty);
    }

    d.opx = opx;
    d.opy = opy;
  }
  while (quit == FALSE);
}
Exemplo n.º 6
0
void create_generations(int fitness,float lamda,int num_of_parents,int number_of_groups_wanted,int row_swapping,
                        int min_gene_R1R2,int max_gene_R1R2,int freq,
                        int min_count,int max_count,int generations_wanted,
                        int generation_change,int pop_size_change,double mutation_rate,
                        int robustness, int robust_changes,int robust_last_bit,
                        gsl_rng *r,
                        FILE *r1Output, FILE *r2Output,FILE *matrixOutput, FILE *countsOutput,FILE *fitnessOutput,
                        FILE *discreteOutput,FILE *robustOutput){
    int i,k,l,p,f;
    population *robust_population;
    group* temp_robust_group,*temp_normal_group;
    int temp;

    for(i=0;i<generations_wanted;i++){
        if(i==0){
            generation_array[i%2]=create_population(number_of_groups_wanted,min_gene_R1R2,max_gene_R1R2,min_count,max_count,1);
        }
        else{
            if (i%2==0) temp=2; else temp=1;
            if(fitness){
                generation_array[i%2]=create_gen_population_fit(temp,num_of_parents,row_swapping,min_count,max_count,mutation_rate,r);
            }
            else{
                generation_array[i%2]=create_gen_population_no_fit(temp,num_of_parents,row_swapping,min_count,max_count,mutation_rate,r);
            }
        }

        if(i==generation_change){
            if(curr_num_of_groups*persons_per_group>pop_size_change){
                delete_groups(curr_num_of_groups-(pop_size_change/persons_per_group),i%2);
            }
            else{
                insert_groups((pop_size_change/persons_per_group)-curr_num_of_groups,lamda,i%2,num_of_parents,fitness,row_swapping,min_count,max_count,mutation_rate,r);
            }
        }

        /* ROBUSTNESS */
        if(i%freq==0 && robustness) {
            robust_population = create_population(curr_num_of_groups,min_gene_R1R2,max_gene_R1R2,min_count,max_count,0);
            temp_robust_group=robust_population->groups_list;
            temp_normal_group=generation_array[i%2]->groups_list;

            for(k=0;k<curr_num_of_groups;k++){ /*sarwse olo ton pli8ismo kai deep copy*/
                /*deep copy atomou*/
                for(l=0;l<persons_per_group;l++){
                    temp_robust_group->person_in_group[l]=deep_copy_person_robust(temp_robust_group->person_in_group[l],temp_normal_group->person_in_group[l]);
                }

                if(temp_robust_group->next!=NULL&&temp_normal_group->next!=NULL){
                    temp_robust_group=temp_robust_group->next;
                    temp_normal_group=temp_normal_group->next;
                }
            }

            for(k=0;k<curr_num_of_groups-1;k++){
                temp_robust_group=temp_robust_group->prev;
            }

            robust_population->groups_list=temp_robust_group;
            check_robustness(robustOutput,robust_population,robust_changes,robust_last_bit,1,r);
            
            /*freeing memory from robustness*/
            for(k=0;k<curr_num_of_groups;k++){
                for(l=0;l<persons_per_group;l++){
                    free(temp_robust_group->person_in_group[l]);
                }

                if(temp_robust_group->next!=NULL){
                    temp_robust_group=temp_robust_group->next;
                }
            }

            free(robust_population);
        }
        /*END OF ROBUSTNESS*/

        /*FREEING MEMORY*/
        if(i!=0){
            for(p=0;p<curr_num_of_groups;p++){
                for(f=0;f<persons_per_group;f++){
                    free(generation_array[temp-1]->groups_list->person_in_group[f]);
                }

                if(generation_array[temp-1]->groups_list->next!=NULL){
                    generation_array[temp-1]->groups_list=generation_array[temp-1]->groups_list->next;
                    free(generation_array[temp-1]->groups_list->prev);
                }
            }
            free(generation_array[temp-1]->groups_list);
            free(generation_array[temp-1]);
        }

        mature_generation(generation_array[i%2],1);
        calculate_fitness(i%2,lamda);

        if(i%freq==0){
            printf("Generation %d Simulated. \n",i);
            extract_R1R2_generation(r1Output, r2Output,i%2);
            extract_gene_dependancies_matrix_generation(matrixOutput, i%2);
            extract_gene_counts_generation(countsOutput, i%2);
            extract_discrete_generation(discreteOutput,i%2);
            extract_fitness_generation(fitnessOutput,i%2);
        }
    }
}
int main() {
	extern counter;

	/* test create_population */
	population* pop;
	population_fitness* pop_fit;
	int x;

	counter = 0;
	pop = create_population( 0, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		delete_population( pop );
		pop = 0;
	}

	counter = 0;
	pop = create_population( 1, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		delete_population( pop );
		pop = 0;
	}

	counter = 0;
	pop = create_population( 10, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		print_population_compact( pop );
		delete_population( pop );
		pop = 0;
	}

	counter = 0;
	printf( "create_population large test: " );
	fflush( stdout );
	pop = create_population( INT_MAX / 64, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		delete_population( pop );
		pop = 0;
	}
	printf( "complete\n" );

	/* memory leak test */
	printf( "create_population memory test: " );
	#ifdef MEMLEAK
	fflush( stdout );
	
	for( x = 0; x < INT_MAX; x++ ) {
		counter = 0;
		pop = create_population( 10, &create_individual );
		if( pop == 0 ) {
			eprintf( "population not created at line: %d\n", __LINE__ );
		} else {
			delete_population( pop );
			pop = 0;
		}
	}
	printf( "complete\n" );
	# else
	printf( "skipped\n" );
	#endif

	/* test create_empty_population */
	pop = create_empty_population( -1 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		delete_population( pop );
		pop = 0;
	}

	pop = create_empty_population( 0 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		delete_population( pop );
		pop = 0;
	}

	pop = create_empty_population( 1 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		delete_population( pop );
		pop = 0;
	}

	pop = create_empty_population( 10 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		print_population( pop, 0 );
		print_population_compact( pop );
		delete_population( pop );
		pop = 0;
	}

	printf( "create_empty_population large test: " );
	fflush( stdout );
	pop = create_empty_population( INT_MAX / 64 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		delete_population( pop );
		pop = 0;
	}
	printf( "complete\n" );

	/* memory leak test */
	printf( "create_population memory test: " );
	#ifdef MEMLEAK
	fflush( stdout );
	for( x = 0; x < INT_MAX; x++ ) {
		counter = 0;
		pop = create_empty_population( 10 );
		if( pop == 0 ) {
			eprintf( "population not created at line: %d\n", __LINE__ );
		} else {
			delete_population( pop );
			pop = 0;
		}
	}
	printf( "complete\n" );
	#else
	printf( "skipped\n" );
	#endif

/* test evaluate_population */
	counter = 0;
	pop = create_empty_population( 1 );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		pop_fit = evaluate_population( pop, &evaluate_individual );
		if( pop_fit == 0 ) {
			printf( "population fitness not created at line: %d\n", __LINE__ );
		} else {
			print_population_fitness( pop_fit, 0 );
			print_population_fitness_compact( pop_fit );
			delete_population_fitness( pop_fit );
			pop_fit = 0;
		}
		delete_population( pop );
		pop = 0;
	}

	pop = create_population( 1, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		pop_fit = evaluate_population( pop, &evaluate_individual );
		if( pop_fit == 0 ) {
			printf( "population fitness not created at line: %d\n", __LINE__ );
		} else {
			print_population_fitness( pop_fit, 0 );
			print_population_fitness_compact( pop_fit );
			delete_population_fitness( pop_fit );
			pop_fit = 0;
		}
		delete_population( pop );
		pop = 0;
	}

	pop = create_population( 10, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		pop_fit = evaluate_population( pop, &evaluate_individual );
		if( pop_fit == 0 ) {
			printf( "population fitness not created at line: %d\n", __LINE__ );
		} else {
			print_population_fitness( pop_fit, 0 );
			print_population_fitness_compact( pop_fit );
			delete_population_fitness( pop_fit );
			pop_fit = 0;
		}
		delete_population( pop );
		pop = 0;
	}

	printf( "evaluate_population large test: " );
	fflush( stdout );
	pop = create_population( INT_MAX / 64, &create_individual );
	if( pop == 0 ) {
		printf( "population not created at line: %d\n", __LINE__ );
	} else {
		pop_fit = evaluate_population( pop, &evaluate_individual );
		if( pop_fit == 0 ) {
			printf( "population_fitness not created at line: %d\n", __LINE__ );
		} else {
			delete_population_fitness( pop_fit );
			pop_fit = 0;
		}
		delete_population( pop );
		pop = 0;
	}
	printf( "complete\n" );

	/* memory leak test */
	printf( "evaluate_population memory test: " );
	#ifdef MEMLEAK
	fflush( stdout );
	counter = 0;
	pop = create_population( 10, &create_individual );
	if( pop == 0 ) {
		eprintf( "population not created at line: %d\n", __LINE__ );
	} else {
		for( x = 0; x < INT_MAX; x++ ) {
			pop_fit = evaluate_population( pop, &evaluate_individual );
			delete_population_fitness( pop_fit );
			pop_fit = 0;
		}
		delete_population( pop );
		pop = 0;
	}
	printf( "complete\n" );
	#else
	printf( "skipped\n" );
	#endif

/* test sort_population */
	counter = 0;
	pop = create_population( 100, &create_individual );
	pop_fit = evaluate_population( pop, &evaluate_individual );
	sort_population( pop_fit );
	delete_population_fitness( pop_fit );
	pop_fit = 0;
	delete_population( pop );
	pop = 0;

/* test create_next_generation */
	counter = 0;
	pop = create_population( 100, &create_individual );
	pop_fit = evaluate_population( pop, &evaluate_individual );
	counter = 0;
	population* pop_next = create_next_generation( 10, 10, 80, pop, pop_fit, &compare_individuals_fitness );
	delete_population_fitness( pop_fit );
	pop_fit = 0;
	delete_population( pop );
	pop = 0;
	delete_population( pop_next );
	pop_next = 0;

/* test create_population_fitness */
//	pop_fit = create_population_fitness( 100 );
/* test delete_population_fitness */
//	delete_population_fitness( pop_fit );









/* former tests */
/*
	srand( time_seed() );

	population* pop;
	population_fitness* pop_fit;
	population_fitness* pop_fit_sorted;

	pop = create_population( 3, &create_individual );
	print_population( pop, 0 );
	pop_fit = evaluate_population( pop, &evaluate_individual );
	print_population_fitness( pop_fit, 0 );
	delete_population_fitness( pop_fit );
	delete_population( pop );

	pop = create_population( 4, &create_individual );
	individual* ic;
	individual* im;
	ic = copy_individual( pop->individuals[0] );
	im = mutate_individual( pop->individuals[0], 2 );
	delete_individual( pop->individuals[1] );
	pop->individuals[1] = ic;
	delete_individual( pop->individuals[2] );
	pop->individuals[2] = im;
	evaluate_population( pop, &evaluate_individual );
	printf( "i1: original, i2: copy, i3 mutant (degree 2)\n" );
	print_population( pop, 0 );
	delete_population( pop );

	pop = create_population( 3, &create_individual );
	individual* off;
	off = mate_individuals( pop->individuals[0], pop->individuals[1], 1 );
	delete_individual( pop->individuals[2] );
	pop->individuals[2] = off;
	evaluate_population( pop, &evaluate_individual );
	printf( "i1: parent1, i2: parent2, i3 offspring\n" );
	print_population( pop, 0 );
	delete_population( pop );
*/
/*
	pop = create_population( 100, &create_individual );
	pop_fit = evaluate_population( pop, &evaluate_individual );
	print_population_fitness( pop_fit, 0 );
	pop_fit_sorted = sort_population( pop_fit );
	printf( "Original Population Fitness\n" );
	print_population_fitness( pop_fit, 0 );
	printf( "Sorted Population Fitness\n" );
	print_population_fitness( pop_fit_sorted, 0 );
*/
}