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 int cmp_individual(const void *a, const void *b);
127 void make_bitmap(struct variant *v, struct symbol *sym);
128 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
129 void make_bitmap_point(struct variant *v, struct sym_point *sp);
130 void make_bitmap_label(struct variant *v, struct sym_text *text);
132 void cut_edge(struct graph_edge *e, double dist);
133 struct request_line *make_new_line(void);
134 struct request_section *make_new_section(struct request_line *rl);
135 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
137 void dump_bitmaps(struct individual *individual);
138 void dump_graph(void);
140 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
141 void bfs_wrapper(void);
143 void dump_longlines(void);
144 void dump_linelabel_requests(void);
145 void dump_individual(struct individual *individual);
146 void print_label(struct symbol *sym);
148 double gen_movement(void);
149 double gen_movement_uniform(void);
150 void gen_coords(struct placement *p);
151 void gen_coords_point(struct placement *p);
152 void gen_coords_segment(struct placement *p);
153 void gen_coords_area(struct placement *p);
155 struct map_part **get_map_parts(struct placement *p);
156 void update_map_parts(struct placement *p);
158 void make_segments_old(void);
160 void labeller_cleanup(void);
162 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
163 void perform_mutation(struct individual *individual);
165 void hide_segment_labels(struct individual *individual);
166 void init_placement(struct placement *p, struct individual *individual, struct request *r);
167 void init_individual(struct individual *i);
168 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
170 int randint(int min, int max);
172 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2);
173 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
174 void move_symbol(struct placement *p);
175 void move_symbol_point(struct placement *p);
176 void move_symbol_segment(struct placement *p);
178 struct placement **get_overlapping(struct placement *p);
179 void filter(struct placement **list, bool *pred);
181 int flip(int a, int b);
182 double randdouble(void);
186 void copy_individual(struct individual *src, struct individual *dest);
188 int max2(int a, int b);
189 int min2(int a, int b);
190 int max4(int a, int b, int c, int d);
191 int min4(int a, int b, int c, int d);
193 struct placement dummy_placement;
195 int max2(int a, int b)
197 return (a > b ? a : b);
200 int min2(int a, int b)
202 return (a < b ? a : b);
205 int max4(int a, int b, int c, int d)
207 return max2(max2(a, b), max2(c, d));
210 int min4(int a, int b, int c, int d)
212 return min2(min2(a, b), min2(c, d));
215 void print_label(struct symbol *sym)
219 case SYMBOLIZER_TEXT: ;
220 struct sym_text *st = (struct sym_text *) sym;
221 printf("%s\n", osm_val_decode(st->text));
228 void labeller_init(void)
230 GARY_INIT(requests_point, 0);
231 GARY_INIT(requests_line, 0);
232 GARY_INIT(requests_area, 0);
233 GARY_INIT(buffer_line, 0);
234 GARY_INIT(buffer_linelabel, 0);
235 ep_individuals = ep_new(sizeof(struct individual), 1);
237 page_width_int = floor(page_width);
238 page_height_int = floor(page_height);
240 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
241 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
242 num_map_parts = num_map_parts_row * num_map_parts_col;
245 void make_bitmap(struct variant *v, struct symbol *sym)
249 case SYMBOLIZER_POINT:
250 make_bitmap_point(v, (struct sym_point *) sym);
252 case SYMBOLIZER_ICON:
253 make_bitmap_icon(v, (struct sym_icon *) sym);
255 case SYMBOLIZER_TEXT:
256 make_bitmap_label(v, (struct sym_text *) sym);
259 ASSERT(sym->type != SYMBOLIZER_INVALID);
263 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
265 v->width = si->sir.width + 1;
266 v->height = si->sir.height + 1;
267 v->bitmap = malloc(v->width * v->height * sizeof(bool));
268 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
271 void make_bitmap_point(struct variant *v, struct sym_point *sp)
273 v->width = v->height = sp->size + 1;
274 v->bitmap = malloc(v->width * v->height * sizeof(bool));
275 // FIXME: Okay, memset would be much nicer here
276 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
279 void make_bitmap_label(struct variant *v, struct sym_text *text)
281 v->width = ceil(text->tw);
282 v->height = ceil(text->th);
283 v->bitmap = malloc(v->width * v->height * sizeof(bool));
284 for (int i=0; i<v->height; i++)
285 for (int j=0; j<v->width; j++)
287 v->bitmap[i*v->width + j] = 1;
291 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
294 printf("Adding point\n");
295 if (object->type != OSM_TYPE_NODE)
297 printf("Warning: Point label requested on non-point object\n");
301 struct request_point *r = GARY_PUSH(requests_point);
303 r->request.type = REQUEST_POINT;
304 r->request.ind = num_requests++;
313 GARY_INIT(r->request.variants, 0);
315 struct variant *v = GARY_PUSH(r->request.variants);
317 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
322 case SYMBOLIZER_ICON:
323 make_bitmap_icon(v, (struct sym_icon *) sym);
324 r->x = ((struct sym_icon *)sym)->sir.x;
325 r->y = ((struct sym_icon *)sym)->sir.y;
327 case SYMBOLIZER_POINT:
328 make_bitmap_point(v, (struct sym_point *) sym);
330 case SYMBOLIZER_TEXT: ;
331 struct sym_text *st = (struct sym_text *) sym;
332 struct osm_node *n = (struct osm_node *) object;
333 make_bitmap_label(v, st);
339 // printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
342 void labeller_add_line(struct symbol *sym, z_index_t zindex)
345 printf("Adding line on %u\n", zindex);
346 struct buffer_line *b = GARY_PUSH(buffer_line);
347 b->line = (struct sym_line *) sym;
349 sym_plan(sym, zindex);
352 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
354 if (o->type != OSM_TYPE_WAY)
361 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
362 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
363 ll->way = (struct osm_way *) o;
368 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
371 printf("Adding area on %u\n", zindex);
372 struct request_area *r = GARY_PUSH(requests_area);
374 r->request.type = REQUEST_AREA;
375 r->request.ind = num_requests++;
377 r->o = (struct osm_multipolygon *) o;
381 osm_obj_center(o, &(r->cx), &(r->cy));
383 GARY_INIT(r->request.variants, 0);
384 struct variant *v = GARY_PUSH(r->request.variants);
387 case SYMBOLIZER_ICON:
389 printf("DEBUG: Icon label\n");
390 make_bitmap_icon(v, (struct sym_icon *) sym);
392 case SYMBOLIZER_TEXT:
394 printf("DEBUG: Text label\n");
395 make_bitmap_label(v, (struct sym_text *) sym);
402 void make_graph(void)
405 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
407 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
408 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
410 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
411 struct graph_node *g_prev = NULL;
412 struct osm_node *o_prev = NULL;
414 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
416 // FIXME: Shall osm_object's type be checked here?
417 struct osm_node *o_node = (struct osm_node *) ref->o;
419 struct graph_node *g_node = hash_find(ref->o->id);
422 g_node = hash_new(ref->o->id);
423 GARY_INIT(g_node->edges, 0);
425 g_node->id = ref->o->id;
426 g_node->num = num_nodes++;
436 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
437 e->num = num_edges++;
438 e->id = buffer_line[i].line->s.o->id;
439 e->color = buffer_line[i].line->color;
440 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
446 e->longline = (uns) -1;
447 e->line = buffer_line[i].line;
451 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
453 edge = GARY_PUSH(g_node->edges);
461 printf("Made graph with %d edges\n", num_edges_dbg);
464 void dump_graph(void)
466 HASH_FOR_ALL(hash, node)
468 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
469 for (uns i=0; i<GARY_SIZE(node->edges); i++)
471 struct graph_edge *e = node->edges[i];
472 printf("\t edge (%d) #%ju to ", e->num, e->id);
473 if (node->edges[i]->n1->id == node->id)
474 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
475 else if (node->edges[i]->n2->id == node->id)
476 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
478 printf("BEWARE! BEWARE! BEWARE!\n");
481 if ((node->edges[i]->label)) printf("Labelled\n");
482 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));
483 printf(" colored %d;", node->edges[i]->color);
484 printf(" length %.2f", node->edges[i]->length);
491 void label_graph(void)
494 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
495 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
497 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
499 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
500 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
503 printf("Looking for node %ju\n", ref->o->id);
504 struct graph_node *n = hash_find(ref->o->id);
507 // FIXME: What shall be done?
512 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
513 for (uns j=0; j<GARY_SIZE(n->edges); j++)
515 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
518 printf("Labelling node %ju\n", n->id);
519 n->edges[j]->label = buffer_linelabel[i].label;
520 n->edges[j]->zindex = buffer_linelabel[i].zindex;
528 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
531 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
532 struct graph_edge *candidate = NULL;
534 for (uns i=0; i<GARY_SIZE(node->edges); i++)
536 struct graph_edge *other = node->edges[i];
537 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
539 if ((uns) other->visited != e->longline) {
541 printf("Pushing new edge %d / %ju\n", other->num, other->id);
542 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
544 other->visited = e->longline;
547 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
548 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
551 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
552 (e->label) && (other->label) &&
553 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
554 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
556 if (! candidate || (other->length > candidate->length))
564 printf("New line in longline %u\n", e->longline);
565 struct graph_edge *other = candidate;
566 other->longline = e->longline;
568 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
569 ((dir == DIR_FWD) && (other->n2->id == node->id)))
571 struct graph_node *swp = other->n2;
572 other->n2 = other->n1;
581 longlines[other->longline].first = other;
594 void bfs(uns longline)
597 printf("BFS called for longline %u\n", longline);
599 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
600 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
602 struct graph_edge *cur = bfs_queue[i];
604 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
605 //ASSERT(! cur->visited);
607 cur->visited = longline;
609 if (cur->longline == (uns) -1)
612 if (cur->dir == DIR_UNSET)
614 cur->dir = DIR_CENTER;
615 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
616 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
623 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
626 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
636 void bfs_wrapper(void)
638 GARY_INIT(bfs_queue, 0);
639 GARY_INIT(longlines, 0);
641 HASH_FOR_ALL(hash, node)
643 for (uns i=0; i<GARY_SIZE(node->edges); i++)
645 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
647 GARY_PUSH(longlines);
648 longlines[num_longlines].first = node->edges[i];
650 printf("Running new BFS\n");
652 printf("Creating longline %u\n", num_longlines);
653 GARY_RESIZE(bfs_queue, 0);
654 struct graph_edge **e = GARY_PUSH(bfs_queue);
656 node->edges[i]->longline = num_longlines;
657 bfs(node->edges[i]->longline);
660 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
662 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
669 GARY_FREE(bfs_queue);
672 void dump_longlines(void)
674 printf("*** Longlines dump\n");
675 for (uns i=0; i<GARY_SIZE(longlines); i++)
677 printf("Longline %u:", i);
678 struct graph_edge *e = longlines[i].first;
679 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
680 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
685 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
686 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
693 struct request_line *make_new_line(void)
695 struct request_line *rl = GARY_PUSH(requests_line);
696 rl->request.ind = num_requests++;
697 rl->request.type = REQUEST_LINE;
698 GARY_INIT(rl->sections, 0);
703 struct request_section *make_new_section(struct request_line *rl)
705 struct request_section *rls = GARY_PUSH(rl->sections);
706 rls->request.ind = num_requests++;
707 rls->request.type = REQUEST_SECTION;
708 rls->num_segments = 0;
709 GARY_INIT(rls->segments, 0);
714 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
716 struct request_segment *rs = GARY_PUSH(rls->segments);
719 rs->request.ind = num_requests++;
720 rs->request.type = REQUEST_SEGMENT;
722 struct variant *v = malloc(sizeof(struct variant));
724 rs->request.variants = v;
729 void cut_edge(struct graph_edge *e, double dist)
732 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);
734 struct graph_edge *new = malloc(sizeof(struct graph_edge));
738 // FIXME? Create new label for new edge, don't only copy pointer?
740 struct osm_node *n1 = e->n1->o;
741 struct osm_node *n2 = e->n2->o;
744 if ((n1->x == n2->x) && (n1->y == n2->y))
746 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
747 printf("Won't cut point\n");
751 struct osm_node *n11 = malloc(sizeof(struct osm_node));
752 struct graph_node *gn = malloc(sizeof(struct graph_node));
754 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
755 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
756 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
758 e->n2 = new->n1 = gn;
760 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
761 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
765 void make_segments(void)
767 for (uns i=0; i<GARY_SIZE(longlines); i++)
769 // Skip lines which are not labelled
770 if (! (longlines[i].first && longlines[i].first->label))
773 struct request_line *request = make_new_line();
774 struct request_section *rls = make_new_section(request);
775 struct request_segment *rs = NULL;
777 struct graph_edge *e = longlines[i].first;
778 double cur_length = 0;
780 struct sym_text *st = NULL;
781 if (e->label->type == SYMBOLIZER_TEXT)
783 st = (struct sym_text *) e->label;
788 printf("Warning: Skipping line\n");
794 printf("New longline\n");
799 printf("BEWARE: Edge cycle\n");
802 e->visited = -1; // FIXME
805 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);
807 if (st && (e->length < st->tw))
810 //printf("Warning: Skipping segment\n");
814 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
817 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);
818 // HACK to prevent cutting to 0 lenght
819 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
822 rs = make_new_segment(rls, e->label);
823 rs->label = malloc(sizeof(struct sym_text));
824 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
826 rs->x1 = e->n1->o->x;
827 rs->y1 = e->n1->o->y;
828 rs->x2 = e->n2->o->x;
829 rs->y2 = e->n2->o->y;
831 // FIXME: Set text rotation
832 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
833 rs->zindex = e->zindex;
835 cur_length += e->length;
836 if (cur_length > conf_max_section_length)
839 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);
841 rls = make_new_section(request);
848 if (request->sections[0].num_segments == 0)
851 printf("WARNING: 0 segment section\n");
852 GARY_POP(requests_line);
858 void dump_linelabel_requests(void)
860 for (uns i=0; i<GARY_SIZE(requests_line); i++)
862 if (requests_line[i].sections[0].num_segments == 0)
867 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
868 print_label(requests_line[i].sections[0].segments[0].label);
869 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
871 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
872 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
874 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
875 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
882 void dump_bitmaps(struct individual *individual)
884 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
885 printf("Bitmap size is %d\n", page_width_int * page_height_int);
886 for (int i=0; i<page_height_int; i++)
887 for (int j=0; j<page_width_int; j++)
888 bitmap[i*page_width_int + j] = 0;
890 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
892 if (individual->placements[i].variant_used == -1) continue;
894 struct placement *p = &(individual->placements[i]);
895 struct variant *v = NULL;
897 switch (p->request->type)
899 case REQUEST_SEGMENT: ;
900 case REQUEST_POINT: ;
902 v = &(p->request->variants[p->variant_used]);
905 ASSERT(p->request->type != REQUEST_INVALID);
909 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
911 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
913 bitmap[row * page_width_int + col] = 1;
919 FILE *fd_dump = fopen("dump.pbm", "w");
920 fprintf(fd_dump, "P1\n");
921 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
922 for (int i=0; i<page_height_int; i++)
924 for (int j=0; j<page_width_int; j++)
926 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
928 fprintf(fd_dump, "\n");
933 void dump_individual(struct individual *individual)
935 printf("*** Dumping INDIVIDUAL ***\n");
936 printf("(There are %d requests)\n", num_requests);
937 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
939 struct placement *p = &(individual->placements[i]);
941 switch (p->request->type)
944 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
947 struct request_line *rl = (struct request_line *) p->request;
949 print_label(rl->sections[0].segments[0].label);
951 case REQUEST_SECTION: ;
954 case REQUEST_SEGMENT: ;
955 if (p->variant_used >= 0)
956 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
958 printf("Segment not placed\n");
961 struct request_area *ra = (struct request_area *) p->request;
962 printf("Area label ");
963 print_label(ra->label);
964 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
967 printf("Testing request type (dump_individual)\n");
968 ASSERT(p->request->type != 0);
971 printf("\nTotal penalty: %d\n", individual->penalty);
974 void plan_individual(struct individual *individual)
976 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
978 struct symbol *s = NULL;
979 z_index_t zindex = 0;
980 if (individual->placements[i].variant_used < 0) continue;
981 switch (individual->placements[i].request->type)
983 case REQUEST_POINT: ;
984 struct request_point *rp = (struct request_point *) individual->placements[i].request;
988 case REQUEST_SEGMENT: ;
989 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
996 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1001 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1006 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1008 if (s) switch (s->type)
1010 case SYMBOLIZER_POINT: ;
1011 struct sym_point *sp = (struct sym_point *) s;
1012 sp->x = individual->placements[i].x;
1013 sp->y = individual->placements[i].y;
1014 sym_plan((struct symbol *) sp, zindex);
1016 case SYMBOLIZER_ICON: ;
1017 struct sym_icon *si = (struct sym_icon *) s;
1018 si->sir.x = individual->placements[i].x;
1019 si->sir.y = individual->placements[i].y;
1020 sym_plan((struct symbol *) si, zindex);
1022 case SYMBOLIZER_TEXT: ;
1023 struct sym_text *st = (struct sym_text *) s;
1024 st->x = individual->placements[i].x;
1025 st->y = individual->placements[i].y;
1026 st->next_duplicate = NULL;
1027 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);
1028 sym_plan((struct symbol *) st, zindex);
1031 ASSERT(s->type != SYMBOLIZER_INVALID);
1037 void labeller_label(void)
1044 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));
1046 GARY_INIT(population1, conf_pop_size);
1047 GARY_INIT(population2, conf_pop_size);
1050 printf("Dealing with %d requests\n", num_requests);
1053 while (! shall_terminate())
1057 struct individual **swp = population1;
1058 population1 = population2;
1064 plan_individual(population1[0]);
1071 void labeller_cleanup(void)
1075 void make_population(void)
1077 for (int i=0; i<conf_pop_size; i++)
1079 struct individual *i2 = ep_alloc(ep_individuals);
1080 init_individual(i2);
1081 population2[i] = i2;
1084 printf("Making individual %d\n", i);
1085 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1086 population1[i] = individual;
1089 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1091 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1094 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1096 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1098 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1100 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1102 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1104 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1109 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1111 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1114 hide_segment_labels(individual);
1116 ASSERT(p == num_requests);
1120 bool shall_terminate(void)
1122 switch (conf_term_cond)
1124 case TERM_COND_PENALTY:
1125 return (population1[0]->penalty < conf_penalty_bound);
1126 case TERM_COND_STAGNATION:
1127 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1128 case TERM_COND_ITERATIONS:
1129 return (iteration >= conf_iteration_limit);
1131 // FIXME: Warn the user that no condition is set
1140 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1141 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1142 struct individual **breed_buffer;
1143 while (i < conf_breed_pop_size)
1145 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1146 int parent1 = randint(1, conf_breed_pop_size);
1147 int parent2 = randint(1, conf_breed_pop_size);
1148 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);
1149 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1150 population2[pop2_ind++] = breed_buffer[0];
1151 population2[pop2_ind++] = breed_buffer[1];
1156 acc += conf_breed_rbest_perc;
1158 return; // FIXME: DEBUG HACK
1160 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1161 int step = remaining / conf_pop_size;
1162 for (; i<conf_pop_size; i += 2)
1164 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1165 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1166 population2[pop2_ind++] = breed_buffer[0];
1167 population2[pop2_ind++] = breed_buffer[1];
1170 // FIXME: Could there be one missing individual?
1173 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1175 struct individual **buffer = malloc(2*sizeof(struct individual));
1176 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1177 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1179 printf("Performing crossover\n");
1181 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1183 printf("%dth placement out of %d\n", i, num_requests);
1184 if (! parent1->placements[i].processed)
1186 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1187 int x = randint(1, 2);
1191 copy_symbols(clos_symbols, parent1, child1);
1192 copy_symbols(clos_symbols, parent2, child2);
1196 copy_symbols(clos_symbols, parent2, child1);
1197 copy_symbols(clos_symbols, parent1, child2);
1199 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1200 GARY_FREE(clos_symbols);
1203 if (conf_mutate_children)
1205 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1206 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1218 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1219 while (i < conf_mutate_rbest_perc * conf_pop_size)
1221 int ind = randint(1, conf_mutate_pop_size);
1222 copy_individual(population2[pop2_ind], population1[ind]);
1223 perform_mutation(population2[pop2_ind]);
1228 void perform_mutation(struct individual *individual)
1230 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1232 int x = randint(1, 1000);
1235 if (x <= acc + conf_mutate_move_bound)
1237 move_symbol(&(individual->placements[i]));
1240 acc += conf_mutate_move_bound;
1242 if (x <= acc + conf_mutate_regen_bound)
1244 gen_coords(&(individual->placements[i]));
1247 acc += conf_mutate_regen_bound;
1249 if (x <= acc + conf_mutate_chvar_bound)
1251 if (0) // if num_variants > 1
1253 // FIXME: assign new variant
1261 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1263 population2[pop2_ind++] = population1[0];
1267 int overlaps(struct placement *p1, struct placement *p2)
1269 if (p1->request->type != REQUEST_POINT &&
1270 p1->request->type != REQUEST_SEGMENT &&
1271 p1->request->type != REQUEST_AREA)
1274 if (p2->request->type != REQUEST_POINT &&
1275 p2->request->type != REQUEST_SEGMENT &&
1276 p2->request->type != REQUEST_AREA)
1279 if (p1->variant_used == -1 || p2->variant_used == -1)
1282 struct variant *v1, *v2;
1284 v1 = &(p1->request->variants[p1->variant_used]);
1285 v2 = &(p2->request->variants[p2->variant_used]);
1287 int p1x = p1->x; int p1y = p1->y;
1288 int p2x = p2->x; int p2y = p2->y;
1291 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1292 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1294 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1295 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1302 int get_overlap(struct placement *p)
1304 struct map_part **parts = get_map_parts(p);
1308 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1312 struct placement **others;
1315 GARY_INIT_ZERO(planned, num_requests);
1316 planned[p->request->ind] = 1;
1317 GARY_INIT(others, 0);
1319 for (uns i=0; i<GARY_SIZE(parts); i++)
1321 struct map_placement *mp = parts[i]->placement->next;
1324 if (! planned[mp->placement->request->ind])
1326 struct placement **p = GARY_PUSH(others);
1328 planned[mp->placement->request->ind] = true;
1335 for (uns i=0; i<GARY_SIZE(others); i++)
1337 overlap += overlaps(p, others[i]);
1347 int individual_overlap(struct individual *individual)
1351 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1353 overlap += get_overlap(&individual->placements[i]);
1359 int cmp_individual(const void *a, const void *b)
1361 struct individual **ia = (struct individual **) a;
1362 struct individual **ib = (struct individual **) b;
1364 return (*ia)->penalty - (*ib)->penalty;
1367 void rank_population(void)
1371 for (int i=0; i<conf_pop_size; i++)
1374 printf("Individual %d\n", i);
1375 population1[i]->penalty = 0;
1377 penalty = individual_overlap(population1[i]);
1379 printf("Increasing penalty by %d for overlap\n", penalty);
1380 population1[i]->penalty += penalty;
1384 struct map_part **get_map_parts(struct placement *p)
1386 if (p->variant_used < 0) return NULL;
1388 struct map_part **buffer;
1389 GARY_INIT(buffer, 0);
1392 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1395 switch (p->request->type)
1398 case REQUEST_SEGMENT:
1400 v = p->request->variants[p->variant_used];
1406 int x_min = max2(0, p->x) / conf_map_part_width;
1407 int x_max = min2(page_width_int, (p->x + v.width + conf_map_part_width - 1)) / conf_map_part_width;
1408 int y_min = max2(0, p->y) / conf_map_part_height;
1409 int y_max = min2(page_height_int, (p->y + v.height + conf_map_part_height - 1)) / conf_map_part_height;
1412 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1414 for (int y=y_min; y<=y_max; y++)
1415 for (int x=x_min; x<=x_max; x++)
1417 struct map_part **m = GARY_PUSH(buffer);
1419 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1420 *m = p->individual->map[y * num_map_parts_row + x];
1426 void update_map_parts(struct placement *p)
1428 struct placement_link *ml = p->map_links;
1431 struct map_placement *mp = ml->mp;
1432 mp->prev = mp->next;
1434 mp->next->prev = mp->prev;
1437 struct placement_link *tmp = ml;
1442 struct map_part **parts = get_map_parts(p);
1443 if (parts == NULL) return;
1445 for (uns i=0; i<GARY_SIZE(parts); i++)
1447 struct map_placement *mp = malloc(sizeof(struct map_placement));
1450 mp->next = parts[i]->placement->next;
1451 parts[i]->placement->next = mp;
1452 if (mp->next) mp->next->prev = mp;
1454 struct placement_link *ml = malloc(sizeof(struct placement_link));
1456 ml->next = p->map_links;
1463 void gen_coords(struct placement *p)
1465 switch(p->request->type)
1468 gen_coords_point(p);
1473 case REQUEST_SEGMENT:
1474 gen_coords_segment(p);
1478 printf("Not yet implemented\n");
1482 printf("Testing request type\n");
1483 ASSERT(p->request->type != REQUEST_INVALID);
1486 update_map_parts(p);
1489 double gen_movement(void)
1491 double m = (random() % 1000000) / 10000;
1492 m = pow(m, 1.0/3) * flip(1, -1);
1494 printf("Movement %.2f\n", m);
1498 double gen_movement_uniform(void)
1500 return (move_max - move_min) * randdouble() * flip(1, -1);
1503 void gen_coords_point(struct placement *p)
1505 p->x = p->x + gen_movement();
1508 void gen_coords_segment(struct placement *p)
1510 struct request_segment *rs = (struct request_segment *) p->request;
1512 p->x = (a == 1 ? rs->x1 : rs->x2);
1513 p->y = (a == 1 ? rs->y1 : rs->y2);
1516 void gen_coords_area(struct placement *p)
1518 struct request_area *ra = (struct request_area *) p->request;
1520 p->x = p->x + gen_movement();
1521 p->y = p->y + gen_movement();
1524 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1527 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1529 struct map_part **buffer;
1530 GARY_INIT(buffer, 0);
1531 int x_min = symbol->x / conf_part_size;
1532 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1533 int y_min = symbol->y / conf_part_size;
1534 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1536 for (int x=x_min; x < x_max; x++)
1537 for (int y=y_min; y < y_max; y++)
1539 struct map_part *m = GARY_PUSH(buffer);
1540 *m = individual->map[x][y];
1546 int randint(int min, int max)
1548 if (min == max) return min;
1550 //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)));
1551 return min + (r % (max - min));
1552 return (r * (max - min));
1555 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1557 printf("Getting closure\n");
1558 struct placement **closure;
1559 GARY_INIT(closure, 0);
1560 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1561 chosen[placement->request->ind] = 1;
1563 struct placement **p = GARY_PUSH(closure); *p = placement;
1566 while (first < GARY_SIZE(closure))
1568 printf("Iterating, first is %d\n", first);
1569 struct placement **overlapping = get_overlapping(placement);
1570 filter(overlapping, chosen);
1571 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1573 p = GARY_PUSH(closure); *p = overlapping[j];
1574 chosen[overlapping[j]->request->ind] = 1;
1576 GARY_FREE(overlapping);
1583 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1585 //printf("%d\n", child->penalty);
1586 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1587 for (uns i=0; i<GARY_SIZE(closure); i++)
1589 int ind = closure[i]->request->ind;
1590 child->placements[ind] = parent->placements[ind];
1591 child->placements[ind].processed = 0;
1595 void move_symbol(struct placement *p)
1597 switch (p->request->type)
1601 move_symbol_point(p);
1603 case REQUEST_SEGMENT:
1604 move_symbol_segment(p);
1607 ASSERT(p->request->type != REQUEST_INVALID);
1611 void move_symbol_point(struct placement *p)
1613 p->x += gen_movement_uniform();
1614 p->y += gen_movement_uniform();
1617 void move_symbol_segment(struct placement *p)
1619 double m = gen_movement_uniform();
1622 p->y += m * ((struct request_segment *) p->request)->slope;
1625 void hide_segment_labels(struct individual *individual)
1627 // BEWARE: This fully depends on current genetic encoding
1629 int used = -1, num = -1;
1630 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1632 switch (individual->placements[i].request->type)
1634 case REQUEST_SECTION:
1635 used = individual->placements[i].variant_used;
1638 case REQUEST_SEGMENT:
1640 individual->placements[i].variant_used = 0;
1642 individual->placements[i].variant_used = -1;
1651 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1656 p->x = p->y = 0; // To prevent valgrind from complaining
1657 p->variant_used = 0;
1658 p->map_links = NULL;
1659 p->individual = individual;
1662 case REQUEST_POINT: ;
1663 struct request_point *rp = (struct request_point *) r;
1667 case REQUEST_LINE: ;
1669 case REQUEST_SECTION: ;
1670 struct request_section *rls = (struct request_section *) r;
1671 p->variant_used = randint(0, rls->num_segments);
1673 case REQUEST_SEGMENT: ;
1674 struct request_segment *rs = (struct request_segment *) r;
1678 case REQUEST_AREA: ;
1679 struct request_area *ra = (struct request_area *) r;
1682 p->variant_used = 0;
1685 ASSERT(p->request->type != REQUEST_INVALID);
1686 printf("Valid request: %d\n", p->request->type);
1691 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1694 void init_individual(struct individual *i)
1696 //printf("Initing individual\n");
1697 GARY_INIT(i->placements, num_requests);
1698 GARY_INIT(i->map, 0);
1699 for (uns j=0; j<num_map_parts; j++)
1701 struct map_part *part = GARY_PUSH(i->map);
1702 GARY_INIT(part->placement, 0);
1703 struct map_placement *mp = GARY_PUSH(part->placement);
1704 mp->placement = &dummy_placement;
1705 mp->next = mp->prev = NULL;
1707 i->penalty = 0; // FIXME
1710 printf("Individual inited, has %u map parts\n", GARY_SIZE(i->map));
1713 struct placement **get_overlapping(struct placement *p UNUSED)
1715 struct placement **buffer;
1716 GARY_INIT(buffer, 0);
1720 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1725 int flip(int a, int b)
1727 return (random() % 2 ? a : b);
1730 double randdouble(void)
1732 return ((double) rand() / (double) RAND_MAX);
1738 GARY_FREE(requests_point);
1739 GARY_FREE(requests_line);
1740 GARY_FREE(requests_area);
1743 void copy_individual(struct individual *src, struct individual *dest)
1745 dest->penalty = src->penalty;
1746 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1747 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1749 dest->placements[i] = src->placements[i];
1750 dest->placements[i].map_links = NULL;
1752 for (uns j=0; j<num_map_parts; j++)
1754 struct map_part *part = GARY_PUSH(dest->map);
1755 GARY_INIT(part->placement, 0);
1756 struct map_placement *mp = GARY_PUSH(part->placement);
1757 mp->placement = &dummy_placement;
1758 mp->next = mp->prev = NULL;