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 = 10;
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 void make_segments_old(void);
108 int max2(int a, int b);
109 int min2(int a, int b);
110 int max4(int a, int b, int c, int d);
111 int min4(int a, int b, int c, int d);
113 int max2(int a, int b)
115 return (a > b ? a : b);
118 int min2(int a, int b)
120 return (a < b ? a : b);
123 int max4(int a, int b, int c, int d)
125 return max2(max2(a, b), max2(c, d));
128 int min4(int a, int b, int c, int d)
130 return min2(min2(a, b), min2(c, d));
133 void print_label(struct symbol *sym)
137 case SYMBOLIZER_TEXT: ;
138 struct sym_text *st = (struct sym_text *) sym;
139 printf("%s\n", osm_val_decode(st->text));
146 void labeller_init(void)
148 GARY_INIT(requests_point, 0);
149 GARY_INIT(requests_line, 0);
150 GARY_INIT(requests_area, 0);
151 GARY_INIT(buffer_line, 0);
152 GARY_INIT(buffer_linelabel, 0);
153 ep_individuals = ep_new(sizeof(struct individual), 1);
155 page_width_int = floor(page_width);
156 page_height_int = floor(page_height);
159 void make_bitmap_icon(struct point_variant *v, struct sym_icon *si)
161 v->width = si->sir.icon->width;
162 v->height = si->sir.icon->height;
163 v->bitmap = malloc((int) ceil(v->width * v->height * sizeof(bool)));
164 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
167 void make_bitmap_point(struct point_variant *v, struct sym_point *sp)
169 v->width = v->height = sp->size;
170 v->bitmap = malloc(sp->size*sp->size * sizeof(bool));
171 // FIXME: Okay, memset would be much nicer here
172 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
175 void make_bitmap_label(struct point_variant *v, struct sym_text *text)
186 v->width = max4(x_ld, x_lu, x_rd, x_ru) - min4(x_ld, x_lu, x_rd, x_ru);
187 v->height = max4(y_ld, y_lu, y_rd, y_ru) - min4(y_ld, y_lu, y_rd, y_ru);
188 //v->bitmap = malloc((int) (ceil(v->width) * ceil(v->height) * sizeof(bool)));
190 v->width = ceil(text->tw);
191 v->height = ceil(text->th);
192 v->bitmap = malloc(v->width * v->height * sizeof(bool));
193 printf("Allocated bitmap of %d bools for %d x %d label\n", v->width * v->height, v->width, v->height);
194 for (int i=0; i<v->height; i++)
195 for (int j=0; j<v->width; j++)
197 v->bitmap[i*v->width + j] = 1;
198 printf("Writing at %d\n", i*v->width + j);
202 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
204 printf("Adding point\n");
205 if (object->type != OSM_TYPE_NODE)
207 printf("Warning: Point label requested on non-point object\n");
211 struct request_point *r = GARY_PUSH(requests_point);
213 r->request.type = REQUEST_POINT;
214 r->request.ind = num_requests++;
223 GARY_INIT(r->variants, 0);
225 struct point_variant *v = GARY_PUSH(r->variants);
227 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
232 case SYMBOLIZER_ICON:
233 make_bitmap_icon(v, (struct sym_icon *) sym);
234 r->x = ((struct sym_icon *)sym)->sir.x;
235 r->y = ((struct sym_icon *)sym)->sir.y;
237 case SYMBOLIZER_POINT:
238 make_bitmap_point(v, (struct sym_point *) sym);
240 case SYMBOLIZER_TEXT: ;
241 struct sym_text *st = (struct sym_text *) sym;
242 struct osm_node *n = (struct osm_node *) object;
243 make_bitmap_label(v, st);
249 printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
252 void labeller_add_line(struct symbol *sym, z_index_t zindex)
254 printf("Adding line on %u\n", zindex);
255 struct buffer_line *b = GARY_PUSH(buffer_line);
256 b->line = (struct sym_line *) sym;
258 sym_plan(sym, zindex);
261 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
263 if (o->type != OSM_TYPE_WAY)
269 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
270 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
271 ll->way = (struct osm_way *) o;
276 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
278 printf("Adding area on %u\n", zindex);
279 struct request_area *r = GARY_PUSH(requests_area);
281 r->request.type = REQUEST_AREA;
282 r->request.ind = num_requests++;
284 r->o = (struct osm_multipolygon *) o;
288 osm_obj_center(o, &(r->cx), &(r->cy));
290 GARY_INIT(r->variants, 0);
291 struct point_variant *v = GARY_PUSH(r->variants);
294 case SYMBOLIZER_ICON:
295 printf("DEBUG: Icon label\n");
296 make_bitmap_icon(v, (struct sym_icon *) sym);
298 case SYMBOLIZER_TEXT:
299 printf("DEBUG: Text label\n");
300 make_bitmap_label(v, (struct sym_text *) sym);
307 void make_graph(void)
310 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
312 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
313 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
315 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
316 struct graph_node *g_prev = NULL;
317 struct osm_node *o_prev = NULL;
319 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
321 // FIXME: Shall osm_object's type be checked here?
322 struct osm_node *o_node = (struct osm_node *) ref->o;
324 struct graph_node *g_node = hash_find(ref->o->id);
327 g_node = hash_new(ref->o->id);
328 GARY_INIT(g_node->edges, 0);
330 g_node->id = ref->o->id;
331 g_node->num = num_nodes++;
341 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
342 e->num = num_edges++;
343 e->id = buffer_line[i].line->s.o->id;
344 e->color = buffer_line[i].line->color;
345 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
351 e->longline = (uns) -1;
352 e->line = buffer_line[i].line;
356 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
358 edge = GARY_PUSH(g_node->edges);
366 printf("Made graph with %d edges\n", num_edges_dbg);
369 void dump_graph(void)
371 HASH_FOR_ALL(hash, node)
373 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
374 for (uns i=0; i<GARY_SIZE(node->edges); i++)
376 struct graph_edge *e = node->edges[i];
377 printf("\t edge (%d) #%ju to ", e->num, e->id);
378 if (node->edges[i]->n1->id == node->id)
379 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
380 else if (node->edges[i]->n2->id == node->id)
381 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
383 printf("BEWARE! BEWARE! BEWARE!\n");
386 if ((node->edges[i]->label)) printf("Labelled\n");
387 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));
388 printf(" colored %d;", node->edges[i]->color);
389 printf(" length %.2f", node->edges[i]->length);
396 void label_graph(void)
398 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
399 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
401 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
402 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
403 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
405 printf("Looking for node %ju\n", ref->o->id);
406 struct graph_node *n = hash_find(ref->o->id);
409 // FIXME: What shall be done?
413 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
414 for (uns j=0; j<GARY_SIZE(n->edges); j++)
416 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
418 printf("Labelling node %ju\n", n->id);
419 n->edges[j]->label = buffer_linelabel[i].label;
420 n->edges[j]->zindex = buffer_linelabel[i].zindex;
428 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
430 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
431 struct graph_edge *candidate = NULL;
433 for (uns i=0; i<GARY_SIZE(node->edges); i++)
435 struct graph_edge *other = node->edges[i];
436 if (e->num == 987) printf("Got label %d\n", e->num);
437 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
438 if (e->num == 987) printf("Continuing with edge %d\n", e->num);
440 printf("Testing %d ?= %d\n", other->visited, e->longline);
441 if ((uns) other->visited != e->longline) {
442 printf("Pushing new edge %d / %ju\n", other->num, other->id);
443 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
445 other->visited = e->longline;
448 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
449 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
452 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
453 (e->label) && (other->label) &&
454 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
455 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
457 if (! candidate || (other->length > candidate->length))
464 printf("New line in longline %u\n", e->longline);
465 struct graph_edge *other = candidate;
466 other->longline = e->longline;
468 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
469 ((dir == DIR_FWD) && (other->n2->id == node->id)))
471 struct graph_node *swp = other->n2;
472 other->n2 = other->n1;
481 longlines[other->longline].first = other;
494 void bfs(uns longline)
496 printf("BFS called for longline %u\n", longline);
497 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
498 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
500 struct graph_edge *cur = bfs_queue[i];
501 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
502 //ASSERT(! cur->visited);
504 cur->visited = longline;
506 if (cur->longline == (uns) -1)
509 if (cur->dir == DIR_UNSET)
511 cur->dir = DIR_CENTER;
512 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
513 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
520 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
523 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
533 void bfs_wrapper(void)
535 GARY_INIT(bfs_queue, 0);
536 GARY_INIT(longlines, 0);
538 HASH_FOR_ALL(hash, node)
540 for (uns i=0; i<GARY_SIZE(node->edges); i++)
542 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
544 GARY_PUSH(longlines);
545 longlines[num_longlines].first = node->edges[i];
546 printf("Running new BFS\n");
547 printf("Creating longline %u\n", num_longlines);
548 GARY_RESIZE(bfs_queue, 0);
549 struct graph_edge **e = GARY_PUSH(bfs_queue);
551 node->edges[i]->longline = num_longlines;
552 bfs(node->edges[i]->longline);
554 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
555 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
562 GARY_FREE(bfs_queue);
565 void dump_longlines(void)
567 printf("*** Longlines dump\n");
568 for (uns i=0; i<GARY_SIZE(longlines); i++)
570 printf("Longline %u:", i);
571 struct graph_edge *e = longlines[i].first;
572 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
573 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
578 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
579 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
586 void make_segments(void)
588 for (uns i=0; i<GARY_SIZE(longlines); i++)
590 // Skip lines which are not labelled
591 if (! (longlines[i].first && longlines[i].first->label))
594 struct request_line *request = GARY_PUSH(requests_line);
595 request->request.ind = num_requests++;
596 request->request.type = REQUEST_LINE;
598 GARY_INIT(request->sections, 0);
601 struct request_section *rls = GARY_PUSH(request->sections);
602 rls->request.ind = num_requests++;
603 rls->request.type = REQUEST_SECTION;
604 rls->num_segments = 0;
605 GARY_INIT(rls->segments, 0);
607 struct graph_edge *e = longlines[i].first;
608 struct request_segment *rs = NULL;
610 struct sym_text *st = NULL;
611 if (e->label->type == SYMBOLIZER_TEXT)
613 st = (struct sym_text *) e->label;
623 if ((cur_length + e->length > conf_max_section_length) &&
624 !(cur_length + e->length < conf_max_section_overlay))
626 struct osm_node *n = e->n1->o;
628 rs = GARY_PUSH(rls->segments);
629 rs->request.type = REQUEST_SEGMENT;
630 rs->request.ind = num_requests++;
634 // FIXME: Truly compute x2, y2
637 rs->zindex = e->zindex;
638 rs->label = e->label;
640 rls = GARY_PUSH(request->sections);
641 rls->request.ind = num_requests++;
642 rls->request.type = REQUEST_SECTION;
643 rls->num_segments = 0;
645 struct point_variant *v = malloc(sizeof(struct point_variant));
646 switch (e->label->type)
648 case SYMBOLIZER_POINT:
649 make_bitmap_point(v, (struct sym_point *) e->label);
651 case SYMBOLIZER_ICON:
652 make_bitmap_icon(v, (struct sym_icon *) e->label);
654 case SYMBOLIZER_TEXT:
655 make_bitmap_label(v, (struct sym_text *) e->label);
662 GARY_INIT(rls->segments, 0);
665 if (e->length < st->tw)
671 rs = GARY_PUSH(rls->segments);
673 rs->request.type = REQUEST_SEGMENT;
674 rs->request.ind = num_requests++;
675 rs->label = e->label;
677 struct point_variant *v = malloc(sizeof(struct point_variant));
678 switch (e->label->type)
680 case SYMBOLIZER_POINT:
681 make_bitmap_point(v, (struct sym_point *) e->label);
683 case SYMBOLIZER_ICON:
684 make_bitmap_icon(v, (struct sym_icon *) e->label);
686 case SYMBOLIZER_TEXT:
687 make_bitmap_label(v, (struct sym_text *) e->label);
695 rs->x1 = e->n1->o->x;
696 rs->y1 = e->n1->o->y;
697 rs->x2 = e->n2->o->x;
698 rs->y2 = e->n2->o->y;
700 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
702 rs->zindex = e->zindex;
704 cur_length += e->length;
712 * void make_segments_old(void)
714 * printf("Analysing %u longlines\n", GARY_SIZE(longlines));
715 * for (uns i=0; i<GARY_SIZE(longlines); i++)
717 * if (! longlines[i].first->label) continue;
718 * // printf("Survived! %s\n", osm_val_decode(longlines[i].first->text->text));
719 * printf("New longline\n");
721 * if (longlines[i].first->label->type != SYMBOLIZER_TEXT)
726 * struct request_line *request = GARY_PUSH(requests_line);
727 * request->request.ind = -1;
728 * request->request.type = REQUEST_LINE;
730 * GARY_INIT(request->segments, 0);
731 * request->num_segments = 0;
733 * // ->num_variants FIXME
734 * // ->variants FIXME
736 * struct graph_edge *e = longlines[i].first;
738 * if (! e) printf("Oops\n");
742 * if (! e->label) break;
744 * struct request_segment *r = GARY_PUSH(request->segments);
745 * request->num_segments++;
747 * r->request.ind = num_requests++;
748 * r->request.type = REQUEST_SEGMENT;
750 * struct osm_node *n = e->n1->o;
757 * printf("Segment [%.2f; %.2f] -- [%.2f; %.2f]\n", r->x1, r->y1, r->x2, r->y2);
759 * r->zindex = e->zindex;
760 * r->variant = malloc(sizeof(struct point_variant)); // FIXME
761 * switch (e->label->type)
763 * case SYMBOLIZER_TEXT: ;
764 * struct sym_text *st = malloc(sizeof(struct sym_text));
765 * ((struct symbol *) st)->o = e->label->o;
766 * ((struct symbol *) st)->type = SYMBOLIZER_TEXT;
769 * st->text = ((struct sym_text *) e->label)->text;
770 * st->text_color = ((struct sym_text *) e->label)->text_color;
771 * st->font = ((struct sym_text *) e->label)->font;
772 * st->opacity = ((struct sym_text *) e->label)->opacity;
773 * st->halo_color = ((struct sym_text *) e->label)->halo_color;
774 * st->halo_radius = ((struct sym_text *) e->label)->halo_radius;
775 * st->halo_opacity = ((struct sym_text *) e->label)->halo_opacity;
776 * st->tw = ((struct sym_text *) e->label)->tw;
777 * st->th = ((struct sym_text *) e->label)->th;
778 * st->td = ((struct sym_text *) e->label)->td;
779 * r->label = (struct symbol *) st;
780 * // FIXME: This shall be done in more elegant way
781 * make_bitmap_label(r->variant, (struct sym_text *) e->label);
785 * printf("Got here?\n");
796 * void dump_linelabel_requests(void)
798 * for (uns i=0; i<GARY_SIZE(requests_line); i++)
800 * print_label(requests_line[i].segments[0].label);
805 void dump_bitmaps(struct individual *individual)
807 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
808 printf("Bitmap size is %d\n", page_width_int * page_height_int);
809 for (int i=0; i<page_height_int; i++)
810 for (int j=0; j<page_width_int; j++)
811 bitmap[i*page_width_int + j] = 0;
813 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
815 fprintf(stderr, "%d-th placement\n", i);
816 struct placement *p = &(individual->placements[i]);
817 struct point_variant *v = NULL;
819 switch (p->request->type)
821 case REQUEST_SEGMENT: ;
822 struct request_segment *rs = (struct request_segment *) p->request;
825 case REQUEST_POINT: ;
826 struct request_point *rp = (struct request_point *) p->request;
827 v = &(rp->variants[p->variant_used]);
830 struct request_area *ra = (struct request_area *) p->request;
831 printf("Using %d-th of %d variants\n", p->variant_used, GARY_SIZE(ra->variants));
832 v = &(ra->variants[p->variant_used]);
835 printf("Testing request type (dump_bitmaps): %d\n", p->request->type);
836 ASSERT(p->request->type != REQUEST_INVALID);
840 printf("Got after with %d-th placement of request type %d\n", i, p->request->type);
842 printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
843 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
845 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
847 printf("Writing to %d\n", row*page_width_int + col);
848 bitmap[row * page_width_int + col] = 1;
854 FILE *fd_dump = fopen("dump.pbm", "w");
855 fprintf(fd_dump, "P1\n");
856 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
857 for (int i=0; i<page_height_int; i++)
859 for (int j=0; j<page_width_int; j++)
861 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
863 fprintf(fd_dump, "\n");
868 void dump_individual(struct individual *individual)
870 printf("(There are %d requests)\n", num_requests);
871 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
873 struct placement *p = &(individual->placements[i]);
875 switch (p->request->type)
878 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
881 struct request_line *rl = (struct request_line *) p->request;
883 print_label(rl->sections[0].segments[0].label);
885 case REQUEST_SECTION: ;
888 case REQUEST_SEGMENT: ;
889 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
892 struct request_area *ra = (struct request_area *) p->request;
893 printf("Area label ");
894 print_label(ra->label);
895 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
898 printf("Testing request type (dump_individual)\n");
899 ASSERT(p->request->type != 0);
902 printf("\nTotal penalty: %d\n", individual->penalty);
905 void labeller_label(void)
913 //dump_linelabel_requests();
915 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));
917 GARY_INIT(population1, conf_pop_size);
918 GARY_INIT(population2, conf_pop_size);
921 printf("Dealing with %d requests\n", num_requests);
924 while (! shall_terminate())
928 struct individual **swp = population1;
929 population1 = population2;
935 dump_individual(population1[0]);
936 dump_bitmaps(population1[0]);
938 for (uns i=0; i<GARY_SIZE(population1[0]->placements); i++)
940 printf("(%d) Coping with %d\n", i, population1[0]->placements[i].request->type);
942 struct symbol *s = NULL;
943 z_index_t zindex = 0;
944 switch (population1[0]->placements[i].request->type)
946 case REQUEST_POINT: ;
947 struct request_point *rp = (struct request_point *) population1[0]->placements[i].request;
950 printf("%u\n", zindex);
952 case REQUEST_SEGMENT: ;
953 struct request_segment *rs = (struct request_segment *) population1[0]->placements[i].request;
955 printf("Assigned label to s\n");
957 printf("%u\n", zindex);
960 struct request_area *ra = (struct request_area *) population1[0]->placements[i].request;
963 printf("%u\n", zindex);
966 printf("Testing request type (flushing final placements)\n");
967 ASSERT(population1[0]->placements[i].request != REQUEST_INVALID);
968 printf("Yep, in default, continuing\n");
972 printf("Will plan symbol on %u\n", zindex);
976 case SYMBOLIZER_POINT: ;
977 struct sym_point *sp = (struct sym_point *) s;
978 sp->x = population1[0]->placements[i].x;
979 sp->y = population1[0]->placements[i].y;
980 sym_plan((struct symbol *) sp, zindex);
982 case SYMBOLIZER_ICON: ;
983 struct sym_icon *si = (struct sym_icon *) s;
984 si->sir.x = population1[0]->placements[i].x;
985 si->sir.y = population1[0]->placements[i].y;
986 sym_plan((struct symbol *) si, zindex);
988 case SYMBOLIZER_TEXT: ;
989 struct sym_text *st = (struct sym_text *) s;
990 st->x = population1[0]->placements[i].x;
991 st->y = population1[0]->placements[i].y;
992 st->next_duplicate = NULL;
993 sym_plan((struct symbol *) st, zindex);
996 printf("Testing symbolizer type\n");
997 ASSERT(s->type != SYMBOLIZER_INVALID);
1003 * switch (population1[0]->placements[i].request->type)
1005 * case REQUEST_POINT: ;
1006 * struct request_point *rp = (struct request_point *) population1[0]->placements[i].request;
1007 * switch (rp->sym->type)
1009 * case SYMBOLIZER_POINT: ;
1010 * printf("Moving point to final destination\n");
1011 * struct sym_point *sp = (struct sym_point *) rp->sym;
1012 * sp->x = population1[0]->placements[i].x;
1013 * sp->y = population1[0]->placements[i].y;
1014 * sym_plan((struct symbol *) sp, rp->zindex);
1016 * case SYMBOLIZER_ICON: ;
1017 * printf("Moving icon to final destination\n");
1018 * struct sym_icon *si = (struct sym_icon *) rp->sym;
1019 * si->sir.x = population1[0]->placements[i].x;
1020 * si->sir.y = population1[0]->placements[i].y;
1021 * sym_plan((struct symbol *) si, rp->zindex);
1024 * printf("Haúúú 1\n");
1028 * case REQUEST_AREA: ;
1029 * struct request_area *ra = (struct request_area *) population1[0]->placements[i].request;
1030 * switch (ra->label->type)
1032 * case SYMBOLIZER_ICON
1033 * case SYMBOLIZER_TEXT: ;
1034 * struct sym_text *st = (struct sym_text *) ra->label;
1035 * st->x = population1[0]->placements[i].x;
1036 * st->y = population1[0]->placements[i].y;
1042 * if (ra->label->type == SYMBOLIZER_INVALID) printf("Haúúú 2\n");
1043 * sym_plan((struct symbol *) ra->label, ra->zindex);
1046 * case REQUEST_LINE: ;
1047 * struct request_line *rl = (struct request_line *) population1[0]->placements[i].request;
1048 * for (uns j=0; j<GARY_SIZE(rl->segments); j++)
1050 * switch (rl->segments[j].label->type)
1052 * case SYMBOLIZER_TEXT:
1053 * ((struct sym_text *) rl->segments[j].label)->next_duplicate = NULL;
1054 * ((struct sym_text *) rl->segments[j].label)->next_in_tile = NULL;
1055 * printf("Planning text ");
1056 * print_label(rl->segments[j].label);
1058 * printf("Haúúú 3\n");
1062 * sym_plan((struct symbol *) rl->segments[j].label, rl->segments[j].zindex); // FIXME: z-index
1066 * case REQUEST_SEGMENT: ;
1067 * //struct request_segment *rs = (struct request_segment *) population1[0]->placements[i].request;
1068 * printf("Segment!\n");
1071 * ASSERT(population1[0]->placements[i].request->type != REQUEST_INVALID);
1079 while (! shall_terminate())
1087 // sort population by fitness
1089 struct individual **swp = population1;
1090 population1 = population2;
1091 printf("Swapped populations\n");
1093 // GARY_RESIZE(population2, 0) -- is it needed?
1098 void make_population(void)
1100 for (int i=0; i<conf_pop_size; i++)
1102 printf("Making individual %d\n", i);
1103 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1104 population1[i] = individual;
1107 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1109 init_placement(&(individual->placements[p++]), (struct request *) &requests_point[j]);
1112 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1114 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j]);
1116 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1118 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k]);
1120 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1122 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k].segments[l]);
1127 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1129 init_placement(&(individual->placements[p++]), (struct request *) &requests_area[j]);
1132 printf("Testing p\n");
1133 ASSERT(p == num_requests);
1137 bool shall_terminate(void)
1139 switch (conf_term_cond)
1141 case TERM_COND_PENALTY:
1142 return (population1[0]->penalty < conf_penalty_bound);
1143 case TERM_COND_STAGNATION:
1144 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1145 case TERM_COND_ITERATIONS:
1146 return (iteration >= conf_iteration_limit);
1148 // FIXME: Warn the user that no condition is set
1157 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1158 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1159 struct individual **breed_buffer;
1160 while (i < conf_breed_pop_size)
1162 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1163 int parent1 = randint(1, conf_breed_pop_size);
1164 int parent2 = randint(1, conf_breed_pop_size);
1165 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);
1166 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1167 population2[pop2_ind++] = breed_buffer[0];
1168 population2[pop2_ind++] = breed_buffer[1];
1173 acc += conf_breed_rbest_perc;
1175 return; // FIXME: DEBUG HACK
1177 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1178 int step = remaining / conf_pop_size;
1179 for (; i<conf_pop_size; i += 2)
1181 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1182 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1183 population2[pop2_ind++] = breed_buffer[0];
1184 population2[pop2_ind++] = breed_buffer[1];
1187 // FIXME: Could there be one missing individual?
1190 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1192 struct individual **buffer = malloc(2*sizeof(struct individual));
1193 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1194 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1196 printf("Performing crossover\n");
1198 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1200 printf("%dth placement out of %d\n", i, num_requests);
1201 if (! parent1->placements[i].processed)
1203 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1204 int x = randint(1, 2);
1208 copy_symbols(clos_symbols, parent1, child1);
1209 copy_symbols(clos_symbols, parent2, child2);
1213 copy_symbols(clos_symbols, parent2, child1);
1214 copy_symbols(clos_symbols, parent1, child2);
1216 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1217 GARY_FREE(clos_symbols);
1220 if (conf_mutate_children)
1222 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1223 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1235 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1236 while (i < conf_mutate_rbest_perc * conf_pop_size)
1238 int ind = randint(1, conf_mutate_pop_size);
1239 copy_individual(population2[pop2_ind], population1[ind]);
1240 perform_mutation(population2[pop2_ind]);
1245 void perform_mutation(struct individual *individual)
1247 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1249 int x = randint(1, 1000);
1252 if (x <= acc + conf_mutate_move_bound)
1254 move_symbol(&(individual->placements[i]));
1257 acc += conf_mutate_move_bound;
1259 if (x <= acc + conf_mutate_regen_bound)
1261 gen_coords(&(individual->placements[i]));
1264 acc += conf_mutate_regen_bound;
1266 if (x <= acc + conf_mutate_chvar_bound)
1268 if (0) // if num_variants > 1
1270 // FIXME: assign new variant
1278 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1280 population2[pop2_ind++] = population1[0];
1284 void rank_population(void)
1289 void gen_coords(struct placement *p)
1291 switch(p->request->type)
1294 gen_coords_point(p);
1299 case REQUEST_SEGMENT:
1300 gen_coords_segment(p);
1303 printf("Not yet implemented\n");
1306 printf("Testing request type\n");
1307 ASSERT(p->request->type != REQUEST_INVALID);
1311 double gen_movement(void)
1313 double m = (random() % 1000000) / 10000;
1314 m = pow(m, 1.0/3) * flip(1, -1);
1315 printf("Movement %.2f\n", m);
1319 void gen_coords_point(struct placement *p)
1321 p->x = p->x + gen_movement();
1324 void gen_coords_segment(struct placement *p)
1326 struct request_segment *rs = (struct request_segment *) p->request;
1328 p->x = (a == 1 ? rs->x1 : rs->x2);
1329 p->y = (a == 1 ? rs->y1 : rs->y2);
1332 void gen_coords_area(struct placement *p)
1334 struct request_area *ra = (struct request_area *) p->request;
1336 p->x = p->x + gen_movement();
1337 p->y = p->y + gen_movement();
1339 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1342 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1344 struct map_part **buffer;
1345 GARY_INIT(buffer, 0);
1346 int x_min = symbol->x / conf_part_size;
1347 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1348 int y_min = symbol->y / conf_part_size;
1349 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1351 for (int x=x_min; x < x_max; x++)
1352 for (int y=y_min; y < y_max; y++)
1354 struct map_part *m = GARY_PUSH(buffer);
1355 *m = individual->map[x][y];
1361 int randint(int min, int max)
1363 if (min == max) return min;
1365 //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)));
1366 return min + (r % (max - min));
1367 return (r * (max - min));
1370 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1372 printf("Getting closure\n");
1373 struct placement **closure;
1374 GARY_INIT(closure, 0);
1375 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1376 chosen[placement->request->ind] = 1;
1378 struct placement **p = GARY_PUSH(closure); *p = placement;
1381 while (first < GARY_SIZE(closure))
1383 printf("Iterating, first is %d\n", first);
1384 struct placement **overlapping = get_overlapping(placement);
1385 filter(overlapping, chosen);
1386 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1388 p = GARY_PUSH(closure); *p = overlapping[j];
1389 chosen[overlapping[j]->request->ind] = 1;
1391 GARY_FREE(overlapping);
1398 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1400 //printf("%d\n", child->penalty);
1401 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1402 for (uns i=0; i<GARY_SIZE(closure); i++)
1404 int ind = closure[i]->request->ind;
1405 child->placements[ind] = parent->placements[ind];
1406 child->placements[ind].processed = 0;
1410 void move_symbol(struct placement *p)
1412 switch (p->request->type)
1415 move_symbol_point(p);
1417 case REQUEST_SEGMENT:
1419 printf("Not yet implemented\n");
1421 ASSERT(p->request->type != REQUEST_INVALID);
1425 void move_symbol_point(struct placement *p)
1427 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1428 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1431 void init_placement(struct placement *p, struct request *r)
1436 p->x = p->y = 0; // To prevent valgrind from complaining
1439 case REQUEST_POINT: ;
1440 struct request_point *rp = (struct request_point *) r;
1444 case REQUEST_LINE: ;
1446 case REQUEST_SECTION: ;
1447 struct request_section *rls = (struct request_section *) r;
1448 p->variant_used = randint(0, rls->num_segments);
1450 case REQUEST_SEGMENT: ;
1451 struct request_segment *rs = (struct request_segment *) r;
1455 case REQUEST_AREA: ;
1456 struct request_area *ra = (struct request_area *) r;
1459 p->variant_used = 0;
1462 ASSERT(p->request->type != REQUEST_INVALID);
1463 printf("Valid request: %d\n", p->request->type);
1467 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1470 void init_individual(struct individual *i)
1472 //printf("Initing individual\n");
1473 GARY_INIT(i->placements, num_requests);
1474 GARY_INIT(i->map, 0);
1475 i->penalty = 0; // FIXME
1478 struct placement **get_overlapping(struct placement *p UNUSED)
1480 struct placement **buffer;
1481 GARY_INIT(buffer, 0);
1485 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1490 int flip(int a, int b)
1492 return (random() % 2 ? a : b);
1495 double randdouble(void)
1497 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1504 GARY_FREE(requests_point);
1505 GARY_FREE(requests_line);
1506 GARY_FREE(requests_area);
1509 void copy_individual(struct individual *src, struct individual *dest)
1511 src->penalty = dest->penalty;
1512 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1513 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1515 dest->placements[i] = src->placements[i];