3 #include <ucw/mempool.h>
4 #include <ucw/eltpool.h>
11 #define HASH_NODE struct graph_node
12 #define HASH_PREFIX(x) hash_##x
13 #define HASH_KEY_ATOMIC id
14 #define HASH_WANT_FIND
16 #define HASH_WANT_CLEANUP
17 #include <ucw/hashtable.h>
25 #define BLOCK_SIZE 4096
27 static struct request_point *requests_point;
28 static struct request_line *requests_line;
29 static struct request_area *requests_area;
31 static struct graph_edge **bfs_queue;
32 static struct longline *longlines;
33 static struct buffer_line *buffer_line;
34 static struct buffer_linelabel *buffer_linelabel;
36 struct eltpool *ep_individuals;
38 struct individual **population1;
39 struct individual **population2;
41 int dbg_segments = VERBOSITY_NONE;
42 int dbg_plan = VERBOSITY_NONE;
43 int dbg_requests = VERBOSITY_NONE;
44 int dbg_graph = VERBOSITY_NONE;
45 int dbg_bfs = VERBOSITY_NONE;
46 int dbg_map_parts = VERBOSITY_NONE;
47 int dbg_movement = VERBOSITY_NONE;
48 int dbg_init = VERBOSITY_NONE;
49 int dbg_overlaps = VERBOSITY_NONE;
50 int dbg_rank = VERBOSITY_NONE;
51 int dbg_evolution = VERBOSITY_INDIVIDUAL;
52 int dbg_mutation = VERBOSITY_NONE;
53 int dbg_breeding = VERBOSITY_NONE;
62 int conf_pop_size = 50;
63 int conf_fit_size = 1;
65 int conf_penalty_bound = 0;
66 int conf_stagnation_bound = 0;
67 int conf_iteration_limit = 100;
69 int conf_term_cond = TERM_COND_ITERATIONS;
71 double conf_breed_pop_size = 0.4;
72 double conf_breed_rbest = 0.2;
77 bool conf_mutate_children = 1;
78 double conf_mutate_children_prob = 1.0;
80 double conf_mutate_pop_size = 0.4;
81 double conf_mutate_rbest = 0.2;
83 double conf_mutate_move_bound = 0.1;
84 double conf_mutate_regen_bound = 0.0;
85 double conf_mutate_chvar_bound = 0.0;
88 int mutate_rbest_size;
90 double conf_elite_pop_size = 0.2;
93 double conf_max_section_length = 100;
94 double conf_max_section_overlay = 10;
96 int old_best = INT_MAX;
104 int num_requests = 0;
105 int num_placements = 0;
108 int conf_map_part_width = 5;
109 int conf_map_part_height = 5;
111 uns num_map_parts_row;
112 uns num_map_parts_col;
115 void compute_sizes(void);
117 void make_population(void);
118 bool shall_terminate(void);
122 void rank_population(void);
123 void plan_individual(struct individual *individual);
125 int overlaps(struct placement *p1, struct placement *p2);
126 int get_overlap(struct placement *p);
127 int individual_overlap(struct individual *individual);
129 double get_distance(struct placement *p);
130 double individual_distances(struct individual *individual);
132 double get_omittment(struct placement *p);
133 double individual_omittment(struct individual *individual);
135 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
136 void perform_mutation(struct individual *individual);
137 void init_placement(struct placement *p, struct individual *individual, struct request *r);
138 void init_individual(struct individual *i);
139 void copy_individual(struct individual *src, struct individual *dest);
140 int cmp_individual(const void *a, const void *b);
142 void clear_individual(struct individual *individual);
143 void clear_population(struct individual **pop);
145 void make_bitmap(struct variant *v, struct symbol *sym);
146 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
147 void make_bitmap_point(struct variant *v, struct sym_point *sp);
148 void make_bitmap_label(struct variant *v, struct sym_text *text);
150 double gen_movement(void);
151 double gen_movement_uniform(void);
152 void move_symbol(struct placement *p);
153 void move_symbol_point(struct placement *p);
154 void move_symbol_segment(struct placement *p);
155 void hide_segment_labels(struct individual *individual);
157 void gen_coords(struct placement *p);
158 void gen_coords_point(struct placement *p);
159 void gen_coords_segment(struct placement *p);
160 void gen_coords_area(struct placement *p);
162 struct map_part **get_map_parts(struct placement *p);
163 void update_map_parts(struct placement *p);
164 void update_map_parts_delete(struct placement *p);
165 void update_map_parts_create(struct placement *p);
166 struct placement **get_closure(struct placement *placement);
167 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
168 struct placement **get_overlapping(struct placement *p);
169 struct placement **filter(struct placement **list, bool **pred_ptr);
172 void make_graph(void);
173 void label_graph(void);
174 void bfs_wrapper(void);
175 void bfs(uns longline);
176 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
177 void make_segments(void);
179 void cut_edge(struct graph_edge *e, double dist);
180 struct request_line *make_new_line(void);
181 struct request_section *make_new_section(struct request_line *rl);
182 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
184 void dump_bitmaps(struct individual *individual);
185 void dump_graph(void);
186 void dump_longlines(void);
187 void dump_linelabel_requests(void);
188 void dump_individual(struct individual *individual);
189 void dump_label(struct symbol *sym);
190 void dump_penalties(struct individual **population);
192 int randint(int min, int max);
193 int flip(int a, int b);
194 double randdouble(void);
196 int max2(int a, int b);
197 int min2(int a, int b);
198 int max4(int a, int b, int c, int d);
199 int min4(int a, int b, int c, int d);
201 struct placement dummy_placement;
203 int max2(int a, int b)
205 return (a > b ? a : b);
208 int min2(int a, int b)
210 return (a < b ? a : b);
213 int max4(int a, int b, int c, int d)
215 return max2(max2(a, b), max2(c, d));
218 int min4(int a, int b, int c, int d)
220 return min2(min2(a, b), min2(c, d));
223 void dump_label(struct symbol *sym)
227 case SYMBOLIZER_TEXT: ;
228 struct sym_text *st = (struct sym_text *) sym;
229 printf("%s\n", osm_val_decode(st->text));
236 void labeller_init(void)
238 GARY_INIT(requests_point, 0);
239 GARY_INIT(requests_line, 0);
240 GARY_INIT(requests_area, 0);
241 GARY_INIT(buffer_line, 0);
242 GARY_INIT(buffer_linelabel, 0);
243 ep_individuals = ep_new(sizeof(struct individual), 1);
248 void make_bitmap(struct variant *v, struct symbol *sym)
250 v->offset_x = v->offset_y = 0;
254 case SYMBOLIZER_POINT:
255 make_bitmap_point(v, (struct sym_point *) sym);
257 case SYMBOLIZER_ICON:
258 make_bitmap_icon(v, (struct sym_icon *) sym);
260 case SYMBOLIZER_TEXT:
261 make_bitmap_label(v, (struct sym_text *) sym);
264 ASSERT(sym->type != SYMBOLIZER_INVALID);
268 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
270 v->width = si->sir.width + 1;
271 v->height = si->sir.height + 1;
272 v->bitmap = malloc(v->width * v->height * sizeof(bool));
273 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
276 void make_bitmap_point(struct variant *v, struct sym_point *sp)
278 v->width = v->height = sp->size + 1;
279 v->bitmap = malloc(v->width * v->height * sizeof(bool));
280 // FIXME: Okay, memset would be much nicer here
281 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
284 void make_bitmap_label(struct variant *v, struct sym_text *text)
286 int tw = ceil(text->tw);
287 int th = ceil(text->th);
289 double rotate_rad = text->rotate / (-180 / M_PI);
291 // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th]
292 // They could be declared before but should not be initialized in code
293 // as reassigning x-coordinate affects computation of y-cordinate
297 int lrx = tw * cos(rotate_rad);
298 int lry = tw * sin(rotate_rad);
300 int ulx = th * sin(rotate_rad);
301 int uly = th * cos(rotate_rad);
303 int urx = tw * cos(rotate_rad) + th * sin(rotate_rad);
304 int ury = tw * sin(rotate_rad) + th * cos(rotate_rad);
306 int min_x = min4(llx, lrx, ulx, urx);
307 int min_y = min4(lly, lry, uly, ury);
308 int max_x = max4(llx, lrx, ulx, urx);
309 int max_y = max4(lly, lry, uly, ury);
311 v->width = max_x - min_x + 1;
312 v->height = max_y - min_y + 1;
313 v->bitmap = malloc(v->width * v->height * sizeof(bool));
314 memset(v->bitmap, 0, v->width * v->height * sizeof(bool));
316 for (int i=0; i<th; i++)
318 for (int j=0; j<tw; j++)
320 int nx = j*cos(rotate_rad) + i*sin(rotate_rad);
321 int ny = j*sin(rotate_rad) + i*cos(rotate_rad);
322 v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1;
327 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
329 if (dbg_requests >= VERBOSITY_PLACEMENT)
330 printf("Adding point\n");
331 if (object->type != OSM_TYPE_NODE)
333 printf("Warning: Point label requested on non-point object\n");
337 struct request_point *r = GARY_PUSH(requests_point);
339 r->request.type = REQUEST_POINT;
340 r->request.ind = num_requests++;
349 GARY_INIT(r->request.variants, 0);
351 struct variant *v = GARY_PUSH(r->request.variants);
353 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
359 case SYMBOLIZER_ICON:
361 r->x = ((struct sym_icon *)sym)->sir.x;
362 r->y = ((struct sym_icon *)sym)->sir.y;
369 if (dbg_requests >= VERBOSITY_PLACEMENT)
370 printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
373 void labeller_add_line(struct symbol *sym, z_index_t zindex)
375 if (dbg_requests >= VERBOSITY_PLACEMENT)
376 printf("Adding line on %u\n", zindex);
377 struct buffer_line *b = GARY_PUSH(buffer_line);
378 b->line = (struct sym_line *) sym;
380 sym_plan(sym, zindex);
383 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
385 if (o->type != OSM_TYPE_WAY)
387 printf("Linelabel request on object which is not way\n");
391 if (dbg_requests >= VERBOSITY_PLACEMENT)
392 printf("Labelling way %ju on %u\n", o->id, zindex);
393 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
394 ll->way = (struct osm_way *) o;
399 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
401 if (dbg_requests >= VERBOSITY_PLACEMENT)
402 printf("Adding area on %u\n", zindex);
403 struct request_area *r = GARY_PUSH(requests_area);
405 r->request.type = REQUEST_AREA;
406 r->request.ind = num_requests++;
408 r->o = (struct osm_multipolygon *) o;
412 osm_obj_center(o, &(r->cx), &(r->cy));
414 GARY_INIT(r->request.variants, 0);
415 struct variant *v = GARY_PUSH(r->request.variants);
419 void make_graph(void)
422 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
424 if (dbg_graph >= VERBOSITY_GENERAL)
425 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
426 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
428 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
429 struct graph_node *g_prev = NULL;
430 struct osm_node *o_prev = NULL;
432 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
434 // FIXME: Shall osm_object's type be checked here?
435 struct osm_node *o_node = (struct osm_node *) ref->o;
437 struct graph_node *g_node = hash_find(ref->o->id);
440 g_node = hash_new(ref->o->id);
441 GARY_INIT(g_node->edges, 0);
443 g_node->id = ref->o->id;
444 g_node->num = num_nodes++;
454 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge));
455 e->num = num_edges++;
456 e->id = buffer_line[i].line->s.o->id;
457 e->color = buffer_line[i].line->color;
458 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
464 e->longline = (uns) -1;
465 e->line = buffer_line[i].line;
469 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
471 edge = GARY_PUSH(g_node->edges);
480 void dump_graph(void)
482 HASH_FOR_ALL(hash, node)
484 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
485 for (uns i=0; i<GARY_SIZE(node->edges); i++)
487 struct graph_edge *e = node->edges[i];
488 printf("\t edge (%d) #%ju to ", e->num, e->id);
489 if (node->edges[i]->n1->id == node->id)
490 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
491 else if (node->edges[i]->n2->id == node->id)
492 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
495 // This shouldn't ever happen
496 printf("BEWARE! Edge is associated with a node it doesn't belongs to!\n");
501 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));
502 else if ((node->edges[i]->label)) printf("Labelled\n");
504 printf(" colored %d;", node->edges[i]->color);
505 printf(" length %.2f", node->edges[i]->length);
512 void label_graph(void)
514 if (dbg_graph >= VERBOSITY_GENERAL)
515 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
516 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
518 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
519 if (dbg_graph >= VERBOSITY_INDIVIDUAL)
520 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
521 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
523 if (dbg_graph >= VERBOSITY_PLACEMENT)
524 printf("Looking for node %ju\n", ref->o->id);
525 struct graph_node *n = hash_find(ref->o->id);
528 printf("BEWARE! Requested node couldn't be found.\n");
532 if (dbg_graph >= VERBOSITY_ALL)
533 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
534 for (uns j=0; j<GARY_SIZE(n->edges); j++)
536 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
538 if (dbg_graph >= VERBOSITY_ALL)
539 printf("Labelling node %ju\n", n->id);
540 n->edges[j]->label = buffer_linelabel[i].label;
541 n->edges[j]->zindex = buffer_linelabel[i].zindex;
549 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
551 if (dbg_bfs >= VERBOSITY_PLACEMENT)
552 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
553 struct graph_edge *candidate = NULL;
555 for (uns i=0; i<GARY_SIZE(node->edges); i++)
557 struct graph_edge *other = node->edges[i];
558 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
560 if ((uns) other->visited != e->longline) {
561 if (dbg_bfs >= VERBOSITY_PLACEMENT)
562 printf("Pushing new edge %d / %ju\n", other->num, other->id);
563 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
565 other->visited = e->longline;
568 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
569 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
572 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
573 (e->label) && (other->label) &&
574 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
575 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
577 if (! candidate || (other->length > candidate->length))
584 if (dbg_bfs >= VERBOSITY_PLACEMENT)
585 printf("New line in longline %u\n", e->longline);
586 struct graph_edge *other = candidate;
587 other->longline = e->longline;
589 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
590 ((dir == DIR_FWD) && (other->n2->id == node->id)))
592 struct graph_node *swp = other->n2;
593 other->n2 = other->n1;
602 longlines[other->longline].first = other;
615 void bfs(uns longline)
617 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
619 printf("BFS called for longline %u\n", longline);
620 printf("%d longlines exist\n", GARY_SIZE(longlines));
623 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
625 struct graph_edge *cur = bfs_queue[i];
626 if (dbg_bfs >= VERBOSITY_PLACEMENT)
627 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
629 cur->visited = longline;
631 if (cur->longline == (uns) -1)
634 if (cur->dir == DIR_UNSET)
636 cur->dir = DIR_CENTER;
637 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
638 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
645 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
648 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
658 void bfs_wrapper(void)
660 GARY_INIT(bfs_queue, 0);
661 GARY_INIT(longlines, 0);
663 HASH_FOR_ALL(hash, node)
665 for (uns i=0; i<GARY_SIZE(node->edges); i++)
667 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
669 GARY_PUSH(longlines);
670 longlines[GARY_SIZE(longlines)-1].first = node->edges[i];
672 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
674 printf("Running new BFS\n");
675 printf("Creating longline %u\n", GARY_SIZE(longlines)-1);
678 GARY_RESIZE(bfs_queue, 0);
679 struct graph_edge **e = GARY_PUSH(bfs_queue);
681 node->edges[i]->longline = GARY_SIZE(longlines)-1;
682 bfs(node->edges[i]->longline);
684 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
686 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
687 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
694 GARY_FREE(bfs_queue);
697 void dump_longlines(void)
699 printf("*** Longlines dump\n");
700 for (uns i=0; i<GARY_SIZE(longlines); i++)
702 printf("Longline %u:", i);
703 struct graph_edge *e = longlines[i].first;
704 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
705 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
710 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
711 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
718 struct request_line *make_new_line(void)
720 struct request_line *rl = GARY_PUSH(requests_line);
721 rl->request.ind = num_requests++;
722 rl->request.type = REQUEST_LINE;
723 GARY_INIT(rl->sections, 0);
724 GARY_INIT(rl->request.variants, 0);
729 struct request_section *make_new_section(struct request_line *rl)
731 struct request_section *rls = GARY_PUSH(rl->sections);
732 rls->request.ind = num_requests++;
733 rls->request.type = REQUEST_SECTION;
734 rls->num_segments = 0;
735 GARY_INIT(rls->segments, 0);
736 GARY_INIT(rls->request.variants, 0);
741 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
743 struct request_segment *rs = GARY_PUSH(rls->segments);
746 rs->request.ind = num_requests++;
747 rs->request.type = REQUEST_SEGMENT;
749 GARY_INIT(rs->request.variants, 0);
752 struct variant *v = GARY_PUSH(rs->request.variants);
759 void cut_edge(struct graph_edge *e, double dist)
761 if (dbg_segments >= VERBOSITY_PLACEMENT)
762 printf("Cutting [%.2f; %.2f] -- [%.2f; %.2f] to dist %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, dist);
764 struct graph_edge *new = malloc(sizeof(struct graph_edge));
768 switch (e->label->type)
770 case SYMBOLIZER_TEXT:
771 new->label = malloc(sizeof(struct sym_text));
772 *((struct sym_text *) new->label) = *((struct sym_text *) e->label);
778 struct osm_node *n1 = e->n1->o;
779 struct osm_node *n2 = e->n2->o;
781 if ((n1->x == n2->x) && (n1->y == n2->y))
783 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
784 if (dbg_segments >= VERBOSITY_PLACEMENT)
785 printf("Won't cut point\n");
789 struct osm_node *n11 = malloc(sizeof(struct osm_node));
790 struct graph_node *gn = malloc(sizeof(struct graph_node));
792 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
793 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
794 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
796 e->n2 = new->n1 = gn;
798 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
799 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
803 void make_segments(void)
805 for (uns i=0; i<GARY_SIZE(longlines); i++)
807 // Skip lines which are not labelled
808 if (! (longlines[i].first && longlines[i].first->label))
811 struct request_line *request = make_new_line();
812 struct request_section *rls = make_new_section(request);
813 struct request_segment *rs = NULL;
815 struct graph_edge *e = longlines[i].first;
816 double cur_length = 0;
818 struct sym_text *st = NULL;
819 if (e->label->type == SYMBOLIZER_TEXT)
821 st = (struct sym_text *) e->label;
825 // FIXME: Should other label types be supported in future?
826 if (dbg_segments >= VERBOSITY_PLACEMENT)
827 printf("Warning: Skipping line\n");
831 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
832 printf("New longline\n");
838 if (dbg_segments >= VERBOSITY_PLACEMENT)
839 printf("BEWARE: Edge cycle\n");
844 if (dbg_segments >= VERBOSITY_PLACEMENT)
845 printf("Taking edge from [%.2f; %.2f] to [%.2f; %.2f] of length %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->length);
847 if (st && (e->length < st->tw))
850 if (dbg_segments >= VERBOSITY_PLACEMENT)
851 printf("Warning: Skipping segment\n");
855 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
857 if (dbg_segments >= VERBOSITY_PLACEMENT)
858 printf("Edge too long, length is %.2f; %.2f - %.2f = %.2f\n", e->length, conf_max_section_length, cur_length, conf_max_section_length - cur_length);
859 // HACK to prevent cutting to 0 lenght
860 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
863 rs = make_new_segment(rls, NULL);
864 rs->label = malloc(sizeof(struct sym_text));
865 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
867 rs->x1 = e->n1->o->x;
868 rs->y1 = e->n1->o->y;
869 rs->x2 = e->n2->o->x;
870 rs->y2 = e->n2->o->y;
872 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
873 ((struct sym_text *) rs->label)->rotate = atan(rs->slope) * (-180 / M_PI);
874 struct variant *v = GARY_PUSH(rs->request.variants);
875 make_bitmap(v, rs->label);
877 rs->zindex = e->zindex;
879 cur_length += e->length;
880 if (cur_length > conf_max_section_length)
882 if (dbg_segments >= VERBOSITY_PLACEMENT)
883 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);
885 rls = make_new_section(request);
892 if (request->sections[0].num_segments == 0)
894 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
895 printf("WARNING: Longline without any segment, skipped\n");
897 struct request_section *rls = &request->sections[0];
898 GARY_FREE(rls->segments);
899 GARY_FREE(rls->request.variants);
901 struct request_line *rl = &requests_line[GARY_SIZE(requests_line)-1];
902 GARY_FREE(rl->sections);
903 GARY_FREE(rl->request.variants);
905 GARY_POP(requests_line);
911 void dump_linelabel_requests(void)
913 for (uns i=0; i<GARY_SIZE(requests_line); i++)
915 if (requests_line[i].sections[0].num_segments == 0)
917 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
918 printf("Beware: Longline without any segment\n");
922 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
923 dump_label(requests_line[i].sections[0].segments[0].label);
924 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
926 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
927 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
929 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
930 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
937 void dump_bitmaps(struct individual *individual)
939 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
940 printf("Bitmap size is %d\n", page_width_int * page_height_int);
941 for (int i=0; i<page_height_int; i++)
942 for (int j=0; j<page_width_int; j++)
943 bitmap[i*page_width_int + j] = 0;
946 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
948 if (individual->placements[i].variant_used == -1) continue;
950 struct placement *p = &(individual->placements[i]);
951 struct variant *v = NULL;
953 switch (p->request->type)
955 case REQUEST_SEGMENT: ;
956 case REQUEST_POINT: ;
958 v = &(p->request->variants[p->variant_used]);
961 ASSERT(p->request->type != REQUEST_INVALID);
965 int base_x = p->x; int base_y = p->y;
966 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
968 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
970 if (v->bitmap[dr * v->width + dc])
972 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
973 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
978 if (dbg_overlaps >= VERBOSITY_GENERAL)
979 printf("There were %d collisions during bitmap dump\n", total);
981 FILE *fd_dump = fopen("dump.pbm", "w");
982 fprintf(fd_dump, "P1\n");
983 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
984 for (int i=0; i<page_height_int; i++)
986 for (int j=0; j<page_width_int; j++)
988 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
990 fprintf(fd_dump, "\n");
997 void dump_individual(struct individual *individual)
999 printf("*** Individual dump\n");
1000 printf("(There are %d requests)\n", num_requests);
1002 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1004 struct placement *p = &(individual->placements[i]);
1006 switch (p->request->type)
1009 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
1011 case REQUEST_LINE: ;
1012 struct request_line *rl = (struct request_line *) p->request;
1014 dump_label(rl->sections[0].segments[0].label);
1016 case REQUEST_SECTION: ;
1019 case REQUEST_SEGMENT: ;
1020 if (p->variant_used >= 0)
1021 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
1023 printf("Segment not placed\n");
1025 case REQUEST_AREA: ;
1026 struct request_area *ra = (struct request_area *) p->request;
1027 printf("Area label ");
1028 dump_label(ra->label);
1029 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
1032 ASSERT(p->request->type != 0);
1035 printf("\nTotal penalty: %d\n", individual->penalty);
1038 void plan_individual(struct individual *individual)
1040 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1042 struct symbol *s = NULL;
1043 z_index_t zindex = 0;
1044 if (individual->placements[i].variant_used < 0) continue;
1045 switch (individual->placements[i].request->type)
1047 case REQUEST_POINT: ;
1048 struct request_point *rp = (struct request_point *) individual->placements[i].request;
1050 zindex = rp->zindex;
1052 case REQUEST_SEGMENT: ;
1053 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1055 zindex = rs->zindex;
1057 case REQUEST_LINE: ;
1059 case REQUEST_AREA: ;
1060 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1062 zindex = ra->zindex;
1065 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1069 if (dbg_plan >= VERBOSITY_PLACEMENT)
1070 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1072 if (s) switch (s->type)
1074 case SYMBOLIZER_POINT: ;
1075 struct sym_point *sp = (struct sym_point *) s;
1076 sp->x = individual->placements[i].x;
1077 sp->y = individual->placements[i].y;
1078 sym_plan((struct symbol *) sp, zindex);
1080 case SYMBOLIZER_ICON: ;
1081 struct sym_icon *si = (struct sym_icon *) s;
1082 si->sir.x = individual->placements[i].x;
1083 si->sir.y = individual->placements[i].y;
1084 sym_plan((struct symbol *) si, zindex);
1086 case SYMBOLIZER_TEXT: ;
1087 struct sym_text *st = (struct sym_text *) s;
1088 st->x = individual->placements[i].x;
1089 st->y = individual->placements[i].y;
1090 st->next_duplicate = NULL;
1091 if (dbg_plan >= VERBOSITY_PLACEMENT)
1092 printf("Planning text %s at [%.2f; %.2f] on %u, with rotation %.2f\n", osm_val_decode(st->text), st->x, st->y, zindex, st->rotate);
1093 sym_plan((struct symbol *) st, zindex);
1096 ASSERT(s->type != SYMBOLIZER_INVALID);
1101 void dump_penalties(struct individual **population)
1103 for (int i=0; i<conf_pop_size; i++)
1105 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1109 void compute_sizes(void)
1111 page_width_int = floor(page_width);
1112 page_height_int = floor(page_height);
1114 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1115 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1116 num_map_parts = num_map_parts_row * num_map_parts_col;
1118 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1119 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1120 if (dbg_evolution >= VERBOSITY_GENERAL)
1122 printf("Breeding parameters:\n");
1123 printf(" %d individuals are created\n", breed_pop_size);
1124 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1127 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1128 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1129 if (dbg_evolution >= VERBOSITY_GENERAL)
1131 printf("Mutation parameters:\n");
1132 printf(" %d individuals are created\n", mutate_pop_size);
1133 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1136 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1137 if (dbg_evolution >= VERBOSITY_GENERAL)
1139 printf("Elitism parameters:\n");
1140 printf(" %d best individuals are copied\n", elite_pop_size);
1143 if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1147 elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1151 fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1152 fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1158 void labeller_label(void)
1165 GARY_INIT(population1, conf_pop_size);
1166 GARY_INIT(population2, conf_pop_size);
1169 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1171 if (dbg_evolution >= VERBOSITY_GENERAL)
1173 printf("Penalties after initialization\n");
1174 dump_penalties(population1);
1177 while (! shall_terminate())
1181 printf("\n*** Iteration %d ***\n", iteration);
1187 struct individual **swp = population1;
1188 population1 = population2;
1191 clear_population(population2);
1195 if (dbg_evolution >= VERBOSITY_GENERAL)
1196 printf("Sorting population\n");
1197 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1199 if (dbg_evolution >= VERBOSITY_GENERAL)
1201 printf("Penalties after sort\n");
1202 dump_penalties(population1);
1205 old_best = population1[0]->penalty;
1208 if (dbg_overlaps >= VERBOSITY_GENERAL)
1209 dump_bitmaps(population1[0]);
1211 plan_individual(population1[0]);
1216 void labeller_cleanup(void)
1219 GARY_FREE(requests_point);
1220 GARY_FREE(requests_line);
1221 GARY_FREE(requests_area);
1224 void make_population(void)
1226 for (int i=0; i<conf_pop_size; i++)
1228 num_placements = 0; // FIXME: This IS a terrible HACK
1229 struct individual *i2 = ep_alloc(ep_individuals);
1230 init_individual(i2);
1231 population2[i] = i2;
1233 if (dbg_init >= VERBOSITY_INDIVIDUAL)
1234 printf("Making individual %d\n", i);
1235 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1236 population1[i] = individual;
1239 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1241 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1244 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1246 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1248 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1250 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1252 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1254 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1259 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1261 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1264 hide_segment_labels(individual);
1266 ASSERT(p == num_requests);
1270 bool shall_terminate(void)
1272 switch (conf_term_cond)
1274 case TERM_COND_PENALTY:
1275 return (population1[0]->penalty < conf_penalty_bound);
1276 case TERM_COND_STAGNATION:
1277 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1278 case TERM_COND_ITERATIONS:
1279 return (iteration >= conf_iteration_limit);
1281 fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1290 struct individual **breed_buffer;
1291 while (i < breed_pop_size)
1293 int parent1 = randint(0, breed_rbest_size);
1294 int parent2 = randint(0, breed_rbest_size);
1295 if (dbg_breeding >= VERBOSITY_INDIVIDUAL)
1296 printf("Will breed %d and %d\n", parent1, parent2);
1298 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1299 population2[pop2_ind++] = breed_buffer[0];
1300 population2[pop2_ind++] = breed_buffer[1];
1306 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1308 struct individual **buffer = malloc(2*sizeof(struct individual));
1309 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1310 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1313 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1315 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1317 if (! processed[parent1->placements[i].ind])
1319 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1320 printf("Creating symbol closure for placement %u\n", i);
1322 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1323 int x = randint(0, 2);
1327 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1328 printf("Copying parent->child 1->1 and 2->2\n");
1329 copy_symbols(clos_symbols, parent1, child1, &processed);
1330 copy_symbols(clos_symbols, parent2, child2, &processed);
1334 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1335 printf("Copying parent->child 2->1 and 1->2\n");
1336 copy_symbols(clos_symbols, parent2, child1, &processed);
1337 copy_symbols(clos_symbols, parent1, child2, &processed);
1340 GARY_FREE(clos_symbols);
1344 GARY_FREE(processed);
1346 if (conf_mutate_children)
1348 if (randdouble() < conf_mutate_children_prob) perform_mutation(child1);
1349 else hide_segment_labels(child1);
1351 if (randdouble() < conf_mutate_children_prob) perform_mutation(child2);
1352 else hide_segment_labels(child2);
1362 for (int i=0; i < mutate_pop_size; i++)
1364 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1365 printf("Creating %d-th individual by mutation\n", i);
1366 int ind = randint(0, mutate_rbest_size);
1367 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1368 printf("Mutating %d-th individual of original population\n", ind);
1369 population2[pop2_ind] = ep_alloc(ep_individuals);
1370 copy_individual(population1[ind], population2[pop2_ind]);
1371 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1372 printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1373 perform_mutation(population2[pop2_ind]);
1378 void perform_mutation(struct individual *individual)
1380 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1382 double x = randdouble();
1385 if (x <= acc + conf_mutate_move_bound)
1387 if (dbg_mutation >= VERBOSITY_PLACEMENT)
1388 printf("Mutation: Moving symbol in placement %u\n", i);
1389 move_symbol(&(individual->placements[i]));
1392 acc += conf_mutate_move_bound;
1394 if (x <= acc + conf_mutate_regen_bound)
1396 gen_coords(&(individual->placements[i]));
1399 acc += conf_mutate_regen_bound;
1401 if (x <= acc + conf_mutate_chvar_bound)
1403 struct placement *p = &(individual->placements[i]);
1404 switch (p->request->type)
1408 // Does nothing when there are 0 variants... does it mind?
1409 p->variant_used = randint(-1, GARY_SIZE(p->request->variants));
1411 case REQUEST_SEGMENT:
1412 p->variant_used = randint(0, GARY_SIZE(p->request->variants));
1414 case REQUEST_SECTION:
1415 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments));
1423 hide_segment_labels(individual);
1428 for (int i=0; i<elite_pop_size; i++)
1430 population2[pop2_ind] = ep_alloc(ep_individuals);
1431 copy_individual(population1[i], population2[pop2_ind++]);
1435 int overlaps(struct placement *p1, struct placement *p2)
1437 if (p1->request->type != REQUEST_POINT &&
1438 p1->request->type != REQUEST_SEGMENT &&
1439 p1->request->type != REQUEST_AREA)
1442 if (p2->request->type != REQUEST_POINT &&
1443 p2->request->type != REQUEST_SEGMENT &&
1444 p2->request->type != REQUEST_AREA)
1447 if (p1->variant_used == -1 || p2->variant_used == -1)
1450 struct variant *v1, *v2;
1452 v1 = &(p1->request->variants[p1->variant_used]);
1453 v2 = &(p2->request->variants[p2->variant_used]);
1455 // FIXME: This doesn't fully respect offset which it probably should
1456 int p1x = p1->x; int p1y = p1->y;
1457 int p2x = p2->x; int p2y = p2->y;
1460 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1461 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1463 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1464 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1471 int get_overlap(struct placement *p)
1473 if (p->variant_used == -1) return 0;
1475 struct map_part **parts = get_map_parts(p);
1478 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1479 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1483 struct placement **others;
1486 GARY_INIT_ZERO(planned, num_requests);
1487 planned[p->request->ind] = 1;
1488 GARY_INIT(others, 0);
1490 for (uns i=0; i<GARY_SIZE(parts); i++)
1492 struct map_placement *mp = parts[i]->placement->next_in_map;
1495 if (! planned[mp->placement->request->ind])
1497 struct placement **p = GARY_PUSH(others);
1499 planned[mp->placement->request->ind] = true;
1501 mp = mp->next_in_map;
1506 for (uns i=0; i<GARY_SIZE(others); i++)
1508 overlap += overlaps(p, others[i]);
1515 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1516 printf("Placement of request %d add %d to overlaps\n", p->request->ind, overlap);
1518 if (p->x < 0) overlap += 0 - p->x;
1519 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1520 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1522 if (p->y < 0) overlap += 0 - p->y;
1523 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1524 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1529 int individual_overlap(struct individual *individual)
1533 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1535 overlap += get_overlap(&individual->placements[i]);
1541 double get_distance(struct placement *p)
1543 if (p->variant_used < 0) return 0;
1544 struct variant *v = &p->request->variants[p->variant_used];
1546 double dx, dy, distance;
1547 switch (p->request->type)
1549 case REQUEST_POINT: ;
1550 struct request_point *rp = (struct request_point *) p->request;
1551 dx = rp->x + v->offset_x - p->x;
1552 dy = rp->y + v->offset_y - p->y;
1553 distance = sqrt(dx*dx + dy*dy);
1554 if (dbg_rank >= VERBOSITY_PLACEMENT)
1555 printf("Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1557 case REQUEST_SEGMENT: ;
1558 struct request_segment *rs = (struct request_segment *) p->request;
1559 struct sym_text *st = (struct sym_text *) rs->label;
1561 double width = p->request->variants[p->variant_used].width;
1562 double rotated_x = p->x + width * sin(st->rotate / (-180 / M_PI));
1563 double rotated_y = p->y + width * cos(st->rotate / (-180 / M_PI));
1565 if (rs->x1 < rs->x2)
1572 else if (rotated_x > rs->x2)
1574 dx = rotated_x - rs->x2;
1575 dy = rotated_y - rs->y2;
1589 else if (rotated_x > rs->x1)
1591 dx = rotated_x - rs->x1;
1592 dy = rotated_y - rs->y1;
1600 distance = sqrt(dx*dx + dy*dy);
1602 case REQUEST_AREA: ;
1603 struct request_area *ra = (struct request_area *) p->request;
1604 dx = ra->cx + v->offset_x - p->x;
1605 dy = ra->cy + v->offset_y - p->y;
1606 distance = sqrt(dx*dx + dy*dy);
1607 if (dbg_rank >= VERBOSITY_PLACEMENT)
1608 printf("Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1614 if (dbg_rank >= VERBOSITY_PLACEMENT)
1615 printf("Placement %d has distance %.2f\n", p->ind, distance);
1619 double individual_distances(struct individual *individual)
1623 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1625 distances += get_distance(&individual->placements[i]);
1631 double get_omittment(struct placement *p)
1633 if (p->variant_used >= 0) return 0;
1636 switch (p->request->type)
1647 double individual_omittment(struct individual *individual)
1651 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1653 omittment += get_omittment(&individual->placements[i]);
1659 int cmp_individual(const void *a, const void *b)
1661 struct individual **ia = (struct individual **) a;
1662 struct individual **ib = (struct individual **) b;
1664 return (*ia)->penalty - (*ib)->penalty;
1667 void rank_population(void)
1671 for (int i=0; i<conf_pop_size; i++)
1673 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1674 printf("Individual %d\n", i);
1675 population1[i]->penalty = 0;
1677 penalty = individual_omittment(population1[i]);
1678 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1679 printf("Increasing penalty by %d for omittment\n", penalty);
1680 population1[i]->penalty += penalty;
1682 penalty = individual_overlap(population1[i]);
1683 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1684 printf("Increasing penalty by %d for overlap\n", penalty);
1685 population1[i]->penalty += penalty;
1687 penalty = individual_distances(population1[i]);
1688 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1689 printf("Increasing penalty by %d for distances\n", penalty);
1690 population1[i]->penalty += penalty;
1694 struct map_part **get_map_parts(struct placement *p)
1696 if (p->variant_used < 0) return NULL;
1698 struct map_part **buffer;
1699 GARY_INIT(buffer, 0);
1701 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1702 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1705 switch (p->request->type)
1708 case REQUEST_SEGMENT:
1710 v = p->request->variants[p->variant_used];
1713 if (dbg_map_parts >= VERBOSITY_ALL)
1714 printf("Skipping unsupported request type (%d)\n", p->request->type);
1718 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1719 printf("Bitmap is %d x %d\n", v.width, v.height);
1721 int x_min = max2(0, p->x) / conf_map_part_width;
1722 // CHECK ME: Is rounding needed?
1723 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1724 int y_min = max2(0, p->y) / conf_map_part_height;
1725 // CHECK ME: Is rounding needed?
1726 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1728 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1729 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1731 for (int y=y_min; y<=y_max; y++)
1732 for (int x=x_min; x<=x_max; x++)
1734 struct map_part **m = GARY_PUSH(buffer);
1735 if (dbg_map_parts >= VERBOSITY_ALL)
1736 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1737 *m = p->individual->map[y * num_map_parts_row + x];
1740 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1741 printf("Returning %u map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1746 void update_map_parts_delete(struct placement *p)
1748 struct map_placement *mp = p->map_links;
1751 mp->prev_in_map->next_in_map = mp->next_in_map;
1752 if (mp->next_in_map)
1753 mp->next_in_map->prev_in_map = mp->prev_in_map;
1755 struct map_placement *tmp = mp;
1756 mp = mp->next_in_placement;
1759 p->map_links = NULL;
1762 void update_map_parts_create(struct placement *p)
1764 struct map_part **parts = get_map_parts(p);
1765 if (parts == NULL) return;
1767 for (uns i=0; i<GARY_SIZE(parts); i++)
1769 struct map_placement *mp = malloc(sizeof(struct map_placement));
1771 mp->part = parts[i];
1773 mp->next_in_map = parts[i]->placement->next_in_map;
1774 mp->prev_in_map = parts[i]->placement;
1775 parts[i]->placement->next_in_map = mp;
1776 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1778 mp->next_in_placement = p->map_links;
1779 mp->prev_in_placement = NULL;
1786 void update_map_parts(struct placement *p)
1788 update_map_parts_delete(p);
1789 update_map_parts_create(p);
1792 void gen_coords(struct placement *p)
1794 switch(p->request->type)
1797 gen_coords_point(p);
1802 case REQUEST_SEGMENT:
1803 gen_coords_segment(p);
1807 printf("Not yet implemented\n");
1810 if (dbg_movement >= VERBOSITY_ALL)
1811 printf("Testing request type\n");
1812 ASSERT(p->request->type != REQUEST_INVALID);
1815 update_map_parts(p);
1818 double gen_movement(void)
1820 double m = (random() % 100000) / 10000;
1821 m = pow(m, 1.0/3) * flip(1, -1);
1822 if (dbg_movement >= VERBOSITY_ALL)
1823 printf("Movement %.2f\n", m);
1827 double gen_movement_uniform(void)
1829 return (move_max - move_min) * randdouble() * flip(1, -1);
1832 void gen_coords_point(struct placement *p)
1834 p->x = p->x + gen_movement();
1837 void gen_coords_segment(struct placement *p)
1839 struct request_segment *rs = (struct request_segment *) p->request;
1840 p->x = (rs->x1 + rs->x2) / 2;
1841 p->y = (rs->y1 + rs->y2) / 2;
1844 void gen_coords_area(struct placement *p)
1846 struct request_area *ra = (struct request_area *) p->request;
1848 p->x = p->x + gen_movement();
1849 p->y = p->y + gen_movement();
1851 if (dbg_movement >= VERBOSITY_PLACEMENT)
1852 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1855 int randint(int min, int max)
1857 if (min == max) return min;
1859 return min + (r % (max - min));
1862 struct placement **get_closure(struct placement *placement)
1864 struct placement **closure;
1865 GARY_INIT(closure, 0);
1866 bool *chosen = malloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1867 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1868 chosen[placement->request->ind] = 1;
1870 struct placement **p = GARY_PUSH(closure); *p = placement;
1873 while (first < GARY_SIZE(closure))
1875 if (dbg_breeding >= VERBOSITY_ALL)
1876 printf("Iterating, first is %d of current %u\n", first, GARY_SIZE(closure));
1877 struct placement **overlapping = get_overlapping(placement);
1878 if (! overlapping) { first++; continue; }
1880 struct placement **filtered = filter(overlapping, &chosen);
1881 if (dbg_breeding >= VERBOSITY_ALL)
1882 printf("There are %u new overlapping symbols\n", GARY_SIZE(filtered));
1883 GARY_FREE(overlapping);
1884 overlapping = filtered;
1885 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1887 if (! chosen[overlapping[j]->request->ind])
1889 if (overlaps(*p, overlapping[j]))
1891 p = GARY_PUSH(closure); *p = overlapping[j];
1892 if (dbg_breeding >= VERBOSITY_ALL)
1893 printf("Adding placement of request %d (in fact at [%.2f; %.2f] of size %d x %d)\n", overlapping[j]->request->ind, overlapping[j]->x, overlapping[j]->y, overlapping[j]->request->variants[overlapping[j]->variant_used].width, overlapping[j]->request->variants[overlapping[j]->variant_used].height);
1894 chosen[overlapping[j]->request->ind] = 1;
1898 GARY_FREE(overlapping);
1907 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1909 bool *processed = *processed_ptr;
1910 if (dbg_breeding >= VERBOSITY_ALL)
1911 printf("Will copy %u symbols\n", GARY_SIZE(closure));
1913 for (uns i=0; i<GARY_SIZE(closure); i++)
1915 processed[closure[i]->ind] = 1;
1916 int ind = closure[i]->ind;
1917 child->placements[ind] = parent->placements[ind];
1918 child->placements[ind].individual = child;
1919 child->placements[ind].processed = 0;
1920 child->placements[ind].map_links = NULL;
1921 update_map_parts(&child->placements[ind]);
1925 void move_symbol(struct placement *p)
1927 switch (p->request->type)
1931 move_symbol_point(p);
1933 case REQUEST_SEGMENT:
1934 move_symbol_segment(p);
1937 ASSERT(p->request->type != REQUEST_INVALID);
1941 void move_symbol_point(struct placement *p)
1943 p->x += gen_movement_uniform();
1944 p->y += gen_movement_uniform();
1947 void move_symbol_segment(struct placement *p)
1949 double m = gen_movement_uniform();
1952 p->y += m * ((struct request_segment *) p->request)->slope;
1955 void hide_segment_labels(struct individual *individual)
1957 // BEWARE: This fully depends on current genetic encoding
1959 int used = -1, num = -1;
1960 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1962 switch (individual->placements[i].request->type)
1964 case REQUEST_SECTION:
1965 used = individual->placements[i].variant_used;
1968 case REQUEST_SEGMENT:
1970 individual->placements[i].variant_used = 0;
1972 individual->placements[i].variant_used = -1;
1981 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1983 p->ind = num_placements++;
1986 p->x = p->y = 0; // To prevent valgrind from complaining
1987 p->variant_used = 0;
1988 p->map_links = NULL;
1989 p->individual = individual;
1992 case REQUEST_POINT: ;
1993 struct request_point *rp = (struct request_point *) r;
1997 case REQUEST_LINE: ;
1999 case REQUEST_SECTION: ;
2000 struct request_section *rls = (struct request_section *) r;
2001 p->variant_used = randint(0, rls->num_segments);
2003 case REQUEST_SEGMENT: ;
2004 struct request_segment *rs = (struct request_segment *) r;
2008 case REQUEST_AREA: ;
2009 struct request_area *ra = (struct request_area *) r;
2012 p->variant_used = 0;
2015 ASSERT(p->request->type != REQUEST_INVALID);
2019 if (dbg_init >= VERBOSITY_PLACEMENT)
2020 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
2023 void reset_individual_map(struct individual *i)
2025 for (uns j=0; j<num_map_parts; j++)
2027 struct map_placement *mp = i->map[j]->placement;
2030 struct map_placement *tmp = mp;
2031 mp = mp->next_in_map;
2036 struct map_part *part = malloc(sizeof(struct map_part));
2039 mp = malloc(sizeof(struct map_placement));
2040 part->placement = mp;
2041 mp->placement = &dummy_placement;
2042 mp->next_in_map = mp->prev_in_map = NULL;
2043 mp->next_in_placement = mp->prev_in_placement = NULL;
2048 void update_individual(struct individual *individual)
2050 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
2052 update_map_parts_delete(&individual->placements[i]);
2056 void clear_individual(struct individual *individual)
2058 for (uns j=0; j<num_map_parts; j++)
2060 struct map_placement *mp = individual->map[j]->placement;
2063 struct map_placement *tmp = mp;
2064 mp = mp->next_in_map;
2068 free(individual->map[j]);
2071 GARY_FREE(individual->map);
2072 GARY_FREE(individual->placements);
2073 ep_free(ep_individuals, individual);
2076 void clear_population(struct individual **pop)
2078 for (uns i=0; i<GARY_SIZE(pop); i++)
2080 clear_individual(pop[i]);
2084 struct placement **get_overlapping(struct placement *p)
2086 struct placement **buffer;
2087 GARY_INIT(buffer, 0);
2089 struct map_part **parts = get_map_parts(p);
2090 if (! parts) return NULL;
2092 for (uns i=0; i<GARY_SIZE(parts); i++)
2094 struct map_placement *mp = parts[i]->placement->next_in_map;
2097 if (p->variant_used >= 0)
2099 struct placement **p = GARY_PUSH(buffer);
2102 mp = mp->next_in_map;
2107 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
2108 printf("Returning %u potentially overlapping placements\n", GARY_SIZE(buffer));
2113 struct placement **filter(struct placement **list, bool **pred_ptr)
2115 bool *pred = *pred_ptr; // As GARY can't be passed directly
2116 struct placement **filtered;
2117 GARY_INIT(filtered, 0);
2119 for (uns i=0; i<GARY_SIZE(list); i++)
2121 if (pred[list[i]->request->ind])
2124 struct placement **p = GARY_PUSH(filtered);
2131 int flip(int a, int b)
2133 return (random() % 2 ? a : b);
2136 double randdouble(void)
2138 return ((double) rand() / (double) RAND_MAX);
2141 void init_individual(struct individual *individual)
2143 GARY_INIT(individual->placements, num_requests);
2144 GARY_INIT(individual->map, 0);
2145 for (uns j=0; j<num_map_parts; j++)
2147 GARY_PUSH(individual->map);
2148 struct map_part *part = malloc(sizeof(struct map_part));
2149 struct map_placement *mp = malloc(sizeof(struct map_placement));
2150 part->placement = mp;
2152 mp->placement = &dummy_placement;
2153 mp->next_in_map = mp->prev_in_map = NULL;
2154 mp->next_in_placement = mp->prev_in_placement = NULL;
2155 individual->map[j] = part;
2157 individual->penalty = 0;
2160 void copy_individual(struct individual *src, struct individual *dest)
2162 init_individual(dest);
2163 dest->penalty = src->penalty;
2165 for (uns i=0; i<GARY_SIZE(src->placements); i++)
2167 dest->placements[i] = src->placements[i];
2168 dest->placements[i].map_links = NULL;
2169 dest->placements[i].individual = dest;
2171 update_map_parts_create(&dest->placements[i]);