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;
52 int conf_pop_size = 50;
54 int conf_penalty_bound = 0;
55 int conf_stagnation_bound = 0;
56 int conf_iteration_limit = 4;
58 int conf_term_cond = TERM_COND_ITERATIONS;
60 int conf_breed_rbest_perc = 80;
61 int conf_breed_pop_size_perc = 20;
62 int conf_breed_perc = 50; // Percentage of new pop created by breeding
64 bool conf_mutate_children = 1;
65 int conf_mutate_children_prob = 0.3;
67 int conf_mutate_rbest_perc = 60;
68 int conf_mutate_pop_size_perc = 20;
70 int conf_mutate_move_bound = 0.2;
71 int conf_mutate_regen_bound = 0.1;
72 int conf_mutate_chvar_bound = 0.1;
74 int conf_elite_perc = 5;
76 double conf_max_section_length = 100;
77 double conf_max_section_overlay = 10;
79 int old_best = 0; // FIXME: Shall be int max
83 int conf_part_size = 50;
90 void dump_graph(void);
92 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
93 void bfs_wrapper(void);
95 void dump_longlines(void);
96 void dump_linelabel_requests(void);
97 void dump_individual(struct individual *individual);
98 void print_label(struct symbol *sym);
100 double gen_movement(void);
101 void gen_coords(struct placement *p);
102 void gen_coords_point(struct placement *p);
103 void gen_coords_segment(struct placement *p);
104 void gen_coords_area(struct placement *p);
106 int max2(int a, int b);
107 int min2(int a, int b);
108 int max4(int a, int b, int c, int d);
109 int min4(int a, int b, int c, int d);
111 int max2(int a, int b)
113 return (a > b ? a : b);
116 int min2(int a, int b)
118 return (a < b ? a : b);
121 int max4(int a, int b, int c, int d)
123 return max2(max2(a, b), max2(c, d));
126 int min4(int a, int b, int c, int d)
128 return min2(min2(a, b), min2(c, d));
131 void print_label(struct symbol *sym)
135 case SYMBOLIZER_TEXT: ;
136 struct sym_text *st = (struct sym_text *) sym;
137 printf("%s\n", osm_val_decode(st->text));
144 void labeller_init(void)
146 GARY_INIT(requests_point, 0);
147 GARY_INIT(requests_line, 0);
148 GARY_INIT(requests_area, 0);
149 GARY_INIT(buffer_line, 0);
150 GARY_INIT(buffer_linelabel, 0);
151 ep_individuals = ep_new(sizeof(struct individual), 1);
153 page_width_int = floor(page_width);
154 page_height_int = floor(page_height);
157 void make_bitmap_icon(struct point_variant *v, struct sym_icon *si)
159 v->width = si->sir.icon->width;
160 v->height = si->sir.icon->height;
161 v->bitmap = malloc((int) ceil(v->width * v->height * sizeof(bool)));
162 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
165 void make_bitmap_point(struct point_variant *v, struct sym_point *sp)
167 v->width = v->height = sp->size;
168 v->bitmap = malloc(sp->size*sp->size * sizeof(bool));
169 // FIXME: Okay, memset would be much nicer here
170 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
173 void make_bitmap_label(struct point_variant *v, struct sym_text *text)
184 v->width = max4(x_ld, x_lu, x_rd, x_ru) - min4(x_ld, x_lu, x_rd, x_ru);
185 v->height = max4(y_ld, y_lu, y_rd, y_ru) - min4(y_ld, y_lu, y_rd, y_ru);
186 //v->bitmap = malloc((int) (ceil(v->width) * ceil(v->height) * sizeof(bool)));
188 v->width = ceil(text->tw);
189 v->height = ceil(text->th);
190 v->bitmap = malloc(v->width * v->height * sizeof(bool));
191 printf("Allocated bitmap of %d bools for %d x %d label\n", v->width * v->height, v->width, v->height);
192 for (int i=0; i<v->height; i++)
193 for (int j=0; j<v->width; j++)
195 v->bitmap[i*v->width + j] = 1;
196 printf("Writing at %d\n", i*v->width + j);
200 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
202 printf("Adding point\n");
203 if (object->type != OSM_TYPE_NODE)
205 printf("Warning: Point label requested on non-point object\n");
209 struct request_point *r = GARY_PUSH(requests_point);
211 r->request.type = REQUEST_POINT;
212 r->request.ind = num_requests++;
221 GARY_INIT(r->variants, 0);
223 struct point_variant *v = GARY_PUSH(r->variants);
225 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
230 case SYMBOLIZER_ICON:
231 make_bitmap_icon(v, (struct sym_icon *) sym);
232 r->x = ((struct sym_icon *)sym)->sir.x;
233 r->y = ((struct sym_icon *)sym)->sir.y;
235 case SYMBOLIZER_POINT:
236 make_bitmap_point(v, (struct sym_point *) sym);
238 case SYMBOLIZER_TEXT: ;
239 struct sym_text *st = (struct sym_text *) sym;
240 struct osm_node *n = (struct osm_node *) object;
241 make_bitmap_label(v, st);
247 printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
250 void labeller_add_line(struct symbol *sym, z_index_t zindex)
252 printf("Adding line on %u\n", zindex);
253 struct buffer_line *b = GARY_PUSH(buffer_line);
254 b->line = (struct sym_line *) sym;
256 sym_plan(sym, zindex);
259 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
261 if (o->type != OSM_TYPE_WAY)
267 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
268 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
269 ll->way = (struct osm_way *) o;
274 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
276 printf("Adding area on %u\n", zindex);
277 struct request_area *r = GARY_PUSH(requests_area);
279 r->request.type = REQUEST_AREA;
280 r->request.ind = num_requests++;
282 r->o = (struct osm_multipolygon *) o;
286 osm_obj_center(o, &(r->cx), &(r->cy));
288 GARY_INIT(r->variants, 0);
289 struct point_variant *v = GARY_PUSH(r->variants);
292 case SYMBOLIZER_ICON:
293 printf("DEBUG: Icon label\n");
294 make_bitmap_icon(v, (struct sym_icon *) sym);
296 case SYMBOLIZER_TEXT:
297 printf("DEBUG: Text label\n");
298 make_bitmap_label(v, (struct sym_text *) sym);
305 void make_graph(void)
308 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
310 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
311 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
313 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
314 struct graph_node *g_prev = NULL;
315 struct osm_node *o_prev = NULL;
317 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
319 // FIXME: Shall osm_object's type be checked here?
320 struct osm_node *o_node = (struct osm_node *) ref->o;
322 struct graph_node *g_node = hash_find(ref->o->id);
325 g_node = hash_new(ref->o->id);
326 GARY_INIT(g_node->edges, 0);
328 g_node->id = ref->o->id;
329 g_node->num = num_nodes++;
339 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
340 e->num = num_edges++;
341 e->id = buffer_line[i].line->s.o->id;
342 e->color = buffer_line[i].line->color;
343 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
349 e->longline = (uns) -1;
350 e->line = buffer_line[i].line;
354 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
356 edge = GARY_PUSH(g_node->edges);
364 printf("Made graph with %d edges\n", num_edges_dbg);
367 void dump_graph(void)
369 HASH_FOR_ALL(hash, node)
371 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
372 for (uns i=0; i<GARY_SIZE(node->edges); i++)
374 struct graph_edge *e = node->edges[i];
375 printf("\t edge (%d) #%ju to ", e->num, e->id);
376 if (node->edges[i]->n1->id == node->id)
377 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
378 else if (node->edges[i]->n2->id == node->id)
379 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
381 printf("BEWARE! BEWARE! BEWARE!\n");
384 if ((node->edges[i]->label)) printf("Labelled\n");
385 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));
386 printf(" colored %d;", node->edges[i]->color);
387 printf(" length %.2f", node->edges[i]->length);
394 void label_graph(void)
396 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
397 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
399 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
400 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
401 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
403 printf("Looking for node %ju\n", ref->o->id);
404 struct graph_node *n = hash_find(ref->o->id);
407 // FIXME: What shall be done?
411 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
412 for (uns j=0; j<GARY_SIZE(n->edges); j++)
414 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
416 printf("Labelling node %ju\n", n->id);
417 n->edges[j]->label = buffer_linelabel[i].label;
418 n->edges[j]->zindex = buffer_linelabel[i].zindex;
429 * void join_edge(struct graph_edge *e, int dir)
431 * struct graph_node *other_node = NULL;
435 * other_node = e->n2;
438 * other_node = e->n1;
443 * struct graph_edge *candidate = NULL;
444 * for (uns i=0; i<GARY_SIZE(other_node->edges); i++)
446 * struct graph_edge *other = other_node->edges[i];
447 * if (! other->visited)
449 * struct graph_edge **new = GARY_PUSH(bfs_queue);
450 * *new = other_node->edges[i];
453 * if ((!other->visited) && (e->text) && (other->text) && (e->text->text == other->text->text))
455 * if (e->color == other_node->edges[i]->color)
457 * if ((!candidate) || (candidate->length < other->length))
464 * // Beware: Name conflict here
471 * candidate->longline = e->longline;
474 * if (candidate->n2 != e->n1)
476 * candidate->n1 = candidate->n2;
477 * candidate->n2 = e->n1;
479 * e->prev = candidate;
480 * candidate->next = e;
482 * longlines[e->longline].first = candidate;
486 * if (candidate->n1 != e->n2)
488 * candidate->n2 = candidate->n1;
489 * candidate->n1 = e->n2;
491 * e->next = candidate;
492 * candidate->prev = e;
501 * GARY_INIT(bfs_queue, 0);
502 * GARY_INIT(longlines, 0);
504 * printf("Making longlines from %u causal lines, using %d graph edges\n", GARY_SIZE(buffer_line), num_edges_dbg);
506 * HASH_FOR_ALL(hash, node)
508 * for (uns i=0; i<GARY_SIZE(node->edges); i++)
510 * struct graph_edge *e = node->edges[i];
512 * // printf("Examining edge from [%.2f; %.2f] to [%.2f; %.2f]\n",
513 * // e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y);
515 * // if (e->visited) HASH_CONTINUE; // FIXME: Is is correct?
516 * if (e->visited) continue;
517 * // printf("Continuing\n");
518 * if (e->longline == (uns) -1)
520 * GARY_PUSH(longlines);
521 * e->longline = num_longlines++;
522 * longlines[e->longline].first = e;
524 * // printf("Longline is %u\n", e->longline);
540 * GARY_FREE(bfs_queue);
544 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
547 struct graph_edge *candidate = NULL;
549 if ((e->num > 31) && (e->num < 48)) printf("Searching for neighbours of %d, in longline %u, BFS dir is %d, edge's dir is %d\n", e->num, e->longline, dir, e->dir);
552 for (uns i=0; i<GARY_SIZE(node->edges); i++)
554 struct graph_edge *other = node->edges[i];
555 printf("Touching %d\n", other->num);
556 if (other->num == 44) printf("Longline of 44 is %u\n", other->longline);
557 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
559 if (! other->visited) {
560 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
565 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
566 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
569 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
570 (e->label) && (other->label) &&
571 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
572 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
573 // (e->text) && (other->text) && (e->text->text == other->text->text))
575 if (! candidate || (other->length > candidate->length))
582 struct graph_edge *other = candidate;
584 other->longline = e->longline;
586 other->anode = (other->n1->id == node->id ? other->n2 : other->n1);
587 other->bnode = (other->n1->id == node->id ? other->n1 : other->n2);
593 longlines[other->longline].first = other;
606 void ya_bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
608 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
609 struct graph_edge *candidate = NULL;
611 for (uns i=0; i<GARY_SIZE(node->edges); i++)
613 struct graph_edge *other = node->edges[i];
614 if (e->num == 987) printf("Got label %d\n", e->num);
615 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
616 if (e->num == 987) printf("Continuing with edge %d\n", e->num);
618 printf("Testing %d ?= %d\n", other->visited, e->longline);
619 if (other->visited != e->longline) {
620 printf("Pushing new edge %d / %ju\n", other->num, other->id);
621 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
623 other->visited = e->longline;
626 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
627 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
630 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
631 (e->label) && (other->label) &&
632 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
633 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
635 if (! candidate || (other->length > candidate->length))
642 printf("New line in longline %u\n", e->longline);
643 struct graph_edge *other = candidate;
644 other->longline = e->longline;
646 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
647 ((dir == DIR_FWD) && (other->n2->id == node->id)))
649 struct graph_node *swp = other->n2;
650 other->n2 = other->n1;
659 longlines[other->longline].first = other;
674 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
676 struct graph_edge *cur = bfs_queue[i];
677 printf("Exploring new edge, %d\n", cur->num);
678 //ASSERT(! cur->visited);
681 if (cur->longline == (uns) -1)
683 GARY_PUSH(longlines);
684 cur->longline = num_longlines++;
685 longlines[cur->longline].first = cur;
690 bfs_edge(cur, cur->n1, cur->n2, 1);
691 bfs_edge(cur, cur->n2, cur->n1, 2);
695 bfs_edge(cur, cur->anode, cur->bnode, cur->dir);
700 void ya_bfs(uns longline)
702 printf("BFS called for longline %u\n", longline);
703 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
704 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
706 struct graph_edge *cur = bfs_queue[i];
707 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
708 //ASSERT(! cur->visited);
710 cur->visited = longline;
712 if (cur->longline == (uns) -1)
715 if (cur->dir == DIR_UNSET)
717 cur->dir = DIR_CENTER;
718 ya_bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
719 ya_bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
726 ya_bfs_edge(cur, cur->n1, cur->n2, cur->dir);
729 ya_bfs_edge(cur, cur->n2, cur->n1, cur->dir);
739 void bfs_wrapper(void)
741 GARY_INIT(bfs_queue, 0);
742 GARY_INIT(longlines, 0);
744 HASH_FOR_ALL(hash, node)
746 for (uns i=0; i<GARY_SIZE(node->edges); i++)
748 if (! node->edges[i]->visited)
750 printf("Running new BFS\n");
751 GARY_RESIZE(bfs_queue, 0);
752 struct graph_edge **e = GARY_PUSH(bfs_queue);
756 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
757 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
763 GARY_FREE(bfs_queue);
766 void ya_bfs_wrapper(void)
768 GARY_INIT(bfs_queue, 0);
769 GARY_INIT(longlines, 0);
771 HASH_FOR_ALL(hash, node)
773 for (uns i=0; i<GARY_SIZE(node->edges); i++)
775 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
777 GARY_PUSH(longlines);
778 longlines[num_longlines].first = node->edges[i];
779 printf("Running new BFS\n");
780 printf("Creating longline %u\n", num_longlines);
781 GARY_RESIZE(bfs_queue, 0);
782 struct graph_edge **e = GARY_PUSH(bfs_queue);
784 node->edges[i]->longline = num_longlines;
785 ya_bfs(node->edges[i]->longline);
787 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
788 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
795 GARY_FREE(bfs_queue);
801 * printf("Starting outer BFS\n");
802 * printf("There are %u buffered lines and %d eges\n", GARY_SIZE(buffer_line), num_edges_dbg);
804 * GARY_INIT(bfs_queue, 0);
805 * GARY_INIT(longlines, 0);
807 * int dbg_bfs_continues = 0;
809 * HASH_FOR_ALL(hash, node)
811 * // FIXME: Skip if visited node
813 * for (uns i=0; i<GARY_SIZE(node->edges); i++)
815 * struct graph_edge *e = node->edges[i];
817 * if (e->visited) continue;
820 * for (uns i1=0; i1<GARY_SIZE(e->n1->edges); i1++)
822 * struct graph_edge *other = e->n1->edges[i1];
823 * if (other->visited) { dbg_bfs_continues++; continue; }
825 * if (((other->n1->id == e->n1->id) || (other->n2->id == e->n1->id)) &&
826 * (e->text) && (other->text) && (e->text->text == other->text->text))
828 * // printf("Hit\n");
829 * other->visited = 1;
832 * for (uns i2=0; i2<GARY_SIZE(e->n2->edges); i2++)
834 * struct graph_edge *other = e->n2->edges[i2];
835 * if (other->visited) {dbg_bfs_continues++; continue; }
837 * if (((other->n1->id == e->n2->id) || (other->n2->id == e->n2->id)) &&
838 * (e->text) && (other->text) && (e->text->text == other->text->text))
840 * // printf("Hit\n");
841 * other->visited = 1;
848 * printf("Total: %d hits, %d visited edges skipped\n", dbg_num_hits, dbg_bfs_continues);
850 * GARY_FREE(bfs_queue);
854 void dump_longlines(void)
856 printf("*** Longlines dump\n");
857 for (uns i=0; i<GARY_SIZE(longlines); i++)
859 printf("Longline %u:", i);
860 struct graph_edge *e = longlines[i].first;
861 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
862 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
867 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
868 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
875 void dump_longlines_old(void)
877 for (uns i=0; i<GARY_SIZE(longlines); i++)
879 struct graph_edge *e = longlines[i].first;
881 printf("> Longline %u;", i);
882 if (longlines[i].first->label && longlines[i].first->label->type == SYMBOLIZER_TEXT) printf(" labelled %s", osm_val_decode(((struct sym_text *) longlines[i].first->label)->text));
886 printf("\t#%ju (%d)", e->id, e->num);
890 printf("[%.2f; %.2f] -- #%ju [%.2f; %.2f] (dir %d)\n", e->anode->o->x, e->anode->o->y, e->bnode->o->o.id, e->bnode->o->x, e->bnode->o->y, e->dir);
893 printf("[%.2f; %.2f] -- #%ju [%.2f; %.2f] (dir %d)\n", e->bnode->o->x, e->bnode->o->y, e->anode->o->o.id, e->anode->o->x, e->anode->o->y, e->dir);
896 printf("[%.2f; %.2f] -- #%ju [%.2f; %.2f] (dir %d)\n", e->n1->o->x, e->n1->o->y, e->n2->o->o.id, e->n2->o->x, e->n2->o->y, e->dir);
899 printf("%d\n", e->dir);
907 void make_segments_new(void)
909 for (uns i=0; i<GARY_SIZE(longlines); i++)
911 // Skip lines which are not labelled
912 if (! (longlines[i].first && longlines[i].first->label))
915 struct request_line *request = GARY_PUSH(requests_line);
916 request->request.ind = -1;
917 request->request.type = REQUEST_LINE;
919 GARY_INIT(request->sections, 0);
922 struct request_section *rls = GARY_PUSH(request->sections);
923 rls->request.ind = num_requests++;
924 rls->request.type = REQUEST_SECTION;
925 rls->num_segments = 0;
926 GARY_INIT(rls->segments, 0);
928 struct graph_edge *e = longlines[i].first;
929 struct request_segment *rs = NULL;
931 struct sym_text *st = NULL;
932 if (e->label->type == SYMBOLIZER_TEXT)
934 st = (struct sym_text *) e->label;
944 if ((cur_length + e->length > conf_max_section_length) &&
945 !(cur_length + e->length < conf_max_section_overlay))
947 struct osm_node *n = e->n1->o;
949 rs = GARY_PUSH(rls->segments);
953 // FIXME: Truly compute x2, y2
957 rls = GARY_PUSH(request->sections);
958 rls->request.ind = num_requests++;
959 rls->request.type = REQUEST_SECTION;
960 rls->num_segments = 0;
961 GARY_INIT(rls->segments, 0);
964 if (e->length < st->tw)
970 rs = GARY_PUSH(rls->segments);
972 rs->request.type = REQUEST_SEGMENT;
973 rs->request.ind = num_requests++;
974 rs->label = e->label;
976 rs->x1 = e->n1->o->x;
977 rs->y1 = e->n1->o->y;
978 rs->x2 = e->n2->o->x;
979 rs->y2 = e->n2->o->y;
981 rs->zindex = e->zindex;
983 cur_length += e->length;
990 void make_segments(void)
992 printf("Analysing %u longlines\n", GARY_SIZE(longlines));
993 for (uns i=0; i<GARY_SIZE(longlines); i++)
995 if (! longlines[i].first->label) continue;
996 // printf("Survived! %s\n", osm_val_decode(longlines[i].first->text->text));
997 printf("New longline\n");
999 if (longlines[i].first->label->type != SYMBOLIZER_TEXT)
1004 struct request_line *request = GARY_PUSH(requests_line);
1005 request->request.ind = -1;
1006 request->request.type = REQUEST_LINE;
1008 GARY_INIT(request->segments, 0);
1009 request->num_segments = 0;
1011 // ->num_variants FIXME
1014 struct graph_edge *e = longlines[i].first;
1016 if (! e) printf("Oops\n");
1020 if (! e->label) break;
1022 struct request_segment *r = GARY_PUSH(request->segments);
1023 request->num_segments++;
1025 r->request.ind = num_requests++;
1026 r->request.type = REQUEST_SEGMENT;
1028 struct osm_node *n = e->n1->o;
1034 r->k = abs(r->x2 - r->x1) / (abs(r->y2 - r->y1) + 0.001); // FIXME: Hack to prevent floating point exception when y2 = y1
1036 printf("Segment [%.2f; %.2f] -- [%.2f; %.2f]\n", r->x1, r->y1, r->x2, r->y2);
1039 r->zindex = e->zindex;
1040 r->variant = malloc(sizeof(struct point_variant)); // FIXME
1041 switch (e->label->type)
1043 case SYMBOLIZER_TEXT: ;
1044 struct sym_text *st = malloc(sizeof(struct sym_text));
1045 ((struct symbol *) st)->o = e->label->o;
1046 ((struct symbol *) st)->type = SYMBOLIZER_TEXT;
1049 st->text = ((struct sym_text *) e->label)->text;
1050 st->text_color = ((struct sym_text *) e->label)->text_color;
1051 st->font = ((struct sym_text *) e->label)->font;
1052 st->opacity = ((struct sym_text *) e->label)->opacity;
1053 st->halo_color = ((struct sym_text *) e->label)->halo_color;
1054 st->halo_radius = ((struct sym_text *) e->label)->halo_radius;
1055 st->halo_opacity = ((struct sym_text *) e->label)->halo_opacity;
1056 st->tw = ((struct sym_text *) e->label)->tw;
1057 st->th = ((struct sym_text *) e->label)->th;
1058 st->td = ((struct sym_text *) e->label)->td;
1059 r->label = (struct symbol *) st;
1060 // FIXME: This shall be done in more elegant way
1061 make_bitmap_label(r->variant, (struct sym_text *) e->label);
1065 printf("Got here?\n");
1074 void dump_linelabel_requests(void)
1076 for (uns i=0; i<GARY_SIZE(requests_line); i++)
1078 print_label(requests_line[i].segments[0].label);
1082 void dump_bitmaps(struct individual *individual)
1084 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
1085 printf("Bitmap size is %d\n", page_width_int * page_height_int);
1086 for (int i=0; i<page_height_int; i++)
1087 for (int j=0; j<page_width_int; j++)
1088 bitmap[i*page_width_int + j] = 0;
1090 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1092 struct placement *p = &(individual->placements[i]);
1093 struct point_variant *v = NULL;
1095 switch (p->request->type)
1097 case REQUEST_SEGMENT: ;
1098 struct request_segment *rs = (struct request_segment *) p->request;
1101 case REQUEST_POINT: ;
1102 struct request_point *rp = (struct request_point *) p->request;
1103 v = &(rp->variants[p->variant_used]);
1105 case REQUEST_AREA: ;
1106 struct request_area *ra = (struct request_area *) p->request;
1107 printf("Using %d-th of %d variants\n", p->variant_used, GARY_SIZE(ra->variants));
1108 v = &(ra->variants[p->variant_used]);
1111 ASSERT(p->request->type != REQUEST_INVALID);
1115 printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
1116 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
1118 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
1120 printf("Writing to %d\n", row*page_width_int + col);
1121 bitmap[row * page_width_int + col] = 1;
1127 FILE *fd_dump = fopen("dump.pbm", "w");
1128 fprintf(fd_dump, "P1\n");
1129 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
1130 for (int i=0; i<page_height_int; i++)
1132 for (int j=0; j<page_width_int; j++)
1134 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
1136 fprintf(fd_dump, "\n");
1141 void dump_individual(struct individual *individual)
1143 printf("(There are %d requests)\n", num_requests);
1144 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1146 struct placement *p = &(individual->placements[i]);
1148 switch (p->request->type)
1151 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
1153 case REQUEST_LINE: ;
1154 struct request_line *rl = (struct request_line *) p->request;
1155 print_label(rl->segments[0].label);
1157 case REQUEST_SEGMENT: ;
1158 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
1160 case REQUEST_AREA: ;
1161 struct request_area *ra = (struct request_area *) p->request;
1162 printf("Area label ");
1163 print_label(ra->label);
1164 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
1167 ASSERT(p->request->type != 0);
1170 printf("\nTotal penalty: %d\n", individual->penalty);
1173 void labeller_label(void)
1180 make_segments_new();
1181 //dump_linelabel_requests();
1183 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));
1185 GARY_INIT(population1, conf_pop_size);
1186 GARY_INIT(population2, conf_pop_size);
1189 printf("Dealing with %d requests\n", num_requests);
1192 while (! shall_terminate())
1196 struct individual **swp = population1;
1197 population1 = population2;
1203 dump_individual(population1[0]);
1204 dump_bitmaps(population1[0]);
1206 for (uns i=0; i<GARY_SIZE(population1[0]->placements); i++)
1208 printf("Coping with %d\n", population1[0]->placements[i].request->type);
1210 struct symbol *s = NULL;
1211 z_index_t zindex = 0;
1212 switch (population1[0]->placements[i].request->type)
1214 case REQUEST_POINT: ;
1215 struct request_point *rp = (struct request_point *) population1[0]->placements[i].request;
1217 zindex = rp->zindex;
1218 printf("%u\n", zindex);
1220 case REQUEST_SEGMENT: ;
1221 struct request_segment *rs = (struct request_segment *) population1[0]->placements[i].request;
1223 printf("Assigned label to s\n");
1224 zindex = rs->zindex;
1225 printf("%u\n", zindex);
1227 case REQUEST_AREA: ;
1228 struct request_area *ra = (struct request_area *) population1[0]->placements[i].request;
1230 zindex = ra->zindex;
1231 printf("%u\n", zindex);
1234 ASSERT(population1[0]->placements[i].request != REQUEST_INVALID);
1235 printf("Yep, in default, continuing\n");
1239 printf("Will plan symbol on %u\n", zindex);
1243 case SYMBOLIZER_POINT: ;
1244 struct sym_point *sp = (struct sym_point *) s;
1245 sp->x = population1[0]->placements[i].x;
1246 sp->y = population1[0]->placements[i].y;
1247 sym_plan((struct symbol *) sp, zindex);
1249 case SYMBOLIZER_ICON: ;
1250 struct sym_icon *si = (struct sym_icon *) s;
1251 si->sir.x = population1[0]->placements[i].x;
1252 si->sir.y = population1[0]->placements[i].y;
1253 sym_plan((struct symbol *) si, zindex);
1255 case SYMBOLIZER_TEXT: ;
1256 struct sym_text *st = (struct sym_text *) s;
1257 st->x = population1[0]->placements[i].x;
1258 st->y = population1[0]->placements[i].y;
1259 st->next_duplicate = NULL;
1260 sym_plan((struct symbol *) st, zindex);
1263 ASSERT(s->type != SYMBOLIZER_INVALID);
1269 switch (population1[0]->placements[i].request->type)
1271 case REQUEST_POINT: ;
1272 struct request_point *rp = (struct request_point *) population1[0]->placements[i].request;
1273 switch (rp->sym->type)
1275 case SYMBOLIZER_POINT: ;
1276 printf("Moving point to final destination\n");
1277 struct sym_point *sp = (struct sym_point *) rp->sym;
1278 sp->x = population1[0]->placements[i].x;
1279 sp->y = population1[0]->placements[i].y;
1280 sym_plan((struct symbol *) sp, rp->zindex);
1282 case SYMBOLIZER_ICON: ;
1283 printf("Moving icon to final destination\n");
1284 struct sym_icon *si = (struct sym_icon *) rp->sym;
1285 si->sir.x = population1[0]->placements[i].x;
1286 si->sir.y = population1[0]->placements[i].y;
1287 sym_plan((struct symbol *) si, rp->zindex);
1290 printf("Haúúú 1\n");
1294 case REQUEST_AREA: ;
1295 struct request_area *ra = (struct request_area *) population1[0]->placements[i].request;
1296 switch (ra->label->type)
1298 case SYMBOLIZER_ICON
1299 case SYMBOLIZER_TEXT: ;
1300 struct sym_text *st = (struct sym_text *) ra->label;
1301 st->x = population1[0]->placements[i].x;
1302 st->y = population1[0]->placements[i].y;
1308 if (ra->label->type == SYMBOLIZER_INVALID) printf("Haúúú 2\n");
1309 sym_plan((struct symbol *) ra->label, ra->zindex);
1312 case REQUEST_LINE: ;
1313 struct request_line *rl = (struct request_line *) population1[0]->placements[i].request;
1314 for (uns j=0; j<GARY_SIZE(rl->segments); j++)
1316 switch (rl->segments[j].label->type)
1318 case SYMBOLIZER_TEXT:
1319 ((struct sym_text *) rl->segments[j].label)->next_duplicate = NULL;
1320 ((struct sym_text *) rl->segments[j].label)->next_in_tile = NULL;
1321 printf("Planning text ");
1322 print_label(rl->segments[j].label);
1324 printf("Haúúú 3\n");
1328 sym_plan((struct symbol *) rl->segments[j].label, rl->segments[j].zindex); // FIXME: z-index
1332 case REQUEST_SEGMENT: ;
1333 //struct request_segment *rs = (struct request_segment *) population1[0]->placements[i].request;
1334 printf("Segment!\n");
1337 ASSERT(population1[0]->placements[i].request->type != REQUEST_INVALID);
1345 while (! shall_terminate())
1353 // sort population by fitness
1355 struct individual **swp = population1;
1356 population1 = population2;
1357 printf("Swapped populations\n");
1359 // GARY_RESIZE(population2, 0) -- is it needed?
1364 void make_population(void)
1366 for (int i=0; i<conf_pop_size; i++)
1368 printf("Making individual %d\n", i);
1369 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1370 population1[i] = individual;
1373 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1375 init_placement(&(individual->placements[p++]), (struct request *) &requests_point[j]);
1377 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1379 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j]);
1380 for (uns k=0; k<GARY_SIZE(requests_line[j].segments); k++)
1382 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].segments[k]);
1385 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1387 init_placement(&(individual->placements[p++]), (struct request *) &requests_area[j]);
1390 ASSERT(p == num_requests);
1394 bool shall_terminate(void)
1396 switch (conf_term_cond)
1398 case TERM_COND_PENALTY:
1399 return (population1[0]->penalty < conf_penalty_bound);
1400 case TERM_COND_STAGNATION:
1401 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1402 case TERM_COND_ITERATIONS:
1403 return (iteration >= conf_iteration_limit);
1405 // FIXME: Warn the user that no condition is set
1414 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1415 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1416 struct individual **breed_buffer;
1417 while (i < conf_breed_pop_size)
1419 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1420 int parent1 = randint(1, conf_breed_pop_size);
1421 int parent2 = randint(1, conf_breed_pop_size);
1422 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);
1423 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1424 population2[pop2_ind++] = breed_buffer[0];
1425 population2[pop2_ind++] = breed_buffer[1];
1430 acc += conf_breed_rbest_perc;
1432 return; // FIXME: DEBUG HACK
1434 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1435 int step = remaining / conf_pop_size;
1436 for (; i<conf_pop_size; i += 2)
1438 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1439 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1440 population2[pop2_ind++] = breed_buffer[0];
1441 population2[pop2_ind++] = breed_buffer[1];
1444 // FIXME: Could there be one missing individual?
1447 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1449 struct individual **buffer = malloc(2*sizeof(struct individual));
1450 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1451 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1453 printf("Performing crossover\n");
1455 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1457 printf("%dth placement out of %d\n", i, num_requests);
1458 if (! parent1->placements[i].processed)
1460 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1461 int x = randint(1, 2);
1465 copy_symbols(clos_symbols, parent1, child1);
1466 copy_symbols(clos_symbols, parent2, child2);
1470 copy_symbols(clos_symbols, parent2, child1);
1471 copy_symbols(clos_symbols, parent1, child2);
1473 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1474 GARY_FREE(clos_symbols);
1477 if (conf_mutate_children)
1479 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1480 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1492 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1493 while (i < conf_mutate_rbest_perc * conf_pop_size)
1495 int ind = randint(1, conf_mutate_pop_size);
1496 copy_individual(population2[pop2_ind], population1[ind]);
1497 perform_mutation(population2[pop2_ind]);
1502 void perform_mutation(struct individual *individual)
1504 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1506 int x = randint(1, 1000);
1509 if (x <= acc + conf_mutate_move_bound)
1511 move_symbol(&(individual->placements[i]));
1514 acc += conf_mutate_move_bound;
1516 if (x <= acc + conf_mutate_regen_bound)
1518 gen_coords(&(individual->placements[i]));
1521 acc += conf_mutate_regen_bound;
1523 if (x <= acc + conf_mutate_chvar_bound)
1525 if (0) // if num_variants > 1
1527 // FIXME: assign new variant
1535 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1537 population2[pop2_ind++] = population1[0];
1541 void rank_population(void)
1546 void gen_coords(struct placement *p)
1548 switch(p->request->type)
1551 gen_coords_point(p);
1556 case REQUEST_SEGMENT:
1557 gen_coords_segment(p);
1560 printf("Not yet implemented\n");
1563 ASSERT(p->request->type != REQUEST_INVALID);
1567 double gen_movement(void)
1569 double m = (random() % 1000000) / 10000;
1570 m = pow(m, 1.0/3) * flip(1, -1);
1571 printf("Movement %.2f\n", m);
1575 void gen_coords_point(struct placement *p)
1577 p->x = p->x + gen_movement();
1580 void gen_coords_segment(struct placement *p)
1582 struct request_segment *rs = (struct request_segment *) p->request;
1584 p->x = (a == 1 ? rs->x1 : rs->x2);
1585 p->y = (a == 1 ? rs->y1 : rs->y2);
1588 void gen_coords_area(struct placement *p)
1590 struct request_area *ra = (struct request_area *) p->request;
1592 p->x = p->x + gen_movement();
1593 p->y = p->y + gen_movement();
1595 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1598 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1600 struct map_part **buffer;
1601 GARY_INIT(buffer, 0);
1602 int x_min = symbol->x / conf_part_size;
1603 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1604 int y_min = symbol->y / conf_part_size;
1605 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1607 for (int x=x_min; x < x_max; x++)
1608 for (int y=y_min; y < y_max; y++)
1610 struct map_part *m = GARY_PUSH(buffer);
1611 *m = individual->map[x][y];
1617 int randint(int min, int max)
1620 //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)));
1621 return min + (r % (max - min));
1622 return (r * (max - min));
1625 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1627 printf("Getting closure\n");
1628 struct placement **closure;
1629 GARY_INIT(closure, 0);
1630 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1631 chosen[placement->request->ind] = 1;
1633 struct placement **p = GARY_PUSH(closure); *p = placement;
1636 while (first < GARY_SIZE(closure))
1638 printf("Iterating, first is %d\n", first);
1639 struct placement **overlapping = get_overlapping(placement);
1640 filter(overlapping, chosen);
1641 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1643 p = GARY_PUSH(closure); *p = overlapping[j];
1644 chosen[overlapping[j]->request->ind] = 1;
1646 GARY_FREE(overlapping);
1653 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1655 //printf("%d\n", child->penalty);
1656 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1657 for (uns i=0; i<GARY_SIZE(closure); i++)
1659 int ind = closure[i]->request->ind;
1660 child->placements[ind] = parent->placements[ind];
1661 child->placements[ind].processed = 0;
1665 void move_symbol(struct placement *p)
1667 switch (p->request->type)
1670 move_symbol_point(p);
1672 case REQUEST_SEGMENT:
1674 printf("Not yet implemented\n");
1676 ASSERT(p->request->type != REQUEST_INVALID);
1680 void move_symbol_point(struct placement *p)
1682 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1683 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1686 void init_placement(struct placement *p, struct request *r)
1691 p->x = p->y = 0; // To prevent valgrind from complaining
1694 case REQUEST_POINT: ;
1695 struct request_point *rp = (struct request_point *) r;
1699 case REQUEST_LINE: ;
1701 case REQUEST_SECTION: ;
1702 struct request_section *rls = (struct request_section *) r;
1703 p->variant_used = randint(0, rls->num_segments);
1705 case REQUEST_SEGMENT: ;
1706 struct request_segment *rs = (struct request_segment *) r;
1710 case REQUEST_AREA: ;
1711 struct request_area *ra = (struct request_area *) r;
1714 p->variant_used = 0;
1717 ASSERT(p->request->type != REQUEST_INVALID);
1718 printf("Valid request: %d\n", p->request->type);
1723 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1726 void init_individual(struct individual *i)
1728 //printf("Initing individual\n");
1729 GARY_INIT(i->placements, num_requests);
1730 GARY_INIT(i->map, 0);
1731 i->penalty = 0; // FIXME
1734 struct placement **get_overlapping(struct placement *p UNUSED)
1736 struct placement **buffer;
1737 GARY_INIT(buffer, 0);
1741 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1746 int flip(int a, int b)
1748 return (random() % 2 ? a : b);
1751 double randdouble(void)
1753 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1760 GARY_FREE(requests_point);
1761 GARY_FREE(requests_line);
1762 GARY_FREE(requests_area);
1765 void copy_individual(struct individual *src, struct individual *dest)
1767 src->penalty = dest->penalty;
1768 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1769 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1771 dest->placements[i] = src->placements[i];