int main(int argc, char const *argv[]) { cgp_init(); cgp_genome_t genome1 = (cgp_genome_t) malloc(sizeof(struct cgp_genome)); struct ga_chr chr1 = { .genome = genome1 }; cgp_genome_t genome2 = (cgp_genome_t) malloc(sizeof(struct cgp_genome)); struct ga_chr chr2 = { .genome = genome2 }; cgp_init_chr(&chr1); cgp_init_chr(&chr2); cgp_dump_chr_compat(&chr1, stdout); printf("\n"); cgp_dump_chr_compat(&chr2, stdout); printf("\n"); cgp_replace_chr(&chr2, &chr1); cgp_dump_chr_compat(&chr2, stdout); free(genome1); free(genome2); cgp_deinit(); }
int cgp_run(size_t *gener, fitness_t *best_fitness, FILE *cfd, struct chromo_t *basec, size_t ccount) { struct cgp_t cgp; if(cgp_init(&cgp)) { handle_error("cgp_init"); goto error_fini; } if(cgp_gen_popul_from(&cgp, basec, ccount)) { handle_error("gcp_gen_popul"); goto error_fini; } struct run_stats_t stats = { .gener = 0, .has_acceptable = 0, .start_gener = 0 }; while(!cgp_done(&cgp)) { if(cgp_next_popul(&cgp)) { handle_error("cgp_next_popul"); goto error_fini; } if(cgp_eval_popul(&cgp)) { handle_error("cgp_eval_popul"); goto error_fini; } run_stats_update(&cgp, &stats); } *gener = cgp.gener; printf("\nGenerations: %zu\n", cgp.gener); cgp_walk_popul(&cgp, &find_best_fitness, best_fitness); printf("Best: %zu\n", *best_fitness); cgp_walk_popul(&cgp, &print_chromo, cfd); // print the best one, when no acceptable found if(!fitness_isacceptable(*best_fitness)) cgp_walk_popul(&cgp, &print_best, best_fitness); cgp_fini(&cgp); return 0; error_fini: cgp_fini(&cgp); return 1; } FILE *args_output_file(int argc, char **argv) { char *cfile = "success.chr"; if(argc >= 3) cfile = argv[2]; FILE *cfd = fopen(cfile, "w"); if(cfd == NULL) { fprintf(stderr, "Invalid file to save best chromosomes: '%s'\n", cfile); return NULL; } else { fprintf(stderr, "Saving chromosome to file: '%s'\n", cfile); } return cfd; }