- case TERM_COND_PENALTY:
- return (population1[0]->penalty < conf_penalty_bound);
- case TERM_COND_STAGNATION:
- return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
- case TERM_COND_ITERATIONS:
- return (iteration >= conf_iteration_limit);
- default:
- // FIXME: Warn the user that no condition is set
- return 1;
- }
-}
-
-void breed(void)
-{
- int acc = 0;
- int i=0;
- int conf_breed_pop_size = conf_breed_pop_size_perc * conf_pop_size;
- struct individual **breed_buffer;
- while (i < conf_breed_rbest_perc * conf_pop_size)
- {
- int parent1 = randint(1, conf_breed_pop_size);
- int parent2 = randint(1, conf_breed_pop_size);
- breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
- population2[2*i] = breed_buffer[0];
- population2[2*i+1] = breed_buffer[1];
- free(breed_buffer);
- }
-
- acc += conf_breed_rbest_perc;
-
- int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
- int step = remaining / conf_pop_size;
- for (; i<conf_pop_size; i += 2)
- {
- breed_buffer = perform_crossover(population1[i*step], population1[i*(step+1)]);
- population2[2*i] = breed_buffer[0];
- population2[2*i+1] = breed_buffer[1];
- }
-
- // FIXME: Could there be one missing individual?
-}
-
-struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
-{
- struct individual **buffer = malloc(2*sizeof(struct individual));
- struct individual *child1 = ep_alloc(ep_individuals);
- struct individual *child2 = ep_alloc(ep_individuals);
-
- for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
- {
- if (! parent1->placements[i].processed)
- {
- struct placement **clos_symbols;
- GARY_INIT(clos_symbols, 0);
- get_closure(clos_symbols, &(parent1->placements[i]), parent1, parent2);
- int x = randint(1, 2);
-
- if (x == 1)
- {
- copy_symbols(clos_symbols, parent1, child1);
- copy_symbols(clos_symbols, parent2, child2);
- }
- else
- {
- copy_symbols(clos_symbols, parent2, child1);
- copy_symbols(clos_symbols, parent1, child2);
- }
- GARY_FREE(clos_symbols);
- }
-
- if (conf_mutate_children)
- {
- if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
- if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
- }
- }
-
- buffer[0] = child1;
- buffer[1] = child2;
- return buffer;
-}
-
-void mutate(void)
-{
- int i = 0;
- int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
- while (i < conf_mutate_rbest_perc * conf_pop_size)
- {
- int ind = randint(1, conf_mutate_pop_size);
- population2[pop2_ind] = population1[ind];
- perform_mutation(population2[pop2_ind]);
- }
-}
-
-void perform_mutation(struct individual *individual)
-{
- for (uns i=0; i<GARY_SIZE(individual->placements); i++)
- {
- int x = randint(1, 1000);
- int acc = 0;
-
- if (x <= acc + conf_mutate_move_bound)
- {
- move_symbol(&(individual->placements[i]));
- continue;
- }
- acc += conf_mutate_move_bound;
-
- if (x <= acc + conf_mutate_regen_bound)
- {
- gen_coords(&(individual->placements[i]));
- continue;
- }
- acc += conf_mutate_regen_bound;
-
- if (x <= acc + conf_mutate_chvar_bound)
- {
- if (0) // if num_variants > 1
- {
- // FIXME: assign new variant
- }
- }
- }
-}
-
-void elite(void)
-{
- for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
- {
- population2[pop2_ind] = population1[0];
- }
-}
-
-void rank_population(void)
-{
- // FIXME
-}
-
-void gen_coords(struct placement *p)
-{
- switch(p->request->type)
- {
- case REQUEST_POINT:
- gen_coords_point(p);
- }
-}
-
-void gen_coords_point(struct placement *p UNUSED)
-{
- // FIXME
-}
-
-struct map_part **get_parts(struct placement *symbol, struct individual *individual)
-{
- struct map_part **buffer;
- GARY_INIT(buffer, 0);
- int x_min = symbol->x / conf_part_size;
- int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
- int y_min = symbol->y / conf_part_size;
- int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
-
- for (int x=x_min; x < x_max; x++)
- for (int y=y_min; y < y_max; y++)