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;
60 int conf_pop_size = 50;
62 int conf_penalty_bound = 0;
63 int conf_stagnation_bound = 0;
64 int conf_iteration_limit = 4;
66 int conf_term_cond = TERM_COND_ITERATIONS;
68 int conf_breed_rbest_perc = 80;
69 int conf_breed_pop_size_perc = 20;
70 int conf_breed_perc = 50; // Percentage of new pop created by breeding
72 bool conf_mutate_children = 1;
73 int conf_mutate_children_prob = 0.3;
75 int conf_mutate_rbest_perc = 60;
76 int conf_mutate_pop_size_perc = 20;
78 int conf_mutate_move_bound = 0.2;
79 int conf_mutate_regen_bound = 0.1;
80 int conf_mutate_chvar_bound = 0.1;
82 int conf_elite_perc = 5;
84 double conf_max_section_length = 100;
85 double conf_max_section_overlay = 10;
87 int old_best = 0; // FIXME: Shall be int max
91 int conf_part_size = 50;
98 int conf_map_part_width = 5;
99 int conf_map_part_height = 5;
100 int num_map_parts_row;
101 int num_map_parts_col;
104 void make_graph(void);
105 void label_graph(void);
106 void join_edge(struct graph_edge *e, int dir);
107 void bfs(uns longline);
108 void make_segments(void);
110 void make_population(void);
111 bool shall_terminate(void);
115 void rank_population(void);
116 void plan_individual(struct individual *individual);
118 void make_bitmap(struct variant *v, struct symbol *sym);
119 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
120 void make_bitmap_point(struct variant *v, struct sym_point *sp);
121 void make_bitmap_label(struct variant *v, struct sym_text *text);
123 void cut_edge(struct graph_edge *e, double dist);
124 struct request_line *make_new_line(void);
125 struct request_section *make_new_section(struct request_line *rl);
126 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
128 void dump_bitmaps(struct individual *individual);
129 void dump_graph(void);
131 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
132 void bfs_wrapper(void);
134 void dump_longlines(void);
135 void dump_linelabel_requests(void);
136 void dump_individual(struct individual *individual);
137 void print_label(struct symbol *sym);
139 double gen_movement(void);
140 void gen_coords(struct placement *p);
141 void gen_coords_point(struct placement *p);
142 void gen_coords_segment(struct placement *p);
143 void gen_coords_area(struct placement *p);
145 struct map_part **get_map_parts(struct placement *p);
146 void update_map_parts(struct placement *p);
148 void make_segments_old(void);
150 void labeller_cleanup(void);
152 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
153 void perform_mutation(struct individual *individual);
155 void hide_segment_labels(struct individual *individual);
156 void init_placement(struct placement *p, struct individual *individual, struct request *r);
157 void init_individual(struct individual *i);
158 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
160 int randint(int min, int max);
162 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2);
163 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
164 void move_symbol(struct placement *p);
165 void move_symbol_point(struct placement *p);
167 struct placement **get_overlapping(struct placement *p);
168 void filter(struct placement **list, bool *pred);
170 int flip(int a, int b);
171 double randdouble(void);
175 void copy_individual(struct individual *src, struct individual *dest);
177 int max2(int a, int b);
178 int min2(int a, int b);
179 int max4(int a, int b, int c, int d);
180 int min4(int a, int b, int c, int d);
182 struct placement dummy_placement;
184 int max2(int a, int b)
186 return (a > b ? a : b);
189 int min2(int a, int b)
191 return (a < b ? a : b);
194 int max4(int a, int b, int c, int d)
196 return max2(max2(a, b), max2(c, d));
199 int min4(int a, int b, int c, int d)
201 return min2(min2(a, b), min2(c, d));
204 void print_label(struct symbol *sym)
208 case SYMBOLIZER_TEXT: ;
209 struct sym_text *st = (struct sym_text *) sym;
210 printf("%s\n", osm_val_decode(st->text));
217 void labeller_init(void)
219 GARY_INIT(requests_point, 0);
220 GARY_INIT(requests_line, 0);
221 GARY_INIT(requests_area, 0);
222 GARY_INIT(buffer_line, 0);
223 GARY_INIT(buffer_linelabel, 0);
224 ep_individuals = ep_new(sizeof(struct individual), 1);
226 page_width_int = floor(page_width);
227 page_height_int = floor(page_height);
229 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
230 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
231 num_map_parts = num_map_parts_row * num_map_parts_col;
234 void make_bitmap(struct variant *v, struct symbol *sym)
238 case SYMBOLIZER_POINT:
239 make_bitmap_point(v, (struct sym_point *) sym);
241 case SYMBOLIZER_ICON:
242 make_bitmap_icon(v, (struct sym_icon *) sym);
244 case SYMBOLIZER_TEXT:
245 make_bitmap_label(v, (struct sym_text *) sym);
248 ASSERT(sym->type != SYMBOLIZER_INVALID);
252 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
254 v->width = si->sir.icon->width;
255 v->height = si->sir.icon->height;
256 v->bitmap = malloc((int) ceil(v->width * v->height * sizeof(bool)));
257 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
260 void make_bitmap_point(struct variant *v, struct sym_point *sp)
262 v->width = v->height = sp->size;
263 v->bitmap = malloc(sp->size*sp->size * sizeof(bool));
264 // FIXME: Okay, memset would be much nicer here
265 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
268 void make_bitmap_label(struct variant *v, struct sym_text *text)
270 v->width = ceil(text->tw);
271 v->height = ceil(text->th);
272 v->bitmap = malloc(v->width * v->height * sizeof(bool));
273 for (int i=0; i<v->height; i++)
274 for (int j=0; j<v->width; j++)
276 v->bitmap[i*v->width + j] = 1;
280 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
282 printf("Adding point\n");
283 if (object->type != OSM_TYPE_NODE)
285 printf("Warning: Point label requested on non-point object\n");
289 struct request_point *r = GARY_PUSH(requests_point);
291 r->request.type = REQUEST_POINT;
292 r->request.ind = num_requests++;
301 GARY_INIT(r->request.variants, 0);
303 struct variant *v = GARY_PUSH(r->request.variants);
305 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
310 case SYMBOLIZER_ICON:
311 make_bitmap_icon(v, (struct sym_icon *) sym);
312 r->x = ((struct sym_icon *)sym)->sir.x;
313 r->y = ((struct sym_icon *)sym)->sir.y;
315 case SYMBOLIZER_POINT:
316 make_bitmap_point(v, (struct sym_point *) sym);
318 case SYMBOLIZER_TEXT: ;
319 struct sym_text *st = (struct sym_text *) sym;
320 struct osm_node *n = (struct osm_node *) object;
321 make_bitmap_label(v, st);
327 // printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
330 void labeller_add_line(struct symbol *sym, z_index_t zindex)
333 printf("Adding line on %u\n", zindex);
334 struct buffer_line *b = GARY_PUSH(buffer_line);
335 b->line = (struct sym_line *) sym;
337 sym_plan(sym, zindex);
340 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
342 if (o->type != OSM_TYPE_WAY)
348 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
349 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
350 ll->way = (struct osm_way *) o;
355 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
357 printf("Adding area on %u\n", zindex);
358 struct request_area *r = GARY_PUSH(requests_area);
360 r->request.type = REQUEST_AREA;
361 r->request.ind = num_requests++;
363 r->o = (struct osm_multipolygon *) o;
367 osm_obj_center(o, &(r->cx), &(r->cy));
369 GARY_INIT(r->request.variants, 0);
370 struct variant *v = GARY_PUSH(r->request.variants);
373 case SYMBOLIZER_ICON:
374 printf("DEBUG: Icon label\n");
375 make_bitmap_icon(v, (struct sym_icon *) sym);
377 case SYMBOLIZER_TEXT:
378 printf("DEBUG: Text label\n");
379 make_bitmap_label(v, (struct sym_text *) sym);
386 void make_graph(void)
389 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
391 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
392 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
394 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
395 struct graph_node *g_prev = NULL;
396 struct osm_node *o_prev = NULL;
398 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
400 // FIXME: Shall osm_object's type be checked here?
401 struct osm_node *o_node = (struct osm_node *) ref->o;
403 struct graph_node *g_node = hash_find(ref->o->id);
406 g_node = hash_new(ref->o->id);
407 GARY_INIT(g_node->edges, 0);
409 g_node->id = ref->o->id;
410 g_node->num = num_nodes++;
420 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
421 e->num = num_edges++;
422 e->id = buffer_line[i].line->s.o->id;
423 e->color = buffer_line[i].line->color;
424 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
430 e->longline = (uns) -1;
431 e->line = buffer_line[i].line;
435 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
437 edge = GARY_PUSH(g_node->edges);
445 printf("Made graph with %d edges\n", num_edges_dbg);
448 void dump_graph(void)
450 HASH_FOR_ALL(hash, node)
452 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
453 for (uns i=0; i<GARY_SIZE(node->edges); i++)
455 struct graph_edge *e = node->edges[i];
456 printf("\t edge (%d) #%ju to ", e->num, e->id);
457 if (node->edges[i]->n1->id == node->id)
458 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
459 else if (node->edges[i]->n2->id == node->id)
460 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
462 printf("BEWARE! BEWARE! BEWARE!\n");
465 if ((node->edges[i]->label)) printf("Labelled\n");
466 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));
467 printf(" colored %d;", node->edges[i]->color);
468 printf(" length %.2f", node->edges[i]->length);
475 void label_graph(void)
478 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
479 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
481 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
483 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
484 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
487 printf("Looking for node %ju\n", ref->o->id);
488 struct graph_node *n = hash_find(ref->o->id);
491 // FIXME: What shall be done?
496 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
497 for (uns j=0; j<GARY_SIZE(n->edges); j++)
499 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
502 printf("Labelling node %ju\n", n->id);
503 n->edges[j]->label = buffer_linelabel[i].label;
504 n->edges[j]->zindex = buffer_linelabel[i].zindex;
512 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
515 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
516 struct graph_edge *candidate = NULL;
518 for (uns i=0; i<GARY_SIZE(node->edges); i++)
520 struct graph_edge *other = node->edges[i];
521 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
523 if ((uns) other->visited != e->longline) {
525 printf("Pushing new edge %d / %ju\n", other->num, other->id);
526 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
528 other->visited = e->longline;
531 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
532 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
535 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
536 (e->label) && (other->label) &&
537 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
538 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
540 if (! candidate || (other->length > candidate->length))
548 printf("New line in longline %u\n", e->longline);
549 struct graph_edge *other = candidate;
550 other->longline = e->longline;
552 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
553 ((dir == DIR_FWD) && (other->n2->id == node->id)))
555 struct graph_node *swp = other->n2;
556 other->n2 = other->n1;
565 longlines[other->longline].first = other;
578 void bfs(uns longline)
581 printf("BFS called for longline %u\n", longline);
583 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
584 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
586 struct graph_edge *cur = bfs_queue[i];
588 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
589 //ASSERT(! cur->visited);
591 cur->visited = longline;
593 if (cur->longline == (uns) -1)
596 if (cur->dir == DIR_UNSET)
598 cur->dir = DIR_CENTER;
599 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
600 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
607 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
610 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
620 void bfs_wrapper(void)
622 GARY_INIT(bfs_queue, 0);
623 GARY_INIT(longlines, 0);
625 HASH_FOR_ALL(hash, node)
627 for (uns i=0; i<GARY_SIZE(node->edges); i++)
629 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
631 GARY_PUSH(longlines);
632 longlines[num_longlines].first = node->edges[i];
634 printf("Running new BFS\n");
636 printf("Creating longline %u\n", num_longlines);
637 GARY_RESIZE(bfs_queue, 0);
638 struct graph_edge **e = GARY_PUSH(bfs_queue);
640 node->edges[i]->longline = num_longlines;
641 bfs(node->edges[i]->longline);
644 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
646 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
653 GARY_FREE(bfs_queue);
656 void dump_longlines(void)
658 printf("*** Longlines dump\n");
659 for (uns i=0; i<GARY_SIZE(longlines); i++)
661 printf("Longline %u:", i);
662 struct graph_edge *e = longlines[i].first;
663 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
664 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
669 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
670 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
677 struct request_line *make_new_line(void)
679 struct request_line *rl = GARY_PUSH(requests_line);
680 rl->request.ind = num_requests++;
681 rl->request.type = REQUEST_LINE;
682 GARY_INIT(rl->sections, 0);
687 struct request_section *make_new_section(struct request_line *rl)
689 struct request_section *rls = GARY_PUSH(rl->sections);
690 rls->request.ind = num_requests++;
691 rls->request.type = REQUEST_SECTION;
692 rls->num_segments = 0;
693 GARY_INIT(rls->segments, 0);
698 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
700 struct request_segment *rs = GARY_PUSH(rls->segments);
703 rs->request.ind = num_requests++;
704 rs->request.type = REQUEST_SEGMENT;
706 struct variant *v = malloc(sizeof(struct variant));
708 rs->request.variants = v;
713 void cut_edge(struct graph_edge *e, double dist)
716 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);
718 struct graph_edge *new = malloc(sizeof(struct graph_edge));
722 // FIXME? Create new label for new edge, don't only copy pointer?
724 struct osm_node *n1 = e->n1->o;
725 struct osm_node *n2 = e->n2->o;
728 if ((n1->x == n2->x) && (n1->y == n2->y))
730 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
731 printf("Won't cut point\n");
735 struct osm_node *n11 = malloc(sizeof(struct osm_node));
736 struct graph_node *gn = malloc(sizeof(struct graph_node));
738 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
739 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
740 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
742 e->n2 = new->n1 = gn;
744 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
745 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
749 void make_segments(void)
751 for (uns i=0; i<GARY_SIZE(longlines); i++)
753 // Skip lines which are not labelled
754 if (! (longlines[i].first && longlines[i].first->label))
757 struct request_line *request = make_new_line();
758 struct request_section *rls = make_new_section(request);
759 struct request_segment *rs = NULL;
761 struct graph_edge *e = longlines[i].first;
762 double cur_length = 0;
764 struct sym_text *st = NULL;
765 if (e->label->type == SYMBOLIZER_TEXT)
767 st = (struct sym_text *) e->label;
772 printf("Warning: Skipping line\n");
778 printf("New longline\n");
783 printf("BEWARE: Edge cycle\n");
786 e->visited = -1; // FIXME
789 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);
791 if (st && (e->length < st->tw))
794 //printf("Warning: Skipping segment\n");
798 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
801 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);
802 // HACK to prevent cutting to 0 lenght
803 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
806 rs = make_new_segment(rls, e->label);
807 rs->label = malloc(sizeof(struct sym_text));
808 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
810 rs->x1 = e->n1->o->x;
811 rs->y1 = e->n1->o->y;
812 rs->x2 = e->n2->o->x;
813 rs->y2 = e->n2->o->y;
815 // FIXME: Set text rotation
816 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
817 rs->zindex = e->zindex;
819 cur_length += e->length;
820 if (cur_length > conf_max_section_length)
823 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);
825 rls = make_new_section(request);
832 if (request->sections[0].num_segments == 0)
835 printf("WARNING: 0 segment section\n");
836 GARY_POP(requests_line);
842 void dump_linelabel_requests(void)
844 for (uns i=0; i<GARY_SIZE(requests_line); i++)
846 if (requests_line[i].sections[0].num_segments == 0)
851 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
852 print_label(requests_line[i].sections[0].segments[0].label);
853 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
855 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
856 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
858 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
859 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
866 void dump_bitmaps(struct individual *individual)
868 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
869 printf("Bitmap size is %d\n", page_width_int * page_height_int);
870 for (int i=0; i<page_height_int; i++)
871 for (int j=0; j<page_width_int; j++)
872 bitmap[i*page_width_int + j] = 0;
874 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
876 fprintf(stderr, "%d-th placement\n", i);
877 if (individual->placements[i].variant_used == -1) continue;
879 struct placement *p = &(individual->placements[i]);
880 struct variant *v = NULL;
882 switch (p->request->type)
884 case REQUEST_SEGMENT: ;
885 case REQUEST_POINT: ;
887 v = &(p->request->variants[p->variant_used]);
890 printf("Testing request type (dump_bitmaps): %d\n", p->request->type);
891 ASSERT(p->request->type != REQUEST_INVALID);
895 printf("Got after with %d-th placement of request type %d\n", i, p->request->type);
897 printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
898 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
900 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
902 printf("Writing to %d\n", row*page_width_int + col);
903 bitmap[row * page_width_int + col] = 1;
909 FILE *fd_dump = fopen("dump.pbm", "w");
910 fprintf(fd_dump, "P1\n");
911 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
912 for (int i=0; i<page_height_int; i++)
914 for (int j=0; j<page_width_int; j++)
916 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
918 fprintf(fd_dump, "\n");
923 void dump_individual(struct individual *individual)
925 printf("*** Dumping INDIVIDUAL ***\n");
926 printf("(There are %d requests)\n", num_requests);
927 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
929 struct placement *p = &(individual->placements[i]);
931 switch (p->request->type)
934 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
937 struct request_line *rl = (struct request_line *) p->request;
939 print_label(rl->sections[0].segments[0].label);
941 case REQUEST_SECTION: ;
944 case REQUEST_SEGMENT: ;
945 if (p->variant_used >= 0)
946 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
948 printf("Segment not placed\n");
951 struct request_area *ra = (struct request_area *) p->request;
952 printf("Area label ");
953 print_label(ra->label);
954 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
957 printf("Testing request type (dump_individual)\n");
958 ASSERT(p->request->type != 0);
961 printf("\nTotal penalty: %d\n", individual->penalty);
964 void plan_individual(struct individual *individual)
966 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
968 struct symbol *s = NULL;
969 z_index_t zindex = 0;
970 if (individual->placements[i].variant_used < 0) continue;
971 switch (individual->placements[i].request->type)
973 case REQUEST_POINT: ;
974 struct request_point *rp = (struct request_point *) individual->placements[i].request;
978 case REQUEST_SEGMENT: ;
979 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
986 struct request_area *ra = (struct request_area *) individual->placements[i].request;
991 ASSERT(individual->placements[i].request != REQUEST_INVALID);
996 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
998 if (s) switch (s->type)
1000 case SYMBOLIZER_POINT: ;
1001 struct sym_point *sp = (struct sym_point *) s;
1002 sp->x = individual->placements[i].x;
1003 sp->y = individual->placements[i].y;
1004 sym_plan((struct symbol *) sp, zindex);
1006 case SYMBOLIZER_ICON: ;
1007 struct sym_icon *si = (struct sym_icon *) s;
1008 si->sir.x = individual->placements[i].x;
1009 si->sir.y = individual->placements[i].y;
1010 sym_plan((struct symbol *) si, zindex);
1012 case SYMBOLIZER_TEXT: ;
1013 struct sym_text *st = (struct sym_text *) s;
1014 st->x = individual->placements[i].x;
1015 st->y = individual->placements[i].y;
1016 st->next_duplicate = NULL;
1017 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);
1018 sym_plan((struct symbol *) st, zindex);
1021 ASSERT(s->type != SYMBOLIZER_INVALID);
1027 void labeller_label(void)
1034 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));
1036 GARY_INIT(population1, conf_pop_size);
1037 GARY_INIT(population2, conf_pop_size);
1040 printf("Dealing with %d requests\n", num_requests);
1043 while (! shall_terminate())
1047 struct individual **swp = population1;
1048 population1 = population2;
1054 plan_individual(population1[0]);
1061 void labeller_cleanup(void)
1065 void make_population(void)
1067 for (int i=0; i<conf_pop_size; i++)
1069 printf("Making individual %d\n", i);
1070 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1071 population1[i] = individual;
1074 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1076 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1079 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1081 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1083 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1085 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1087 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1089 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1094 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1096 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1099 hide_segment_labels(individual);
1101 ASSERT(p == num_requests);
1105 bool shall_terminate(void)
1107 switch (conf_term_cond)
1109 case TERM_COND_PENALTY:
1110 return (population1[0]->penalty < conf_penalty_bound);
1111 case TERM_COND_STAGNATION:
1112 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1113 case TERM_COND_ITERATIONS:
1114 return (iteration >= conf_iteration_limit);
1116 // FIXME: Warn the user that no condition is set
1125 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1126 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1127 struct individual **breed_buffer;
1128 while (i < conf_breed_pop_size)
1130 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1131 int parent1 = randint(1, conf_breed_pop_size);
1132 int parent2 = randint(1, conf_breed_pop_size);
1133 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);
1134 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1135 population2[pop2_ind++] = breed_buffer[0];
1136 population2[pop2_ind++] = breed_buffer[1];
1141 acc += conf_breed_rbest_perc;
1143 return; // FIXME: DEBUG HACK
1145 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1146 int step = remaining / conf_pop_size;
1147 for (; i<conf_pop_size; i += 2)
1149 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1150 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1151 population2[pop2_ind++] = breed_buffer[0];
1152 population2[pop2_ind++] = breed_buffer[1];
1155 // FIXME: Could there be one missing individual?
1158 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1160 struct individual **buffer = malloc(2*sizeof(struct individual));
1161 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1162 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1164 printf("Performing crossover\n");
1166 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1168 printf("%dth placement out of %d\n", i, num_requests);
1169 if (! parent1->placements[i].processed)
1171 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1172 int x = randint(1, 2);
1176 copy_symbols(clos_symbols, parent1, child1);
1177 copy_symbols(clos_symbols, parent2, child2);
1181 copy_symbols(clos_symbols, parent2, child1);
1182 copy_symbols(clos_symbols, parent1, child2);
1184 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1185 GARY_FREE(clos_symbols);
1188 if (conf_mutate_children)
1190 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1191 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1203 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1204 while (i < conf_mutate_rbest_perc * conf_pop_size)
1206 int ind = randint(1, conf_mutate_pop_size);
1207 copy_individual(population2[pop2_ind], population1[ind]);
1208 perform_mutation(population2[pop2_ind]);
1213 void perform_mutation(struct individual *individual)
1215 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1217 int x = randint(1, 1000);
1220 if (x <= acc + conf_mutate_move_bound)
1222 move_symbol(&(individual->placements[i]));
1225 acc += conf_mutate_move_bound;
1227 if (x <= acc + conf_mutate_regen_bound)
1229 gen_coords(&(individual->placements[i]));
1232 acc += conf_mutate_regen_bound;
1234 if (x <= acc + conf_mutate_chvar_bound)
1236 if (0) // if num_variants > 1
1238 // FIXME: assign new variant
1246 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1248 population2[pop2_ind++] = population1[0];
1252 void rank_population(void)
1257 struct map_part **get_map_parts(struct placement *p)
1259 if (p->variant_used < 0) return NULL;
1261 struct map_part **buffer;
1262 GARY_INIT(buffer, 0);
1265 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1268 switch (p->request->type)
1271 case REQUEST_SEGMENT:
1273 v = 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);
1351 printf("Not yet implemented\n");
1355 printf("Testing request type\n");
1356 ASSERT(p->request->type != REQUEST_INVALID);
1359 update_map_parts(p);
1362 double gen_movement(void)
1364 double m = (random() % 1000000) / 10000;
1365 m = pow(m, 1.0/3) * flip(1, -1);
1367 printf("Movement %.2f\n", m);
1371 void gen_coords_point(struct placement *p)
1373 p->x = p->x + gen_movement();
1376 void gen_coords_segment(struct placement *p)
1378 struct request_segment *rs = (struct request_segment *) p->request;
1380 p->x = (a == 1 ? rs->x1 : rs->x2);
1381 p->y = (a == 1 ? rs->y1 : rs->y2);
1384 void gen_coords_area(struct placement *p)
1386 struct request_area *ra = (struct request_area *) p->request;
1388 p->x = p->x + gen_movement();
1389 p->y = p->y + gen_movement();
1391 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1394 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1396 struct map_part **buffer;
1397 GARY_INIT(buffer, 0);
1398 int x_min = symbol->x / conf_part_size;
1399 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1400 int y_min = symbol->y / conf_part_size;
1401 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1403 for (int x=x_min; x < x_max; x++)
1404 for (int y=y_min; y < y_max; y++)
1406 struct map_part *m = GARY_PUSH(buffer);
1407 *m = individual->map[x][y];
1413 int randint(int min, int max)
1415 if (min == max) return min;
1417 //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)));
1418 return min + (r % (max - min));
1419 return (r * (max - min));
1422 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1424 printf("Getting closure\n");
1425 struct placement **closure;
1426 GARY_INIT(closure, 0);
1427 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1428 chosen[placement->request->ind] = 1;
1430 struct placement **p = GARY_PUSH(closure); *p = placement;
1433 while (first < GARY_SIZE(closure))
1435 printf("Iterating, first is %d\n", first);
1436 struct placement **overlapping = get_overlapping(placement);
1437 filter(overlapping, chosen);
1438 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1440 p = GARY_PUSH(closure); *p = overlapping[j];
1441 chosen[overlapping[j]->request->ind] = 1;
1443 GARY_FREE(overlapping);
1450 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1452 //printf("%d\n", child->penalty);
1453 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1454 for (uns i=0; i<GARY_SIZE(closure); i++)
1456 int ind = closure[i]->request->ind;
1457 child->placements[ind] = parent->placements[ind];
1458 child->placements[ind].processed = 0;
1462 void move_symbol(struct placement *p)
1464 switch (p->request->type)
1467 move_symbol_point(p);
1469 case REQUEST_SEGMENT:
1472 printf("Not yet implemented\n");
1474 ASSERT(p->request->type != REQUEST_INVALID);
1478 void move_symbol_point(struct placement *p)
1480 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1481 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1484 void hide_segment_labels(struct individual *individual)
1486 // BEWARE: This fully depends on current genetic encoding
1488 int used = -1, num = -1;
1489 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1491 switch (individual->placements[i].request->type)
1493 case REQUEST_SECTION:
1494 used = individual->placements[i].variant_used;
1497 case REQUEST_SEGMENT:
1499 individual->placements[i].variant_used = 0;
1501 individual->placements[i].variant_used = -1;
1510 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1515 p->x = p->y = 0; // To prevent valgrind from complaining
1516 p->variant_used = 0;
1517 p->map_links = NULL;
1518 p->individual = individual;
1521 case REQUEST_POINT: ;
1522 struct request_point *rp = (struct request_point *) r;
1526 case REQUEST_LINE: ;
1528 case REQUEST_SECTION: ;
1529 struct request_section *rls = (struct request_section *) r;
1530 p->variant_used = randint(0, rls->num_segments);
1532 case REQUEST_SEGMENT: ;
1533 struct request_segment *rs = (struct request_segment *) r;
1537 case REQUEST_AREA: ;
1538 struct request_area *ra = (struct request_area *) r;
1541 p->variant_used = 0;
1544 ASSERT(p->request->type != REQUEST_INVALID);
1545 printf("Valid request: %d\n", p->request->type);
1549 // printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1552 void init_individual(struct individual *i)
1554 //printf("Initing individual\n");
1555 GARY_INIT(i->placements, num_requests);
1556 GARY_INIT(i->map, 0);
1557 for (uns j=0; j<num_map_parts; j++)
1559 struct map_part *part = GARY_PUSH(i->map);
1560 GARY_INIT(part->placement, 0);
1561 struct map_placement *mp = GARY_PUSH(part->placement);
1562 mp->placement = &dummy_placement;
1563 mp->next = mp->prev = NULL;
1565 i->penalty = 0; // FIXME
1568 struct placement **get_overlapping(struct placement *p UNUSED)
1570 struct placement **buffer;
1571 GARY_INIT(buffer, 0);
1575 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1580 int flip(int a, int b)
1582 return (random() % 2 ? a : b);
1585 double randdouble(void)
1587 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1594 GARY_FREE(requests_point);
1595 GARY_FREE(requests_line);
1596 GARY_FREE(requests_area);
1599 void copy_individual(struct individual *src, struct individual *dest)
1601 src->penalty = dest->penalty;
1602 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1603 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1605 dest->placements[i] = src->placements[i];