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; int num_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 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
133 void perform_mutation(struct individual *individual);
134 void init_placement(struct placement *p, struct individual *individual, struct request *r);
135 void init_individual(struct individual *i);
136 void copy_individual(struct individual *src, struct individual *dest);
137 int cmp_individual(const void *a, const void *b);
139 void clear_individual(struct individual *individual);
140 void clear_population(struct individual **pop);
142 void make_bitmap(struct variant *v, struct symbol *sym);
143 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
144 void make_bitmap_point(struct variant *v, struct sym_point *sp);
145 void make_bitmap_label(struct variant *v, struct sym_text *text);
147 double gen_movement(void);
148 double gen_movement_uniform(void);
149 void move_symbol(struct placement *p);
150 void move_symbol_point(struct placement *p);
151 void move_symbol_segment(struct placement *p);
152 void hide_segment_labels(struct individual *individual);
154 void gen_coords(struct placement *p);
155 void gen_coords_point(struct placement *p);
156 void gen_coords_segment(struct placement *p);
157 void gen_coords_area(struct placement *p);
159 struct map_part **get_map_parts(struct placement *p);
160 void update_map_parts(struct placement *p);
161 struct placement **get_closure(struct placement *placement);
162 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
163 struct placement **get_overlapping(struct placement *p);
164 struct placement **filter(struct placement **list, bool **pred_ptr);
167 void make_graph(void);
168 void label_graph(void);
169 void bfs_wrapper(void);
170 void bfs(uns longline);
171 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
172 void make_segments(void);
174 void cut_edge(struct graph_edge *e, double dist);
175 struct request_line *make_new_line(void);
176 struct request_section *make_new_section(struct request_line *rl);
177 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
179 void dump_bitmaps(struct individual *individual);
180 void dump_graph(void);
181 void dump_longlines(void);
182 void dump_linelabel_requests(void);
183 void dump_individual(struct individual *individual);
184 void dump_label(struct symbol *sym);
185 void dump_penalties(struct individual **population);
187 int randint(int min, int max);
188 int flip(int a, int b);
189 double randdouble(void);
191 int max2(int a, int b);
192 int min2(int a, int b);
193 int max4(int a, int b, int c, int d);
194 int min4(int a, int b, int c, int d);
196 struct placement dummy_placement;
198 int max2(int a, int b)
200 return (a > b ? a : b);
203 int min2(int a, int b)
205 return (a < b ? a : b);
208 int max4(int a, int b, int c, int d)
210 return max2(max2(a, b), max2(c, d));
213 int min4(int a, int b, int c, int d)
215 return min2(min2(a, b), min2(c, d));
218 void dump_label(struct symbol *sym)
222 case SYMBOLIZER_TEXT: ;
223 struct sym_text *st = (struct sym_text *) sym;
224 printf("%s\n", osm_val_decode(st->text));
231 void labeller_init(void)
233 GARY_INIT(requests_point, 0);
234 GARY_INIT(requests_line, 0);
235 GARY_INIT(requests_area, 0);
236 GARY_INIT(buffer_line, 0);
237 GARY_INIT(buffer_linelabel, 0);
238 ep_individuals = ep_new(sizeof(struct individual), 1);
243 void make_bitmap(struct variant *v, struct symbol *sym)
245 v->offset_x = v->offset_y = 0;
249 case SYMBOLIZER_POINT:
250 make_bitmap_point(v, (struct sym_point *) sym);
252 case SYMBOLIZER_ICON:
253 make_bitmap_icon(v, (struct sym_icon *) sym);
255 case SYMBOLIZER_TEXT:
256 make_bitmap_label(v, (struct sym_text *) sym);
259 ASSERT(sym->type != SYMBOLIZER_INVALID);
263 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
265 v->width = si->sir.width + 1;
266 v->height = si->sir.height + 1;
267 v->bitmap = malloc(v->width * v->height * sizeof(bool));
268 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
271 void make_bitmap_point(struct variant *v, struct sym_point *sp)
273 v->width = v->height = sp->size + 1;
274 v->bitmap = malloc(v->width * v->height * sizeof(bool));
275 // FIXME: Okay, memset would be much nicer here
276 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
279 void make_bitmap_label(struct variant *v, struct sym_text *text)
281 v->width = ceil(text->tw);
282 v->height = ceil(text->th);
283 v->bitmap = malloc(v->width * v->height * sizeof(bool));
284 for (int i=0; i<v->height; i++)
285 for (int j=0; j<v->width; j++)
287 v->bitmap[i*v->width + j] = 1;
291 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
293 if (dbg_requests >= VERBOSITY_PLACEMENT)
294 printf("Adding point\n");
295 if (object->type != OSM_TYPE_NODE)
297 printf("Warning: Point label requested on non-point object\n");
301 struct request_point *r = GARY_PUSH(requests_point);
303 r->request.type = REQUEST_POINT;
304 r->request.ind = num_requests++;
313 GARY_INIT(r->request.variants, 0);
315 struct variant *v = GARY_PUSH(r->request.variants);
317 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
323 case SYMBOLIZER_ICON:
325 r->x = ((struct sym_icon *)sym)->sir.x;
326 r->y = ((struct sym_icon *)sym)->sir.y;
333 if (dbg_requests >= VERBOSITY_PLACEMENT)
334 printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
337 void labeller_add_line(struct symbol *sym, z_index_t zindex)
339 if (dbg_requests >= VERBOSITY_PLACEMENT)
340 printf("Adding line on %u\n", zindex);
341 struct buffer_line *b = GARY_PUSH(buffer_line);
342 b->line = (struct sym_line *) sym;
344 sym_plan(sym, zindex);
347 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
349 if (o->type != OSM_TYPE_WAY)
351 printf("Linelabel request on object which is not way\n");
355 if (dbg_requests >= VERBOSITY_PLACEMENT)
356 printf("Labelling way %ju on %u\n", o->id, zindex);
357 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
358 ll->way = (struct osm_way *) o;
363 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
365 if (dbg_requests >= VERBOSITY_PLACEMENT)
366 printf("Adding area on %u\n", zindex);
367 struct request_area *r = GARY_PUSH(requests_area);
369 r->request.type = REQUEST_AREA;
370 r->request.ind = num_requests++;
372 r->o = (struct osm_multipolygon *) o;
376 osm_obj_center(o, &(r->cx), &(r->cy));
378 GARY_INIT(r->request.variants, 0);
379 struct variant *v = GARY_PUSH(r->request.variants);
383 void make_graph(void)
386 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
388 if (dbg_graph >= VERBOSITY_GENERAL)
389 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
390 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
392 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
393 struct graph_node *g_prev = NULL;
394 struct osm_node *o_prev = NULL;
396 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
398 // FIXME: Shall osm_object's type be checked here?
399 struct osm_node *o_node = (struct osm_node *) ref->o;
401 struct graph_node *g_node = hash_find(ref->o->id);
404 g_node = hash_new(ref->o->id);
405 GARY_INIT(g_node->edges, 0);
407 g_node->id = ref->o->id;
408 g_node->num = num_nodes++;
418 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge));
419 e->num = num_edges++;
420 e->id = buffer_line[i].line->s.o->id;
421 e->color = buffer_line[i].line->color;
422 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
428 e->longline = (uns) -1;
429 e->line = buffer_line[i].line;
433 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
435 edge = GARY_PUSH(g_node->edges);
444 void dump_graph(void)
446 HASH_FOR_ALL(hash, node)
448 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
449 for (uns i=0; i<GARY_SIZE(node->edges); i++)
451 struct graph_edge *e = node->edges[i];
452 printf("\t edge (%d) #%ju to ", e->num, e->id);
453 if (node->edges[i]->n1->id == node->id)
454 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
455 else if (node->edges[i]->n2->id == node->id)
456 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
459 // This shouldn't ever happen
460 printf("BEWARE! Edge is associated with a node it doesn't belongs to!\n");
465 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));
466 else if ((node->edges[i]->label)) printf("Labelled\n");
468 printf(" colored %d;", node->edges[i]->color);
469 printf(" length %.2f", node->edges[i]->length);
476 void label_graph(void)
478 if (dbg_graph >= VERBOSITY_GENERAL)
479 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
480 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
482 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
483 if (dbg_graph >= VERBOSITY_INDIVIDUAL)
484 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
485 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
487 if (dbg_graph >= VERBOSITY_PLACEMENT)
488 printf("Looking for node %ju\n", ref->o->id);
489 struct graph_node *n = hash_find(ref->o->id);
492 printf("BEWARE! Requested node couldn't be found.\n");
496 if (dbg_graph >= VERBOSITY_ALL)
497 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
498 for (uns j=0; j<GARY_SIZE(n->edges); j++)
500 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
502 if (dbg_graph >= VERBOSITY_ALL)
503 printf("Labelling node %ju\n", n->id);
504 n->edges[j]->label = buffer_linelabel[i].label;
505 n->edges[j]->zindex = buffer_linelabel[i].zindex;
513 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
515 if (dbg_bfs >= VERBOSITY_PLACEMENT)
516 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
517 struct graph_edge *candidate = NULL;
519 for (uns i=0; i<GARY_SIZE(node->edges); i++)
521 struct graph_edge *other = node->edges[i];
522 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
524 if ((uns) other->visited != e->longline) {
525 if (dbg_bfs >= VERBOSITY_PLACEMENT)
526 printf("Pushing new edge %d / %ju\n", other->num, other->id);
527 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
529 other->visited = e->longline;
532 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
533 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
536 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
537 (e->label) && (other->label) &&
538 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
539 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
541 if (! candidate || (other->length > candidate->length))
548 if (dbg_bfs >= VERBOSITY_PLACEMENT)
549 printf("New line in longline %u\n", e->longline);
550 struct graph_edge *other = candidate;
551 other->longline = e->longline;
553 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
554 ((dir == DIR_FWD) && (other->n2->id == node->id)))
556 struct graph_node *swp = other->n2;
557 other->n2 = other->n1;
566 longlines[other->longline].first = other;
579 void bfs(uns longline)
581 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
583 printf("BFS called for longline %u\n", longline);
584 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
587 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
589 struct graph_edge *cur = bfs_queue[i];
590 if (dbg_bfs >= VERBOSITY_PLACEMENT)
591 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
593 cur->visited = longline;
595 if (cur->longline == (uns) -1)
598 if (cur->dir == DIR_UNSET)
600 cur->dir = DIR_CENTER;
601 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
602 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
609 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
612 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
622 void bfs_wrapper(void)
624 GARY_INIT(bfs_queue, 0);
625 GARY_INIT(longlines, 0);
627 HASH_FOR_ALL(hash, node)
629 for (uns i=0; i<GARY_SIZE(node->edges); i++)
631 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
633 GARY_PUSH(longlines);
634 longlines[num_longlines].first = node->edges[i];
636 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
638 printf("Running new BFS\n");
639 printf("Creating longline %u\n", num_longlines);
642 GARY_RESIZE(bfs_queue, 0);
643 struct graph_edge **e = GARY_PUSH(bfs_queue);
645 node->edges[i]->longline = num_longlines;
646 bfs(node->edges[i]->longline);
648 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
650 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
651 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
659 GARY_FREE(bfs_queue);
662 void dump_longlines(void)
664 printf("*** Longlines dump\n");
665 for (uns i=0; i<GARY_SIZE(longlines); i++)
667 printf("Longline %u:", i);
668 struct graph_edge *e = longlines[i].first;
669 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
670 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
675 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
676 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
683 struct request_line *make_new_line(void)
685 struct request_line *rl = GARY_PUSH(requests_line);
686 rl->request.ind = num_requests++;
687 rl->request.type = REQUEST_LINE;
688 GARY_INIT(rl->sections, 0);
689 GARY_INIT(rl->request.variants, 0);
694 struct request_section *make_new_section(struct request_line *rl)
696 struct request_section *rls = GARY_PUSH(rl->sections);
697 rls->request.ind = num_requests++;
698 rls->request.type = REQUEST_SECTION;
699 rls->num_segments = 0;
700 GARY_INIT(rls->segments, 0);
701 GARY_INIT(rls->request.variants, 0);
706 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
708 struct request_segment *rs = GARY_PUSH(rls->segments);
711 rs->request.ind = num_requests++;
712 rs->request.type = REQUEST_SEGMENT;
714 GARY_INIT(rs->request.variants, 0);
715 struct variant *v = GARY_PUSH(rs->request.variants);
721 void cut_edge(struct graph_edge *e, double dist)
723 if (dbg_segments >= VERBOSITY_PLACEMENT)
724 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);
726 struct graph_edge *new = malloc(sizeof(struct graph_edge));
730 switch (e->label->type)
732 case SYMBOLIZER_TEXT:
733 new->label = malloc(sizeof(struct sym_text));
734 *((struct sym_text *) new->label) = *((struct sym_text *) e->label);
740 struct osm_node *n1 = e->n1->o;
741 struct osm_node *n2 = e->n2->o;
743 if ((n1->x == n2->x) && (n1->y == n2->y))
745 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
746 if (dbg_segments >= VERBOSITY_PLACEMENT)
747 printf("Won't cut point\n");
751 struct osm_node *n11 = malloc(sizeof(struct osm_node));
752 struct graph_node *gn = malloc(sizeof(struct graph_node));
754 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
755 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
756 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
758 e->n2 = new->n1 = gn;
760 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
761 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
765 void make_segments(void)
767 for (uns i=0; i<GARY_SIZE(longlines); i++)
769 // Skip lines which are not labelled
770 if (! (longlines[i].first && longlines[i].first->label))
773 struct request_line *request = make_new_line();
774 struct request_section *rls = make_new_section(request);
775 struct request_segment *rs = NULL;
777 struct graph_edge *e = longlines[i].first;
778 double cur_length = 0;
780 struct sym_text *st = NULL;
781 if (e->label->type == SYMBOLIZER_TEXT)
783 st = (struct sym_text *) e->label;
787 // FIXME: Should other label types be supported in future?
788 if (dbg_segments >= VERBOSITY_PLACEMENT)
789 printf("Warning: Skipping line\n");
793 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
794 printf("New longline\n");
800 if (dbg_segments >= VERBOSITY_PLACEMENT)
801 printf("BEWARE: Edge cycle\n");
806 if (dbg_segments >= VERBOSITY_PLACEMENT)
807 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);
809 if (st && (e->length < st->tw))
812 if (dbg_segments >= VERBOSITY_PLACEMENT)
813 printf("Warning: Skipping segment\n");
817 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
819 if (dbg_segments >= VERBOSITY_PLACEMENT)
820 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);
821 // HACK to prevent cutting to 0 lenght
822 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
825 rs = make_new_segment(rls, e->label);
826 rs->label = malloc(sizeof(struct sym_text));
827 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
829 rs->x1 = e->n1->o->x;
830 rs->y1 = e->n1->o->y;
831 rs->x2 = e->n2->o->x;
832 rs->y2 = e->n2->o->y;
834 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
835 rs->zindex = e->zindex;
837 cur_length += e->length;
838 if (cur_length > conf_max_section_length)
840 if (dbg_segments >= VERBOSITY_PLACEMENT)
841 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);
843 rls = make_new_section(request);
850 if (request->sections[0].num_segments == 0)
852 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
853 printf("WARNING: Longline without any segment, skipped\n");
855 struct request_section *rls = &request->sections[0];
856 GARY_FREE(rls->segments);
857 GARY_FREE(rls->request.variants);
859 struct request_line *rl = &requests_line[GARY_SIZE(requests_line)-1];
860 GARY_FREE(rl->sections);
861 GARY_FREE(rl->request.variants);
863 GARY_POP(requests_line);
869 void dump_linelabel_requests(void)
871 for (uns i=0; i<GARY_SIZE(requests_line); i++)
873 if (requests_line[i].sections[0].num_segments == 0)
875 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
876 printf("Beware: Longline without any segment\n");
880 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
881 dump_label(requests_line[i].sections[0].segments[0].label);
882 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
884 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
885 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
887 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
888 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
895 void dump_bitmaps(struct individual *individual)
897 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
898 printf("Bitmap size is %d\n", page_width_int * page_height_int);
899 for (int i=0; i<page_height_int; i++)
900 for (int j=0; j<page_width_int; j++)
901 bitmap[i*page_width_int + j] = 0;
904 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
906 if (individual->placements[i].variant_used == -1) continue;
908 struct placement *p = &(individual->placements[i]);
909 struct variant *v = NULL;
911 switch (p->request->type)
913 case REQUEST_SEGMENT: ;
914 case REQUEST_POINT: ;
916 v = &(p->request->variants[p->variant_used]);
919 ASSERT(p->request->type != REQUEST_INVALID);
923 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
925 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
927 if (bitmap[row * page_width_int + col] == 1) total += 1;
928 bitmap[row * page_width_int + col] = 1;
932 if (dbg_overlaps >= VERBOSITY_GENERAL)
933 printf("There were %d collisions during bitmap dump\n", total);
935 FILE *fd_dump = fopen("dump.pbm", "w");
936 fprintf(fd_dump, "P1\n");
937 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
938 for (int i=0; i<page_height_int; i++)
940 for (int j=0; j<page_width_int; j++)
942 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
944 fprintf(fd_dump, "\n");
951 void dump_individual(struct individual *individual)
953 printf("*** Individual dump\n");
954 printf("(There are %d requests)\n", num_requests);
956 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
958 struct placement *p = &(individual->placements[i]);
960 switch (p->request->type)
963 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
966 struct request_line *rl = (struct request_line *) p->request;
968 dump_label(rl->sections[0].segments[0].label);
970 case REQUEST_SECTION: ;
973 case REQUEST_SEGMENT: ;
974 if (p->variant_used >= 0)
975 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
977 printf("Segment not placed\n");
980 struct request_area *ra = (struct request_area *) p->request;
981 printf("Area label ");
982 dump_label(ra->label);
983 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
986 ASSERT(p->request->type != 0);
989 printf("\nTotal penalty: %d\n", individual->penalty);
992 void plan_individual(struct individual *individual)
994 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
996 struct symbol *s = NULL;
997 z_index_t zindex = 0;
998 if (individual->placements[i].variant_used < 0) continue;
999 switch (individual->placements[i].request->type)
1001 case REQUEST_POINT: ;
1002 struct request_point *rp = (struct request_point *) individual->placements[i].request;
1004 zindex = rp->zindex;
1006 case REQUEST_SEGMENT: ;
1007 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1009 zindex = rs->zindex;
1011 case REQUEST_LINE: ;
1013 case REQUEST_AREA: ;
1014 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1016 zindex = ra->zindex;
1019 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1023 if (dbg_plan >= VERBOSITY_PLACEMENT)
1024 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1026 if (s) switch (s->type)
1028 case SYMBOLIZER_POINT: ;
1029 struct sym_point *sp = (struct sym_point *) s;
1030 sp->x = individual->placements[i].x;
1031 sp->y = individual->placements[i].y;
1032 sym_plan((struct symbol *) sp, zindex);
1034 case SYMBOLIZER_ICON: ;
1035 struct sym_icon *si = (struct sym_icon *) s;
1036 si->sir.x = individual->placements[i].x;
1037 si->sir.y = individual->placements[i].y;
1038 sym_plan((struct symbol *) si, zindex);
1040 case SYMBOLIZER_TEXT: ;
1041 struct sym_text *st = (struct sym_text *) s;
1042 st->x = individual->placements[i].x;
1043 st->y = individual->placements[i].y;
1044 st->next_duplicate = NULL;
1045 if (dbg_plan >= VERBOSITY_PLACEMENT)
1046 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);
1047 sym_plan((struct symbol *) st, zindex);
1050 ASSERT(s->type != SYMBOLIZER_INVALID);
1055 void dump_penalties(struct individual **population)
1057 for (int i=0; i<conf_pop_size; i++)
1059 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1063 void compute_sizes(void)
1065 page_width_int = floor(page_width);
1066 page_height_int = floor(page_height);
1068 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1069 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1070 num_map_parts = num_map_parts_row * num_map_parts_col;
1072 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1073 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1074 if (dbg_evolution >= VERBOSITY_GENERAL)
1076 printf("Breeding parameters:\n");
1077 printf(" %d individuals are created\n", breed_pop_size);
1078 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1081 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1082 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1083 if (dbg_evolution >= VERBOSITY_GENERAL)
1085 printf("Mutation parameters:\n");
1086 printf(" %d individuals are created\n", mutate_pop_size);
1087 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1090 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1091 if (dbg_evolution >= VERBOSITY_GENERAL)
1093 printf("Elitism parameters:\n");
1094 printf(" %d best individuals are copied\n", elite_pop_size);
1097 if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1101 elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1105 fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1106 fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1112 void labeller_label(void)
1119 GARY_INIT(population1, conf_pop_size);
1120 GARY_INIT(population2, conf_pop_size);
1123 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1125 if (dbg_evolution >= VERBOSITY_GENERAL)
1127 printf("Penalties after initialization\n");
1128 dump_penalties(population1);
1131 while (! shall_terminate())
1135 printf("\n*** Iteration %d ***\n", iteration);
1141 struct individual **swp = population1;
1142 population1 = population2;
1145 clear_population(population2);
1149 if (dbg_evolution >= VERBOSITY_GENERAL)
1150 printf("Sorting population\n");
1151 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1153 if (dbg_evolution >= VERBOSITY_GENERAL)
1155 printf("Penalties after sort\n");
1156 dump_penalties(population1);
1159 old_best = population1[0]->penalty;
1162 if (dbg_overlaps >= VERBOSITY_GENERAL)
1163 dump_bitmaps(population1[0]);
1165 plan_individual(population1[0]);
1172 void labeller_cleanup(void)
1175 GARY_FREE(requests_point);
1176 GARY_FREE(requests_line);
1177 GARY_FREE(requests_area);
1180 void make_population(void)
1182 for (int i=0; i<conf_pop_size; i++)
1184 num_placements = 0; // FIXME: This IS a terrible HACK
1185 struct individual *i2 = ep_alloc(ep_individuals);
1186 init_individual(i2);
1187 population2[i] = i2;
1189 if (dbg_init >= VERBOSITY_INDIVIDUAL)
1190 printf("Making individual %d\n", i);
1191 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1192 population1[i] = individual;
1195 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1197 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1200 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1202 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1204 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1206 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1208 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1210 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1215 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1217 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1220 hide_segment_labels(individual);
1222 ASSERT(p == num_requests);
1226 bool shall_terminate(void)
1228 switch (conf_term_cond)
1230 case TERM_COND_PENALTY:
1231 return (population1[0]->penalty < conf_penalty_bound);
1232 case TERM_COND_STAGNATION:
1233 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1234 case TERM_COND_ITERATIONS:
1235 return (iteration >= conf_iteration_limit);
1237 fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1246 struct individual **breed_buffer;
1247 while (i < breed_pop_size)
1249 int parent1 = randint(0, breed_rbest_size);
1250 int parent2 = randint(0, breed_rbest_size);
1251 if (dbg_breeding >= VERBOSITY_INDIVIDUAL)
1252 printf("Will breed %d and %d\n", parent1, parent2);
1254 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1255 population2[pop2_ind++] = breed_buffer[0];
1256 population2[pop2_ind++] = breed_buffer[1];
1264 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1266 struct individual **buffer = malloc(2*sizeof(struct individual));
1267 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1268 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1271 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1273 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1275 if (! processed[parent1->placements[i].ind])
1277 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1278 printf("Creating symbol closure for placement %u\n", i);
1280 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1281 int x = randint(0, 2);
1285 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1286 printf("Copying parent->child 1->1 and 2->2\n");
1287 copy_symbols(clos_symbols, parent1, child1, &processed);
1288 copy_symbols(clos_symbols, parent2, child2, &processed);
1292 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1293 printf("Copying parent->child 2->1 and 1->2\n");
1294 copy_symbols(clos_symbols, parent2, child1, &processed);
1295 copy_symbols(clos_symbols, parent1, child2, &processed);
1298 GARY_FREE(clos_symbols);
1302 GARY_FREE(processed);
1304 if (conf_mutate_children)
1306 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1307 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1317 for (int i=0; i < mutate_pop_size; i++)
1319 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1320 printf("Creating %d-th individual by mutation\n", i);
1321 int ind = randint(0, mutate_rbest_size);
1322 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1323 printf("Mutating %d-th individual of original population\n", ind);
1324 population2[pop2_ind] = ep_alloc(ep_individuals);
1325 copy_individual(population1[ind], population2[pop2_ind]);
1326 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1327 printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1328 perform_mutation(population2[pop2_ind]);
1333 void perform_mutation(struct individual *individual)
1335 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1337 double x = randdouble();
1340 if (x <= acc + conf_mutate_move_bound)
1342 if (dbg_mutation >= VERBOSITY_PLACEMENT)
1343 printf("Mutation: Moving symbol in placement %u\n", i);
1344 move_symbol(&(individual->placements[i]));
1347 acc += conf_mutate_move_bound;
1349 if (x <= acc + conf_mutate_regen_bound)
1351 gen_coords(&(individual->placements[i]));
1354 acc += conf_mutate_regen_bound;
1356 if (x <= acc + conf_mutate_chvar_bound)
1358 struct placement *p = &(individual->placements[i]);
1359 switch (p->request->type)
1362 case REQUEST_SEGMENT:
1364 // Does nothing when there are 0 variants... does it mind?
1365 p->variant_used = randint(0, GARY_SIZE(p->request->variants) - 1);
1367 case REQUEST_SECTION:
1368 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments)-1);
1376 hide_segment_labels(individual);
1381 for (int i=0; i<elite_pop_size; i++)
1383 population2[pop2_ind] = ep_alloc(ep_individuals);
1384 copy_individual(population1[i], population2[pop2_ind++]);
1388 int overlaps(struct placement *p1, struct placement *p2)
1390 if (p1->request->type != REQUEST_POINT &&
1391 p1->request->type != REQUEST_SEGMENT &&
1392 p1->request->type != REQUEST_AREA)
1395 if (p2->request->type != REQUEST_POINT &&
1396 p2->request->type != REQUEST_SEGMENT &&
1397 p2->request->type != REQUEST_AREA)
1400 if (p1->variant_used == -1 || p2->variant_used == -1)
1403 struct variant *v1, *v2;
1405 v1 = &(p1->request->variants[p1->variant_used]);
1406 v2 = &(p2->request->variants[p2->variant_used]);
1408 // FIXME: This doesn't fully respect offset which it probably should
1409 int p1x = p1->x; int p1y = p1->y;
1410 int p2x = p2->x; int p2y = p2->y;
1413 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1414 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1416 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1417 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1424 int get_overlap(struct placement *p)
1426 if (p->variant_used == -1) return 0;
1428 struct map_part **parts = get_map_parts(p);
1431 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1432 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1436 struct placement **others;
1439 GARY_INIT_ZERO(planned, num_requests);
1440 planned[p->request->ind] = 1;
1441 GARY_INIT(others, 0);
1443 for (uns i=0; i<GARY_SIZE(parts); i++)
1445 struct map_placement *mp = parts[i]->placement->next_in_map;
1448 if (! planned[mp->placement->request->ind])
1450 struct placement **p = GARY_PUSH(others);
1452 planned[mp->placement->request->ind] = true;
1454 mp = mp->next_in_map;
1459 for (uns i=0; i<GARY_SIZE(others); i++)
1461 overlap += overlaps(p, others[i]);
1468 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1469 printf("Placement of request %d add %d to overlaps\n", p->request->ind, overlap);
1471 if (p->x < 0) overlap += 0 - p->x;
1472 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1473 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1475 if (p->y < 0) overlap += 0 - p->y;
1476 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1477 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1482 int individual_overlap(struct individual *individual)
1486 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1488 overlap += get_overlap(&individual->placements[i]);
1494 double get_distance(struct placement *p)
1496 if (p->variant_used < 0) return 0;
1497 struct variant *v = &p->request->variants[p->variant_used];
1499 double dx, dy, distance;
1500 switch (p->request->type)
1502 case REQUEST_POINT: ;
1503 struct request_point *rp = (struct request_point *) p->request;
1504 dx = rp->x + v->offset_x - p->x;
1505 dy = rp->y + v->offset_y - p->y;
1506 distance = sqrt(dx*dx + dy*dy);
1507 if (dbg_rank >= VERBOSITY_PLACEMENT)
1508 printf("Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1510 case REQUEST_AREA: ;
1511 struct request_area *ra = (struct request_area *) p->request;
1512 dx = ra->cx + v->offset_x - p->x;
1513 dy = ra->cy + v->offset_y - p->y;
1514 distance = sqrt(dx*dx + dy*dy);
1515 if (dbg_rank >= VERBOSITY_PLACEMENT)
1516 printf("Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1522 if (dbg_rank >= VERBOSITY_PLACEMENT)
1523 printf("Placement %d has distance %.2f\n", p->ind, distance);
1527 double individual_distances(struct individual *individual)
1531 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1533 distances += get_distance(&individual->placements[i]);
1539 int cmp_individual(const void *a, const void *b)
1541 struct individual **ia = (struct individual **) a;
1542 struct individual **ib = (struct individual **) b;
1544 return (*ia)->penalty - (*ib)->penalty;
1547 void rank_population(void)
1551 for (int i=0; i<conf_pop_size; i++)
1553 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1554 printf("Individual %d\n", i);
1555 population1[i]->penalty = 0;
1557 penalty = individual_overlap(population1[i]);
1558 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1559 printf("Increasing penalty by %d for overlap\n", penalty);
1560 population1[i]->penalty += penalty;
1562 penalty = individual_distances(population1[i]);
1563 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1564 printf("Increasing penalty by %d for distances\n", penalty);
1565 population1[i]->penalty += penalty;
1569 struct map_part **get_map_parts(struct placement *p)
1571 if (p->variant_used < 0) return NULL;
1573 struct map_part **buffer;
1574 GARY_INIT(buffer, 0);
1576 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1577 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1580 switch (p->request->type)
1583 case REQUEST_SEGMENT:
1585 v = p->request->variants[p->variant_used];
1588 if (dbg_map_parts >= VERBOSITY_ALL)
1589 printf("Skipping unsupported request type (%d)\n", p->request->type);
1593 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1594 printf("Bitmap is %d x %d\n", v.width, v.height);
1596 int x_min = max2(0, p->x) / conf_map_part_width;
1597 // CHECK ME: Is rounding needed?
1598 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1599 int y_min = max2(0, p->y) / conf_map_part_height;
1600 // CHECK ME: Is rounding needed?
1601 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1603 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1604 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1606 for (int y=y_min; y<=y_max; y++)
1607 for (int x=x_min; x<=x_max; x++)
1609 struct map_part **m = GARY_PUSH(buffer);
1610 if (dbg_map_parts >= VERBOSITY_ALL)
1611 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1612 *m = p->individual->map[y * num_map_parts_row + x];
1615 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1616 printf("Returning %u map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1621 void update_map_parts_delete(struct placement *p)
1623 struct map_placement *mp = p->map_links;
1626 mp->prev_in_map->next_in_map = mp->next_in_map;
1627 if (mp->next_in_map)
1628 mp->next_in_map->prev_in_map = mp->prev_in_map;
1630 struct map_placement *tmp = mp;
1631 mp = mp->next_in_placement;
1634 p->map_links = NULL;
1637 void update_map_parts_create(struct placement *p)
1639 struct map_part **parts = get_map_parts(p);
1640 if (parts == NULL) return;
1642 for (uns i=0; i<GARY_SIZE(parts); i++)
1644 struct map_placement *mp = malloc(sizeof(struct map_placement));
1646 mp->part = parts[i];
1648 mp->next_in_map = parts[i]->placement->next_in_map;
1649 mp->prev_in_map = parts[i]->placement;
1650 parts[i]->placement->next_in_map = mp;
1651 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1653 mp->next_in_placement = p->map_links;
1654 mp->prev_in_placement = NULL;
1661 void update_map_parts(struct placement *p)
1663 update_map_parts_delete(p);
1664 update_map_parts_create(p);
1667 void gen_coords(struct placement *p)
1669 switch(p->request->type)
1672 gen_coords_point(p);
1677 case REQUEST_SEGMENT:
1678 gen_coords_segment(p);
1682 printf("Not yet implemented\n");
1685 if (dbg_movement >= VERBOSITY_ALL)
1686 printf("Testing request type\n");
1687 ASSERT(p->request->type != REQUEST_INVALID);
1690 update_map_parts(p);
1693 double gen_movement(void)
1695 double m = (random() % 100000) / 10000;
1696 m = pow(m, 1.0/3) * flip(1, -1);
1697 if (dbg_movement >= VERBOSITY_ALL)
1698 printf("Movement %.2f\n", m);
1702 double gen_movement_uniform(void)
1704 return (move_max - move_min) * randdouble() * flip(1, -1);
1707 void gen_coords_point(struct placement *p)
1709 p->x = p->x + gen_movement();
1712 void gen_coords_segment(struct placement *p)
1714 struct request_segment *rs = (struct request_segment *) p->request;
1716 p->x = (a == 1 ? rs->x1 : rs->x2);
1717 p->y = (a == 1 ? rs->y1 : rs->y2);
1720 void gen_coords_area(struct placement *p)
1722 struct request_area *ra = (struct request_area *) p->request;
1724 p->x = p->x + gen_movement();
1725 p->y = p->y + gen_movement();
1727 if (dbg_movement >= VERBOSITY_PLACEMENT)
1728 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1731 int randint(int min, int max)
1733 if (min == max) return min;
1735 return min + (r % (max - min));
1738 struct placement **get_closure(struct placement *placement)
1740 struct placement **closure;
1741 GARY_INIT(closure, 0);
1742 bool *chosen = malloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1743 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1744 chosen[placement->request->ind] = 1;
1746 struct placement **p = GARY_PUSH(closure); *p = placement;
1749 while (first < GARY_SIZE(closure))
1751 if (dbg_breeding >= VERBOSITY_ALL)
1752 printf("Iterating, first is %d of current %u\n", first, GARY_SIZE(closure));
1753 struct placement **overlapping = get_overlapping(placement);
1754 if (! overlapping) { first++; continue; }
1756 struct placement **filtered = filter(overlapping, &chosen);
1757 if (dbg_breeding >= VERBOSITY_ALL)
1758 printf("There are %u new overlapping symbols\n", GARY_SIZE(filtered));
1759 GARY_FREE(overlapping);
1760 overlapping = filtered;
1761 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1763 if (! chosen[overlapping[j]->request->ind])
1765 if (overlaps(*p, overlapping[j]))
1767 p = GARY_PUSH(closure); *p = overlapping[j];
1768 if (dbg_breeding >= VERBOSITY_ALL)
1769 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);
1770 chosen[overlapping[j]->request->ind] = 1;
1774 GARY_FREE(overlapping);
1783 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1785 bool *processed = *processed_ptr;
1786 if (dbg_breeding >= VERBOSITY_ALL)
1787 printf("Will copy %u symbols\n", GARY_SIZE(closure));
1789 for (uns i=0; i<GARY_SIZE(closure); i++)
1791 processed[closure[i]->ind] = 1;
1792 int ind = closure[i]->ind;
1793 child->placements[ind] = parent->placements[ind];
1794 child->placements[ind].individual = child;
1795 child->placements[ind].processed = 0;
1796 child->placements[ind].map_links = NULL;
1797 update_map_parts(&child->placements[ind]);
1801 void move_symbol(struct placement *p)
1803 switch (p->request->type)
1807 move_symbol_point(p);
1809 case REQUEST_SEGMENT:
1810 move_symbol_segment(p);
1813 ASSERT(p->request->type != REQUEST_INVALID);
1817 void move_symbol_point(struct placement *p)
1819 p->x += gen_movement_uniform();
1820 p->y += gen_movement_uniform();
1823 void move_symbol_segment(struct placement *p)
1825 double m = gen_movement_uniform();
1828 p->y += m * ((struct request_segment *) p->request)->slope;
1831 void hide_segment_labels(struct individual *individual)
1833 // BEWARE: This fully depends on current genetic encoding
1835 int used = -1, num = -1;
1836 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1838 switch (individual->placements[i].request->type)
1840 case REQUEST_SECTION:
1841 used = individual->placements[i].variant_used;
1844 case REQUEST_SEGMENT:
1846 individual->placements[i].variant_used = 0;
1848 individual->placements[i].variant_used = -1;
1857 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1859 p->ind = num_placements++;
1862 p->x = p->y = 0; // To prevent valgrind from complaining
1863 p->variant_used = 0;
1864 p->map_links = NULL;
1865 p->individual = individual;
1868 case REQUEST_POINT: ;
1869 struct request_point *rp = (struct request_point *) r;
1873 case REQUEST_LINE: ;
1875 case REQUEST_SECTION: ;
1876 struct request_section *rls = (struct request_section *) r;
1877 p->variant_used = randint(0, rls->num_segments);
1879 case REQUEST_SEGMENT: ;
1880 struct request_segment *rs = (struct request_segment *) r;
1884 case REQUEST_AREA: ;
1885 struct request_area *ra = (struct request_area *) r;
1888 p->variant_used = 0;
1891 ASSERT(p->request->type != REQUEST_INVALID);
1895 if (dbg_init >= VERBOSITY_PLACEMENT)
1896 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1899 void reset_individual_map(struct individual *i)
1901 for (uns j=0; j<num_map_parts; j++)
1903 struct map_placement *mp = i->map[j]->placement;
1906 struct map_placement *tmp = mp;
1907 mp = mp->next_in_map;
1912 struct map_part *part = malloc(sizeof(struct map_part));
1915 mp = malloc(sizeof(struct map_placement));
1916 part->placement = mp;
1917 mp->placement = &dummy_placement;
1918 mp->next_in_map = mp->prev_in_map = NULL;
1919 mp->next_in_placement = mp->prev_in_placement = NULL;
1924 void update_individual(struct individual *individual)
1926 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1928 update_map_parts_delete(&individual->placements[i]);
1932 void clear_individual(struct individual *individual)
1934 for (uns j=0; j<num_map_parts; j++)
1936 struct map_placement *mp = individual->map[j]->placement;
1939 struct map_placement *tmp = mp;
1940 mp = mp->next_in_map;
1944 free(individual->map[j]);
1947 GARY_FREE(individual->map);
1948 GARY_FREE(individual->placements);
1949 ep_free(ep_individuals, individual);
1952 void clear_population(struct individual **pop)
1954 for (uns i=0; i<GARY_SIZE(pop); i++)
1956 clear_individual(pop[i]);
1960 struct placement **get_overlapping(struct placement *p)
1962 struct placement **buffer;
1963 GARY_INIT(buffer, 0);
1965 struct map_part **parts = get_map_parts(p);
1966 if (! parts) return NULL;
1968 for (uns i=0; i<GARY_SIZE(parts); i++)
1970 struct map_placement *mp = parts[i]->placement->next_in_map;
1973 if (p->variant_used >= 0)
1975 struct placement **p = GARY_PUSH(buffer);
1978 mp = mp->next_in_map;
1983 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1984 printf("Returning %u potentially overlapping placements\n", GARY_SIZE(buffer));
1989 struct placement **filter(struct placement **list, bool **pred_ptr)
1991 bool *pred = *pred_ptr; // As GARY can't be passed directly
1992 struct placement **filtered;
1993 GARY_INIT(filtered, 0);
1995 for (uns i=0; i<GARY_SIZE(list); i++)
1997 if (pred[list[i]->request->ind])
2000 struct placement **p = GARY_PUSH(filtered);
2007 int flip(int a, int b)
2009 return (random() % 2 ? a : b);
2012 double randdouble(void)
2014 return ((double) rand() / (double) RAND_MAX);
2017 void init_individual(struct individual *individual)
2019 GARY_INIT(individual->placements, num_requests);
2020 GARY_INIT(individual->map, 0);
2021 for (uns j=0; j<num_map_parts; j++)
2023 GARY_PUSH(individual->map);
2024 struct map_part *part = malloc(sizeof(struct map_part));
2025 struct map_placement *mp = malloc(sizeof(struct map_placement));
2026 part->placement = mp;
2028 mp->placement = &dummy_placement;
2029 mp->next_in_map = mp->prev_in_map = NULL;
2030 mp->next_in_placement = mp->prev_in_placement = NULL;
2031 individual->map[j] = part;
2033 individual->penalty = 0;
2036 void copy_individual(struct individual *src, struct individual *dest)
2038 init_individual(dest);
2039 dest->penalty = src->penalty;
2041 for (uns i=0; i<GARY_SIZE(src->placements); i++)
2043 dest->placements[i] = src->placements[i];
2044 dest->placements[i].map_links = NULL;
2045 dest->placements[i].individual = dest;
2047 update_map_parts_create(&dest->placements[i]);