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 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 printf("New longline\n");
596 struct request_line *request = GARY_PUSH(requests_line);
597 request->request.ind = num_requests++;
598 request->request.type = REQUEST_LINE;
600 GARY_INIT(request->sections, 0);
603 struct request_section *rls = GARY_PUSH(request->sections);
604 rls->request.ind = num_requests++;
605 rls->request.type = REQUEST_SECTION;
606 rls->num_segments = 0;
607 GARY_INIT(rls->segments, 0);
609 struct graph_edge *e = longlines[i].first;
610 struct request_segment *rs = NULL;
612 struct sym_text *st = NULL;
613 if (e->label->type == SYMBOLIZER_TEXT)
615 st = (struct sym_text *) e->label;
619 printf("Warning: Skipping line\n");
627 printf("Edge %d\n", dbg_e);
629 if (e < (void *) 100)
633 if ((cur_length + e->length > conf_max_section_length) &&
634 !(cur_length + e->length < conf_max_section_overlay))
636 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);
637 struct osm_node *n = e->n1->o;
639 rs = GARY_PUSH(rls->segments);
640 rs->request.type = REQUEST_SEGMENT;
641 rs->request.ind = num_requests++;
645 // FIXME: Truly compute x2, y2
648 rs->zindex = e->zindex;
649 rs->label = e->label;
651 rls = GARY_PUSH(request->sections);
652 rls->request.ind = num_requests++;
653 rls->request.type = REQUEST_SECTION;
654 rls->num_segments = 0;
656 struct point_variant *v = malloc(sizeof(struct point_variant));
657 switch (e->label->type)
659 case SYMBOLIZER_POINT:
660 make_bitmap_point(v, (struct sym_point *) e->label);
662 case SYMBOLIZER_ICON:
663 make_bitmap_icon(v, (struct sym_icon *) e->label);
665 case SYMBOLIZER_TEXT:
666 make_bitmap_label(v, (struct sym_text *) e->label);
673 GARY_INIT(rls->segments, 0);
676 if (st && (e->length < st->tw))
679 printf("Warning: Skipping segment\n");
683 rs = GARY_PUSH(rls->segments);
685 rs->request.type = REQUEST_SEGMENT;
686 rs->request.ind = num_requests++;
687 rs->label = e->label;
689 struct point_variant *v = malloc(sizeof(struct point_variant));
690 switch (e->label->type)
692 case SYMBOLIZER_POINT:
693 make_bitmap_point(v, (struct sym_point *) e->label);
695 case SYMBOLIZER_ICON:
696 make_bitmap_icon(v, (struct sym_icon *) e->label);
698 case SYMBOLIZER_TEXT:
699 make_bitmap_label(v, (struct sym_text *) e->label);
707 rs->x1 = e->n1->o->x;
708 rs->y1 = e->n1->o->y;
709 rs->x2 = e->n2->o->x;
710 rs->y2 = e->n2->o->y;
712 rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
714 rs->zindex = e->zindex;
716 cur_length += e->length;
721 if (request->sections[0].num_segments == 0)
724 printf("WARNING: 0 segment section\n");
725 GARY_POP(requests_line);
731 void dump_linelabel_requests(void)
733 for (uns i=0; i<GARY_SIZE(requests_line); i++)
735 if (requests_line[i].sections[0].num_segments == 0)
740 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
741 print_label(requests_line[i].sections[0].segments[0].label);
742 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
744 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
745 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
747 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
748 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
755 void dump_bitmaps(struct individual *individual)
757 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
758 printf("Bitmap size is %d\n", page_width_int * page_height_int);
759 for (int i=0; i<page_height_int; i++)
760 for (int j=0; j<page_width_int; j++)
761 bitmap[i*page_width_int + j] = 0;
763 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
765 fprintf(stderr, "%d-th placement\n", i);
766 struct placement *p = &(individual->placements[i]);
767 struct point_variant *v = NULL;
769 switch (p->request->type)
771 case REQUEST_SEGMENT: ;
772 struct request_segment *rs = (struct request_segment *) p->request;
775 case REQUEST_POINT: ;
776 struct request_point *rp = (struct request_point *) p->request;
777 v = &(rp->variants[p->variant_used]);
780 struct request_area *ra = (struct request_area *) p->request;
781 printf("Using %d-th of %d variants\n", p->variant_used, GARY_SIZE(ra->variants));
782 v = &(ra->variants[p->variant_used]);
785 printf("Testing request type (dump_bitmaps): %d\n", p->request->type);
786 ASSERT(p->request->type != REQUEST_INVALID);
790 printf("Got after with %d-th placement of request type %d\n", i, p->request->type);
792 printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
793 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
795 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
797 printf("Writing to %d\n", row*page_width_int + col);
798 bitmap[row * page_width_int + col] = 1;
804 FILE *fd_dump = fopen("dump.pbm", "w");
805 fprintf(fd_dump, "P1\n");
806 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
807 for (int i=0; i<page_height_int; i++)
809 for (int j=0; j<page_width_int; j++)
811 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
813 fprintf(fd_dump, "\n");
818 void dump_individual(struct individual *individual)
820 printf("*** Dumping INDIVIDUAL ***\n");
821 printf("(There are %d requests)\n", num_requests);
822 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
824 struct placement *p = &(individual->placements[i]);
826 switch (p->request->type)
829 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
832 struct request_line *rl = (struct request_line *) p->request;
834 print_label(rl->sections[0].segments[0].label);
836 case REQUEST_SECTION: ;
839 case REQUEST_SEGMENT: ;
840 if (p->variant_used >= 0)
841 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
843 printf("Segment not placed\n");
846 struct request_area *ra = (struct request_area *) p->request;
847 printf("Area label ");
848 print_label(ra->label);
849 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
852 printf("Testing request type (dump_individual)\n");
853 ASSERT(p->request->type != 0);
856 printf("\nTotal penalty: %d\n", individual->penalty);
859 void labeller_label(void)
867 dump_linelabel_requests();
869 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));
871 GARY_INIT(population1, conf_pop_size);
872 GARY_INIT(population2, conf_pop_size);
875 printf("Dealing with %d requests\n", num_requests);
878 while (! shall_terminate())
882 struct individual **swp = population1;
883 population1 = population2;
889 dump_individual(population1[0]);
890 //dump_bitmaps(population1[0]);
892 for (uns i=0; i<GARY_SIZE(population1[0]->placements); i++)
894 //printf("(%d) Coping with %d\n", i, population1[0]->placements[i].request->type);
896 struct symbol *s = NULL;
897 z_index_t zindex = 0;
898 switch (population1[0]->placements[i].request->type)
900 case REQUEST_POINT: ;
901 struct request_point *rp = (struct request_point *) population1[0]->placements[i].request;
904 // printf("%u\n", zindex);
906 case REQUEST_SEGMENT: ;
907 struct request_segment *rs = (struct request_segment *) population1[0]->placements[i].request;
909 // printf("Assigned label to s\n");
912 // printf("%u\n", zindex);
915 // printf("*** Line detected ***\n");
918 struct request_area *ra = (struct request_area *) population1[0]->placements[i].request;
921 // printf("%u\n", zindex);
924 // printf("Testing request type (flushing final placements)\n");
925 ASSERT(population1[0]->placements[i].request != REQUEST_INVALID);
926 // printf("Yep, in default, continuing\n");
930 printf("Will plan symbol at [%.2f; %.2f] on %u\n", population1[0]->placements[i].x, population1[0]->placements[i].y, zindex);
932 if (s) switch (s->type)
934 case SYMBOLIZER_POINT: ;
935 struct sym_point *sp = (struct sym_point *) s;
936 sp->x = population1[0]->placements[i].x;
937 sp->y = population1[0]->placements[i].y;
938 sym_plan((struct symbol *) sp, zindex);
940 case SYMBOLIZER_ICON: ;
941 struct sym_icon *si = (struct sym_icon *) s;
942 si->sir.x = population1[0]->placements[i].x;
943 si->sir.y = population1[0]->placements[i].y;
944 sym_plan((struct symbol *) si, zindex);
946 case SYMBOLIZER_TEXT: ;
947 struct sym_text *st = (struct sym_text *) s;
948 st->x = population1[0]->placements[i].x;
949 st->y = population1[0]->placements[i].y;
950 st->next_duplicate = NULL;
951 printf("Planning text %s at [%.2f; %.2f] on %u\n", osm_val_decode(st->text), st->x, st->y, zindex);
952 sym_plan((struct symbol *) st, zindex);
955 // printf("Testing symbolizer type\n");
956 ASSERT(s->type != SYMBOLIZER_INVALID);
963 void make_population(void)
965 for (int i=0; i<conf_pop_size; i++)
967 printf("Making individual %d\n", i);
968 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
969 population1[i] = individual;
972 for (uns j=0; j<GARY_SIZE(requests_point); j++)
974 init_placement(&(individual->placements[p++]), (struct request *) &requests_point[j]);
977 for (uns j=0; j<GARY_SIZE(requests_line); j++)
979 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j]);
981 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
983 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k]);
985 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
987 init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k].segments[l]);
992 for (uns j=0; j<GARY_SIZE(requests_area); j++)
994 init_placement(&(individual->placements[p++]), (struct request *) &requests_area[j]);
997 if (p != num_requests)
1003 printf("Testing p\n");
1004 ASSERT(p == num_requests);
1008 bool shall_terminate(void)
1010 switch (conf_term_cond)
1012 case TERM_COND_PENALTY:
1013 return (population1[0]->penalty < conf_penalty_bound);
1014 case TERM_COND_STAGNATION:
1015 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1016 case TERM_COND_ITERATIONS:
1017 return (iteration >= conf_iteration_limit);
1019 // FIXME: Warn the user that no condition is set
1028 printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1029 int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1030 struct individual **breed_buffer;
1031 while (i < conf_breed_pop_size)
1033 printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1034 int parent1 = randint(1, conf_breed_pop_size);
1035 int parent2 = randint(1, conf_breed_pop_size);
1036 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);
1037 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1038 population2[pop2_ind++] = breed_buffer[0];
1039 population2[pop2_ind++] = breed_buffer[1];
1044 acc += conf_breed_rbest_perc;
1046 return; // FIXME: DEBUG HACK
1048 int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1049 int step = remaining / conf_pop_size;
1050 for (; i<conf_pop_size; i += 2)
1052 printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1053 breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1054 population2[pop2_ind++] = breed_buffer[0];
1055 population2[pop2_ind++] = breed_buffer[1];
1058 // FIXME: Could there be one missing individual?
1061 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1063 struct individual **buffer = malloc(2*sizeof(struct individual));
1064 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1065 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1067 printf("Performing crossover\n");
1069 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1071 printf("%dth placement out of %d\n", i, num_requests);
1072 if (! parent1->placements[i].processed)
1074 struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1075 int x = randint(1, 2);
1079 copy_symbols(clos_symbols, parent1, child1);
1080 copy_symbols(clos_symbols, parent2, child2);
1084 copy_symbols(clos_symbols, parent2, child1);
1085 copy_symbols(clos_symbols, parent1, child2);
1087 printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1088 GARY_FREE(clos_symbols);
1091 if (conf_mutate_children)
1093 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1094 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1106 int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1107 while (i < conf_mutate_rbest_perc * conf_pop_size)
1109 int ind = randint(1, conf_mutate_pop_size);
1110 copy_individual(population2[pop2_ind], population1[ind]);
1111 perform_mutation(population2[pop2_ind]);
1116 void perform_mutation(struct individual *individual)
1118 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1120 int x = randint(1, 1000);
1123 if (x <= acc + conf_mutate_move_bound)
1125 move_symbol(&(individual->placements[i]));
1128 acc += conf_mutate_move_bound;
1130 if (x <= acc + conf_mutate_regen_bound)
1132 gen_coords(&(individual->placements[i]));
1135 acc += conf_mutate_regen_bound;
1137 if (x <= acc + conf_mutate_chvar_bound)
1139 if (0) // if num_variants > 1
1141 // FIXME: assign new variant
1149 for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1151 population2[pop2_ind++] = population1[0];
1155 void rank_population(void)
1160 void gen_coords(struct placement *p)
1162 switch(p->request->type)
1165 gen_coords_point(p);
1170 case REQUEST_SEGMENT:
1171 gen_coords_segment(p);
1174 printf("Not yet implemented\n");
1177 printf("Testing request type\n");
1178 ASSERT(p->request->type != REQUEST_INVALID);
1182 double gen_movement(void)
1184 double m = (random() % 1000000) / 10000;
1185 m = pow(m, 1.0/3) * flip(1, -1);
1186 printf("Movement %.2f\n", m);
1190 void gen_coords_point(struct placement *p)
1192 p->x = p->x + gen_movement();
1195 void gen_coords_segment(struct placement *p)
1197 struct request_segment *rs = (struct request_segment *) p->request;
1199 p->x = (a == 1 ? rs->x1 : rs->x2);
1200 p->y = (a == 1 ? rs->y1 : rs->y2);
1203 void gen_coords_area(struct placement *p)
1205 struct request_area *ra = (struct request_area *) p->request;
1207 p->x = p->x + gen_movement();
1208 p->y = p->y + gen_movement();
1210 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1213 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1215 struct map_part **buffer;
1216 GARY_INIT(buffer, 0);
1217 int x_min = symbol->x / conf_part_size;
1218 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1219 int y_min = symbol->y / conf_part_size;
1220 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1222 for (int x=x_min; x < x_max; x++)
1223 for (int y=y_min; y < y_max; y++)
1225 struct map_part *m = GARY_PUSH(buffer);
1226 *m = individual->map[x][y];
1232 int randint(int min, int max)
1234 if (min == max) return min;
1236 //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)));
1237 return min + (r % (max - min));
1238 return (r * (max - min));
1241 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1243 printf("Getting closure\n");
1244 struct placement **closure;
1245 GARY_INIT(closure, 0);
1246 bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1247 chosen[placement->request->ind] = 1;
1249 struct placement **p = GARY_PUSH(closure); *p = placement;
1252 while (first < GARY_SIZE(closure))
1254 printf("Iterating, first is %d\n", first);
1255 struct placement **overlapping = get_overlapping(placement);
1256 filter(overlapping, chosen);
1257 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1259 p = GARY_PUSH(closure); *p = overlapping[j];
1260 chosen[overlapping[j]->request->ind] = 1;
1262 GARY_FREE(overlapping);
1269 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1271 //printf("%d\n", child->penalty);
1272 //printf("Closure size: %lld\n", GARY_SIZE(closure));
1273 for (uns i=0; i<GARY_SIZE(closure); i++)
1275 int ind = closure[i]->request->ind;
1276 child->placements[ind] = parent->placements[ind];
1277 child->placements[ind].processed = 0;
1281 void move_symbol(struct placement *p)
1283 switch (p->request->type)
1286 move_symbol_point(p);
1288 case REQUEST_SEGMENT:
1290 printf("Not yet implemented\n");
1292 ASSERT(p->request->type != REQUEST_INVALID);
1296 void move_symbol_point(struct placement *p)
1298 p->x += (double) (move_min + randdouble()) * flip(1, -1);
1299 p->y += (double) (move_min + randdouble()) * flip(1, -1);
1302 void init_placement(struct placement *p, struct request *r)
1307 p->x = p->y = 0; // To prevent valgrind from complaining
1308 p->variant_used = 0;
1311 case REQUEST_POINT: ;
1312 struct request_point *rp = (struct request_point *) r;
1316 case REQUEST_LINE: ;
1318 case REQUEST_SECTION: ;
1319 struct request_section *rls = (struct request_section *) r;
1320 p->variant_used = randint(0, rls->num_segments);
1322 case REQUEST_SEGMENT: ;
1323 struct request_segment *rs = (struct request_segment *) r;
1327 case REQUEST_AREA: ;
1328 struct request_area *ra = (struct request_area *) r;
1331 p->variant_used = 0;
1334 ASSERT(p->request->type != REQUEST_INVALID);
1335 printf("Valid request: %d\n", p->request->type);
1339 // printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1342 void init_individual(struct individual *i)
1344 //printf("Initing individual\n");
1345 GARY_INIT(i->placements, num_requests);
1346 GARY_INIT(i->map, 0);
1347 i->penalty = 0; // FIXME
1350 struct placement **get_overlapping(struct placement *p UNUSED)
1352 struct placement **buffer;
1353 GARY_INIT(buffer, 0);
1357 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1362 int flip(int a, int b)
1364 return (random() % 2 ? a : b);
1367 double randdouble(void)
1369 // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1376 GARY_FREE(requests_point);
1377 GARY_FREE(requests_line);
1378 GARY_FREE(requests_area);
1381 void copy_individual(struct individual *src, struct individual *dest)
1383 src->penalty = dest->penalty;
1384 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1385 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1387 dest->placements[i] = src->placements[i];