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 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 struct placement **get_closure(struct placement *placement);
165 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
166 struct placement **get_overlapping(struct placement *p);
167 struct placement **filter(struct placement **list, bool **pred_ptr);
170 void make_graph(void);
171 void label_graph(void);
172 void bfs_wrapper(void);
173 void bfs(uns longline);
174 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
175 void make_segments(void);
177 void cut_edge(struct graph_edge *e, double dist);
178 struct request_line *make_new_line(void);
179 struct request_section *make_new_section(struct request_line *rl);
180 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
182 void dump_bitmaps(struct individual *individual);
183 void dump_graph(void);
184 void dump_longlines(void);
185 void dump_linelabel_requests(void);
186 void dump_individual(struct individual *individual);
187 void dump_label(struct symbol *sym);
188 void dump_penalties(struct individual **population);
190 int randint(int min, int max);
191 int flip(int a, int b);
192 double randdouble(void);
194 int max2(int a, int b);
195 int min2(int a, int b);
196 int max4(int a, int b, int c, int d);
197 int min4(int a, int b, int c, int d);
199 struct placement dummy_placement;
201 int max2(int a, int b)
203 return (a > b ? a : b);
206 int min2(int a, int b)
208 return (a < b ? a : b);
211 int max4(int a, int b, int c, int d)
213 return max2(max2(a, b), max2(c, d));
216 int min4(int a, int b, int c, int d)
218 return min2(min2(a, b), min2(c, d));
221 void dump_label(struct symbol *sym)
225 case SYMBOLIZER_TEXT: ;
226 struct sym_text *st = (struct sym_text *) sym;
227 printf("%s\n", osm_val_decode(st->text));
234 void labeller_init(void)
236 GARY_INIT(requests_point, 0);
237 GARY_INIT(requests_line, 0);
238 GARY_INIT(requests_area, 0);
239 GARY_INIT(buffer_line, 0);
240 GARY_INIT(buffer_linelabel, 0);
241 ep_individuals = ep_new(sizeof(struct individual), 1);
246 void make_bitmap(struct variant *v, struct symbol *sym)
248 v->offset_x = v->offset_y = 0;
252 case SYMBOLIZER_POINT:
253 make_bitmap_point(v, (struct sym_point *) sym);
255 case SYMBOLIZER_ICON:
256 make_bitmap_icon(v, (struct sym_icon *) sym);
258 case SYMBOLIZER_TEXT:
259 make_bitmap_label(v, (struct sym_text *) sym);
262 ASSERT(sym->type != SYMBOLIZER_INVALID);
266 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
268 v->width = si->sir.width + 1;
269 v->height = si->sir.height + 1;
270 v->bitmap = malloc(v->width * v->height * sizeof(bool));
271 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
274 void make_bitmap_point(struct variant *v, struct sym_point *sp)
276 v->width = v->height = sp->size + 1;
277 v->bitmap = malloc(v->width * v->height * sizeof(bool));
278 // FIXME: Okay, memset would be much nicer here
279 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
282 void make_bitmap_label(struct variant *v, struct sym_text *text)
284 v->width = ceil(text->tw);
285 v->height = ceil(text->th);
286 v->bitmap = malloc(v->width * v->height * sizeof(bool));
287 for (int i=0; i<v->height; i++)
288 for (int j=0; j<v->width; j++)
290 v->bitmap[i*v->width + j] = 1;
294 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
296 if (dbg_requests >= VERBOSITY_PLACEMENT)
297 printf("Adding point\n");
298 if (object->type != OSM_TYPE_NODE)
300 printf("Warning: Point label requested on non-point object\n");
304 struct request_point *r = GARY_PUSH(requests_point);
306 r->request.type = REQUEST_POINT;
307 r->request.ind = num_requests++;
316 GARY_INIT(r->request.variants, 0);
318 struct variant *v = GARY_PUSH(r->request.variants);
320 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
326 case SYMBOLIZER_ICON:
328 r->x = ((struct sym_icon *)sym)->sir.x;
329 r->y = ((struct sym_icon *)sym)->sir.y;
336 if (dbg_requests >= VERBOSITY_PLACEMENT)
337 printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
340 void labeller_add_line(struct symbol *sym, z_index_t zindex)
342 if (dbg_requests >= VERBOSITY_PLACEMENT)
343 printf("Adding line on %u\n", zindex);
344 struct buffer_line *b = GARY_PUSH(buffer_line);
345 b->line = (struct sym_line *) sym;
347 sym_plan(sym, zindex);
350 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
352 if (o->type != OSM_TYPE_WAY)
354 printf("Linelabel request on object which is not way\n");
358 if (dbg_requests >= VERBOSITY_PLACEMENT)
359 printf("Labelling way %ju on %u\n", o->id, zindex);
360 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
361 ll->way = (struct osm_way *) o;
366 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
368 if (dbg_requests >= VERBOSITY_PLACEMENT)
369 printf("Adding area on %u\n", zindex);
370 struct request_area *r = GARY_PUSH(requests_area);
372 r->request.type = REQUEST_AREA;
373 r->request.ind = num_requests++;
375 r->o = (struct osm_multipolygon *) o;
379 osm_obj_center(o, &(r->cx), &(r->cy));
381 GARY_INIT(r->request.variants, 0);
382 struct variant *v = GARY_PUSH(r->request.variants);
386 void make_graph(void)
389 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
391 if (dbg_graph >= VERBOSITY_GENERAL)
392 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
393 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
395 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
396 struct graph_node *g_prev = NULL;
397 struct osm_node *o_prev = NULL;
399 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
401 // FIXME: Shall osm_object's type be checked here?
402 struct osm_node *o_node = (struct osm_node *) ref->o;
404 struct graph_node *g_node = hash_find(ref->o->id);
407 g_node = hash_new(ref->o->id);
408 GARY_INIT(g_node->edges, 0);
410 g_node->id = ref->o->id;
411 g_node->num = num_nodes++;
421 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge));
422 e->num = num_edges++;
423 e->id = buffer_line[i].line->s.o->id;
424 e->color = buffer_line[i].line->color;
425 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
431 e->longline = (uns) -1;
432 e->line = buffer_line[i].line;
436 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
438 edge = GARY_PUSH(g_node->edges);
447 void dump_graph(void)
449 HASH_FOR_ALL(hash, node)
451 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
452 for (uns i=0; i<GARY_SIZE(node->edges); i++)
454 struct graph_edge *e = node->edges[i];
455 printf("\t edge (%d) #%ju to ", e->num, e->id);
456 if (node->edges[i]->n1->id == node->id)
457 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
458 else if (node->edges[i]->n2->id == node->id)
459 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
462 // This shouldn't ever happen
463 printf("BEWARE! Edge is associated with a node it doesn't belongs to!\n");
468 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));
469 else if ((node->edges[i]->label)) printf("Labelled\n");
471 printf(" colored %d;", node->edges[i]->color);
472 printf(" length %.2f", node->edges[i]->length);
479 void label_graph(void)
481 if (dbg_graph >= VERBOSITY_GENERAL)
482 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
483 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
485 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
486 if (dbg_graph >= VERBOSITY_INDIVIDUAL)
487 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
488 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
490 if (dbg_graph >= VERBOSITY_PLACEMENT)
491 printf("Looking for node %ju\n", ref->o->id);
492 struct graph_node *n = hash_find(ref->o->id);
495 printf("BEWARE! Requested node couldn't be found.\n");
499 if (dbg_graph >= VERBOSITY_ALL)
500 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
501 for (uns j=0; j<GARY_SIZE(n->edges); j++)
503 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
505 if (dbg_graph >= VERBOSITY_ALL)
506 printf("Labelling node %ju\n", n->id);
507 n->edges[j]->label = buffer_linelabel[i].label;
508 n->edges[j]->zindex = buffer_linelabel[i].zindex;
516 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
518 if (dbg_bfs >= VERBOSITY_PLACEMENT)
519 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
520 struct graph_edge *candidate = NULL;
522 for (uns i=0; i<GARY_SIZE(node->edges); i++)
524 struct graph_edge *other = node->edges[i];
525 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
527 if ((uns) other->visited != e->longline) {
528 if (dbg_bfs >= VERBOSITY_PLACEMENT)
529 printf("Pushing new edge %d / %ju\n", other->num, other->id);
530 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
532 other->visited = e->longline;
535 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
536 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
539 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
540 (e->label) && (other->label) &&
541 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
542 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
544 if (! candidate || (other->length > candidate->length))
551 if (dbg_bfs >= VERBOSITY_PLACEMENT)
552 printf("New line in longline %u\n", e->longline);
553 struct graph_edge *other = candidate;
554 other->longline = e->longline;
556 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
557 ((dir == DIR_FWD) && (other->n2->id == node->id)))
559 struct graph_node *swp = other->n2;
560 other->n2 = other->n1;
569 longlines[other->longline].first = other;
582 void bfs(uns longline)
584 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
586 printf("BFS called for longline %u\n", longline);
587 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
590 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
592 struct graph_edge *cur = bfs_queue[i];
593 if (dbg_bfs >= VERBOSITY_PLACEMENT)
594 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
596 cur->visited = longline;
598 if (cur->longline == (uns) -1)
601 if (cur->dir == DIR_UNSET)
603 cur->dir = DIR_CENTER;
604 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
605 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
612 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
615 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
625 void bfs_wrapper(void)
627 GARY_INIT(bfs_queue, 0);
628 GARY_INIT(longlines, 0);
630 HASH_FOR_ALL(hash, node)
632 for (uns i=0; i<GARY_SIZE(node->edges); i++)
634 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
636 GARY_PUSH(longlines);
637 longlines[num_longlines].first = node->edges[i];
639 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
641 printf("Running new BFS\n");
642 printf("Creating longline %u\n", num_longlines);
645 GARY_RESIZE(bfs_queue, 0);
646 struct graph_edge **e = GARY_PUSH(bfs_queue);
648 node->edges[i]->longline = num_longlines;
649 bfs(node->edges[i]->longline);
651 if (dbg_bfs >= VERBOSITY_INDIVIDUAL)
653 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
654 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
662 GARY_FREE(bfs_queue);
665 void dump_longlines(void)
667 printf("*** Longlines dump\n");
668 for (uns i=0; i<GARY_SIZE(longlines); i++)
670 printf("Longline %u:", i);
671 struct graph_edge *e = longlines[i].first;
672 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
673 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
678 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
679 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
686 struct request_line *make_new_line(void)
688 struct request_line *rl = GARY_PUSH(requests_line);
689 rl->request.ind = num_requests++;
690 rl->request.type = REQUEST_LINE;
691 GARY_INIT(rl->sections, 0);
692 GARY_INIT(rl->request.variants, 0);
697 struct request_section *make_new_section(struct request_line *rl)
699 struct request_section *rls = GARY_PUSH(rl->sections);
700 rls->request.ind = num_requests++;
701 rls->request.type = REQUEST_SECTION;
702 rls->num_segments = 0;
703 GARY_INIT(rls->segments, 0);
704 GARY_INIT(rls->request.variants, 0);
709 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
711 struct request_segment *rs = GARY_PUSH(rls->segments);
714 rs->request.ind = num_requests++;
715 rs->request.type = REQUEST_SEGMENT;
717 GARY_INIT(rs->request.variants, 0);
718 struct variant *v = GARY_PUSH(rs->request.variants);
724 void cut_edge(struct graph_edge *e, double dist)
726 if (dbg_segments >= VERBOSITY_PLACEMENT)
727 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);
729 struct graph_edge *new = malloc(sizeof(struct graph_edge));
733 switch (e->label->type)
735 case SYMBOLIZER_TEXT:
736 new->label = malloc(sizeof(struct sym_text));
737 *((struct sym_text *) new->label) = *((struct sym_text *) e->label);
743 struct osm_node *n1 = e->n1->o;
744 struct osm_node *n2 = e->n2->o;
746 if ((n1->x == n2->x) && (n1->y == n2->y))
748 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
749 if (dbg_segments >= VERBOSITY_PLACEMENT)
750 printf("Won't cut point\n");
754 struct osm_node *n11 = malloc(sizeof(struct osm_node));
755 struct graph_node *gn = malloc(sizeof(struct graph_node));
757 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
758 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
759 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
761 e->n2 = new->n1 = gn;
763 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
764 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
768 void make_segments(void)
770 for (uns i=0; i<GARY_SIZE(longlines); i++)
772 // Skip lines which are not labelled
773 if (! (longlines[i].first && longlines[i].first->label))
776 struct request_line *request = make_new_line();
777 struct request_section *rls = make_new_section(request);
778 struct request_segment *rs = NULL;
780 struct graph_edge *e = longlines[i].first;
781 double cur_length = 0;
783 struct sym_text *st = NULL;
784 if (e->label->type == SYMBOLIZER_TEXT)
786 st = (struct sym_text *) e->label;
790 // FIXME: Should other label types be supported in future?
791 if (dbg_segments >= VERBOSITY_PLACEMENT)
792 printf("Warning: Skipping line\n");
796 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
797 printf("New longline\n");
803 if (dbg_segments >= VERBOSITY_PLACEMENT)
804 printf("BEWARE: Edge cycle\n");
809 if (dbg_segments >= VERBOSITY_PLACEMENT)
810 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);
812 if (st && (e->length < st->tw))
815 if (dbg_segments >= VERBOSITY_PLACEMENT)
816 printf("Warning: Skipping segment\n");
820 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
822 if (dbg_segments >= VERBOSITY_PLACEMENT)
823 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);
824 // HACK to prevent cutting to 0 lenght
825 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
828 rs = make_new_segment(rls, e->label);
829 rs->label = malloc(sizeof(struct sym_text));
830 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
832 rs->x1 = e->n1->o->x;
833 rs->y1 = e->n1->o->y;
834 rs->x2 = e->n2->o->x;
835 rs->y2 = e->n2->o->y;
837 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
838 rs->zindex = e->zindex;
840 cur_length += e->length;
841 if (cur_length > conf_max_section_length)
843 if (dbg_segments >= VERBOSITY_PLACEMENT)
844 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);
846 rls = make_new_section(request);
853 if (request->sections[0].num_segments == 0)
855 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
856 printf("WARNING: Longline without any segment, skipped\n");
858 struct request_section *rls = &request->sections[0];
859 GARY_FREE(rls->segments);
860 GARY_FREE(rls->request.variants);
862 struct request_line *rl = &requests_line[GARY_SIZE(requests_line)-1];
863 GARY_FREE(rl->sections);
864 GARY_FREE(rl->request.variants);
866 GARY_POP(requests_line);
872 void dump_linelabel_requests(void)
874 for (uns i=0; i<GARY_SIZE(requests_line); i++)
876 if (requests_line[i].sections[0].num_segments == 0)
878 if (dbg_segments >= VERBOSITY_INDIVIDUAL)
879 printf("Beware: Longline without any segment\n");
883 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
884 dump_label(requests_line[i].sections[0].segments[0].label);
885 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
887 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
888 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
890 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
891 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
898 void dump_bitmaps(struct individual *individual)
900 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
901 printf("Bitmap size is %d\n", page_width_int * page_height_int);
902 for (int i=0; i<page_height_int; i++)
903 for (int j=0; j<page_width_int; j++)
904 bitmap[i*page_width_int + j] = 0;
907 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
909 if (individual->placements[i].variant_used == -1) continue;
911 struct placement *p = &(individual->placements[i]);
912 struct variant *v = NULL;
914 switch (p->request->type)
916 case REQUEST_SEGMENT: ;
917 case REQUEST_POINT: ;
919 v = &(p->request->variants[p->variant_used]);
922 ASSERT(p->request->type != REQUEST_INVALID);
926 int base_x = p->x; int base_y = p->y;
927 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
929 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
931 if (v->bitmap[dr * v->width + dc])
933 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
934 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
939 if (dbg_overlaps >= VERBOSITY_GENERAL)
940 printf("There were %d collisions during bitmap dump\n", total);
942 FILE *fd_dump = fopen("dump.pbm", "w");
943 fprintf(fd_dump, "P1\n");
944 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
945 for (int i=0; i<page_height_int; i++)
947 for (int j=0; j<page_width_int; j++)
949 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
951 fprintf(fd_dump, "\n");
958 void dump_individual(struct individual *individual)
960 printf("*** Individual dump\n");
961 printf("(There are %d requests)\n", num_requests);
963 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
965 struct placement *p = &(individual->placements[i]);
967 switch (p->request->type)
970 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
973 struct request_line *rl = (struct request_line *) p->request;
975 dump_label(rl->sections[0].segments[0].label);
977 case REQUEST_SECTION: ;
980 case REQUEST_SEGMENT: ;
981 if (p->variant_used >= 0)
982 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
984 printf("Segment not placed\n");
987 struct request_area *ra = (struct request_area *) p->request;
988 printf("Area label ");
989 dump_label(ra->label);
990 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
993 ASSERT(p->request->type != 0);
996 printf("\nTotal penalty: %d\n", individual->penalty);
999 void plan_individual(struct individual *individual)
1001 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1003 struct symbol *s = NULL;
1004 z_index_t zindex = 0;
1005 if (individual->placements[i].variant_used < 0) continue;
1006 switch (individual->placements[i].request->type)
1008 case REQUEST_POINT: ;
1009 struct request_point *rp = (struct request_point *) individual->placements[i].request;
1011 zindex = rp->zindex;
1013 case REQUEST_SEGMENT: ;
1014 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1016 zindex = rs->zindex;
1018 case REQUEST_LINE: ;
1020 case REQUEST_AREA: ;
1021 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1023 zindex = ra->zindex;
1026 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1030 if (dbg_plan >= VERBOSITY_PLACEMENT)
1031 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1033 if (s) switch (s->type)
1035 case SYMBOLIZER_POINT: ;
1036 struct sym_point *sp = (struct sym_point *) s;
1037 sp->x = individual->placements[i].x;
1038 sp->y = individual->placements[i].y;
1039 sym_plan((struct symbol *) sp, zindex);
1041 case SYMBOLIZER_ICON: ;
1042 struct sym_icon *si = (struct sym_icon *) s;
1043 si->sir.x = individual->placements[i].x;
1044 si->sir.y = individual->placements[i].y;
1045 sym_plan((struct symbol *) si, zindex);
1047 case SYMBOLIZER_TEXT: ;
1048 struct sym_text *st = (struct sym_text *) s;
1049 st->x = individual->placements[i].x;
1050 st->y = individual->placements[i].y;
1051 st->next_duplicate = NULL;
1052 if (dbg_plan >= VERBOSITY_PLACEMENT)
1053 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);
1054 sym_plan((struct symbol *) st, zindex);
1057 ASSERT(s->type != SYMBOLIZER_INVALID);
1062 void dump_penalties(struct individual **population)
1064 for (int i=0; i<conf_pop_size; i++)
1066 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1070 void compute_sizes(void)
1072 page_width_int = floor(page_width);
1073 page_height_int = floor(page_height);
1075 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1076 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1077 num_map_parts = num_map_parts_row * num_map_parts_col;
1079 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1080 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1081 if (dbg_evolution >= VERBOSITY_GENERAL)
1083 printf("Breeding parameters:\n");
1084 printf(" %d individuals are created\n", breed_pop_size);
1085 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1088 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1089 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1090 if (dbg_evolution >= VERBOSITY_GENERAL)
1092 printf("Mutation parameters:\n");
1093 printf(" %d individuals are created\n", mutate_pop_size);
1094 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1097 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1098 if (dbg_evolution >= VERBOSITY_GENERAL)
1100 printf("Elitism parameters:\n");
1101 printf(" %d best individuals are copied\n", elite_pop_size);
1104 if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1108 elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1112 fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1113 fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1119 void labeller_label(void)
1126 GARY_INIT(population1, conf_pop_size);
1127 GARY_INIT(population2, conf_pop_size);
1130 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1132 if (dbg_evolution >= VERBOSITY_GENERAL)
1134 printf("Penalties after initialization\n");
1135 dump_penalties(population1);
1138 while (! shall_terminate())
1142 printf("\n*** Iteration %d ***\n", iteration);
1148 struct individual **swp = population1;
1149 population1 = population2;
1152 clear_population(population2);
1156 if (dbg_evolution >= VERBOSITY_GENERAL)
1157 printf("Sorting population\n");
1158 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1160 if (dbg_evolution >= VERBOSITY_GENERAL)
1162 printf("Penalties after sort\n");
1163 dump_penalties(population1);
1166 old_best = population1[0]->penalty;
1169 if (dbg_overlaps >= VERBOSITY_GENERAL)
1170 dump_bitmaps(population1[0]);
1172 plan_individual(population1[0]);
1179 void labeller_cleanup(void)
1182 GARY_FREE(requests_point);
1183 GARY_FREE(requests_line);
1184 GARY_FREE(requests_area);
1187 void make_population(void)
1189 for (int i=0; i<conf_pop_size; i++)
1191 num_placements = 0; // FIXME: This IS a terrible HACK
1192 struct individual *i2 = ep_alloc(ep_individuals);
1193 init_individual(i2);
1194 population2[i] = i2;
1196 if (dbg_init >= VERBOSITY_INDIVIDUAL)
1197 printf("Making individual %d\n", i);
1198 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1199 population1[i] = individual;
1202 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1204 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1207 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1209 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1211 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1213 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1215 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1217 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1222 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1224 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1227 hide_segment_labels(individual);
1229 ASSERT(p == num_requests);
1233 bool shall_terminate(void)
1235 switch (conf_term_cond)
1237 case TERM_COND_PENALTY:
1238 return (population1[0]->penalty < conf_penalty_bound);
1239 case TERM_COND_STAGNATION:
1240 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1241 case TERM_COND_ITERATIONS:
1242 return (iteration >= conf_iteration_limit);
1244 fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1253 struct individual **breed_buffer;
1254 while (i < breed_pop_size)
1256 int parent1 = randint(0, breed_rbest_size);
1257 int parent2 = randint(0, breed_rbest_size);
1258 if (dbg_breeding >= VERBOSITY_INDIVIDUAL)
1259 printf("Will breed %d and %d\n", parent1, parent2);
1261 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1262 population2[pop2_ind++] = breed_buffer[0];
1263 population2[pop2_ind++] = breed_buffer[1];
1271 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1273 struct individual **buffer = malloc(2*sizeof(struct individual));
1274 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1275 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1278 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1280 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1282 if (! processed[parent1->placements[i].ind])
1284 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1285 printf("Creating symbol closure for placement %u\n", i);
1287 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1288 int x = randint(0, 2);
1292 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1293 printf("Copying parent->child 1->1 and 2->2\n");
1294 copy_symbols(clos_symbols, parent1, child1, &processed);
1295 copy_symbols(clos_symbols, parent2, child2, &processed);
1299 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1300 printf("Copying parent->child 2->1 and 1->2\n");
1301 copy_symbols(clos_symbols, parent2, child1, &processed);
1302 copy_symbols(clos_symbols, parent1, child2, &processed);
1305 GARY_FREE(clos_symbols);
1309 GARY_FREE(processed);
1311 if (conf_mutate_children)
1313 if (randdouble() < conf_mutate_children_prob) perform_mutation(child1);
1314 if (randdouble() < conf_mutate_children_prob) perform_mutation(child2);
1324 for (int i=0; i < mutate_pop_size; i++)
1326 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1327 printf("Creating %d-th individual by mutation\n", i);
1328 int ind = randint(0, mutate_rbest_size);
1329 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1330 printf("Mutating %d-th individual of original population\n", ind);
1331 population2[pop2_ind] = ep_alloc(ep_individuals);
1332 copy_individual(population1[ind], population2[pop2_ind]);
1333 if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1334 printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1335 perform_mutation(population2[pop2_ind]);
1340 void perform_mutation(struct individual *individual)
1342 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1344 double x = randdouble();
1347 if (x <= acc + conf_mutate_move_bound)
1349 if (dbg_mutation >= VERBOSITY_PLACEMENT)
1350 printf("Mutation: Moving symbol in placement %u\n", i);
1351 move_symbol(&(individual->placements[i]));
1354 acc += conf_mutate_move_bound;
1356 if (x <= acc + conf_mutate_regen_bound)
1358 gen_coords(&(individual->placements[i]));
1361 acc += conf_mutate_regen_bound;
1363 if (x <= acc + conf_mutate_chvar_bound)
1365 struct placement *p = &(individual->placements[i]);
1366 switch (p->request->type)
1369 case REQUEST_SEGMENT:
1371 // Does nothing when there are 0 variants... does it mind?
1372 p->variant_used = randint(-1, GARY_SIZE(p->request->variants));
1374 case REQUEST_SECTION:
1375 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments));
1383 hide_segment_labels(individual);
1388 for (int i=0; i<elite_pop_size; i++)
1390 population2[pop2_ind] = ep_alloc(ep_individuals);
1391 copy_individual(population1[i], population2[pop2_ind++]);
1395 int overlaps(struct placement *p1, struct placement *p2)
1397 if (p1->request->type != REQUEST_POINT &&
1398 p1->request->type != REQUEST_SEGMENT &&
1399 p1->request->type != REQUEST_AREA)
1402 if (p2->request->type != REQUEST_POINT &&
1403 p2->request->type != REQUEST_SEGMENT &&
1404 p2->request->type != REQUEST_AREA)
1407 if (p1->variant_used == -1 || p2->variant_used == -1)
1410 struct variant *v1, *v2;
1412 v1 = &(p1->request->variants[p1->variant_used]);
1413 v2 = &(p2->request->variants[p2->variant_used]);
1415 // FIXME: This doesn't fully respect offset which it probably should
1416 int p1x = p1->x; int p1y = p1->y;
1417 int p2x = p2->x; int p2y = p2->y;
1420 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1421 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1423 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1424 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1431 int get_overlap(struct placement *p)
1433 if (p->variant_used == -1) return 0;
1435 struct map_part **parts = get_map_parts(p);
1438 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1439 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1443 struct placement **others;
1446 GARY_INIT_ZERO(planned, num_requests);
1447 planned[p->request->ind] = 1;
1448 GARY_INIT(others, 0);
1450 for (uns i=0; i<GARY_SIZE(parts); i++)
1452 struct map_placement *mp = parts[i]->placement->next_in_map;
1455 if (! planned[mp->placement->request->ind])
1457 struct placement **p = GARY_PUSH(others);
1459 planned[mp->placement->request->ind] = true;
1461 mp = mp->next_in_map;
1466 for (uns i=0; i<GARY_SIZE(others); i++)
1468 overlap += overlaps(p, others[i]);
1475 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1476 printf("Placement of request %d add %d to overlaps\n", p->request->ind, overlap);
1478 if (p->x < 0) overlap += 0 - p->x;
1479 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1480 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1482 if (p->y < 0) overlap += 0 - p->y;
1483 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1484 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1489 int individual_overlap(struct individual *individual)
1493 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1495 overlap += get_overlap(&individual->placements[i]);
1501 double get_distance(struct placement *p)
1503 if (p->variant_used < 0) return 0;
1504 struct variant *v = &p->request->variants[p->variant_used];
1506 double dx, dy, distance;
1507 switch (p->request->type)
1509 case REQUEST_POINT: ;
1510 struct request_point *rp = (struct request_point *) p->request;
1511 dx = rp->x + v->offset_x - p->x;
1512 dy = rp->y + v->offset_y - p->y;
1513 distance = sqrt(dx*dx + dy*dy);
1514 if (dbg_rank >= VERBOSITY_PLACEMENT)
1515 printf("Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1517 case REQUEST_AREA: ;
1518 struct request_area *ra = (struct request_area *) p->request;
1519 dx = ra->cx + v->offset_x - p->x;
1520 dy = ra->cy + v->offset_y - p->y;
1521 distance = sqrt(dx*dx + dy*dy);
1522 if (dbg_rank >= VERBOSITY_PLACEMENT)
1523 printf("Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1529 if (dbg_rank >= VERBOSITY_PLACEMENT)
1530 printf("Placement %d has distance %.2f\n", p->ind, distance);
1534 double individual_distances(struct individual *individual)
1538 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1540 distances += get_distance(&individual->placements[i]);
1546 double get_omittment(struct placement *p)
1548 if (p->variant_used >= 0) return 0;
1551 switch (p->request->type)
1562 double individual_omittment(struct individual *individual)
1566 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1568 omittment += get_omittment(&individual->placements[i]);
1574 int cmp_individual(const void *a, const void *b)
1576 struct individual **ia = (struct individual **) a;
1577 struct individual **ib = (struct individual **) b;
1579 return (*ia)->penalty - (*ib)->penalty;
1582 void rank_population(void)
1586 for (int i=0; i<conf_pop_size; i++)
1588 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1589 printf("Individual %d\n", i);
1590 population1[i]->penalty = 0;
1592 penalty = individual_omittment(population1[i]);
1593 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1594 printf("Increasing penalty by %d for omittment\n", penalty);
1595 population1[i]->penalty += penalty;
1597 penalty = individual_overlap(population1[i]);
1598 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1599 printf("Increasing penalty by %d for overlap\n", penalty);
1600 population1[i]->penalty += penalty;
1602 penalty = individual_distances(population1[i]);
1603 if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1604 printf("Increasing penalty by %d for distances\n", penalty);
1605 population1[i]->penalty += penalty;
1609 struct map_part **get_map_parts(struct placement *p)
1611 if (p->variant_used < 0) return NULL;
1613 struct map_part **buffer;
1614 GARY_INIT(buffer, 0);
1616 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1617 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1620 switch (p->request->type)
1623 case REQUEST_SEGMENT:
1625 v = p->request->variants[p->variant_used];
1628 if (dbg_map_parts >= VERBOSITY_ALL)
1629 printf("Skipping unsupported request type (%d)\n", p->request->type);
1633 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1634 printf("Bitmap is %d x %d\n", v.width, v.height);
1636 int x_min = max2(0, p->x) / conf_map_part_width;
1637 // CHECK ME: Is rounding needed?
1638 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1639 int y_min = max2(0, p->y) / conf_map_part_height;
1640 // CHECK ME: Is rounding needed?
1641 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1643 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1644 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1646 for (int y=y_min; y<=y_max; y++)
1647 for (int x=x_min; x<=x_max; x++)
1649 struct map_part **m = GARY_PUSH(buffer);
1650 if (dbg_map_parts >= VERBOSITY_ALL)
1651 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1652 *m = p->individual->map[y * num_map_parts_row + x];
1655 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1656 printf("Returning %u map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1661 void update_map_parts_delete(struct placement *p)
1663 struct map_placement *mp = p->map_links;
1666 mp->prev_in_map->next_in_map = mp->next_in_map;
1667 if (mp->next_in_map)
1668 mp->next_in_map->prev_in_map = mp->prev_in_map;
1670 struct map_placement *tmp = mp;
1671 mp = mp->next_in_placement;
1674 p->map_links = NULL;
1677 void update_map_parts_create(struct placement *p)
1679 struct map_part **parts = get_map_parts(p);
1680 if (parts == NULL) return;
1682 for (uns i=0; i<GARY_SIZE(parts); i++)
1684 struct map_placement *mp = malloc(sizeof(struct map_placement));
1686 mp->part = parts[i];
1688 mp->next_in_map = parts[i]->placement->next_in_map;
1689 mp->prev_in_map = parts[i]->placement;
1690 parts[i]->placement->next_in_map = mp;
1691 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1693 mp->next_in_placement = p->map_links;
1694 mp->prev_in_placement = NULL;
1701 void update_map_parts(struct placement *p)
1703 update_map_parts_delete(p);
1704 update_map_parts_create(p);
1707 void gen_coords(struct placement *p)
1709 switch(p->request->type)
1712 gen_coords_point(p);
1717 case REQUEST_SEGMENT:
1718 gen_coords_segment(p);
1722 printf("Not yet implemented\n");
1725 if (dbg_movement >= VERBOSITY_ALL)
1726 printf("Testing request type\n");
1727 ASSERT(p->request->type != REQUEST_INVALID);
1730 update_map_parts(p);
1733 double gen_movement(void)
1735 double m = (random() % 100000) / 10000;
1736 m = pow(m, 1.0/3) * flip(1, -1);
1737 if (dbg_movement >= VERBOSITY_ALL)
1738 printf("Movement %.2f\n", m);
1742 double gen_movement_uniform(void)
1744 return (move_max - move_min) * randdouble() * flip(1, -1);
1747 void gen_coords_point(struct placement *p)
1749 p->x = p->x + gen_movement();
1752 void gen_coords_segment(struct placement *p)
1754 struct request_segment *rs = (struct request_segment *) p->request;
1756 p->x = (a == 1 ? rs->x1 : rs->x2);
1757 p->y = (a == 1 ? rs->y1 : rs->y2);
1760 void gen_coords_area(struct placement *p)
1762 struct request_area *ra = (struct request_area *) p->request;
1764 p->x = p->x + gen_movement();
1765 p->y = p->y + gen_movement();
1767 if (dbg_movement >= VERBOSITY_PLACEMENT)
1768 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1771 int randint(int min, int max)
1773 if (min == max) return min;
1775 return min + (r % (max - min));
1778 struct placement **get_closure(struct placement *placement)
1780 struct placement **closure;
1781 GARY_INIT(closure, 0);
1782 bool *chosen = malloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1783 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1784 chosen[placement->request->ind] = 1;
1786 struct placement **p = GARY_PUSH(closure); *p = placement;
1789 while (first < GARY_SIZE(closure))
1791 if (dbg_breeding >= VERBOSITY_ALL)
1792 printf("Iterating, first is %d of current %u\n", first, GARY_SIZE(closure));
1793 struct placement **overlapping = get_overlapping(placement);
1794 if (! overlapping) { first++; continue; }
1796 struct placement **filtered = filter(overlapping, &chosen);
1797 if (dbg_breeding >= VERBOSITY_ALL)
1798 printf("There are %u new overlapping symbols\n", GARY_SIZE(filtered));
1799 GARY_FREE(overlapping);
1800 overlapping = filtered;
1801 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1803 if (! chosen[overlapping[j]->request->ind])
1805 if (overlaps(*p, overlapping[j]))
1807 p = GARY_PUSH(closure); *p = overlapping[j];
1808 if (dbg_breeding >= VERBOSITY_ALL)
1809 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);
1810 chosen[overlapping[j]->request->ind] = 1;
1814 GARY_FREE(overlapping);
1823 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1825 bool *processed = *processed_ptr;
1826 if (dbg_breeding >= VERBOSITY_ALL)
1827 printf("Will copy %u symbols\n", GARY_SIZE(closure));
1829 for (uns i=0; i<GARY_SIZE(closure); i++)
1831 processed[closure[i]->ind] = 1;
1832 int ind = closure[i]->ind;
1833 child->placements[ind] = parent->placements[ind];
1834 child->placements[ind].individual = child;
1835 child->placements[ind].processed = 0;
1836 child->placements[ind].map_links = NULL;
1837 update_map_parts(&child->placements[ind]);
1841 void move_symbol(struct placement *p)
1843 switch (p->request->type)
1847 move_symbol_point(p);
1849 case REQUEST_SEGMENT:
1850 move_symbol_segment(p);
1853 ASSERT(p->request->type != REQUEST_INVALID);
1857 void move_symbol_point(struct placement *p)
1859 p->x += gen_movement_uniform();
1860 p->y += gen_movement_uniform();
1863 void move_symbol_segment(struct placement *p)
1865 double m = gen_movement_uniform();
1868 p->y += m * ((struct request_segment *) p->request)->slope;
1871 void hide_segment_labels(struct individual *individual)
1873 // BEWARE: This fully depends on current genetic encoding
1875 int used = -1, num = -1;
1876 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1878 switch (individual->placements[i].request->type)
1880 case REQUEST_SECTION:
1881 used = individual->placements[i].variant_used;
1884 case REQUEST_SEGMENT:
1886 individual->placements[i].variant_used = 0;
1888 individual->placements[i].variant_used = -1;
1897 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1899 p->ind = num_placements++;
1902 p->x = p->y = 0; // To prevent valgrind from complaining
1903 p->variant_used = 0;
1904 p->map_links = NULL;
1905 p->individual = individual;
1908 case REQUEST_POINT: ;
1909 struct request_point *rp = (struct request_point *) r;
1913 case REQUEST_LINE: ;
1915 case REQUEST_SECTION: ;
1916 struct request_section *rls = (struct request_section *) r;
1917 p->variant_used = randint(0, rls->num_segments);
1919 case REQUEST_SEGMENT: ;
1920 struct request_segment *rs = (struct request_segment *) r;
1924 case REQUEST_AREA: ;
1925 struct request_area *ra = (struct request_area *) r;
1928 p->variant_used = 0;
1931 ASSERT(p->request->type != REQUEST_INVALID);
1935 if (dbg_init >= VERBOSITY_PLACEMENT)
1936 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1939 void reset_individual_map(struct individual *i)
1941 for (uns j=0; j<num_map_parts; j++)
1943 struct map_placement *mp = i->map[j]->placement;
1946 struct map_placement *tmp = mp;
1947 mp = mp->next_in_map;
1952 struct map_part *part = malloc(sizeof(struct map_part));
1955 mp = malloc(sizeof(struct map_placement));
1956 part->placement = mp;
1957 mp->placement = &dummy_placement;
1958 mp->next_in_map = mp->prev_in_map = NULL;
1959 mp->next_in_placement = mp->prev_in_placement = NULL;
1964 void update_individual(struct individual *individual)
1966 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1968 update_map_parts_delete(&individual->placements[i]);
1972 void clear_individual(struct individual *individual)
1974 for (uns j=0; j<num_map_parts; j++)
1976 struct map_placement *mp = individual->map[j]->placement;
1979 struct map_placement *tmp = mp;
1980 mp = mp->next_in_map;
1984 free(individual->map[j]);
1987 GARY_FREE(individual->map);
1988 GARY_FREE(individual->placements);
1989 ep_free(ep_individuals, individual);
1992 void clear_population(struct individual **pop)
1994 for (uns i=0; i<GARY_SIZE(pop); i++)
1996 clear_individual(pop[i]);
2000 struct placement **get_overlapping(struct placement *p)
2002 struct placement **buffer;
2003 GARY_INIT(buffer, 0);
2005 struct map_part **parts = get_map_parts(p);
2006 if (! parts) return NULL;
2008 for (uns i=0; i<GARY_SIZE(parts); i++)
2010 struct map_placement *mp = parts[i]->placement->next_in_map;
2013 if (p->variant_used >= 0)
2015 struct placement **p = GARY_PUSH(buffer);
2018 mp = mp->next_in_map;
2023 if (dbg_map_parts >= VERBOSITY_PLACEMENT)
2024 printf("Returning %u potentially overlapping placements\n", GARY_SIZE(buffer));
2029 struct placement **filter(struct placement **list, bool **pred_ptr)
2031 bool *pred = *pred_ptr; // As GARY can't be passed directly
2032 struct placement **filtered;
2033 GARY_INIT(filtered, 0);
2035 for (uns i=0; i<GARY_SIZE(list); i++)
2037 if (pred[list[i]->request->ind])
2040 struct placement **p = GARY_PUSH(filtered);
2047 int flip(int a, int b)
2049 return (random() % 2 ? a : b);
2052 double randdouble(void)
2054 return ((double) rand() / (double) RAND_MAX);
2057 void init_individual(struct individual *individual)
2059 GARY_INIT(individual->placements, num_requests);
2060 GARY_INIT(individual->map, 0);
2061 for (uns j=0; j<num_map_parts; j++)
2063 GARY_PUSH(individual->map);
2064 struct map_part *part = malloc(sizeof(struct map_part));
2065 struct map_placement *mp = malloc(sizeof(struct map_placement));
2066 part->placement = mp;
2068 mp->placement = &dummy_placement;
2069 mp->next_in_map = mp->prev_in_map = NULL;
2070 mp->next_in_placement = mp->prev_in_placement = NULL;
2071 individual->map[j] = part;
2073 individual->penalty = 0;
2076 void copy_individual(struct individual *src, struct individual *dest)
2078 init_individual(dest);
2079 dest->penalty = src->penalty;
2081 for (uns i=0; i<GARY_SIZE(src->placements); i++)
2083 dest->placements[i] = src->placements[i];
2084 dest->placements[i].map_links = NULL;
2085 dest->placements[i].individual = dest;
2087 update_map_parts_create(&dest->placements[i]);