/** * main program */ int main (void) { float cpu1,cpu2; cpu1 = ((float) clock())/CLOCKS_PER_SEC; srand (time (NULL)); ga_struct *population = malloc (sizeof (ga_struct) * POPSIZE); ga_struct *beta_population = malloc (sizeof (ga_struct) * POPSIZE); init_population (population, beta_population); char *gen_str = population[0].gen; char element[5] = "\0"; strncpy (element, gen_str, 4); //if (strcmp ("0000", element) == 0) int index = 0; for (; index < POPSIZE; index++) { cal_fitness (population); sort_by_fitness (population); // print current best individual printf ("binary string: %s - fitness: %d\n", population[0].gen, population[0].fitness); if (population[0].fitness == 0) { //~ print equation decode_gen (&population[0]); break; } mate (population, beta_population); swap (&population, &beta_population); } free_population (population); free_population (beta_population); cpu2 = ((float) clock())/CLOCKS_PER_SEC; printf("Execution time (s) = %le\n",cpu2-cpu1); return 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; }
/* Free metapopulation */ void free_metapopulation(struct metapopulation *in){ int i, npat=get_maxnpat(in), npop=get_npop(in); for(i=0;i<npat;i++){ if(in->pathogens[i] != NULL) free_pathogen((in->pathogens)[i]); } for(i=0;i<npop;i++){ if(in->populations[i] != NULL) free_population(in->populations[i]); } free(in->pathogens); free(in->populations); free(in); }
void reduce_population_through_ranking(GeneWrapper * population_after, int population_size_after, GeneWrapper * population_before, int population_size_before, GeneWrapper * base_population, int base_population_size) { char buffer[51]; int i = population_size_after - 1; int j = population_size_before - 1; GeneWrapper * population_before_copy = NULL; copy_population(&population_before_copy, population_before, population_size_before); rank_compete(population_before_copy, population_size_before, base_population, base_population_size); while (i >= 0) { NewStringFromGene(population_before_copy[j].gene, buffer); SetGeneFromString(buffer, population_after[i].gene); i--; j--; } free_population(population_before_copy, population_size_before); }
void reduce_population_through_competition(GeneWrapper * initial_population, GeneWrapper * new_population, int initial_population_size, int new_population_size) { char buffer[51]; int i = initial_population_size - 1; int j = new_population_size - 1; GeneWrapper * new_population_copy = NULL; copy_population(&new_population_copy, new_population, new_population_size); mutual_compete(new_population_size, new_population_copy); while (i >= 0) { NewStringFromGene(new_population_copy[j].gene, buffer); SetGeneFromString(buffer, initial_population[i].gene); i--; j--; } free_population(new_population_copy, new_population_size); }
int main(int argc, char **argv) { extern char *optarg; int optchar; char *treefile_name = NULL, *genomefile_name = NULL, *outfile_name = NULL, *corrfile_name = NULL; FILE *treefile, *genomefile, *outfile, *corrfile; long genome_generation, tree_generation, i, j, num_trees; double tree_d, gen_d, change_rate, m1, m_a, mutation_rate; double *tree_dist = NULL, *m_app = NULL; double grade, y0, r; long num_data; PHYLTREE phyltree; POPULATION population; int ret_code; phyl_init_tree(&phyltree); while ((optchar = getopt(argc, argv, "hg:t:o:c:m:")) != -1) { switch (optchar) { case 'g': genomefile_name = optarg; break; case 't': treefile_name = optarg; break; case 'c': corrfile_name = optarg; break; case 'o': outfile_name = optarg; break; case 'h': printf("mutcheck -- analyze apparent mutation rates\n"); printf("\n"); printf("Command line usage:\n"); printf("-g <filename>: Specify genome file (mandatory)\n"); printf("-t <filename>: Specify tree file (mandatory)\n"); printf("-o <filename>: Specify name for individual generation's output file\n"); printf("-c <filename>: Specify file for treedist vs reconstructed dist correlations\n"); printf("-h: Print this help and exit\n"); exit (EXIT_SUCCESS); } } if (treefile_name == NULL) { fprintf(stderr, "no tree file specified -- exit\n"); exit (EXIT_FAILURE); } if ((treefile = fopen(treefile_name, "r")) == NULL) { fprintf(stderr, "failed to open tree file \"%s\" -- exit\n", treefile_name); exit (EXIT_FAILURE); } if (genomefile_name == NULL) { fprintf(stderr, "No genome file specified -- exit\n"); fclose(treefile); exit (EXIT_FAILURE); } if ((genomefile = fopen(genomefile_name, "r")) == NULL) { fprintf(stderr, "Failed to open genome file \"%s\" -- exit\n", genomefile_name); fclose(treefile); exit (EXIT_FAILURE); } if (corrfile_name) { if ((corrfile = fopen(corrfile_name, "w")) == NULL) { fprintf(stderr, "Failed to open \"%s\" for correlation output -- exit\n", corrfile_name); fclose(treefile); fclose(genomefile); exit (EXIT_FAILURE); } } else { corrfile = stdout; corrfile_name = "stdout"; } if (outfile_name) { if ((outfile = fopen(outfile_name, "w")) == NULL) { fprintf(stderr, "mutcheck: Failed to open %s for output -- exit\n", outfile_name); fclose(treefile); fclose(genomefile); if (corrfile_name) fclose(corrfile); exit (EXIT_FAILURE); } } else outfile = NULL; while (!feof(genomefile) && !ferror(genomefile) && !feof(treefile) && !ferror(treefile)) { fgets(buf, 256, treefile); if ((buf[0] != 'g') || (buf[1] != ' ')) { fprintf(stderr, "Treefile corrupt after generation %ld\n", tree_generation); break; } tree_generation = strtol(buf + 2, NULL, 10); fgets(buf, 256, genomefile); if ((buf[0] != 'g') || (buf[1] != ' ')) { fprintf(stderr, "Genome file corrupt after generation %ld\n", genome_generation); break; } genome_generation = strtol(buf + 2, NULL, 10); fgets(buf, 256, treefile); num_trees = strtol(buf, (char **) NULL, 10); if (num_trees != 1) { fprintf(stderr, "%ld trees in generation %ld -- skipping\n", num_trees, tree_generation); for (i = 0; i < num_trees; i++) { phyl_read_tree(treefile, &phyltree); phyl_free_tree(&phyltree); } } else phyl_read_tree(treefile, &phyltree); read_genomes(genomefile, &population); if ((num_trees == 1) && (tree_generation == genome_generation)) { if ((tree_dist = (double *) malloc((population.size * population.size - 1) / 2 * sizeof(double))) == NULL) fprintf(stderr, "Failed to allocate array of tree distances\n"); else { if ((m_app = (double *) malloc(population.size * (population.size - 1) / 2 * sizeof(double))) == NULL) fprintf(stderr, "Failed to allocate array of reconstructed distances\n"); else { num_data = 0; for (i = 0; i < population.size; i++) { for (j = 0; j < i; j++) { tree_d = phyl_leafdistance(&phyltree, population.seq[i].name, population.seq[j].name); if (tree_d < 0) { fprintf(stderr, "Generation %ld: Trees and genomes inconsistent: error #%f\n", tree_generation, tree_d); break; } gen_d = diffchar_distance(population.seq[i].genome.length, population.seq[i].genome.g, population.seq[j].genome.length, population.seq[j].genome.g); change_rate = gen_d / (population.seq[i].genome.length); m1 = 1.0 - 256.0 / 255.0 * change_rate; if (m1 > 0.0) { errno = 0; m_a = 1.0 - pow(m1, 1.0 / 2.0 / tree_d); if (errno) perror("mutcheck: error in pow()"); else { tree_dist[num_data] = tree_d; m_app[num_data] = m_a; num_data++; if (outfile) fprintf(outfile, "%f %f\n", tree_d, m_a); } } else if (outfile) fprintf(outfile, "# Negative base operand to exponentiation: %f)\n", m1); } if (j < i) break; } if ((ret_code = linear_regression(num_data, tree_dist, m_app, &grade, &y0, &r)) < 0) { fprintf(stderr, "Error #%d in linear regression\n", ret_code); } else { mutation_rate = 1.0 - exp(grade); if (corrfile) fprintf(corrfile, "%ld %f %f\n", tree_generation, grade, r); } free(m_app); } free(tree_dist); } } phyl_free_tree(&phyltree); free_population(&population); #ifdef MEMDEBUG print_MemdebugStatistics(); #endif } if (corrfile != stdout) fclose(corrfile); if (outfile) fclose(outfile); fclose(treefile); fclose(genomefile); return (EXIT_SUCCESS); }
//HJJA population *change_population ( population *oldpop, breedphase *bp ) { population *newpop; int i, j; int numphases; double totalrate = 0.0; double r, r2; int prob_oper = atoi ( get_parameter ( "probabilistic_operators" ) ); /* allocate the new population. */ newpop = allocate_population ( oldpop->size ); globaldata* g = get_globaldata();//get the lists if(g->bUseHFC){ if( IsHighestActiveLevel(g->current_population)){ /*reproduce best guys into the new population. */ for(i=0;i<run_stats[0].bestn;i++){ //printf("best %d ind\n", i); duplicate_individual ( (newpop->ind)+newpop->next, run_stats[0].best[i]->ind); newpop->ind[newpop->next].flags = FLAG_NONE; ++(newpop->next); //HJJS structure niching: mutation //Assumption: we think any structure mutation will create a new structure //for numeric mutation, we won't change the structure CStruct* pSt; int structID = newpop->ind[newpop->next].structID; pSt = g->pStructHash->find(structID); if(pSt==NULL){ //make sure,though redudency code pSt = new CStruct(); pSt->structID = ++(g->currMaxStructID); pSt->age=1; pSt->lastGen=g->current_generation+1; pSt->nIndividuals=0; pSt->nIndNextGen=1; newpop->ind[newpop->next].structID=pSt->structID; g->pStructHash->insert(pSt); } else{ if(pSt->lastGen != g->current_generation+1){//the first individual in this generation pSt->age++; pSt->lastGen = g->current_generation+1; } pSt->nIndNextGen++;//increase the ind no of this population } //HJJS end } } //End HJJ_ELITISM }//End useHFC /* the first element of the breedphase table is a dummy -- its operator field stores the number of phases. */ numphases = bp[0].operatorID; /* call the start method for each phase. */ for ( i = 1; i <= numphases; ++i ) { totalrate += bp[i].rate; if ( bp[i].operator_start ) bp[i].operator_start ( oldpop, bp[i].data ); } /* now fill the new population. */ while ( newpop->next < newpop->size ) { /** select an operator, either stochastically or not depending on the probabilistic_operators parameter. **/ if ( prob_oper ) r = totalrate * random_double(); else r = totalrate * ((double)newpop->next/(double)newpop->size); r2 = bp[1].rate; for ( i = 1; r2 < r; ) r2 += bp[++i].rate; #ifdef DEBUG fprintf ( stderr, "picked %10.3lf; operator %d\n", r, i ); #endif /* call the phase's method to do the operation. */ if ( bp[i].operator_operate ){ bp[i].operator_operate ( oldpop, newpop, bp[i].data ); } } /* call each phase's method to do cleanup. */ for ( i = 1; i <= numphases; ++i ) { if ( bp[i].operator_end ) bp[i].operator_end ( bp[i].data ); } /* mark all the ERCs referenced in the new population. */ for ( i = 0; i < newpop->size; ++i ) for ( j = 0; j < tree_count; ++j ) reference_ephem_constants ( newpop->ind[i].tr[j].data, 1 ); /* free the old population. */ free_population ( oldpop ); //Bug when do reproduction ? return ( newpop ); }
int main(int argc, char *argv[]) { // Need to change to 3. need to remove the logging. if (argc != 4) { printf("Usage: ./maxcut <input data path> <output data path> <log file>\n"); exit(-1); } // Need to remove when submitting. log_file = fopen(argv[3], "w"); fprintf(log_file, "rate, elasped time (s), max val, avg val\n"); start_time = get_seconds(); in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); int i, j, v1, v2, w; // (v1, v2) is the vertex and w is the weight fscanf(in, "%d %d\n", &num_of_vertex, &num_of_edge); int edge[num_of_vertex+1][num_of_vertex+1]; for (i=0; i<=SIZE; i++) for (j=0; j<=SIZE; j++) edge[i][j] = 0; while (fscanf(in, "%d %d %d\n", &v1, &v2, &w) != EOF) { edge[v1][v2] = w; edge[v2][v1] = w; } init_population(); init_offsprings(); init_cost(edge); sort_population(); init_crossover(); int p1, p2; while (!(stop_condition())) { generation++; for (i=1; i<=K; i++) { selection(&p1, &p2); crossover(i, p1, p2); mutation(i); local_optimization(i, edge); } replacement(edge); sort_population(); } for (i=1; i<=SIZE; i++) { if (population[N]->ch[i] == 1) fprintf(out, "%d ", i); } free_population(); fclose(in); fclose(out); printf("N: %d, K: %d, S_RATE: %lf, M_THRE: %lf, P0: %lf, POINTS: %d, K_FIT: %d, T: %lf\n", N, K, S_RATE, M_THRE, P0, POINTS, K_FIT, T); return 0; }
int main(int argc, char **argv) { extern char *optarg; int optchar; char *treefile_name = NULL, *genomefile_name = NULL, *outfile_name = NULL, *corrfile_name = NULL, *mutfile_name = NULL; FILE *treefile, *genomefile, *outfile, *corrfile, *mutfile; long genome_generation, tree_generation, i, j, num_trees; double tree_d, rec_d, rec_d1, change_rate, mutation_rate; double *tree_dist = NULL, *reconst_dist = NULL; double lambda, y0, r; long num_data; PHYLTREE phyltree; POPULATION population; int ret_code; phyl_init_tree(&phyltree); while ((optchar = getopt(argc, argv, "hg:t:o:c:m:")) != -1) { switch (optchar) { case 'g': genomefile_name = optarg; break; case 't': treefile_name = optarg; break; case 'c': corrfile_name = optarg; break; case 'm': mutfile_name = optarg; break; case 'o': outfile_name = optarg; break; case 'h': printf("treecorr -- check correlation between corrected\n"); printf(" edit or hamming diatance and true tree distance\n"); printf("\n"); printf("Command line usage:\n"); printf("-g <filename>: Specify genome file (mandatory)\n"); printf("-t <filename>: Specify tree file (mandatory)\n"); printf("-o <filename>: Specify base name for individual generation's output file\n"); printf("-c <filename>: Specify file for treedist vs reconstructed dist correlations\n"); printf("-m <filename>: Specify file for reconstructed mutation rates\n"); printf("-h: Print this help and exit\n"); exit (EXIT_SUCCESS); } } if (treefile_name == NULL) { fprintf(stderr, "no tree file specified -- exit\n"); exit (EXIT_FAILURE); } if ((treefile = fopen(treefile_name, "r")) == NULL) { fprintf(stderr, "failed to open tree file \"%s\" -- exit\n", treefile_name); exit (EXIT_FAILURE); } if (genomefile_name == NULL) { fprintf(stderr, "No genome file specified -- exit\n"); fclose(treefile); exit (EXIT_FAILURE); } if ((genomefile = fopen(genomefile_name, "r")) == NULL) { fprintf(stderr, "Failed to open genome file \"%s\" -- exit\n", genomefile_name); fclose(treefile); exit (EXIT_FAILURE); } if (corrfile_name) { if ((corrfile = fopen(corrfile_name, "w")) == NULL) { fprintf(stderr, "Failed to open \"%s\" for correlation output -- exit\n", corrfile_name); fclose(treefile); fclose(genomefile); exit (EXIT_FAILURE); } } else { corrfile = stdout; corrfile_name = "stdout"; } if (mutfile_name) { if ((mutfile = fopen(mutfile_name, "w")) == NULL) { fprintf(stderr, "Failed to open \"%s\" for mutation output -- exit\n", mutfile_name); fclose(treefile); fclose(genomefile); if (corrfile != stdout) fclose(corrfile); exit (EXIT_FAILURE); } } else mutfile = NULL; while (!feof(genomefile) && !ferror(genomefile) && !feof(treefile) && !ferror(treefile)) { fgets(buf, 256, treefile); if ((buf[0] != 'g') || (buf[1] != ' ')) { fprintf(stderr, "Treefile corrupt after generation %ld\n", tree_generation); break; } tree_generation = strtol(buf + 2, NULL, 10); fgets(buf, 256, genomefile); if ((buf[0] != 'g') || (buf[1] != ' ')) { fprintf(stderr, "Genome file corrupt after generation %ld\n", genome_generation); break; } genome_generation = strtol(buf + 2, NULL, 10); fgets(buf, 256, treefile); num_trees = strtol(buf, (char **) NULL, 10); if (num_trees != 1) { fprintf(stderr, "%ld trees in generation %ld -- skipping\n", num_trees, tree_generation); for (i = 0; i < num_trees; i++) { phyl_read_tree(treefile, &phyltree); phyl_free_tree(&phyltree); } } else phyl_read_tree(treefile, &phyltree); read_genomes(genomefile, &population); if ((num_trees == 1) && (tree_generation == genome_generation)) { if ((tree_dist = (double *) malloc((population.size * population.size - 1) / 2 * sizeof(double))) == NULL) fprintf(stderr, "Failed to allocate array of tree distances\n"); else { if ((reconst_dist = (double *) malloc(population.size * (population.size - 1) / 2 * sizeof(double))) == NULL) fprintf(stderr, "Failed to allocate array of reconstructed distances\n"); else { if (outfile_name) { sprintf(buf, "%s-%05ld.gpd", outfile_name, tree_generation); outfile = fopen(buf, "w"); fprintf(outfile, "# generation %ld\n", tree_generation); } num_data = 0; for (i = 0; i < population.size; i++) { for (j = 0; j < i; j++) { tree_d = phyl_leafdistance(&phyltree, population.seq[i].name, population.seq[j].name); if (tree_d < 0) { fprintf(stderr, "Generation %ld: Trees and genomes inconsistent: error #%f\n", tree_generation, tree_d); break; } rec_d = diffchar_distance(population.seq[i].genome.length, population.seq[i].genome.g, population.seq[j].genome.length, population.seq[j].genome.g); change_rate = rec_d / (population.seq[i].genome.length); rec_d1 = 1.0 - 256.0 / 255.0 * change_rate; if (rec_d1 > 0.0) { rec_d = log(rec_d1); tree_dist[num_data] = tree_d; reconst_dist[num_data] = rec_d; num_data++; if (outfile) fprintf(outfile, "%f %f\n", tree_d, rec_d); } else if (outfile) fprintf(outfile, "# Infinite distance (log operand: %f)\n", rec_d1); } if (j < i) break; } if (outfile_name && outfile) fclose(outfile); if ((ret_code = linear_regression(num_data, tree_dist, reconst_dist, &lambda, &y0, &r)) < 0) { fprintf(stderr, "Error #%d in linear regression\n", ret_code); } else { mutation_rate = 1.0 - exp(lambda); if (corrfile) fprintf(corrfile, "%ld %f\n", tree_generation, r); if (mutfile) fprintf(mutfile, "%ld %f\n", tree_generation, mutation_rate); } free(reconst_dist); } free(tree_dist); } } phyl_free_tree(&phyltree); free_population(&population); #ifdef MEMDEBUG print_MemdebugStatistics(); #endif } if (mutfile) fclose(mutfile); if (corrfile != stdout) fclose(corrfile); fclose(treefile); fclose(genomefile); return (EXIT_SUCCESS); }
int main(int argc, char **argv) { extern int optind, opterr; extern char *optarg; int optchar; char *treefile_name = NULL, *genomefile_name = NULL, *outfile_basename = NULL, g_outfile_name[FILENAME_MAX]; FILE *treefile, *genomefile, *outfile; long generation, g, num_trees, i; int ret_code; genomefile_name = NULL; treefile_name = NULL; outfile_basename = NULL; generation = -1; phyl_init_tree(&phyltree); while ((optchar = getopt(argc, argv, "t:g:o:h")) != -1) { switch (optchar) { case 'g': genomefile_name = optarg; break; case 't': treefile_name = optarg; break; case 'o': outfile_basename = optarg; break; case 'h': printf("distcorr -- a hack to visualize correlation between\n"); printf(" phylogenetic and edit distances\n"); printf("Usage of command line options:\n"); printf("-g <filename>: Specify genome file\n"); printf("-t <filename>: Specify tree file\n"); printf("-o <outfile>: specify output file basename:"); printf("-h: Print this info and exit\n"); exit (EXIT_SUCCESS); } } if (treefile_name == NULL) { fprintf(stderr, "no tree file specified -- exit\n"); exit (EXIT_FAILURE); } if ((treefile = fopen(treefile_name, "r")) == NULL) { fprintf(stderr, "failed to open tree file \"%s\" -- exit\n", treefile_name); exit (EXIT_FAILURE); } if (genomefile_name == NULL) { fprintf(stderr, "no tree file specified -- exit\n"); exit (EXIT_FAILURE); } if ((genomefile = fopen(genomefile_name, "r")) == NULL) { fprintf(stderr, "failed to open genome file \"%s\" -- exit\n", genomefile_name); exit (EXIT_FAILURE); } while (!feof(treefile) && !ferror(treefile) && !feof(genomefile) && !ferror(genomefile)) { get_line(buf, 256, treefile); if (feof(treefile) || ferror(treefile)) break; if (buf[0] != 'g') { fprintf(stderr, "error in header of treefile after generation %ld -- exit\n", generation); exit (EXIT_FAILURE); } generation = strtol(buf + 1, (char **) NULL, 10); get_line(buf, 256, genomefile); if (feof(genomefile) || ferror(genomefile)) break; if (buf[0] != 'g') { fprintf(stderr, "error in header of genomefile after generation %ld -- exit\n", g); exit (EXIT_FAILURE); } g = strtol(buf + 1, (char **) NULL, 10); if (g != generation) { fprintf(stderr, "treefile: g=%ld, genomefile: g=%ld -- out of sync\n", generation, g); break; } get_line(buf, 256, treefile); num_trees = strtol(buf, NULL, 10); if (num_trees < 1) { fprintf(stderr, "%ld trees in treefile at generation %ld\n", num_trees, generation); break; } if (num_trees > 1) { fprintf(stderr, "distcorr: multiple trees in treefile\n"); for (i = 0; i < num_trees; i++) { if ((ret_code = phyl_read_tree(treefile, &phyltree)) < 0) fprintf(stderr, "error #%d while reading tree #%ld of generation %ld\n", ret_code, i, generation); phyl_free_tree(&phyltree); } read_population(genomefile, g); free_population(); continue; } if ((ret_code = phyl_read_tree(treefile, &phyltree)) < 0) { fprintf(stderr, "error #%d while reading tree #%ld of generation %ld\n", ret_code, i, generation); break; } if ((ret_code = read_population(genomefile, g)) < 0) { fprintf(stderr, "error %d reading genomes of generation %ld\n", ret_code, g); phyl_free_tree(&phyltree); break; } if (outfile_basename) { sprintf(g_outfile_name, "%s-%06ld-dc.gpd", outfile_basename, generation); if ((outfile = fopen(g_outfile_name, "w")) == NULL) { fprintf(stderr, "failed to open \"%s\" for output -- exit\n", g_outfile_name); exit (EXIT_FAILURE); } } else outfile = stdout; ret_code = distcorr(outfile, generation); if (ret_code < 0) fprintf(stderr, "error %d in distcorr\n", ret_code); if (outfile_basename) fclose(outfile); free_population(); phyl_free_tree(&phyltree); } fclose(treefile); fclose(genomefile); return (EXIT_SUCCESS); }