5 #include <ucw/mempool.h>
6 #include <ucw/eltpool.h>
13 #define HASH_NODE struct graph_node
14 #define HASH_PREFIX(x) hash_##x
15 #define HASH_KEY_ATOMIC id
16 #define HASH_WANT_FIND
18 #define HASH_WANT_CLEANUP
19 #include <ucw/hashtable.h>
26 #define BLOCK_SIZE 4096
28 //struct mempool *mpool_requests;
30 static struct request_point *requests_point;
31 static struct request_line *requests_line;
32 static struct request_area *requests_area;
34 static struct graph_edge **bfs_queue;
35 static struct longline *longlines; int num_longlines;
36 static struct buffer_line *buffer_line;
37 static struct buffer_linelabel *buffer_linelabel;
39 struct eltpool *ep_individuals;
41 struct individual **population1;
42 struct individual **population2;
49 int dbg_map_parts = 0;
63 int conf_pop_size = 50;
65 int conf_penalty_bound = 0;
66 int conf_stagnation_bound = 0;
67 int conf_iteration_limit = 4;
69 int conf_term_cond = TERM_COND_ITERATIONS;
71 int conf_breed_rbest_perc = 80;
72 int conf_breed_pop_size_perc = 20;
73 int conf_breed_perc = 50; // Percentage of new pop created by breeding
75 bool conf_mutate_children = 1;
76 int conf_mutate_children_prob = 0.3;
78 int conf_mutate_rbest_perc = 60;
79 int conf_mutate_pop_size_perc = 20;
81 int conf_mutate_move_bound = 0.2;
82 int conf_mutate_regen_bound = 0.1;
83 int conf_mutate_chvar_bound = 0.1;
85 int conf_elite_perc = 5;
87 double conf_max_section_length = 100;
88 double conf_max_section_overlay = 10;
90 int old_best = 0; // FIXME: Shall be int max
94 int conf_part_size = 50;
101 int conf_map_part_width = 5;
102 int conf_map_part_height = 5;
103 int num_map_parts_row;
104 int num_map_parts_col;
107 void make_graph(void);
108 void label_graph(void);
109 void join_edge(struct graph_edge *e, int dir);
110 void bfs(uns longline);
111 void make_segments(void);
113 int overlaps(struct placement *p1, struct placement *p2);
114 int get_overlap(struct placement *p);
115 int individual_overlap(struct individual *individual);
117 void make_population(void);
118 bool shall_terminate(void);
122 void rank_population(void);
123 void plan_individual(struct individual *individual);
125 void make_bitmap(struct variant *v, struct symbol *sym);
126 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
127 void make_bitmap_point(struct variant *v, struct sym_point *sp);
128 void make_bitmap_label(struct variant *v, struct sym_text *text);
130 void cut_edge(struct graph_edge *e, double dist);
131 struct request_line *make_new_line(void);
132 struct request_section *make_new_section(struct request_line *rl);
133 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
135 void dump_bitmaps(struct individual *individual);
136 void dump_graph(void);
138 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
139 void bfs_wrapper(void);
141 void dump_longlines(void);
142 void dump_linelabel_requests(void);
143 void dump_individual(struct individual *individual);
144 void print_label(struct symbol *sym);
146 double gen_movement(void);
147 void gen_coords(struct placement *p);
148 void gen_coords_point(struct placement *p);
149 void gen_coords_segment(struct placement *p);
150 void gen_coords_area(struct placement *p);
152 struct map_part **get_map_parts(struct placement *p);
153 void update_map_parts(struct placement *p);
155 void make_segments_old(void);
157 void labeller_cleanup(void);
159 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
160 void perform_mutation(struct individual *individual);
162 void hide_segment_labels(struct individual *individual);
163 void init_placement(struct placement *p, struct individual *individual, struct request *r);
164 void init_individual(struct individual *i);
165 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
167 int randint(int min, int max);
169 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2);
170 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
171 void move_symbol(struct placement *p);
172 void move_symbol_point(struct placement *p);
174 struct placement **get_overlapping(struct placement *p);
175 void filter(struct placement **list, bool *pred);
177 int flip(int a, int b);
178 double randdouble(void);
182 void copy_individual(struct individual *src, struct individual *dest);
184 int max2(int a, int b);
185 int min2(int a, int b);
186 int max4(int a, int b, int c, int d);
187 int min4(int a, int b, int c, int d);
189 struct placement dummy_placement;
191 int max2(int a, int b)
193 return (a > b ? a : b);
196 int min2(int a, int b)
198 return (a < b ? a : b);
201 int max4(int a, int b, int c, int d)
203 return max2(max2(a, b), max2(c, d));
206 int min4(int a, int b, int c, int d)
208 return min2(min2(a, b), min2(c, d));
211 void print_label(struct symbol *sym)
215 case SYMBOLIZER_TEXT: ;
216 struct sym_text *st = (struct sym_text *) sym;
217 printf("%s\n", osm_val_decode(st->text));
224 void labeller_init(void)
226 GARY_INIT(requests_point, 0);
227 GARY_INIT(requests_line, 0);
228 GARY_INIT(requests_area, 0);
229 GARY_INIT(buffer_line, 0);
230 GARY_INIT(buffer_linelabel, 0);
231 ep_individuals = ep_new(sizeof(struct individual), 1);
233 page_width_int = floor(page_width);
234 page_height_int = floor(page_height);
236 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
237 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
238 num_map_parts = num_map_parts_row * num_map_parts_col;
241 void make_bitmap(struct variant *v, struct symbol *sym)
245 case SYMBOLIZER_POINT:
246 make_bitmap_point(v, (struct sym_point *) sym);
248 case SYMBOLIZER_ICON:
249 make_bitmap_icon(v, (struct sym_icon *) sym);
251 case SYMBOLIZER_TEXT:
252 make_bitmap_label(v, (struct sym_text *) sym);
255 ASSERT(sym->type != SYMBOLIZER_INVALID);
259 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
261 v->width = si->sir.width + 1;
262 v->height = si->sir.height + 1;
263 v->bitmap = malloc(v->width * v->height * sizeof(bool));
264 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
267 void make_bitmap_point(struct variant *v, struct sym_point *sp)
269 v->width = v->height = sp->size + 1;
270 v->bitmap = malloc(v->width * v->height * sizeof(bool));
271 // FIXME: Okay, memset would be much nicer here
272 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
275 void make_bitmap_label(struct variant *v, struct sym_text *text)
277 v->width = ceil(text->tw);
278 v->height = ceil(text->th);
279 v->bitmap = malloc(v->width * v->height * sizeof(bool));
280 for (int i=0; i<v->height; i++)
281 for (int j=0; j<v->width; j++)
283 v->bitmap[i*v->width + j] = 1;
287 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
290 printf("Adding point\n");
291 if (object->type != OSM_TYPE_NODE)
293 printf("Warning: Point label requested on non-point object\n");
297 struct request_point *r = GARY_PUSH(requests_point);
299 r->request.type = REQUEST_POINT;
300 r->request.ind = num_requests++;
309 GARY_INIT(r->request.variants, 0);
311 struct variant *v = GARY_PUSH(r->request.variants);
313 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
318 case SYMBOLIZER_ICON:
319 make_bitmap_icon(v, (struct sym_icon *) sym);
320 r->x = ((struct sym_icon *)sym)->sir.x;
321 r->y = ((struct sym_icon *)sym)->sir.y;
323 case SYMBOLIZER_POINT:
324 make_bitmap_point(v, (struct sym_point *) sym);
326 case SYMBOLIZER_TEXT: ;
327 struct sym_text *st = (struct sym_text *) sym;
328 struct osm_node *n = (struct osm_node *) object;
329 make_bitmap_label(v, st);
335 // printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
338 void labeller_add_line(struct symbol *sym, z_index_t zindex)
341 printf("Adding line on %u\n", zindex);
342 struct buffer_line *b = GARY_PUSH(buffer_line);
343 b->line = (struct sym_line *) sym;
345 sym_plan(sym, zindex);
348 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
350 if (o->type != OSM_TYPE_WAY)
357 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
358 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
359 ll->way = (struct osm_way *) o;
364 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
367 printf("Adding area on %u\n", zindex);
368 struct request_area *r = GARY_PUSH(requests_area);
370 r->request.type = REQUEST_AREA;
371 r->request.ind = num_requests++;
373 r->o = (struct osm_multipolygon *) o;
377 osm_obj_center(o, &(r->cx), &(r->cy));
379 GARY_INIT(r->request.variants, 0);
380 struct variant *v = GARY_PUSH(r->request.variants);
383 case SYMBOLIZER_ICON:
385 printf("DEBUG: Icon label\n");
386 make_bitmap_icon(v, (struct sym_icon *) sym);
388 case SYMBOLIZER_TEXT:
390 printf("DEBUG: Text label\n");
391 make_bitmap_label(v, (struct sym_text *) sym);
398 void make_graph(void)
401 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
403 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
404 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
406 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
407 struct graph_node *g_prev = NULL;
408 struct osm_node *o_prev = NULL;
410 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
412 // FIXME: Shall osm_object's type be checked here?
413 struct osm_node *o_node = (struct osm_node *) ref->o;
415 struct graph_node *g_node = hash_find(ref->o->id);
418 g_node = hash_new(ref->o->id);
419 GARY_INIT(g_node->edges, 0);
421 g_node->id = ref->o->id;
422 g_node->num = num_nodes++;
432 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
433 e->num = num_edges++;
434 e->id = buffer_line[i].line->s.o->id;
435 e->color = buffer_line[i].line->color;
436 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
442 e->longline = (uns) -1;
443 e->line = buffer_line[i].line;
447 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
449 edge = GARY_PUSH(g_node->edges);
457 printf("Made graph with %d edges\n", num_edges_dbg);
460 void dump_graph(void)
462 HASH_FOR_ALL(hash, node)
464 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
465 for (uns i=0; i<GARY_SIZE(node->edges); i++)
467 struct graph_edge *e = node->edges[i];
468 printf("\t edge (%d) #%ju to ", e->num, e->id);
469 if (node->edges[i]->n1->id == node->id)
470 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
471 else if (node->edges[i]->n2->id == node->id)
472 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
474 printf("BEWARE! BEWARE! BEWARE!\n");
477 if ((node->edges[i]->label)) printf("Labelled\n");
478 if ((node->edges[i]->label) && (node->edges[i]->label->type == SYMBOLIZER_TEXT)) printf(" labelled %s;", osm_val_decode(((struct sym_text *) node->edges[i]->label)->text));
479 printf(" colored %d;", node->edges[i]->color);
480 printf(" length %.2f", node->edges[i]->length);
487 void label_graph(void)
490 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
491 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
493 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
495 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
496 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
499 printf("Looking for node %ju\n", ref->o->id);
500 struct graph_node *n = hash_find(ref->o->id);
503 // FIXME: What shall be done?
508 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
509 for (uns j=0; j<GARY_SIZE(n->edges); j++)
511 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
514 printf("Labelling node %ju\n", n->id);
515 n->edges[j]->label = buffer_linelabel[i].label;
516 n->edges[j]->zindex = buffer_linelabel[i].zindex;
524 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
527 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
528 struct graph_edge *candidate = NULL;
530 for (uns i=0; i<GARY_SIZE(node->edges); i++)
532 struct graph_edge *other = node->edges[i];
533 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
535 if ((uns) other->visited != e->longline) {
537 printf("Pushing new edge %d / %ju\n", other->num, other->id);
538 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
540 other->visited = e->longline;
543 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
544 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
547 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
548 (e->label) && (other->label) &&
549 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
550 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
552 if (! candidate || (other->length > candidate->length))
560 printf("New line in longline %u\n", e->longline);
561 struct graph_edge *other = candidate;
562 other->longline = e->longline;
564 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
565 ((dir == DIR_FWD) && (other->n2->id == node->id)))
567 struct graph_node *swp = other->n2;
568 other->n2 = other->n1;
577 longlines[other->longline].first = other;
590 void bfs(uns longline)
593 printf("BFS called for longline %u\n", longline);
595 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
596 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
598 struct graph_edge *cur = bfs_queue[i];
600 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
601 //ASSERT(! cur->visited);
603 cur->visited = longline;
605 if (cur->longline == (uns) -1)
608 if (cur->dir == DIR_UNSET)
610 cur->dir = DIR_CENTER;
611 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
612 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
619 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
622 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
632 void bfs_wrapper(void)
634 GARY_INIT(bfs_queue, 0);
635 GARY_INIT(longlines, 0);
637 HASH_FOR_ALL(hash, node)
639 for (uns i=0; i<GARY_SIZE(node->edges); i++)
641 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
643 GARY_PUSH(longlines);
644 longlines[num_longlines].first = node->edges[i];
646 printf("Running new BFS\n");
648 printf("Creating longline %u\n", num_longlines);
649 GARY_RESIZE(bfs_queue, 0);
650 struct graph_edge **e = GARY_PUSH(bfs_queue);
652 node->edges[i]->longline = num_longlines;
653 bfs(node->edges[i]->longline);
656 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
658 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
665 GARY_FREE(bfs_queue);
668 void dump_longlines(void)
670 printf("*** Longlines dump\n");
671 for (uns i=0; i<GARY_SIZE(longlines); i++)
673 printf("Longline %u:", i);
674 struct graph_edge *e = longlines[i].first;
675 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
676 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
681 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
682 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
689 struct request_line *make_new_line(void)
691 struct request_line *rl = GARY_PUSH(requests_line);
692 rl->request.ind = num_requests++;
693 rl->request.type = REQUEST_LINE;
694 GARY_INIT(rl->sections, 0);
699 struct request_section *make_new_section(struct request_line *rl)
701 struct request_section *rls = GARY_PUSH(rl->sections);
702 rls->request.ind = num_requests++;
703 rls->request.type = REQUEST_SECTION;
704 rls->num_segments = 0;
705 GARY_INIT(rls->segments, 0);
710 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
712 struct request_segment *rs = GARY_PUSH(rls->segments);
715 rs->request.ind = num_requests++;
716 rs->request.type = REQUEST_SEGMENT;
718 struct variant *v = malloc(sizeof(struct variant));
720 rs->request.variants = v;
725 void cut_edge(struct graph_edge *e, double dist)
728 printf("Cutting [%.2f; %.2f] -- [%.2f; %.2f] to dist %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, dist);
730 struct graph_edge *new = malloc(sizeof(struct graph_edge));
734 // FIXME? Create new label for new edge, don't only copy pointer?
736 struct osm_node *n1 = e->n1->o;
737 struct osm_node *n2 = e->n2->o;
740 if ((n1->x == n2->x) && (n1->y == n2->y))
742 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
743 printf("Won't cut point\n");
747 struct osm_node *n11 = malloc(sizeof(struct osm_node));
748 struct graph_node *gn = malloc(sizeof(struct graph_node));
750 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
751 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
752 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
754 e->n2 = new->n1 = gn;
756 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
757 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
761 void make_segments(void)
763 for (uns i=0; i<GARY_SIZE(longlines); i++)
765 // Skip lines which are not labelled
766 if (! (longlines[i].first && longlines[i].first->label))
769 struct request_line *request = make_new_line();
770 struct request_section *rls = make_new_section(request);
771 struct request_segment *rs = NULL;
773 struct graph_edge *e = longlines[i].first;
774 double cur_length = 0;
776 struct sym_text *st = NULL;
777 if (e->label->type == SYMBOLIZER_TEXT)
779 st = (struct sym_text *) e->label;
784 printf("Warning: Skipping line\n");
790 printf("New longline\n");
795 printf("BEWARE: Edge cycle\n");
798 e->visited = -1; // FIXME
801 printf("Taking edge from [%.2f; %.2f] to [%.2f; %.2f] of length %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->length);
803 if (st && (e->length < st->tw))
806 //printf("Warning: Skipping segment\n");
810 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
813 printf("Edge too long, length is %.2f; %.2f - %.2f = %.2f\n", e->length, conf_max_section_length, cur_length, conf_max_section_length - cur_length);
814 // HACK to prevent cutting to 0 lenght
815 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
818 rs = make_new_segment(rls, e->label);
819 rs->label = malloc(sizeof(struct sym_text));
820 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
822 rs->x1 = e->n1->o->x;
823 rs->y1 = e->n1->o->y;
824 rs->x2 = e->n2->o->x;
825 rs->y2 = e->n2->o->y;
827 // FIXME: Set text rotation
828 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
829 rs->zindex = e->zindex;
831 cur_length += e->length;
832 if (cur_length > conf_max_section_length)
835 printf("Making new section, new length would be %f, allowed is %.2f / %.2f\n", cur_length + e->length, conf_max_section_length, conf_max_section_overlay);
837 rls = make_new_section(request);
844 if (request->sections[0].num_segments == 0)
847 printf("WARNING: 0 segment section\n");
848 GARY_POP(requests_line);
854 void dump_linelabel_requests(void)
856 for (uns i=0; i<GARY_SIZE(requests_line); i++)
858 if (requests_line[i].sections[0].num_segments == 0)
863 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
864 print_label(requests_line[i].sections[0].segments[0].label);
865 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
867 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
868 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
870 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
871 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
878 void dump_bitmaps(struct individual *individual)
880 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
881 printf("Bitmap size is %d\n", page_width_int * page_height_int);
882 for (int i=0; i<page_height_int; i++)
883 for (int j=0; j<page_width_int; j++)
884 bitmap[i*page_width_int + j] = 0;
886 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
888 if (individual->placements[i].variant_used == -1) continue;
890 struct placement *p = &(individual->placements[i]);
891 struct variant *v = NULL;
893 switch (p->request->type)
895 case REQUEST_SEGMENT: ;
896 case REQUEST_POINT: ;
898 v = &(p->request->variants[p->variant_used]);
901 ASSERT(p->request->type != REQUEST_INVALID);
905 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
907 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
909 bitmap[row * page_width_int + col] = 1;
915 FILE *fd_dump = fopen("dump.pbm", "w");
916 fprintf(fd_dump, "P1\n");
917 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
918 for (int i=0; i<page_height_int; i++)
920 for (int j=0; j<page_width_int; j++)
922 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
924 fprintf(fd_dump, "\n");
929 void dump_individual(struct individual *individual)
931 printf("*** Dumping INDIVIDUAL ***\n");
932 printf("(There are %d requests)\n", num_requests);
933 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
935 struct placement *p = &(individual->placements[i]);
937 switch (p->request->type)
940 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
943 struct request_line *rl = (struct request_line *) p->request;
945 print_label(rl->sections[0].segments[0].label);
947 case REQUEST_SECTION: ;
950 case REQUEST_SEGMENT: ;
951 if (p->variant_used >= 0)
952 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
954 printf("Segment not placed\n");
957 struct request_area *ra = (struct request_area *) p->request;
958 printf("Area label ");
959 print_label(ra->label);
960 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
963 printf("Testing request type (dump_individual)\n");
964 ASSERT(p->request->type != 0);
967 printf("\nTotal penalty: %d\n", individual->penalty);
970 void plan_individual(struct individual *individual)
972 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
974 struct symbol *s = NULL;
975 z_index_t zindex = 0;
976 if (individual->placements[i].variant_used < 0) continue;
977 switch (individual->placements[i].request->type)
979 case REQUEST_POINT: ;
980 struct request_point *rp = (struct request_point *) individual->placements[i].request;
984 case REQUEST_SEGMENT: ;
985 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
992 struct request_area *ra = (struct request_area *) individual->placements[i].request;
997 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1002 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1004 if (s) switch (s->type)
1006 case SYMBOLIZER_POINT: ;
1007 struct sym_point *sp = (struct sym_point *) s;
1008 sp->x = individual->placements[i].x;
1009 sp->y = individual->placements[i].y;
1010 sym_plan((struct symbol *) sp, zindex);
1012 case SYMBOLIZER_ICON: ;
1013 struct sym_icon *si = (struct sym_icon *) s;
1014 si->sir.x = individual->placements[i].x;
1015 si->sir.y = individual->placements[i].y;
1016 sym_plan((struct symbol *) si, zindex);
1018 case SYMBOLIZER_TEXT: ;
1019 struct sym_text *st = (struct sym_text *) s;
1020 st->x = individual->placements[i].x;
1021 st->y = individual->placements[i].y;
1022 st->next_duplicate = NULL;
1023 if (dbg_plan) printf("Planning text %s at [%.2f; %.2f] on %u, with rotation %.2f\n", osm_val_decode(st->text), st->x, st->y, zindex, st->rotate);
1024 sym_plan((struct symbol *) st, zindex);
1027 ASSERT(s->type != SYMBOLIZER_INVALID);
1033 void labeller_label(void)
1040 printf("Having %u point requests, %u line requests and %u area requests\n", GARY_SIZE(requests_point), GARY_SIZE(requests_line), GARY_SIZE(requests_area));
1042 GARY_INIT(population1, conf_pop_size);
1043 GARY_INIT(population2, conf_pop_size);
1046 printf("Dealing with %d requests\n", num_requests);
1049 while (! shall_terminate())
1053 struct individual **swp = population1;
1054 population1 = population2;
1060 plan_individual(population1[0]);
1067 void labeller_cleanup(void)
1071 void make_population(void)
1073 for (int i=0; i<conf_pop_size; i++)
1076 printf("Making individual %d\n", i);
1077 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1078 population1[i] = individual;
1081 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1083 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1086 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1088 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1090 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1092 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1094 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1096 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1101 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1103 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1106 hide_segment_labels(individual);
1108 ASSERT(p == num_requests);
1112 bool shall_terminate(void)
1114 switch (conf_term_cond)
1116 case TERM_COND_PENALTY:
1117 return (population1[0]->penalty < conf_penalty_bound);
1118 case TERM_COND_STAGNATION:
1119 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1120 case TERM_COND_ITERATIONS:
1121 return (iteration >= conf_iteration_limit);
1123 // FIXME: Warn the user that no condition is set
1132 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1133 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1134 struct individual **breed_buffer;
1135 while (i < conf_breed_pop_size)
1137 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1138 int parent1 = randint(1, conf_breed_pop_size);
1139 int parent2 = randint(1, conf_breed_pop_size);
1140 printf("Will breed %d and %d, chosen of %d best of %d population (intended to be %d)\n", parent1, parent2, conf_breed_pop_size, GARY_SIZE(population1), conf_pop_size);
1141 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1142 population2[pop2_ind++] = breed_buffer[0];
1143 population2[pop2_ind++] = breed_buffer[1];
1148 acc += conf_breed_rbest_perc;
1150 return; // FIXME: DEBUG HACK
1152 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1153 int step = remaining / conf_pop_size;
1154 for (; i<conf_pop_size; i += 2)
1156 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1157 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1158 population2[pop2_ind++] = breed_buffer[0];
1159 population2[pop2_ind++] = breed_buffer[1];
1162 // FIXME: Could there be one missing individual?
1165 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1167 struct individual **buffer = malloc(2*sizeof(struct individual));
1168 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1169 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1171 printf("Performing crossover\n");
1173 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1175 printf("%dth placement out of %d\n", i, num_requests);
1176 if (! parent1->placements[i].processed)
1178 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1179 int x = randint(1, 2);
1183 copy_symbols(clos_symbols, parent1, child1);
1184 copy_symbols(clos_symbols, parent2, child2);
1188 copy_symbols(clos_symbols, parent2, child1);
1189 copy_symbols(clos_symbols, parent1, child2);
1191 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1192 GARY_FREE(clos_symbols);
1195 if (conf_mutate_children)
1197 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1198 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1210 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1211 while (i < conf_mutate_rbest_perc * conf_pop_size)
1213 int ind = randint(1, conf_mutate_pop_size);
1214 copy_individual(population2[pop2_ind], population1[ind]);
1215 perform_mutation(population2[pop2_ind]);
1220 void perform_mutation(struct individual *individual)
1222 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1224 int x = randint(1, 1000);
1227 if (x <= acc + conf_mutate_move_bound)
1229 move_symbol(&(individual->placements[i]));
1232 acc += conf_mutate_move_bound;
1234 if (x <= acc + conf_mutate_regen_bound)
1236 gen_coords(&(individual->placements[i]));
1239 acc += conf_mutate_regen_bound;
1241 if (x <= acc + conf_mutate_chvar_bound)
1243 if (0) // if num_variants > 1
1245 // FIXME: assign new variant
1253 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1255 population2[pop2_ind++] = population1[0];
1259 int overlaps(struct placement *p1, struct placement *p2)
1261 if (p1->request->type != REQUEST_POINT &&
1262 p1->request->type != REQUEST_SEGMENT &&
1263 p1->request->type != REQUEST_AREA)
1266 if (p2->request->type != REQUEST_POINT &&
1267 p2->request->type != REQUEST_SEGMENT &&
1268 p2->request->type != REQUEST_AREA)
1271 if (p1->variant_used == -1 || p2->variant_used == -1)
1274 struct variant *v1, *v2;
1276 v1 = &(p1->request->variants[p1->variant_used]);
1277 v2 = &(p2->request->variants[p2->variant_used]);
1279 int p1x = p1->x; int p1y = p1->y;
1280 int p2x = p2->x; int p2y = p2->y;
1283 for (int y=max2(p1y, p2y); y<=min2(p1y+v1->height, p2y+v2->height); y++)
1284 for (int x=max2(p1x, p2x); x<=min2(p1x+v1->width, p2x+v2->width); x++)
1286 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1287 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1294 int get_overlap(struct placement *p)
1296 struct map_part **parts = get_map_parts(p);
1300 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1304 struct placement **others;
1307 GARY_INIT_ZERO(planned, num_requests);
1308 planned[p->request->ind] = 1;
1309 GARY_INIT(others, 0);
1311 for (uns i=0; i<GARY_SIZE(parts); i++)
1313 struct map_placement *mp = parts[i]->placement->next;
1316 if (! planned[mp->placement->request->ind])
1318 struct placement **p = GARY_PUSH(others);
1320 planned[mp->placement->request->ind] = true;
1327 for (uns i=0; i<GARY_SIZE(others); i++)
1329 overlap += overlaps(p, others[i]);
1339 int individual_overlap(struct individual *individual)
1343 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1345 overlap += get_overlap(&individual->placements[i]);
1351 void rank_population(void)
1355 for (int i=0; i<conf_pop_size; i++)
1358 printf("Individual %d\n", i);
1359 population1[i]->penalty = 0;
1361 penalty = individual_overlap(population1[i]);
1363 printf("Increasing penalty by %d for overlap\n", penalty);
1364 population1[i]->penalty += penalty;
1368 struct map_part **get_map_parts(struct placement *p)
1370 if (p->variant_used < 0) return NULL;
1372 struct map_part **buffer;
1373 GARY_INIT(buffer, 0);
1376 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1379 switch (p->request->type)
1382 case REQUEST_SEGMENT:
1384 v = p->request->variants[p->variant_used];
1390 int x_min = max2(0, p->x) / conf_map_part_width;
1391 int x_max = min2(page_width_int, (p->x + v.width + conf_map_part_width - 1)) / conf_map_part_width;
1392 int y_min = max2(0, p->y) / conf_map_part_height;
1393 int y_max = min2(page_height_int, (p->y + v.height + conf_map_part_height - 1)) / conf_map_part_height;
1396 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1398 for (int y=y_min; y<=y_max; y++)
1399 for (int x=x_min; x<=x_max; x++)
1401 struct map_part **m = GARY_PUSH(buffer);
1403 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1404 *m = p->individual->map[y * num_map_parts_row + x];
1410 void update_map_parts(struct placement *p)
1412 struct placement_link *ml = p->map_links;
1415 struct map_placement *mp = ml->mp;
1416 mp->prev = mp->next;
1418 mp->next->prev = mp->prev;
1421 struct placement_link *tmp = ml;
1426 struct map_part **parts = get_map_parts(p);
1427 if (parts == NULL) return;
1429 for (uns i=0; i<GARY_SIZE(parts); i++)
1431 struct map_placement *mp = malloc(sizeof(struct map_placement));
1434 mp->next = parts[i]->placement->next;
1435 parts[i]->placement->next = mp;
1436 if (mp->next) mp->next->prev = mp;
1438 struct placement_link *ml = malloc(sizeof(struct placement_link));
1440 ml->next = p->map_links;
1447 void gen_coords(struct placement *p)
1449 switch(p->request->type)
1452 gen_coords_point(p);
1457 case REQUEST_SEGMENT:
1458 gen_coords_segment(p);
1462 printf("Not yet implemented\n");
1466 printf("Testing request type\n");
1467 ASSERT(p->request->type != REQUEST_INVALID);
1470 update_map_parts(p);
1473 double gen_movement(void)
1475 double m = (random() % 1000000) / 10000;
1476 m = pow(m, 1.0/3) * flip(1, -1);
1478 printf("Movement %.2f\n", m);
1482 void gen_coords_point(struct placement *p)
1484 p->x = p->x + gen_movement();
1487 void gen_coords_segment(struct placement *p)
1489 struct request_segment *rs = (struct request_segment *) p->request;
1491 p->x = (a == 1 ? rs->x1 : rs->x2);
1492 p->y = (a == 1 ? rs->y1 : rs->y2);
1495 void gen_coords_area(struct placement *p)
1497 struct request_area *ra = (struct request_area *) p->request;
1499 p->x = p->x + gen_movement();
1500 p->y = p->y + gen_movement();
1503 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1506 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1508 struct map_part **buffer;
1509 GARY_INIT(buffer, 0);
1510 int x_min = symbol->x / conf_part_size;
1511 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1512 int y_min = symbol->y / conf_part_size;
1513 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1515 for (int x=x_min; x < x_max; x++)
1516 for (int y=y_min; y < y_max; y++)
1518 struct map_part *m = GARY_PUSH(buffer);
1519 *m = individual->map[x][y];
1525 int randint(int min, int max)
1527 if (min == max) return min;
1529 //printf("Returning %d + (%d %% (%d - %d)) = %d + %d %% %d = %d + %d = %d\n", min, r, max, min, min, r, max-min, min, r%(max-min), min+(r%(max-min)));
1530 return min + (r % (max - min));
1531 return (r * (max - min));
1534 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1536 printf("Getting closure\n");
1537 struct placement **closure;
1538 GARY_INIT(closure, 0);
1539 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1540 chosen[placement->request->ind] = 1;
1542 struct placement **p = GARY_PUSH(closure); *p = placement;
1545 while (first < GARY_SIZE(closure))
1547 printf("Iterating, first is %d\n", first);
1548 struct placement **overlapping = get_overlapping(placement);
1549 filter(overlapping, chosen);
1550 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1552 p = GARY_PUSH(closure); *p = overlapping[j];
1553 chosen[overlapping[j]->request->ind] = 1;
1555 GARY_FREE(overlapping);
1562 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1564 //printf("%d\n", child->penalty);
1565 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1566 for (uns i=0; i<GARY_SIZE(closure); i++)
1568 int ind = closure[i]->request->ind;
1569 child->placements[ind] = parent->placements[ind];
1570 child->placements[ind].processed = 0;
1574 void move_symbol(struct placement *p)
1576 switch (p->request->type)
1579 move_symbol_point(p);
1581 case REQUEST_SEGMENT:
1584 printf("Not yet implemented\n");
1586 ASSERT(p->request->type != REQUEST_INVALID);
1590 void move_symbol_point(struct placement *p)
1592 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1593 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1596 void hide_segment_labels(struct individual *individual)
1598 // BEWARE: This fully depends on current genetic encoding
1600 int used = -1, num = -1;
1601 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1603 switch (individual->placements[i].request->type)
1605 case REQUEST_SECTION:
1606 used = individual->placements[i].variant_used;
1609 case REQUEST_SEGMENT:
1611 individual->placements[i].variant_used = 0;
1613 individual->placements[i].variant_used = -1;
1622 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1627 p->x = p->y = 0; // To prevent valgrind from complaining
1628 p->variant_used = 0;
1629 p->map_links = NULL;
1630 p->individual = individual;
1633 case REQUEST_POINT: ;
1634 struct request_point *rp = (struct request_point *) r;
1638 case REQUEST_LINE: ;
1640 case REQUEST_SECTION: ;
1641 struct request_section *rls = (struct request_section *) r;
1642 p->variant_used = randint(0, rls->num_segments);
1644 case REQUEST_SEGMENT: ;
1645 struct request_segment *rs = (struct request_segment *) r;
1649 case REQUEST_AREA: ;
1650 struct request_area *ra = (struct request_area *) r;
1653 p->variant_used = 0;
1656 ASSERT(p->request->type != REQUEST_INVALID);
1657 printf("Valid request: %d\n", p->request->type);
1662 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1665 void init_individual(struct individual *i)
1667 //printf("Initing individual\n");
1668 GARY_INIT(i->placements, num_requests);
1669 GARY_INIT(i->map, 0);
1670 for (uns j=0; j<num_map_parts; j++)
1672 struct map_part *part = GARY_PUSH(i->map);
1673 GARY_INIT(part->placement, 0);
1674 struct map_placement *mp = GARY_PUSH(part->placement);
1675 mp->placement = &dummy_placement;
1676 mp->next = mp->prev = NULL;
1678 i->penalty = 0; // FIXME
1681 printf("Individual inited, has %u map parts\n", GARY_SIZE(i->map));
1684 struct placement **get_overlapping(struct placement *p UNUSED)
1686 struct placement **buffer;
1687 GARY_INIT(buffer, 0);
1691 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1696 int flip(int a, int b)
1698 return (random() % 2 ? a : b);
1701 double randdouble(void)
1703 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1710 GARY_FREE(requests_point);
1711 GARY_FREE(requests_line);
1712 GARY_FREE(requests_area);
1715 void copy_individual(struct individual *src, struct individual *dest)
1717 src->penalty = dest->penalty;
1718 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1719 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1721 dest->placements[i] = src->placements[i];