X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=labeller.c;h=3c5e4fa486c1ec6ca1e908b1fec10a60f29eb4da;hb=e488aed919c541b40dc8a10de305980e964a4889;hp=f2792ee091b8912e17e1328abc88c50761c88db6;hpb=b54512ef7fcbb57d7e3a088c75d75688b2af4016;p=leo.git diff --git a/labeller.c b/labeller.c index f2792ee..3c5e4fa 100644 --- a/labeller.c +++ b/labeller.c @@ -51,6 +51,8 @@ int dbg_movement = 0; int dbg_init = 0; int dbg_overlaps = 0; int dbg_rank = 0; +int dbg_evolution = 0; +int dbg_mutation = 0; int page_width_int; int page_height_int; @@ -73,16 +75,20 @@ int conf_breed_pop_size_perc = 20; int conf_breed_perc = 50; // Percentage of new pop created by breeding bool conf_mutate_children = 1; -int conf_mutate_children_prob = 0.3; +double conf_mutate_children_prob = 0.3; -int conf_mutate_rbest_perc = 60; -int conf_mutate_pop_size_perc = 20; +double conf_mutate_rbest = 1; +double conf_mutate_pop_size = 0.9; -int conf_mutate_move_bound = 0.2; -int conf_mutate_regen_bound = 0.1; -int conf_mutate_chvar_bound = 0.1; +double conf_mutate_move_bound = 1.0; +double conf_mutate_regen_bound = 0.0; +double conf_mutate_chvar_bound = 0.0; -int conf_elite_perc = 5; +int mutate_pop_size; +int mutate_rbest_size; + +double conf_elite_pop_size = 0.1; +int elite_pop_size; double conf_max_section_length = 100; double conf_max_section_overlay = 10; @@ -144,8 +150,10 @@ void dump_longlines(void); void dump_linelabel_requests(void); void dump_individual(struct individual *individual); void print_label(struct symbol *sym); +void dump_penalties(void); double gen_movement(void); +double gen_movement_uniform(void); void gen_coords(struct placement *p); void gen_coords_point(struct placement *p); void gen_coords_segment(struct placement *p); @@ -172,6 +180,7 @@ struct placement **get_closure(struct placement *placement, struct individual *p void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child); void move_symbol(struct placement *p); void move_symbol_point(struct placement *p); +void move_symbol_segment(struct placement *p); struct placement **get_overlapping(struct placement *p); void filter(struct placement **list, bool *pred); @@ -694,6 +703,7 @@ struct request_line *make_new_line(void) rl->request.ind = num_requests++; rl->request.type = REQUEST_LINE; GARY_INIT(rl->sections, 0); + GARY_INIT(rl->request.variants, 0); return rl; } @@ -705,6 +715,7 @@ struct request_section *make_new_section(struct request_line *rl) rls->request.type = REQUEST_SECTION; rls->num_segments = 0; GARY_INIT(rls->segments, 0); + GARY_INIT(rls->request.variants, 0); return rls; } @@ -717,9 +728,9 @@ struct request_segment *make_new_segment(struct request_section *rls, struct sym rs->request.ind = num_requests++; rs->request.type = REQUEST_SEGMENT; - struct variant *v = malloc(sizeof(struct variant)); + GARY_INIT(rs->request.variants, 0); + struct variant *v = GARY_PUSH(rs->request.variants); make_bitmap(v, sym); - rs->request.variants = v; return rs; } @@ -1029,7 +1040,14 @@ if (dbg_plan) ASSERT(s->type != SYMBOLIZER_INVALID); } } +} +void dump_penalties(void) +{ + for (int i=0; ipenalty); + } } void labeller_label(void) @@ -1044,20 +1062,53 @@ printf("Having %u point requests, %u line requests and %u area requests\n", GARY GARY_INIT(population1, conf_pop_size); GARY_INIT(population2, conf_pop_size); make_population(); + rank_population(); + qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual); + + if (dbg_evolution) + dump_penalties(); printf("Dealing with %d requests\n", num_requests); -/* + mutate_pop_size = conf_mutate_pop_size * conf_pop_size; + mutate_rbest_size = conf_mutate_rbest * conf_pop_size; + if (dbg_evolution) + { + printf("Mutation parameters:\n"); + printf(" %d individuals are created\n", mutate_pop_size); + printf(" %d best individuals in old population are considered\n", mutate_rbest_size); + } + + elite_pop_size = conf_elite_pop_size * conf_pop_size; + if (dbg_evolution) + { + printf("Elitism parameters:\n"); + printf(" %d best individuals are copied\n", elite_pop_size); + } + while (! shall_terminate()) { iteration++; + if (dbg_evolution) + printf("*** Iteration %d ***\n", iteration); + + mutate(); + elite(); struct individual **swp = population1; population1 = population2; population2 = swp; pop2_ind = 0; + + if (dbg_evolution) + dump_penalties(); + + rank_population(); + qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual); + + if (dbg_evolution) + dump_penalties(); } -*/ plan_individual(population1[0]); @@ -1074,6 +1125,10 @@ void make_population(void) { for (int i=0; iplacements); i++) { - int x = randint(1, 1000); - int acc = 0; + double x = randdouble(); + double acc = 0; if (x <= acc + conf_mutate_move_bound) { + if (dbg_mutation) + printf("Mutation: Moving symbol in placement %u\n", i); move_symbol(&(individual->placements[i])); continue; } @@ -1242,9 +1303,20 @@ void perform_mutation(struct individual *individual) if (x <= acc + conf_mutate_chvar_bound) { - if (0) // if num_variants > 1 + struct placement *p = &(individual->placements[i]); + switch (p->request->type) { - // FIXME: assign new variant + case REQUEST_POINT: + case REQUEST_SEGMENT: + case REQUEST_AREA: + // Does nothing when there are 0 variants... does it mind? + p->variant_used = randint(0, GARY_SIZE(p->request->variants) - 1); + break; + case REQUEST_SECTION: + p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments)-1); + break; + default: + ; } } } @@ -1252,9 +1324,9 @@ void perform_mutation(struct individual *individual) void elite(void) { - for (int i=0; ix; int p2y = p2->y; int overlap = 0; - for (int y=max2(p1y, p2y); y<=min2(p1y+v1->height, p2y+v2->height); y++) - for (int x=max2(p1x, p2x); x<=min2(p1x+v1->width, p2x+v2->width); x++) + for (int y=max2(0, max2(p1y, p2y)); yheight, p2y+v2->height)); y++) + for (int x=max2(0, max2(p1x, p2x)); xwidth, p2x+v2->width)); x++) { if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] && v2->bitmap[(y-p2y)*v2->width + (x-p2x)]) @@ -1423,7 +1495,8 @@ void update_map_parts(struct placement *p) while (ml) { struct map_placement *mp = ml->mp; - mp->prev = mp->next; + + mp->prev->next = mp->next; if (mp->next) mp->next->prev = mp->prev; free(mp); @@ -1432,6 +1505,7 @@ void update_map_parts(struct placement *p) ml = ml->next; free(tmp); } + p->map_links = NULL; struct map_part **parts = get_map_parts(p); if (parts == NULL) return; @@ -1442,6 +1516,7 @@ void update_map_parts(struct placement *p) mp->placement = p; mp->next = parts[i]->placement->next; + mp->prev = parts[i]->placement; parts[i]->placement->next = mp; if (mp->next) mp->next->prev = mp; @@ -1489,6 +1564,11 @@ double gen_movement(void) return m; } +double gen_movement_uniform(void) +{ + return (move_max - move_min) * randdouble() * flip(1, -1); +} + void gen_coords_point(struct placement *p) { p->x = p->x + gen_movement(); @@ -1586,12 +1666,12 @@ void move_symbol(struct placement *p) switch (p->request->type) { case REQUEST_POINT: + case REQUEST_AREA: move_symbol_point(p); - case REQUEST_LINE: + break; case REQUEST_SEGMENT: - case REQUEST_AREA: - if (dbg_movement) - printf("Not yet implemented\n"); + move_symbol_segment(p); + break; default: ASSERT(p->request->type != REQUEST_INVALID); } @@ -1599,8 +1679,16 @@ void move_symbol(struct placement *p) void move_symbol_point(struct placement *p) { - p->x += (double) (move_min + randdouble()) * flip(1, -1); - p->y += (double) (move_min + randdouble()) * flip(1, -1); + p->x += gen_movement_uniform(); + p->y += gen_movement_uniform(); +} + +void move_symbol_segment(struct placement *p) +{ + double m = gen_movement_uniform(); + // CHECK ME + p->x += m; + p->y += m * ((struct request_segment *) p->request)->slope; } void hide_segment_labels(struct individual *individual) @@ -1723,10 +1811,19 @@ void cleanup(void) void copy_individual(struct individual *src, struct individual *dest) { - src->penalty = dest->penalty; + dest->penalty = src->penalty; GARY_INIT(dest->placements, GARY_SIZE(src->placements)); for (uns i=0; iplacements); i++) { dest->placements[i] = src->placements[i]; + dest->placements[i].map_links = NULL; + } + for (uns j=0; jmap); + GARY_INIT(part->placement, 0); + struct map_placement *mp = GARY_PUSH(part->placement); + mp->placement = &dummy_placement; + mp->next = mp->prev = NULL; } }