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;
55 int conf_pop_size = 50;
57 int conf_penalty_bound = 0;
58 int conf_stagnation_bound = 0;
59 int conf_iteration_limit = 4;
61 int conf_term_cond = TERM_COND_ITERATIONS;
63 int conf_breed_rbest_perc = 80;
64 int conf_breed_pop_size_perc = 20;
65 int conf_breed_perc = 50; // Percentage of new pop created by breeding
67 bool conf_mutate_children = 1;
68 int conf_mutate_children_prob = 0.3;
70 int conf_mutate_rbest_perc = 60;
71 int conf_mutate_pop_size_perc = 20;
73 int conf_mutate_move_bound = 0.2;
74 int conf_mutate_regen_bound = 0.1;
75 int conf_mutate_chvar_bound = 0.1;
77 int conf_elite_perc = 5;
79 double conf_max_section_length = 100;
80 double conf_max_section_overlay = 10;
82 int old_best = 0; // FIXME: Shall be int max
86 int conf_part_size = 50;
93 int conf_map_part_width = 5;
94 int conf_map_part_height = 5;
95 int num_map_parts_row;
96 int num_map_parts_col;
99 void make_graph(void);
100 void label_graph(void);
101 void join_edge(struct graph_edge *e, int dir);
102 void bfs(uns longline);
103 void make_segments(void);
105 void make_population(void);
106 bool shall_terminate(void);
110 void rank_population(void);
111 void plan_individual(struct individual *individual);
113 void make_bitmap(struct point_variant *v, struct symbol *sym);
114 void make_bitmap_icon(struct point_variant *v, struct sym_icon *si);
115 void make_bitmap_point(struct point_variant *v, struct sym_point *sp);
116 void make_bitmap_label(struct point_variant *v, struct sym_text *text);
118 void cut_edge(struct graph_edge *e, double dist);
119 struct request_line *make_new_line(void);
120 struct request_section *make_new_section(struct request_line *rl);
121 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
123 void dump_bitmaps(struct individual *individual);
124 void dump_graph(void);
126 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
127 void bfs_wrapper(void);
129 void dump_longlines(void);
130 void dump_linelabel_requests(void);
131 void dump_individual(struct individual *individual);
132 void print_label(struct symbol *sym);
134 double gen_movement(void);
135 void gen_coords(struct placement *p);
136 void gen_coords_point(struct placement *p);
137 void gen_coords_segment(struct placement *p);
138 void gen_coords_area(struct placement *p);
140 struct map_part **get_map_parts(struct placement *p);
141 void update_map_parts(struct placement *p);
143 void make_segments_old(void);
145 void labeller_cleanup(void);
147 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
148 void perform_mutation(struct individual *individual);
150 void hide_segment_labels(struct individual *individual);
151 void init_placement(struct placement *p, struct individual *individual, struct request *r);
152 void init_individual(struct individual *i);
153 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
155 int randint(int min, int max);
157 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2);
158 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
159 void move_symbol(struct placement *p);
160 void move_symbol_point(struct placement *p);
162 struct placement **get_overlapping(struct placement *p);
163 void filter(struct placement **list, bool *pred);
165 int flip(int a, int b);
166 double randdouble(void);
170 void copy_individual(struct individual *src, struct individual *dest);
172 int max2(int a, int b);
173 int min2(int a, int b);
174 int max4(int a, int b, int c, int d);
175 int min4(int a, int b, int c, int d);
177 struct placement dummy_placement;
179 int max2(int a, int b)
181 return (a > b ? a : b);
184 int min2(int a, int b)
186 return (a < b ? a : b);
189 int max4(int a, int b, int c, int d)
191 return max2(max2(a, b), max2(c, d));
194 int min4(int a, int b, int c, int d)
196 return min2(min2(a, b), min2(c, d));
199 void print_label(struct symbol *sym)
203 case SYMBOLIZER_TEXT: ;
204 struct sym_text *st = (struct sym_text *) sym;
205 printf("%s\n", osm_val_decode(st->text));
212 void labeller_init(void)
214 GARY_INIT(requests_point, 0);
215 GARY_INIT(requests_line, 0);
216 GARY_INIT(requests_area, 0);
217 GARY_INIT(buffer_line, 0);
218 GARY_INIT(buffer_linelabel, 0);
219 ep_individuals = ep_new(sizeof(struct individual), 1);
221 page_width_int = floor(page_width);
222 page_height_int = floor(page_height);
224 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
225 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
226 num_map_parts = num_map_parts_row * num_map_parts_col;
229 void make_bitmap(struct point_variant *v, struct symbol *sym)
233 case SYMBOLIZER_POINT:
234 make_bitmap_point(v, (struct sym_point *) sym);
236 case SYMBOLIZER_ICON:
237 make_bitmap_icon(v, (struct sym_icon *) sym);
239 case SYMBOLIZER_TEXT:
240 make_bitmap_label(v, (struct sym_text *) sym);
243 ASSERT(sym->type != SYMBOLIZER_INVALID);
247 void make_bitmap_icon(struct point_variant *v, struct sym_icon *si)
249 v->width = si->sir.icon->width;
250 v->height = si->sir.icon->height;
251 v->bitmap = malloc((int) ceil(v->width * v->height * sizeof(bool)));
252 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
255 void make_bitmap_point(struct point_variant *v, struct sym_point *sp)
257 v->width = v->height = sp->size;
258 v->bitmap = malloc(sp->size*sp->size * sizeof(bool));
259 // FIXME: Okay, memset would be much nicer here
260 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
263 void make_bitmap_label(struct point_variant *v, struct sym_text *text)
265 v->width = ceil(text->tw);
266 v->height = ceil(text->th);
267 v->bitmap = malloc(v->width * v->height * sizeof(bool));
268 for (int i=0; i<v->height; i++)
269 for (int j=0; j<v->width; j++)
271 v->bitmap[i*v->width + j] = 1;
275 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
277 printf("Adding point\n");
278 if (object->type != OSM_TYPE_NODE)
280 printf("Warning: Point label requested on non-point object\n");
284 struct request_point *r = GARY_PUSH(requests_point);
286 r->request.type = REQUEST_POINT;
287 r->request.ind = num_requests++;
296 GARY_INIT(r->variants, 0);
298 struct point_variant *v = GARY_PUSH(r->variants);
300 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
305 case SYMBOLIZER_ICON:
306 make_bitmap_icon(v, (struct sym_icon *) sym);
307 r->x = ((struct sym_icon *)sym)->sir.x;
308 r->y = ((struct sym_icon *)sym)->sir.y;
310 case SYMBOLIZER_POINT:
311 make_bitmap_point(v, (struct sym_point *) sym);
313 case SYMBOLIZER_TEXT: ;
314 struct sym_text *st = (struct sym_text *) sym;
315 struct osm_node *n = (struct osm_node *) object;
316 make_bitmap_label(v, st);
322 // printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
325 void labeller_add_line(struct symbol *sym, z_index_t zindex)
327 printf("Adding line on %u\n", zindex);
328 struct buffer_line *b = GARY_PUSH(buffer_line);
329 b->line = (struct sym_line *) sym;
331 sym_plan(sym, zindex);
334 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
336 if (o->type != OSM_TYPE_WAY)
342 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
343 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
344 ll->way = (struct osm_way *) o;
349 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
351 printf("Adding area on %u\n", zindex);
352 struct request_area *r = GARY_PUSH(requests_area);
354 r->request.type = REQUEST_AREA;
355 r->request.ind = num_requests++;
357 r->o = (struct osm_multipolygon *) o;
361 osm_obj_center(o, &(r->cx), &(r->cy));
363 GARY_INIT(r->variants, 0);
364 struct point_variant *v = GARY_PUSH(r->variants);
367 case SYMBOLIZER_ICON:
368 printf("DEBUG: Icon label\n");
369 make_bitmap_icon(v, (struct sym_icon *) sym);
371 case SYMBOLIZER_TEXT:
372 printf("DEBUG: Text label\n");
373 make_bitmap_label(v, (struct sym_text *) sym);
380 void make_graph(void)
383 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
385 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
386 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
388 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
389 struct graph_node *g_prev = NULL;
390 struct osm_node *o_prev = NULL;
392 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
394 // FIXME: Shall osm_object's type be checked here?
395 struct osm_node *o_node = (struct osm_node *) ref->o;
397 struct graph_node *g_node = hash_find(ref->o->id);
400 g_node = hash_new(ref->o->id);
401 GARY_INIT(g_node->edges, 0);
403 g_node->id = ref->o->id;
404 g_node->num = num_nodes++;
414 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
415 e->num = num_edges++;
416 e->id = buffer_line[i].line->s.o->id;
417 e->color = buffer_line[i].line->color;
418 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
424 e->longline = (uns) -1;
425 e->line = buffer_line[i].line;
429 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
431 edge = GARY_PUSH(g_node->edges);
439 printf("Made graph with %d edges\n", num_edges_dbg);
442 void dump_graph(void)
444 HASH_FOR_ALL(hash, node)
446 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
447 for (uns i=0; i<GARY_SIZE(node->edges); i++)
449 struct graph_edge *e = node->edges[i];
450 printf("\t edge (%d) #%ju to ", e->num, e->id);
451 if (node->edges[i]->n1->id == node->id)
452 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
453 else if (node->edges[i]->n2->id == node->id)
454 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
456 printf("BEWARE! BEWARE! BEWARE!\n");
459 if ((node->edges[i]->label)) printf("Labelled\n");
460 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));
461 printf(" colored %d;", node->edges[i]->color);
462 printf(" length %.2f", node->edges[i]->length);
469 void label_graph(void)
471 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
472 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
474 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
475 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
476 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
478 printf("Looking for node %ju\n", ref->o->id);
479 struct graph_node *n = hash_find(ref->o->id);
482 // FIXME: What shall be done?
486 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
487 for (uns j=0; j<GARY_SIZE(n->edges); j++)
489 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
491 printf("Labelling node %ju\n", n->id);
492 n->edges[j]->label = buffer_linelabel[i].label;
493 n->edges[j]->zindex = buffer_linelabel[i].zindex;
501 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
503 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
504 struct graph_edge *candidate = NULL;
506 for (uns i=0; i<GARY_SIZE(node->edges); i++)
508 struct graph_edge *other = node->edges[i];
509 if (e->num == 987) printf("Got label %d\n", e->num);
510 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
511 if (e->num == 987) printf("Continuing with edge %d\n", e->num);
513 printf("Testing %d ?= %d\n", other->visited, e->longline);
514 if ((uns) other->visited != e->longline) {
515 printf("Pushing new edge %d / %ju\n", other->num, other->id);
516 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
518 other->visited = e->longline;
521 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
522 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
525 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
526 (e->label) && (other->label) &&
527 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
528 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
530 if (! candidate || (other->length > candidate->length))
537 printf("New line in longline %u\n", e->longline);
538 struct graph_edge *other = candidate;
539 other->longline = e->longline;
541 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
542 ((dir == DIR_FWD) && (other->n2->id == node->id)))
544 struct graph_node *swp = other->n2;
545 other->n2 = other->n1;
554 longlines[other->longline].first = other;
567 void bfs(uns longline)
569 printf("BFS called for longline %u\n", longline);
570 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
571 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
573 struct graph_edge *cur = bfs_queue[i];
574 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
575 //ASSERT(! cur->visited);
577 cur->visited = longline;
579 if (cur->longline == (uns) -1)
582 if (cur->dir == DIR_UNSET)
584 cur->dir = DIR_CENTER;
585 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
586 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
593 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
596 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
606 void bfs_wrapper(void)
608 GARY_INIT(bfs_queue, 0);
609 GARY_INIT(longlines, 0);
611 HASH_FOR_ALL(hash, node)
613 for (uns i=0; i<GARY_SIZE(node->edges); i++)
615 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
617 GARY_PUSH(longlines);
618 longlines[num_longlines].first = node->edges[i];
619 printf("Running new BFS\n");
620 printf("Creating longline %u\n", num_longlines);
621 GARY_RESIZE(bfs_queue, 0);
622 struct graph_edge **e = GARY_PUSH(bfs_queue);
624 node->edges[i]->longline = num_longlines;
625 bfs(node->edges[i]->longline);
627 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
628 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
635 GARY_FREE(bfs_queue);
638 void dump_longlines(void)
640 printf("*** Longlines dump\n");
641 for (uns i=0; i<GARY_SIZE(longlines); i++)
643 printf("Longline %u:", i);
644 struct graph_edge *e = longlines[i].first;
645 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
646 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
651 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
652 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
659 struct request_line *make_new_line(void)
661 struct request_line *rl = GARY_PUSH(requests_line);
662 rl->request.ind = num_requests++;
663 rl->request.type = REQUEST_LINE;
664 GARY_INIT(rl->sections, 0);
669 struct request_section *make_new_section(struct request_line *rl)
671 struct request_section *rls = GARY_PUSH(rl->sections);
672 rls->request.ind = num_requests++;
673 rls->request.type = REQUEST_SECTION;
674 rls->num_segments = 0;
675 GARY_INIT(rls->segments, 0);
680 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
682 struct request_segment *rs = GARY_PUSH(rls->segments);
685 rs->request.ind = num_requests++;
686 rs->request.type = REQUEST_SEGMENT;
688 struct point_variant *v = malloc(sizeof(struct point_variant));
695 void cut_edge(struct graph_edge *e, double dist)
698 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);
700 struct graph_edge *new = malloc(sizeof(struct graph_edge));
704 struct osm_node *n1 = e->n1->o;
705 struct osm_node *n2 = e->n2->o;
708 if ((n1->x == n2->x) && (n1->y == n2->y))
710 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
711 printf("Won't cut point\n");
715 struct osm_node *n11 = malloc(sizeof(struct osm_node));
716 struct graph_node *gn = malloc(sizeof(struct graph_node));
718 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
719 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
720 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
722 e->n2 = new->n1 = gn;
724 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
725 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
728 void make_segments(void)
730 for (uns i=0; i<GARY_SIZE(longlines); i++)
732 // Skip lines which are not labelled
733 if (! (longlines[i].first && longlines[i].first->label))
736 struct request_line *request = make_new_line();
737 struct request_section *rls = make_new_section(request);
738 struct request_segment *rs = NULL;
740 struct graph_edge *e = longlines[i].first;
741 double cur_length = 0;
743 struct sym_text *st = NULL;
744 if (e->label->type == SYMBOLIZER_TEXT)
746 st = (struct sym_text *) e->label;
750 printf("Warning: Skipping line\n");
755 printf("New longline\n");
758 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
761 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);
762 cut_edge(e, conf_max_section_length - cur_length);
765 if (cur_length + e->length > conf_max_section_length)
768 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);
770 struct osm_node *n1 = e->n1->o;
771 struct osm_node *n2 = e->n2->o;
772 rs = make_new_segment(rls, e->label);
777 rs->zindex = e->zindex;
779 rs->label = malloc(sizeof(struct sym_text));
780 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
781 rls = make_new_section(request);
785 if (st && (e->length < st->tw))
788 printf("Warning: Skipping segment\n");
792 rs = make_new_segment(rls, e->label);
793 rs->label = malloc(sizeof(struct sym_text));
794 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
796 rs->x1 = e->n1->o->x;
797 rs->y1 = e->n1->o->y;
798 rs->x2 = e->n2->o->x;
799 rs->y2 = e->n2->o->y;
801 // FIXME: Set text rotation
802 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
803 rs->zindex = e->zindex;
805 cur_length += e->length;
809 if (request->sections[0].num_segments == 0)
812 printf("WARNING: 0 segment section\n");
813 GARY_POP(requests_line);
819 void dump_linelabel_requests(void)
821 for (uns i=0; i<GARY_SIZE(requests_line); i++)
823 if (requests_line[i].sections[0].num_segments == 0)
828 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
829 print_label(requests_line[i].sections[0].segments[0].label);
830 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
832 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
833 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
835 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
836 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
843 void dump_bitmaps(struct individual *individual)
845 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
846 printf("Bitmap size is %d\n", page_width_int * page_height_int);
847 for (int i=0; i<page_height_int; i++)
848 for (int j=0; j<page_width_int; j++)
849 bitmap[i*page_width_int + j] = 0;
851 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
853 fprintf(stderr, "%d-th placement\n", i);
854 struct placement *p = &(individual->placements[i]);
855 struct point_variant *v = NULL;
857 switch (p->request->type)
859 case REQUEST_SEGMENT: ;
860 struct request_segment *rs = (struct request_segment *) p->request;
863 case REQUEST_POINT: ;
864 struct request_point *rp = (struct request_point *) p->request;
865 v = &(rp->variants[p->variant_used]);
868 struct request_area *ra = (struct request_area *) p->request;
869 printf("Using %d-th of %d variants\n", p->variant_used, GARY_SIZE(ra->variants));
870 v = &(ra->variants[p->variant_used]);
873 printf("Testing request type (dump_bitmaps): %d\n", p->request->type);
874 ASSERT(p->request->type != REQUEST_INVALID);
878 printf("Got after with %d-th placement of request type %d\n", i, p->request->type);
880 printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
881 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
883 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
885 printf("Writing to %d\n", row*page_width_int + col);
886 bitmap[row * page_width_int + col] = 1;
892 FILE *fd_dump = fopen("dump.pbm", "w");
893 fprintf(fd_dump, "P1\n");
894 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
895 for (int i=0; i<page_height_int; i++)
897 for (int j=0; j<page_width_int; j++)
899 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
901 fprintf(fd_dump, "\n");
906 void dump_individual(struct individual *individual)
908 printf("*** Dumping INDIVIDUAL ***\n");
909 printf("(There are %d requests)\n", num_requests);
910 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
912 struct placement *p = &(individual->placements[i]);
914 switch (p->request->type)
917 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
920 struct request_line *rl = (struct request_line *) p->request;
922 print_label(rl->sections[0].segments[0].label);
924 case REQUEST_SECTION: ;
927 case REQUEST_SEGMENT: ;
928 if (p->variant_used >= 0)
929 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
931 printf("Segment not placed\n");
934 struct request_area *ra = (struct request_area *) p->request;
935 printf("Area label ");
936 print_label(ra->label);
937 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
940 printf("Testing request type (dump_individual)\n");
941 ASSERT(p->request->type != 0);
944 printf("\nTotal penalty: %d\n", individual->penalty);
947 void plan_individual(struct individual *individual)
949 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
951 struct symbol *s = NULL;
952 z_index_t zindex = 0;
953 if (individual->placements[i].variant_used < 0) continue;
954 switch (individual->placements[i].request->type)
956 case REQUEST_POINT: ;
957 struct request_point *rp = (struct request_point *) individual->placements[i].request;
961 case REQUEST_SEGMENT: ;
962 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
969 struct request_area *ra = (struct request_area *) individual->placements[i].request;
974 ASSERT(individual->placements[i].request != REQUEST_INVALID);
979 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
981 if (s) switch (s->type)
983 case SYMBOLIZER_POINT: ;
984 struct sym_point *sp = (struct sym_point *) s;
985 sp->x = individual->placements[i].x;
986 sp->y = individual->placements[i].y;
987 sym_plan((struct symbol *) sp, zindex);
989 case SYMBOLIZER_ICON: ;
990 struct sym_icon *si = (struct sym_icon *) s;
991 si->sir.x = individual->placements[i].x;
992 si->sir.y = individual->placements[i].y;
993 sym_plan((struct symbol *) si, zindex);
995 case SYMBOLIZER_TEXT: ;
996 struct sym_text *st = (struct sym_text *) s;
997 st->x = individual->placements[i].x;
998 st->y = individual->placements[i].y;
999 st->next_duplicate = NULL;
1000 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);
1001 sym_plan((struct symbol *) st, zindex);
1004 ASSERT(s->type != SYMBOLIZER_INVALID);
1010 void labeller_label(void)
1018 dump_linelabel_requests();
1020 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));
1022 GARY_INIT(population1, conf_pop_size);
1023 GARY_INIT(population2, conf_pop_size);
1026 printf("Dealing with %d requests\n", num_requests);
1029 while (! shall_terminate())
1033 struct individual **swp = population1;
1034 population1 = population2;
1040 dump_individual(population1[0]);
1041 //dump_bitmaps(population1[0]);
1043 plan_individual(population1[0]);
1050 void labeller_cleanup(void)
1054 void make_population(void)
1056 for (int i=0; i<conf_pop_size; i++)
1058 printf("Making individual %d\n", i);
1059 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1060 population1[i] = individual;
1063 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1065 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1068 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1070 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1072 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1074 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1076 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1078 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1083 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1085 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1088 hide_segment_labels(individual);
1090 if (p != num_requests)
1092 printf("Say bye\n");
1096 printf("Testing p\n");
1097 ASSERT(p == num_requests);
1101 bool shall_terminate(void)
1103 switch (conf_term_cond)
1105 case TERM_COND_PENALTY:
1106 return (population1[0]->penalty < conf_penalty_bound);
1107 case TERM_COND_STAGNATION:
1108 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1109 case TERM_COND_ITERATIONS:
1110 return (iteration >= conf_iteration_limit);
1112 // FIXME: Warn the user that no condition is set
1121 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1122 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1123 struct individual **breed_buffer;
1124 while (i < conf_breed_pop_size)
1126 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1127 int parent1 = randint(1, conf_breed_pop_size);
1128 int parent2 = randint(1, conf_breed_pop_size);
1129 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);
1130 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1131 population2[pop2_ind++] = breed_buffer[0];
1132 population2[pop2_ind++] = breed_buffer[1];
1137 acc += conf_breed_rbest_perc;
1139 return; // FIXME: DEBUG HACK
1141 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1142 int step = remaining / conf_pop_size;
1143 for (; i<conf_pop_size; i += 2)
1145 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1146 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1147 population2[pop2_ind++] = breed_buffer[0];
1148 population2[pop2_ind++] = breed_buffer[1];
1151 // FIXME: Could there be one missing individual?
1154 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1156 struct individual **buffer = malloc(2*sizeof(struct individual));
1157 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1158 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1160 printf("Performing crossover\n");
1162 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1164 printf("%dth placement out of %d\n", i, num_requests);
1165 if (! parent1->placements[i].processed)
1167 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1168 int x = randint(1, 2);
1172 copy_symbols(clos_symbols, parent1, child1);
1173 copy_symbols(clos_symbols, parent2, child2);
1177 copy_symbols(clos_symbols, parent2, child1);
1178 copy_symbols(clos_symbols, parent1, child2);
1180 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1181 GARY_FREE(clos_symbols);
1184 if (conf_mutate_children)
1186 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1187 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1199 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1200 while (i < conf_mutate_rbest_perc * conf_pop_size)
1202 int ind = randint(1, conf_mutate_pop_size);
1203 copy_individual(population2[pop2_ind], population1[ind]);
1204 perform_mutation(population2[pop2_ind]);
1209 void perform_mutation(struct individual *individual)
1211 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1213 int x = randint(1, 1000);
1216 if (x <= acc + conf_mutate_move_bound)
1218 move_symbol(&(individual->placements[i]));
1221 acc += conf_mutate_move_bound;
1223 if (x <= acc + conf_mutate_regen_bound)
1225 gen_coords(&(individual->placements[i]));
1228 acc += conf_mutate_regen_bound;
1230 if (x <= acc + conf_mutate_chvar_bound)
1232 if (0) // if num_variants > 1
1234 // FIXME: assign new variant
1242 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1244 population2[pop2_ind++] = population1[0];
1248 void rank_population(void)
1253 struct map_part **get_map_parts(struct placement *p)
1255 if (p->variant_used < 0) return NULL;
1257 struct map_part **buffer;
1258 GARY_INIT(buffer, 0);
1261 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1263 struct point_variant v;
1264 switch (p->request->type)
1267 v = ((struct request_point *) p->request)->variants[p->variant_used];
1269 case REQUEST_SEGMENT:
1270 v = ((struct request_segment *) p->request)->variant[p->variant_used];
1273 v = ((struct request_area *) p->request)->variants[p->variant_used];
1279 int x_min = max2(0, p->x) / conf_map_part_width;
1280 int x_max = min2(page_width_int, (p->x + v.width + conf_map_part_width - 1)) / conf_map_part_width;
1281 int y_min = max2(0, p->y) / conf_map_part_height;
1282 int y_max = min2(page_height_int, (p->y + v.height + conf_map_part_height - 1)) / conf_map_part_height;
1285 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1287 for (int y=y_min; y<=y_max; y++)
1288 for (int x=x_min; x<=x_max; x++)
1290 struct map_part **m = GARY_PUSH(buffer);
1292 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1293 *m = p->individual->map[y * num_map_parts_row + x];
1299 void update_map_parts(struct placement *p)
1301 struct placement_link *ml = p->map_links;
1304 struct map_placement *mp = ml->mp;
1305 mp->prev = mp->next;
1307 mp->next->prev = mp->prev;
1310 struct placement_link *tmp = ml;
1315 struct map_part **parts = get_map_parts(p);
1316 if (parts == NULL) return;
1318 for (uns i=0; i<GARY_SIZE(parts); i++)
1320 struct map_placement *mp = malloc(sizeof(struct map_placement));
1323 mp->next = parts[i]->placement->next;
1324 parts[i]->placement->next = mp;
1325 if (mp->next) mp->next->prev = mp;
1327 struct placement_link *ml = malloc(sizeof(struct placement_link));
1329 ml->next = p->map_links;
1336 void gen_coords(struct placement *p)
1338 switch(p->request->type)
1341 gen_coords_point(p);
1346 case REQUEST_SEGMENT:
1347 gen_coords_segment(p);
1350 printf("Not yet implemented\n");
1353 printf("Testing request type\n");
1354 ASSERT(p->request->type != REQUEST_INVALID);
1357 update_map_parts(p);
1360 double gen_movement(void)
1362 double m = (random() % 1000000) / 10000;
1363 m = pow(m, 1.0/3) * flip(1, -1);
1364 printf("Movement %.2f\n", m);
1368 void gen_coords_point(struct placement *p)
1370 p->x = p->x + gen_movement();
1373 void gen_coords_segment(struct placement *p)
1375 struct request_segment *rs = (struct request_segment *) p->request;
1377 p->x = (a == 1 ? rs->x1 : rs->x2);
1378 p->y = (a == 1 ? rs->y1 : rs->y2);
1381 void gen_coords_area(struct placement *p)
1383 struct request_area *ra = (struct request_area *) p->request;
1385 p->x = p->x + gen_movement();
1386 p->y = p->y + gen_movement();
1388 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1391 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1393 struct map_part **buffer;
1394 GARY_INIT(buffer, 0);
1395 int x_min = symbol->x / conf_part_size;
1396 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1397 int y_min = symbol->y / conf_part_size;
1398 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1400 for (int x=x_min; x < x_max; x++)
1401 for (int y=y_min; y < y_max; y++)
1403 struct map_part *m = GARY_PUSH(buffer);
1404 *m = individual->map[x][y];
1410 int randint(int min, int max)
1412 if (min == max) return min;
1414 //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)));
1415 return min + (r % (max - min));
1416 return (r * (max - min));
1419 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1421 printf("Getting closure\n");
1422 struct placement **closure;
1423 GARY_INIT(closure, 0);
1424 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1425 chosen[placement->request->ind] = 1;
1427 struct placement **p = GARY_PUSH(closure); *p = placement;
1430 while (first < GARY_SIZE(closure))
1432 printf("Iterating, first is %d\n", first);
1433 struct placement **overlapping = get_overlapping(placement);
1434 filter(overlapping, chosen);
1435 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1437 p = GARY_PUSH(closure); *p = overlapping[j];
1438 chosen[overlapping[j]->request->ind] = 1;
1440 GARY_FREE(overlapping);
1447 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1449 //printf("%d\n", child->penalty);
1450 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1451 for (uns i=0; i<GARY_SIZE(closure); i++)
1453 int ind = closure[i]->request->ind;
1454 child->placements[ind] = parent->placements[ind];
1455 child->placements[ind].processed = 0;
1459 void move_symbol(struct placement *p)
1461 switch (p->request->type)
1464 move_symbol_point(p);
1466 case REQUEST_SEGMENT:
1468 printf("Not yet implemented\n");
1470 ASSERT(p->request->type != REQUEST_INVALID);
1474 void move_symbol_point(struct placement *p)
1476 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1477 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1480 void hide_segment_labels(struct individual *individual)
1482 // BEWARE: This fully depends on current genetic encoding
1484 int used = -1, num = -1;
1485 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1487 switch (individual->placements[i].request->type)
1489 case REQUEST_SECTION:
1490 used = individual->placements[i].variant_used;
1493 case REQUEST_SEGMENT:
1495 individual->placements[i].variant_used = 0;
1497 individual->placements[i].variant_used = -1;
1506 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1511 p->x = p->y = 0; // To prevent valgrind from complaining
1512 p->variant_used = 0;
1513 p->map_links = NULL;
1514 p->individual = individual;
1517 case REQUEST_POINT: ;
1518 struct request_point *rp = (struct request_point *) r;
1522 case REQUEST_LINE: ;
1524 case REQUEST_SECTION: ;
1525 struct request_section *rls = (struct request_section *) r;
1526 p->variant_used = randint(0, rls->num_segments);
1528 case REQUEST_SEGMENT: ;
1529 struct request_segment *rs = (struct request_segment *) r;
1533 case REQUEST_AREA: ;
1534 struct request_area *ra = (struct request_area *) r;
1537 p->variant_used = 0;
1540 ASSERT(p->request->type != REQUEST_INVALID);
1541 printf("Valid request: %d\n", p->request->type);
1545 // printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1548 void init_individual(struct individual *i)
1550 //printf("Initing individual\n");
1551 GARY_INIT(i->placements, num_requests);
1552 GARY_INIT(i->map, 0);
1553 for (uns j=0; j<num_map_parts; j++)
1555 struct map_part *part = GARY_PUSH(i->map);
1556 GARY_INIT(part->placement, 0);
1557 struct map_placement *mp = GARY_PUSH(part->placement);
1558 mp->placement = &dummy_placement;
1559 mp->next = mp->prev = NULL;
1561 i->penalty = 0; // FIXME
1564 struct placement **get_overlapping(struct placement *p UNUSED)
1566 struct placement **buffer;
1567 GARY_INIT(buffer, 0);
1571 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1576 int flip(int a, int b)
1578 return (random() % 2 ? a : b);
1581 double randdouble(void)
1583 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1590 GARY_FREE(requests_point);
1591 GARY_FREE(requests_line);
1592 GARY_FREE(requests_area);
1595 void copy_individual(struct individual *src, struct individual *dest)
1597 src->penalty = dest->penalty;
1598 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1599 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1601 dest->placements[i] = src->placements[i];