void run(double *params) { CohoPopulation *p; ResultBlock *r; int maxgen = params[MaxGenerations]; int i, n; double x; n = params[NPopulations]; for (i=0; i<n; i++) { p = new_population(0,params); r = p->run(maxgen); delete p; delete r; } }
/* Main evolution method, called by user and handles main loop */ static VALUE method_evolve(VALUE self, VALUE vN_gens, VALUE vPop_size) { // This is the object to be evolved: store globally phenome = self; // Process method parameters int n_gens = FIX2INT(vN_gens); int pop_size = FIX2INT(vPop_size); // We need to call encode once, to obtain genome length VALUE ary_genome = rb_funcall(self, rb_intern("encode"), 0); int g_len = RARRAY(ary_genome)->len; // space for new genotype to be created char *child_genome = (char *)malloc(g_len * sizeof(char)); // create a new population with random genotypes Population *pop = new_population(self, pop_size, g_len, TRUE); update_fitness(pop, g_len); // main loop int g; for(g=0; g < n_gens; ++g) { if(g % 100 == 0) printf("Processing generation %d\n", g); Candidate *c1 = tournament_select(pop); Candidate *c2 = tournament_select(pop); two_point_crossover(child_genome, c1, c2, g_len); Candidate *low = weakest(pop); low->genome = child_genome; low->rq_update = TRUE; update_fitness(pop, g_len); Candidate *high = strongest(pop); printf("weakest is %i\n", low->fitness); printf("strongest is %i\n", high->fitness); } }
void rcps_solver_solve(struct rcps_solver *s, struct rcps_problem *p) { int i, j, k; struct rcps_genome *genome; struct rcps_phenotype *pheno; /* init the random number generator */ srand(time(NULL)); /* fix the indices */ for (i = 0; i < p->job_count; i++) { p->jobs[i]->index = i; } for (i = 0; i < p->resource_count; i++) { p->resources[i]->index = i; } /* fix the predeccessors */ for (i = 0; i < p->job_count; i++) { free(p->jobs[i]->predeccessors); p->jobs[i]->predeccessor_count = 0; } for (i = 0; i < p->job_count; i++) { for (j = 0; j < p->jobs[i]->successor_count; j++) { rcps_job_predeccessor_add(p->jobs[i]->successors[j], p->jobs[i], p->jobs[i]->successor_types[j]); } } /* do other precalculations */ /* modes and alternatives */ p->genome_modes = 0; p->genome_alternatives = 0; for (i = 0; i < p->job_count; i++) { struct rcps_job *job = p->jobs[i]; // do the modes if (job->mode_count > 1) { p->genome_modes++; p->modes_max = (int*) realloc(p->modes_max, p->genome_modes*sizeof(int)); p->modes_max[p->genome_modes-1] = job->mode_count; job->genome_position = p->genome_modes-1; } else { job->genome_position = -1; } job->res_start = -1; job->res_mode = -1; for (j = 0; j < job->mode_count; j++) { struct rcps_mode *mode = job->modes[j]; for (k = 0; k < mode->request_count; k++) { struct rcps_request *request = mode->requests[k]; request->res_alternative = -1; // do the alternatives if (request->alternative_count > 1) { p->genome_alternatives++; p->alternatives_max = (int*) realloc(p->alternatives_max, p->genome_alternatives*sizeof(int)); p->alternatives_max[p->genome_alternatives-1] = request->alternative_count; request->genome_position = p->genome_alternatives-1; } else { request->genome_position = -1; } } } } /* hash of predecessor relations */ free(s->predecessor_hash); s->predecessor_hash = (char*)malloc(sizeof(char)*p->job_count*p->job_count); for (i = 0; i < p->job_count*p->job_count; i++) { s->predecessor_hash[i] = -1; } /* initialize the population */ s->population = new_population(s, p); /* here we run the algorithm */ #ifdef HAVE_PTHREAD if (s->jobs <= 1) { s->reproductions = run_alg(s, p); } else { s->reproductions = 0; pthread_t *threads = (pthread_t*)malloc(sizeof(pthread_t) * s->jobs); struct thread_arg args; args.s = s; args.p = p; int i; for (i = 0; i < s->jobs; i++) { int result = pthread_create(&threads[i], NULL, threadfunc, &args); assert(result == 0); } // XXX join them all! for (i = 0; i < s->jobs; i++) { int result = pthread_join(threads[i], NULL); assert(result == 0); } } #else s->reproductions = run_alg(s, p); #endif // struct rcps_fitness fit = ((struct rcps_individual*)slist_node_getdata(slist_first( // s->population->individuals)))->fitness; // printf("cycles: \t%i\nfitness:\t[%d, %d]\n", tcount, fit.group, fit.weight); // transfer the results to the problem structure genome = &((struct rcps_individual*)slist_node_getdata( slist_first(s->population->individuals)))->genome; // XXX we could save us this decoding step if we would keep the best ones // from before, not really worth it // save a warning if the schedule is not valid pheno = decode(s, p, genome); if (pheno->overuse_count) { s->warnings = 1; } else { s->warnings = 0; } for (i = 0; i < p->job_count; i++) { struct rcps_job *job; struct rcps_mode *mode; job = p->jobs[i]; job->res_start = pheno->job_start[job->index]; job->res_mode = job->genome_position == -1 ? 0 : genome->modes[job->genome_position]; mode = job->modes[job->res_mode]; for (j = 0; j < mode->request_count; j++) { struct rcps_request *request; request = mode->requests[j]; request->res_alternative = request->genome_position == -1 ? 0 : genome->alternatives[request->genome_position]; } } }
stResult<TCity> * main_genetic(mySlimTree* SlimTree, TCity * queryObject, stDistance range) { time_t begin, end; begin = time(NULL); srand(time(NULL)); Population * pop = new_population(); Population * aux_pop = new_empty_population(); infeasible(SlimTree, pop); set_genotype(pop); evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG print_population(pop ,"Pop", number_of_gerations); #endif while( (number_of_gerations < MAX_NUMBER_OF_GERATIONS) && (delta_fitness > 0.0001) ) { number_of_gerations++; #if DEBUG cout << "Number of Gerations: "<< number_of_gerations << endl; #endif selection_by_tournament(pop, aux_pop); #if DEBUG cout << "Torneio" << endl; #endif crossover_uniform(aux_pop); #if DEBUG cout << "Crossover Uniforme" << endl; #endif mutation(aux_pop); #if DEBUG cout << "Mutação" << endl; #endif set_phenotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif infeasible(SlimTree, aux_pop); #if DEBUG cout << "Infactibilidade" << endl; #endif set_genotype(aux_pop); #if DEBUG cout << "Fenotipo" << endl; #endif copy_population(aux_pop, pop); #if DEBUG cout << "Copiar aux pop" << endl; #endif evaluate_fitness(pop, SlimTree, queryObject, range); #if DEBUG cout << "Fitness" << endl; #endif old_media_of_fitness = media_of_fitness; media_of_fitness = get_media_of_population(pop); delta_fitness = fabs(media_of_fitness - old_media_of_fitness); } end = time(NULL); #if DEBUG print_population(pop, "PopulatioN", number_of_gerations); print_statistic(pop, begin, end); #endif }