4 #include <ucw/mempool.h>
5 #include <ucw/eltpool.h>
12 #define HASH_NODE struct graph_node
13 #define HASH_PREFIX(x) hash_##x
14 #define HASH_KEY_ATOMIC id
15 #define HASH_WANT_FIND
17 #define HASH_WANT_CLEANUP
18 #include <ucw/hashtable.h>
26 #define BLOCK_SIZE 4096
28 #define DEBUG(dbg_sec, dbg_lvl, msg, ...) if (dbg_sec >= dbg_lvl) printf(msg, ##__VA_ARGS__)
30 static struct request_point *requests_point;
31 static struct request_line *requests_line;
32 static struct request_area *requests_area;
34 static struct graph_edge **bfs_queue;
35 static struct longline *longlines;
36 static struct buffer_line *buffer_line;
37 static struct buffer_linelabel *buffer_linelabel;
39 struct eltpool *ep_individuals;
41 struct individual **population1;
42 struct individual **population2;
44 int dbg_segments = VERBOSITY_NONE;
45 int dbg_plan = VERBOSITY_NONE;
46 int dbg_requests = VERBOSITY_NONE;
47 int dbg_graph = VERBOSITY_NONE;
48 int dbg_bfs = VERBOSITY_NONE;
49 int dbg_map_parts = VERBOSITY_NONE;
50 int dbg_movement = VERBOSITY_NONE;
51 int dbg_init = VERBOSITY_NONE;
52 int dbg_overlaps = VERBOSITY_GENERAL;
53 int dbg_rank = VERBOSITY_NONE;
54 int dbg_evolution = VERBOSITY_POPULATION;
55 int dbg_mutation = VERBOSITY_NONE;
56 int dbg_breeding = VERBOSITY_NONE;
69 int mutate_rbest_size;
74 int old_best = INT_MAX;
83 int num_placements = 0;
86 int conf_map_part_width = 5;
87 int conf_map_part_height = 5;
89 uns num_map_parts_row;
90 uns num_map_parts_col;
93 int conf_pop_size = 200, conf_fit_size = 1, conf_term_cond = TERM_COND_ITERATIONS;
95 double conf_penalty_bound = 100, conf_stagnation_bound = 10;
96 int conf_iteration_limit = 200;
98 double conf_breed_pop_size = 0.45, conf_breed_rbest = 1,
99 conf_mutate_children, conf_mutate_children_prob = 0.6,
100 conf_mutate_pop_size = 0.45, conf_mutate_rbest = 1,
101 conf_mutate_move_bound = 0.1, conf_mutate_regen_bound = 0.05, conf_mutate_chvar_bound = 0.1,
102 conf_elite_pop_size = 0.1;
104 double conf_max_section_length = 80, conf_max_section_overlay = 10;
106 static struct cf_section evolution_cf = {
108 CF_INT("PopSize", &conf_pop_size),
109 CF_INT("FitSize", &conf_fit_size),
110 CF_INT("TermCond", &conf_term_cond),
111 CF_DOUBLE("PenaltyBound", &conf_penalty_bound),
112 CF_DOUBLE("StagnationBound", &conf_stagnation_bound),
113 CF_INT("IterationLimit", &conf_iteration_limit),
114 CF_DOUBLE("BreedPopSize", &conf_breed_pop_size),
115 CF_DOUBLE("BreedNumBest", &conf_breed_rbest),
116 CF_DOUBLE("MutateChild", &conf_mutate_children_prob),
117 CF_DOUBLE("MutatePopSize", &conf_mutate_pop_size),
118 CF_DOUBLE("MutateNumBest", &conf_mutate_rbest),
119 CF_DOUBLE("MutateMoveBound", &conf_mutate_move_bound),
120 CF_DOUBLE("MutateRegenBound", &conf_mutate_regen_bound),
121 CF_DOUBLE("MutateChvarBound", &conf_mutate_chvar_bound),
122 CF_DOUBLE("ElitePopSize", &conf_elite_pop_size),
123 CF_DOUBLE("MaxSectionLenght", &conf_max_section_length),
124 CF_DOUBLE("MaxSectionOverlay", &conf_max_section_overlay),
129 void compute_sizes(void);
131 void make_population(void);
132 bool shall_terminate(void);
136 void rank_population(void);
137 void plan_individual(struct individual *individual);
139 int overlaps(struct placement *p1, struct placement *p2);
140 int get_overlap(struct placement *p, int **planed_ptr, int iteration);
141 int individual_overlap(struct individual *individual);
143 double get_distance(struct placement *p);
144 double individual_distances(struct individual *individual);
146 double get_omittment(struct placement *p);
147 double individual_omittment(struct individual *individual);
149 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
150 void perform_mutation(struct individual *individual);
151 void init_placement(struct placement *p, struct individual *individual, struct request *r);
152 void init_individual(struct individual *i);
153 void copy_individual(struct individual *src, struct individual *dest);
154 int cmp_individual(const void *a, const void *b);
156 void clear_individual(struct individual *individual);
157 void clear_population(struct individual **pop);
159 void make_bitmap(struct variant *v, struct symbol *sym);
160 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
161 void make_bitmap_point(struct variant *v, struct sym_point *sp);
162 void make_bitmap_label(struct variant *v, struct sym_text *text);
164 double gen_movement(void);
165 double gen_movement_uniform(void);
166 void move_symbol(struct placement *p);
167 void move_symbol_point(struct placement *p);
168 void move_symbol_segment(struct placement *p);
169 void hide_segment_labels(struct individual *individual);
171 void gen_coords(struct placement *p);
172 void gen_coords_point(struct placement *p);
173 void gen_coords_segment(struct placement *p);
174 void gen_coords_area(struct placement *p);
176 struct map_part **get_map_parts(struct placement *p);
177 void update_map_parts(struct placement *p);
178 void update_map_parts_delete(struct placement *p);
179 void update_map_parts_create(struct placement *p);
180 struct placement **get_closure(struct placement *placement);
181 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
182 struct placement **get_overlapping(struct placement *p);
183 struct placement **filter(struct placement **list, bool **pred_ptr);
186 void make_graph(void);
187 void label_graph(void);
188 void bfs_wrapper(void);
189 void bfs(uns longline);
190 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
191 void make_segments(void);
193 void cut_edge(struct graph_edge *e, double dist);
194 struct request_line *make_new_line(void);
195 struct request_section *make_new_section(struct request_line *rl);
196 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
198 void dump_bitmaps(struct individual *individual);
199 void dump_graph(void);
200 void dump_longlines(void);
201 void dump_linelabel_requests(void);
202 void dump_individual(struct individual *individual);
203 void dump_label(struct symbol *sym);
204 void dump_penalties(struct individual **population);
205 void dump_placements(struct individual *individual);
206 void dump_placement_links(struct placement *p);
207 void dump_part_links(struct map_part *part);
208 void dump_links(struct individual *individual);
210 int randint(int min, int max);
211 int flip(int a, int b);
212 double randdouble(void);
214 int max2(int a, int b);
215 int min2(int a, int b);
216 int max4(int a, int b, int c, int d);
217 int min4(int a, int b, int c, int d);
219 struct placement dummy_placement;
221 int max2(int a, int b)
223 return (a > b ? a : b);
226 int min2(int a, int b)
228 return (a < b ? a : b);
231 int max4(int a, int b, int c, int d)
233 return max2(max2(a, b), max2(c, d));
236 int min4(int a, int b, int c, int d)
238 return min2(min2(a, b), min2(c, d));
241 void dump_label(struct symbol *sym)
245 case SYMBOLIZER_TEXT: ;
246 struct sym_text *st = (struct sym_text *) sym;
247 printf("%s\n", osm_val_decode(st->text));
254 void labeller_conf(void)
256 cf_declare_section("Evolution", &evolution_cf, 0);
259 void labeller_init(void)
261 GARY_INIT(requests_point, 0);
262 GARY_INIT(requests_line, 0);
263 GARY_INIT(requests_area, 0);
264 GARY_INIT(buffer_line, 0);
265 GARY_INIT(buffer_linelabel, 0);
266 ep_individuals = ep_new(sizeof(struct individual), 1);
271 void make_bitmap(struct variant *v, struct symbol *sym)
273 v->offset_x = v->offset_y = 0;
277 case SYMBOLIZER_POINT:
278 make_bitmap_point(v, (struct sym_point *) sym);
280 case SYMBOLIZER_ICON:
281 make_bitmap_icon(v, (struct sym_icon *) sym);
283 case SYMBOLIZER_TEXT:
284 make_bitmap_label(v, (struct sym_text *) sym);
287 ASSERT(sym->type != SYMBOLIZER_INVALID);
291 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
293 v->width = si->sir.width + 1;
294 v->height = si->sir.height + 1;
295 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
296 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
299 void make_bitmap_point(struct variant *v, struct sym_point *sp)
301 v->width = v->height = sp->size + 1;
302 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
303 // FIXME: Okay, memset would be much nicer here
304 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
307 void make_bitmap_label(struct variant *v, struct sym_text *text)
309 int tw = ceil(text->tw);
310 int th = ceil(text->th);
312 double rotate_rad = text->rotate / (-180 / M_PI);
314 // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th]
315 // They could be declared before but should not be initialized in code
316 // as reassigning x-coordinate affects computation of y-cordinate
320 int lrx = tw * cos(rotate_rad);
321 int lry = tw * sin(rotate_rad);
323 int ulx = th * sin(rotate_rad);
324 int uly = th * cos(rotate_rad);
326 int urx = tw * cos(rotate_rad) + th * sin(rotate_rad);
327 int ury = tw * sin(rotate_rad) + th * cos(rotate_rad);
329 int min_x = min4(llx, lrx, ulx, urx);
330 int min_y = min4(lly, lry, uly, ury);
331 int max_x = max4(llx, lrx, ulx, urx);
332 int max_y = max4(lly, lry, uly, ury);
334 v->width = max_x - min_x + 1;
335 v->height = max_y - min_y + 1;
336 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
337 memset(v->bitmap, 0, v->width * v->height * sizeof(bool));
339 for (int i=0; i<th; i++)
341 for (int j=0; j<tw; j++)
343 int nx = j*cos(rotate_rad) + i*sin(rotate_rad);
344 int ny = j*sin(rotate_rad) + i*cos(rotate_rad);
345 v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1;
350 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
352 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding point\n");
353 if (object->type != OSM_TYPE_NODE)
355 printf("Warning: Point label requested on non-point object\n");
359 struct request_point *r = GARY_PUSH(requests_point);
361 r->request.type = REQUEST_POINT;
362 r->request.ind = num_requests++;
371 GARY_INIT(r->request.variants, 0);
373 struct variant *v = GARY_PUSH(r->request.variants);
375 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
381 case SYMBOLIZER_ICON:
383 r->x = ((struct sym_icon *)sym)->sir.x;
384 r->y = ((struct sym_icon *)sym)->sir.y;
391 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
394 void labeller_add_line(struct symbol *sym, z_index_t zindex)
396 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding line on %u\n", zindex);
397 struct buffer_line *b = GARY_PUSH(buffer_line);
398 b->line = (struct sym_line *) sym;
400 sym_plan(sym, zindex);
403 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
405 if (o->type != OSM_TYPE_WAY)
407 printf("Linelabel request on object which is not way\n");
411 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Labelling way %ju on %u\n", o->id, zindex);
412 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
413 ll->way = (struct osm_way *) o;
418 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
420 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding area on %u\n", zindex);
421 struct request_area *r = GARY_PUSH(requests_area);
423 r->request.type = REQUEST_AREA;
424 r->request.ind = num_requests++;
426 r->o = (struct osm_multipolygon *) o;
430 osm_obj_center(o, &(r->cx), &(r->cy));
432 GARY_INIT(r->request.variants, 0);
433 struct variant *v = GARY_PUSH(r->request.variants);
437 void make_graph(void)
440 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
442 DEBUG(dbg_graph, VERBOSITY_GENERAL, "Extracting nodes, will iterate over %zu ways\n", GARY_SIZE(buffer_line));
443 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
445 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
446 struct graph_node *g_prev = NULL;
447 struct osm_node *o_prev = NULL;
449 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
451 // FIXME: Shall osm_object's type be checked here?
452 struct osm_node *o_node = (struct osm_node *) ref->o;
454 struct graph_node *g_node = hash_find(ref->o->id);
457 g_node = hash_new(ref->o->id);
458 GARY_INIT(g_node->edges, 0);
460 g_node->id = ref->o->id;
461 g_node->num = num_nodes++;
471 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge));
472 e->num = num_edges++;
473 e->id = buffer_line[i].line->s.o->id;
474 e->color = buffer_line[i].line->color;
475 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
481 e->longline = (uns) -1;
482 e->line = buffer_line[i].line;
486 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
488 edge = GARY_PUSH(g_node->edges);
497 void dump_graph(void)
499 HASH_FOR_ALL(hash, node)
501 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
502 for (uns i=0; i<GARY_SIZE(node->edges); i++)
504 struct graph_edge *e = node->edges[i];
505 printf("\t edge (%d) #%ju to ", e->num, e->id);
506 if (node->edges[i]->n1->id == node->id)
507 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
508 else if (node->edges[i]->n2->id == node->id)
509 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
512 // This shouldn't ever happen
513 printf("BEWARE! Edge is associated with a node it doesn't belongs to!\n");
518 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));
519 else if ((node->edges[i]->label)) printf("Labelled\n");
521 printf(" colored %d;", node->edges[i]->color);
522 printf(" length %.2f", node->edges[i]->length);
529 void label_graph(void)
531 DEBUG(dbg_graph, VERBOSITY_GENERAL, "There are %zu line labels requested\n", GARY_SIZE(buffer_linelabel));
532 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
534 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
535 DEBUG(dbg_graph, VERBOSITY_INDIVIDUAL, "Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
536 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
538 DEBUG(dbg_graph, VERBOSITY_PLACEMENT, "Looking for node %ju\n", ref->o->id);
539 struct graph_node *n = hash_find(ref->o->id);
542 printf("BEWARE! Requested node couldn't be found.\n");
546 DEBUG(dbg_graph, VERBOSITY_ALL, "Searching among %zu edges\n", GARY_SIZE(n->edges));
547 for (uns j=0; j<GARY_SIZE(n->edges); j++)
549 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
551 DEBUG(dbg_graph, VERBOSITY_ALL, "Labelling node %ju\n", n->id);
552 n->edges[j]->label = buffer_linelabel[i].label;
553 n->edges[j]->zindex = buffer_linelabel[i].zindex;
561 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
563 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
564 struct graph_edge *candidate = NULL;
566 for (uns i=0; i<GARY_SIZE(node->edges); i++)
568 struct graph_edge *other = node->edges[i];
569 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
571 if ((uns) other->visited != e->longline) {
572 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "Pushing new edge %d / %ju\n", other->num, other->id);
573 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
575 other->visited = e->longline;
578 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
579 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
582 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
583 (e->label) && (other->label) &&
584 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
585 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
587 if (! candidate || (other->length > candidate->length))
594 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "New line in longline %u\n", e->longline);
595 struct graph_edge *other = candidate;
596 other->longline = e->longline;
598 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
599 ((dir == DIR_FWD) && (other->n2->id == node->id)))
601 struct graph_node *swp = other->n2;
602 other->n2 = other->n1;
611 longlines[other->longline].first = other;
624 void bfs(uns longline)
626 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "BFS called for longline %u\n", longline);
627 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "%zu longlines exist\n", GARY_SIZE(longlines));
629 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
631 struct graph_edge *cur = bfs_queue[i];
632 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "Exploring new edge %d; %zu remaining\n", cur->num, GARY_SIZE(bfs_queue));
634 cur->visited = longline;
636 if (cur->longline == (uns) -1)
639 if (cur->dir == DIR_UNSET)
641 cur->dir = DIR_CENTER;
642 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
643 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
650 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
653 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
663 void bfs_wrapper(void)
665 GARY_INIT(bfs_queue, 0);
666 GARY_INIT(longlines, 0);
668 HASH_FOR_ALL(hash, node)
670 for (uns i=0; i<GARY_SIZE(node->edges); i++)
672 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
674 GARY_PUSH(longlines);
675 longlines[GARY_SIZE(longlines)-1].first = node->edges[i];
677 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Running new BFS\n");
678 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Creating longline %zu\n", GARY_SIZE(longlines)-1);
680 GARY_RESIZE(bfs_queue, 0);
681 struct graph_edge **e = GARY_PUSH(bfs_queue);
683 node->edges[i]->longline = GARY_SIZE(longlines)-1;
684 bfs(node->edges[i]->longline);
686 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
687 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Planned %zu edges\n", GARY_SIZE(bfs_queue));
693 GARY_FREE(bfs_queue);
696 void dump_longlines(void)
698 printf("*** Longlines dump\n");
699 for (uns i=0; i<GARY_SIZE(longlines); i++)
701 printf("Longline %u:", i);
702 struct graph_edge *e = longlines[i].first;
703 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
704 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
709 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
710 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
717 struct request_line *make_new_line(void)
719 struct request_line *rl = GARY_PUSH(requests_line);
720 rl->request.ind = num_requests++;
721 rl->request.type = REQUEST_LINE;
722 GARY_INIT(rl->sections, 0);
723 GARY_INIT(rl->request.variants, 0);
728 struct request_section *make_new_section(struct request_line *rl)
730 struct request_section *rls = GARY_PUSH(rl->sections);
731 rls->request.ind = num_requests++;
732 rls->request.type = REQUEST_SECTION;
733 rls->num_segments = 0;
734 GARY_INIT(rls->segments, 0);
735 GARY_INIT(rls->request.variants, 0);
740 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
742 struct request_segment *rs = GARY_PUSH(rls->segments);
745 rs->request.ind = num_requests++;
746 rs->request.type = REQUEST_SEGMENT;
748 GARY_INIT(rs->request.variants, 0);
751 struct variant *v = GARY_PUSH(rs->request.variants);
758 void cut_edge(struct graph_edge *e, double dist)
760 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "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);
762 struct graph_edge *new = xmalloc(sizeof(struct graph_edge));
766 switch (e->label->type)
768 case SYMBOLIZER_TEXT:
769 new->label = xmalloc(sizeof(struct sym_text));
770 *((struct sym_text *) new->label) = *((struct sym_text *) e->label);
776 struct osm_node *n1 = e->n1->o;
777 struct osm_node *n2 = e->n2->o;
779 if ((n1->x == n2->x) && (n1->y == n2->y))
781 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
782 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Won't cut point\n");
786 struct osm_node *n11 = xmalloc(sizeof(struct osm_node));
787 struct graph_node *gn = xmalloc(sizeof(struct graph_node));
789 double vsize = hypot(n1->x - n2->x, n1->y - n2->y);
790 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
791 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
793 e->n2 = new->n1 = gn;
795 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
796 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
800 void make_segments(void)
802 for (uns i=0; i<GARY_SIZE(longlines); i++)
804 // Skip lines which are not labelled
805 if (! (longlines[i].first && longlines[i].first->label))
808 struct request_line *request = make_new_line();
809 struct request_section *rls = make_new_section(request);
810 struct request_segment *rs = NULL;
812 struct graph_edge *e = longlines[i].first;
813 double cur_length = 0;
815 struct sym_text *st = NULL;
816 if (e->label->type == SYMBOLIZER_TEXT)
818 st = (struct sym_text *) e->label;
822 // FIXME: Should other label types be supported in future?
823 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Warning: Skipping line\n");
827 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "New longline\n");
833 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "BEWARE: Edge cycle\n");
838 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "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);
840 if (st && (e->length < st->tw))
843 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Warning: Skipping segment\n");
847 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
849 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Edge too long, length is %.2f; %.2f - %.2f = %.2f\n", e->length, conf_max_section_length, cur_length, conf_max_section_length - cur_length);
850 // HACK to prevent cutting to 0 lenght
851 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
854 rs = make_new_segment(rls, NULL);
855 rs->label = xmalloc(sizeof(struct sym_text));
856 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
858 rs->x1 = e->n1->o->x;
859 rs->y1 = e->n1->o->y;
860 rs->x2 = e->n2->o->x;
861 rs->y2 = e->n2->o->y;
863 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
864 ((struct sym_text *) rs->label)->rotate = atan(rs->slope) * (-180 / M_PI);
865 struct variant *v = GARY_PUSH(rs->request.variants);
866 make_bitmap(v, rs->label);
868 rs->zindex = e->zindex;
870 cur_length += e->length;
871 if (cur_length > conf_max_section_length)
873 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "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);
875 rls = make_new_section(request);
882 if (request->sections[0].num_segments == 0)
884 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "WARNING: Longline without any segment, skipped\n");
886 struct request_section *rls = &request->sections[0];
887 GARY_FREE(rls->segments);
888 GARY_FREE(rls->request.variants);
890 struct request_line *rl = &requests_line[GARY_SIZE(requests_line)-1];
891 GARY_FREE(rl->sections);
892 GARY_FREE(rl->request.variants);
894 GARY_POP(requests_line);
900 void dump_linelabel_requests(void)
902 for (uns i=0; i<GARY_SIZE(requests_line); i++)
904 if (requests_line[i].sections[0].num_segments == 0)
906 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "Beware: Longline without any segment\n");
910 printf("Request for linelabel, %zu sections\n", GARY_SIZE(requests_line[i].sections));
911 dump_label(requests_line[i].sections[0].segments[0].label);
912 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
914 printf("%u section, %zu segments [%d]\n", j, GARY_SIZE(requests_line[i].sections[j].segments), requests_line[i].sections[j].request.ind);
915 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
917 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
918 printf("[%.2f; %.2f] -- [%.2f; %.2f]\t\t[%d]\n", rs->x1, rs->y1, rs->x2, rs->y2, rs->request.ind);
925 void dump_bitmaps(struct individual *individual)
927 bool *bitmap = xmalloc(page_width_int * page_height_int * sizeof(bool));
928 printf("Bitmap size is %d\n", page_width_int * page_height_int);
929 for (int i=0; i<page_height_int; i++)
930 for (int j=0; j<page_width_int; j++)
931 bitmap[i*page_width_int + j] = 0;
934 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
936 if (individual->placements[i].variant_used == -1) continue;
938 struct placement *p = &(individual->placements[i]);
939 struct variant *v = NULL;
941 switch (p->request->type)
943 case REQUEST_SEGMENT: ;
944 case REQUEST_POINT: ;
946 v = &(p->request->variants[p->variant_used]);
949 ASSERT(p->request->type != REQUEST_INVALID);
953 int base_x = p->x; int base_y = p->y;
954 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
956 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
958 if (v->bitmap[dr * v->width + dc])
960 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
961 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
966 DEBUG(dbg_overlaps, VERBOSITY_GENERAL, "There were %d collisions during bitmap dump\n", total);
968 FILE *fd_dump = fopen("dump.pbm", "w");
969 fprintf(fd_dump, "P1\n");
970 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
971 for (int i=0; i<page_height_int; i++)
973 for (int j=0; j<page_width_int; j++)
975 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
977 fprintf(fd_dump, "\n");
984 void dump_individual(struct individual *individual)
986 printf("*** Individual dump\n");
987 printf("(There are %d requests)\n", num_requests);
989 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
991 struct placement *p = &(individual->placements[i]);
993 switch (p->request->type)
996 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
999 struct request_line *rl = (struct request_line *) p->request;
1001 dump_label(rl->sections[0].segments[0].label);
1003 case REQUEST_SECTION: ;
1006 case REQUEST_SEGMENT: ;
1007 if (p->variant_used >= 0)
1008 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
1010 printf("Segment not placed\n");
1012 case REQUEST_AREA: ;
1013 struct request_area *ra = (struct request_area *) p->request;
1014 printf("Area label ");
1015 dump_label(ra->label);
1016 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
1019 ASSERT(p->request->type != 0);
1022 printf("\nTotal penalty: %d\n", individual->penalty);
1025 void dump_placements(struct individual *individual)
1027 printf("*** Individual placements dump\n");
1029 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1031 struct placement *p = &individual->placements[i];
1032 printf("Placement %d:\tPlacet at [%.2f; %.2f] using variant %d\n", p->ind, p->x, p->y, p->variant_used);
1036 void plan_individual(struct individual *individual)
1038 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1040 struct symbol *s = NULL;
1041 z_index_t zindex = 0;
1043 if (individual->placements[i].variant_used < 0)
1045 printf("Skipping placement of request %d\n", individual->placements[i].request->ind);
1048 if (individual->placements[i].variant_used < 0) continue;
1049 switch (individual->placements[i].request->type)
1051 case REQUEST_POINT: ;
1052 struct request_point *rp = (struct request_point *) individual->placements[i].request;
1054 zindex = rp->zindex;
1056 case REQUEST_SEGMENT: ;
1057 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1059 zindex = rs->zindex;
1061 case REQUEST_LINE: ;
1063 case REQUEST_AREA: ;
1064 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1066 zindex = ra->zindex;
1069 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1073 DEBUG(dbg_plan, VERBOSITY_PLACEMENT, "Will plan symbol of request %d at [%.2f; %.2f] on %u\n", individual->placements[i].request->ind, individual->placements[i].x, individual->placements[i].y, zindex);
1075 if (s) switch (s->type)
1077 case SYMBOLIZER_POINT: ;
1078 struct sym_point *sp = (struct sym_point *) s;
1079 sp->x = individual->placements[i].x;
1080 sp->y = individual->placements[i].y;
1081 sym_plan((struct symbol *) sp, zindex);
1083 case SYMBOLIZER_ICON: ;
1084 struct sym_icon *si = (struct sym_icon *) s;
1085 si->sir.x = individual->placements[i].x;
1086 si->sir.y = individual->placements[i].y;
1087 sym_plan((struct symbol *) si, zindex);
1089 case SYMBOLIZER_TEXT: ;
1090 struct sym_text *st = (struct sym_text *) s;
1091 st->x = individual->placements[i].x;
1092 st->y = individual->placements[i].y;
1093 st->next_duplicate = NULL;
1094 DEBUG(dbg_plan, VERBOSITY_PLACEMENT, "Planning text %s at [%.2f; %.2f] on %u, with rotation %.2f\n", osm_val_decode(st->text), st->x, st->y, zindex, st->rotate);
1095 sym_plan((struct symbol *) st, zindex);
1098 ASSERT(s->type != SYMBOLIZER_INVALID);
1103 void dump_penalties(struct individual **population)
1105 for (int i=0; i<conf_pop_size; i++)
1107 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1111 void dump_placement_links(struct placement *p)
1113 struct map_placement *mp = p->map_links;
1117 printf(" %d", mp->part->ind);
1118 mp = mp->next_in_placement;
1124 void dump_part_links(struct map_part *part)
1126 struct map_placement *mp = part->placement->next_in_map;
1130 printf(" %d", mp->placement->ind);
1131 mp = mp->next_in_map;
1137 void dump_links(struct individual *individual)
1139 printf("Dumping links in individual\n");
1140 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1142 dump_placement_links(&individual->placements[i]);
1146 void compute_sizes(void)
1148 page_width_int = floor(page_width);
1149 page_height_int = floor(page_height);
1151 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1152 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1153 num_map_parts = num_map_parts_row * num_map_parts_col;
1155 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1156 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1157 if (dbg_evolution >= VERBOSITY_GENERAL)
1159 printf("Breeding parameters:\n");
1160 printf(" %d individuals are created\n", breed_pop_size);
1161 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1164 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1165 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1166 if (dbg_evolution >= VERBOSITY_GENERAL)
1168 printf("Mutation parameters:\n");
1169 printf(" %d individuals are created\n", mutate_pop_size);
1170 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1173 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1174 if (dbg_evolution >= VERBOSITY_GENERAL)
1176 printf("Elitism parameters:\n");
1177 printf(" %d best individuals are copied\n", elite_pop_size);
1180 if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1184 elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1188 fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1189 fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1195 void labeller_label(void)
1197 //srandom(29011992);
1203 printf("Will deal with %d requests\n", num_requests);
1205 GARY_INIT(population1, conf_pop_size);
1206 GARY_INIT(population2, conf_pop_size);
1209 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1211 if (dbg_evolution >= VERBOSITY_GENERAL)
1213 printf("Penalties after initialization\n");
1214 dump_penalties(population1);
1217 while (! shall_terminate())
1221 printf("\n*** Iteration %d ***\n", iteration);
1227 struct individual **swp = population1;
1228 population1 = population2;
1231 clear_population(population2);
1235 if (dbg_evolution >= VERBOSITY_INDIVIDUAL)
1237 printf("Penalties before sort\n");
1238 dump_penalties(population1);
1241 DEBUG(dbg_evolution, VERBOSITY_GENERAL, "Sorting population\n");
1242 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1244 if (dbg_evolution >= VERBOSITY_GENERAL)
1246 printf("Penalties after sort\n");
1247 dump_penalties(population1);
1250 old_best = population1[0]->penalty;
1253 if (dbg_overlaps >= VERBOSITY_GENERAL)
1254 dump_bitmaps(population1[0]);
1256 plan_individual(population1[0]);
1261 void labeller_cleanup(void)
1264 GARY_FREE(longlines);
1266 GARY_FREE(requests_point);
1267 GARY_FREE(requests_area);
1269 for (uns i=0; i<GARY_SIZE(requests_line); i++)
1271 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
1273 GARY_FREE(requests_line[i].sections[j].segments);
1275 GARY_FREE(requests_line[i].sections);
1277 GARY_FREE(requests_line);
1280 void make_population(void)
1282 for (int i=0; i<conf_pop_size; i++)
1284 num_placements = 0; // FIXME: This IS a terrible HACK
1285 struct individual *i2 = ep_alloc(ep_individuals);
1286 init_individual(i2);
1287 population2[i] = i2;
1289 DEBUG(dbg_init, VERBOSITY_INDIVIDUAL, "Making individual %d\n", i);
1290 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1291 population1[i] = individual;
1294 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1296 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1299 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1301 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1303 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1305 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1307 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1309 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1314 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1316 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1319 hide_segment_labels(individual);
1321 ASSERT(p == num_requests);
1325 bool shall_terminate(void)
1327 switch (conf_term_cond)
1329 case TERM_COND_PENALTY:
1330 return (population1[0]->penalty < conf_penalty_bound);
1331 case TERM_COND_STAGNATION:
1332 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1333 case TERM_COND_ITERATIONS:
1334 return (iteration >= conf_iteration_limit);
1336 fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1345 struct individual **breed_buffer;
1346 while (i < breed_pop_size)
1348 int parent1 = randint(0, breed_rbest_size);
1349 int parent2 = randint(0, breed_rbest_size);
1350 DEBUG(dbg_breeding, VERBOSITY_INDIVIDUAL, "Will breed %d and %d\n", parent1, parent2);
1352 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1353 population2[pop2_ind++] = breed_buffer[0];
1354 population2[pop2_ind++] = breed_buffer[1];
1360 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1362 struct individual **buffer = xmalloc(2*sizeof(struct individual));
1363 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1364 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1367 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1369 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1371 if (! processed[parent1->placements[i].ind])
1373 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Creating symbol closure for placement %u\n", i);
1375 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1376 int x = randint(0, 2);
1380 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Copying parent->child 1->1 and 2->2\n");
1381 copy_symbols(clos_symbols, parent1, child1, &processed);
1382 copy_symbols(clos_symbols, parent2, child2, &processed);
1386 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Copying parent->child 2->1 and 1->2\n");
1387 copy_symbols(clos_symbols, parent2, child1, &processed);
1388 copy_symbols(clos_symbols, parent1, child2, &processed);
1391 GARY_FREE(clos_symbols);
1395 GARY_FREE(processed);
1397 if (conf_mutate_children)
1399 if (randdouble() < conf_mutate_children_prob) perform_mutation(child1);
1400 else hide_segment_labels(child1);
1402 if (randdouble() < conf_mutate_children_prob) perform_mutation(child2);
1403 else hide_segment_labels(child2);
1413 for (int i=0; i < mutate_pop_size; i++)
1415 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Creating %d-th individual by mutation\n", i);
1416 int ind = randint(0, mutate_rbest_size);
1417 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Mutating %d-th individual of original population\n", ind);
1418 population2[pop2_ind] = ep_alloc(ep_individuals);
1419 copy_individual(population1[ind], population2[pop2_ind]);
1420 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1421 perform_mutation(population2[pop2_ind]);
1426 void perform_mutation(struct individual *individual)
1428 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1430 double x = randdouble();
1433 if (x <= acc + conf_mutate_move_bound)
1435 DEBUG(dbg_mutation, VERBOSITY_PLACEMENT, "Mutation: Moving symbol in placement %u\n", i);
1436 move_symbol(&(individual->placements[i]));
1439 acc += conf_mutate_move_bound;
1441 if (x <= acc + conf_mutate_regen_bound)
1443 gen_coords(&(individual->placements[i]));
1446 acc += conf_mutate_regen_bound;
1448 if (x <= acc + conf_mutate_chvar_bound)
1450 struct placement *p = &(individual->placements[i]);
1451 switch (p->request->type)
1455 // Does nothing when there are 0 variants... does it mind?
1456 p->variant_used = randint(-1, GARY_SIZE(p->request->variants));
1458 case REQUEST_SEGMENT:
1459 p->variant_used = randint(0, GARY_SIZE(p->request->variants));
1461 case REQUEST_SECTION:
1462 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments));
1470 hide_segment_labels(individual);
1475 for (int i=0; i<elite_pop_size; i++)
1477 DEBUG(dbg_evolution, VERBOSITY_INDIVIDUAL, "Iteration %d: Copying individual #%d from pop 1 to #%d in pop 2\n", iteration, i, pop2_ind);
1478 population2[pop2_ind] = ep_alloc(ep_individuals);
1479 copy_individual(population1[i], population2[pop2_ind++]);
1483 int overlaps(struct placement *p1, struct placement *p2)
1485 if (p1->request->type != REQUEST_POINT &&
1486 p1->request->type != REQUEST_SEGMENT &&
1487 p1->request->type != REQUEST_AREA)
1490 if (p2->request->type != REQUEST_POINT &&
1491 p2->request->type != REQUEST_SEGMENT &&
1492 p2->request->type != REQUEST_AREA)
1495 if (p1->variant_used == -1 || p2->variant_used == -1)
1498 struct variant *v1, *v2;
1500 v1 = &(p1->request->variants[p1->variant_used]);
1501 v2 = &(p2->request->variants[p2->variant_used]);
1503 // FIXME: This doesn't fully respect offset which it probably should
1504 int p1x = p1->x; int p1y = p1->y;
1505 int p2x = p2->x; int p2y = p2->y;
1508 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1509 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1511 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1512 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1519 int get_overlap(struct placement *p, int **planned_ptr, int iteration)
1521 int *planned = *planned_ptr;
1523 if (p->variant_used == -1) return 0;
1525 struct map_part **parts = get_map_parts(p);
1528 DEBUG(dbg_overlaps, VERBOSITY_PLACEMENT, "Placement of request %d seems not to be placed\n", p->request->ind);
1532 struct placement **others;
1534 planned[p->request->ind] = iteration;
1535 GARY_INIT(others, 0);
1537 //printf("Iterating over parts of placement %d (at [%.2f; %.2f] / %d)\n", p->ind, p->x, p->y, p->variant_used);
1538 for (uns i=0; i<GARY_SIZE(parts); i++)
1540 //printf("%d:\t", parts[i]->ind);
1541 //dump_part_links(parts[i]);
1542 struct map_placement *mp = parts[i]->placement->next_in_map;
1545 if (planned[mp->placement->request->ind] != iteration)
1547 struct placement **p = GARY_PUSH(others);
1549 planned[mp->placement->request->ind] = iteration;
1551 mp = mp->next_in_map;
1556 for (uns i=0; i<GARY_SIZE(others); i++)
1558 overlap += overlaps(p, others[i]);
1564 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1566 printf("Placement %d (of request %d) add %d to overlaps", p->ind, p->request->ind, overlap);
1567 dump_placement_links(p);
1570 if (p->x < 0) overlap += 0 - p->x;
1571 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1572 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1574 if (p->y < 0) overlap += 0 - p->y;
1575 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1576 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1581 int individual_overlap(struct individual *individual)
1586 GARY_INIT_ZERO(planned, GARY_SIZE(individual->placements));
1588 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1590 overlap += get_overlap(&individual->placements[i], &planned, i+1);
1595 // printf("Total overlap is %d\n", overlap);
1600 double get_distance(struct placement *p)
1602 if (p->variant_used < 0) return 0;
1603 struct variant *v = &p->request->variants[p->variant_used];
1605 double dx, dy, distance;
1606 switch (p->request->type)
1608 case REQUEST_POINT: ;
1609 struct request_point *rp = (struct request_point *) p->request;
1610 dx = rp->x + v->offset_x - p->x;
1611 dy = rp->y + v->offset_y - p->y;
1612 distance = hypot(dx, dy);
1613 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1615 case REQUEST_SEGMENT: ;
1616 struct request_segment *rs = (struct request_segment *) p->request;
1617 struct sym_text *st = (struct sym_text *) rs->label;
1619 double width = p->request->variants[p->variant_used].width;
1620 double rotated_x = p->x + width * sin(st->rotate / (-180 / M_PI));
1621 double rotated_y = p->y + width * cos(st->rotate / (-180 / M_PI));
1623 if (rs->x1 < rs->x2)
1630 else if (rotated_x > rs->x2)
1632 dx = rotated_x - rs->x2;
1633 dy = rotated_y - rs->y2;
1647 else if (rotated_x > rs->x1)
1649 dx = rotated_x - rs->x1;
1650 dy = rotated_y - rs->y1;
1658 distance = hypot(dx, dy);
1660 case REQUEST_AREA: ;
1661 struct request_area *ra = (struct request_area *) p->request;
1662 dx = ra->cx + v->offset_x - p->x;
1663 dy = ra->cy + v->offset_y - p->y;
1664 distance = hypot(dx, dy);
1665 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1671 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Placement %d has distance %.2f\n", p->ind, distance);
1675 double individual_distances(struct individual *individual)
1679 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1681 distances += get_distance(&individual->placements[i]);
1687 double get_omittment(struct placement *p)
1689 if (p->variant_used >= 0) return 0;
1692 switch (p->request->type)
1703 double individual_omittment(struct individual *individual)
1707 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1709 omittment += get_omittment(&individual->placements[i]);
1715 int cmp_individual(const void *a, const void *b)
1717 struct individual **ia = (struct individual **) a;
1718 struct individual **ib = (struct individual **) b;
1720 return (*ia)->penalty - (*ib)->penalty;
1723 void rank_population(void)
1727 for (int i=0; i<conf_pop_size; i++)
1729 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Individual %d\n", i);
1730 population1[i]->penalty = 0;
1732 penalty = individual_omittment(population1[i]);
1733 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for omittment\n", penalty);
1734 population1[i]->penalty += penalty;
1736 penalty = individual_overlap(population1[i]);
1737 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for overlap\n", penalty);
1738 population1[i]->penalty += penalty;
1740 penalty = individual_distances(population1[i]);
1741 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for distances\n", penalty);
1742 population1[i]->penalty += penalty;
1746 struct map_part **get_map_parts(struct placement *p)
1748 if (p->variant_used < 0) return NULL;
1750 struct map_part **buffer;
1751 GARY_INIT(buffer, 0);
1753 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1756 switch (p->request->type)
1759 case REQUEST_SEGMENT:
1761 v = p->request->variants[p->variant_used];
1764 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Skipping unsupported request type (%d)\n", p->request->type);
1768 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Bitmap is %d x %d\n", v.width, v.height);
1770 int x_min = max2(0, p->x) / conf_map_part_width;
1771 // CHECK ME: Is rounding needed?
1772 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1773 int y_min = max2(0, p->y) / conf_map_part_height;
1774 // CHECK ME: Is rounding needed?
1775 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1777 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1779 for (int y=y_min; y<=y_max; y++)
1780 for (int x=x_min; x<=x_max; x++)
1782 struct map_part **m = GARY_PUSH(buffer);
1783 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Asking for %d of %zu\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1784 *m = p->individual->map[y * num_map_parts_row + x];
1787 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1792 void update_map_parts_delete(struct placement *p)
1794 struct map_placement *mp = p->map_links;
1797 mp->prev_in_map->next_in_map = mp->next_in_map;
1798 if (mp->next_in_map)
1799 mp->next_in_map->prev_in_map = mp->prev_in_map;
1801 struct map_placement *tmp = mp;
1802 mp = mp->next_in_placement;
1805 p->map_links = NULL;
1808 void update_map_parts_create(struct placement *p)
1810 struct map_part **parts = get_map_parts(p);
1811 if (parts == NULL) return;
1813 for (uns i=0; i<GARY_SIZE(parts); i++)
1815 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
1817 mp->part = parts[i];
1819 mp->next_in_map = parts[i]->placement->next_in_map;
1820 mp->prev_in_map = parts[i]->placement;
1821 parts[i]->placement->next_in_map = mp;
1822 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1824 mp->next_in_placement = p->map_links;
1825 mp->prev_in_placement = NULL;
1832 void update_map_parts(struct placement *p)
1834 update_map_parts_delete(p);
1835 update_map_parts_create(p);
1838 void gen_coords(struct placement *p)
1840 switch(p->request->type)
1843 gen_coords_point(p);
1848 case REQUEST_SEGMENT:
1849 gen_coords_segment(p);
1853 printf("Not yet implemented\n");
1856 DEBUG(dbg_movement, VERBOSITY_ALL, "Testing request type\n");
1857 ASSERT(p->request->type != REQUEST_INVALID);
1860 update_map_parts(p);
1863 double gen_movement(void)
1865 double m = (random() % 100000) / 10000;
1866 m = pow(m, 1.0/3) * flip(1, -1);
1867 DEBUG(dbg_movement, VERBOSITY_ALL, "Movement %.2f\n", m);
1871 double gen_movement_uniform(void)
1873 return (move_max - move_min) * randdouble() * flip(1, -1);
1876 void gen_coords_point(struct placement *p)
1878 p->x = p->x + gen_movement();
1881 void gen_coords_segment(struct placement *p)
1883 struct request_segment *rs = (struct request_segment *) p->request;
1884 p->x = (rs->x1 + rs->x2) / 2;
1885 p->y = (rs->y1 + rs->y2) / 2;
1888 void gen_coords_area(struct placement *p)
1890 struct request_area *ra = (struct request_area *) p->request;
1892 p->x = p->x + gen_movement();
1893 p->y = p->y + gen_movement();
1895 DEBUG(dbg_movement, VERBOSITY_PLACEMENT, "Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1898 int randint(int min, int max)
1900 if (min == max) return min;
1902 return min + (r % (max - min));
1905 struct placement **get_closure(struct placement *placement)
1907 struct placement **closure;
1908 GARY_INIT(closure, 0);
1909 bool *chosen = xmalloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1910 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1911 chosen[placement->request->ind] = 1;
1913 struct placement **p = GARY_PUSH(closure); *p = placement;
1916 while (first < GARY_SIZE(closure))
1918 DEBUG(dbg_breeding, VERBOSITY_ALL, "Iterating, first is %u of current %zu\n", first, GARY_SIZE(closure));
1919 struct placement **overlapping = get_overlapping(placement);
1920 if (! overlapping) { first++; continue; }
1922 struct placement **filtered = filter(overlapping, &chosen);
1923 DEBUG(dbg_breeding, VERBOSITY_ALL, "There are %zu new overlapping symbols\n", GARY_SIZE(filtered));
1924 GARY_FREE(overlapping);
1925 overlapping = filtered;
1926 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1928 if (! chosen[overlapping[j]->request->ind])
1930 if (overlaps(*p, overlapping[j]))
1932 p = GARY_PUSH(closure); *p = overlapping[j];
1933 DEBUG(dbg_breeding, VERBOSITY_ALL, "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);
1934 chosen[overlapping[j]->request->ind] = 1;
1938 GARY_FREE(overlapping);
1947 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1949 bool *processed = *processed_ptr;
1950 DEBUG(dbg_breeding, VERBOSITY_ALL, "Will copy %zu symbols\n", GARY_SIZE(closure));
1952 for (uns i=0; i<GARY_SIZE(closure); i++)
1954 processed[closure[i]->ind] = 1;
1955 int ind = closure[i]->ind;
1956 child->placements[ind] = parent->placements[ind];
1957 child->placements[ind].individual = child;
1958 child->placements[ind].processed = 0;
1959 child->placements[ind].map_links = NULL;
1960 update_map_parts(&child->placements[ind]);
1964 void move_symbol(struct placement *p)
1966 switch (p->request->type)
1970 move_symbol_point(p);
1972 case REQUEST_SEGMENT:
1973 move_symbol_segment(p);
1976 ASSERT(p->request->type != REQUEST_INVALID);
1980 void move_symbol_point(struct placement *p)
1982 p->x += gen_movement_uniform();
1983 p->y += gen_movement_uniform();
1986 void move_symbol_segment(struct placement *p)
1988 double m = gen_movement_uniform();
1991 p->y += m * ((struct request_segment *) p->request)->slope;
1994 void hide_segment_labels(struct individual *individual)
1996 // BEWARE: This fully depends on current genetic encoding
1998 int used = -1, num = -1;
1999 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
2001 switch (individual->placements[i].request->type)
2003 case REQUEST_SECTION:
2004 used = individual->placements[i].variant_used;
2007 case REQUEST_SEGMENT:
2009 individual->placements[i].variant_used = 0;
2011 individual->placements[i].variant_used = -1;
2020 void init_placement(struct placement *p, struct individual *individual, struct request *r)
2022 p->ind = num_placements++;
2025 p->x = p->y = 0; // To prevent valgrind from complaining
2026 p->variant_used = 0;
2027 p->map_links = NULL;
2028 p->individual = individual;
2031 case REQUEST_POINT: ;
2032 struct request_point *rp = (struct request_point *) r;
2036 case REQUEST_LINE: ;
2038 case REQUEST_SECTION: ;
2039 struct request_section *rls = (struct request_section *) r;
2040 p->variant_used = randint(0, rls->num_segments);
2042 case REQUEST_SEGMENT: ;
2043 struct request_segment *rs = (struct request_segment *) r;
2047 case REQUEST_AREA: ;
2048 struct request_area *ra = (struct request_area *) r;
2051 p->variant_used = 0;
2054 ASSERT(p->request->type != REQUEST_INVALID);
2058 DEBUG(dbg_init, VERBOSITY_PLACEMENT, "Inited placement to [%.2f; %.2f]\n", p->x, p->y);
2062 void reset_individual_map(struct individual *i)
2064 for (uns j=0; j<num_map_parts; j++)
2066 struct map_placement *mp = i->map[j]->placement;
2069 struct map_placement *tmp = mp;
2070 mp = mp->next_in_map;
2075 struct map_part *part = xmalloc(sizeof(struct map_part));
2078 mp = xmalloc(sizeof(struct map_placement));
2079 part->placement = mp;
2080 mp->placement = &dummy_placement;
2081 mp->next_in_map = mp->prev_in_map = NULL;
2082 mp->next_in_placement = mp->prev_in_placement = NULL;
2089 void update_individual(struct individual *individual)
2091 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
2093 update_map_parts_delete(&individual->placements[i]);
2098 void clear_individual(struct individual *individual)
2100 for (uns j=0; j<num_map_parts; j++)
2102 struct map_placement *mp = individual->map[j]->placement;
2105 struct map_placement *tmp = mp;
2106 mp = mp->next_in_map;
2110 free(individual->map[j]);
2113 GARY_FREE(individual->map);
2114 GARY_FREE(individual->placements);
2115 ep_free(ep_individuals, individual);
2118 void clear_population(struct individual **pop)
2120 for (uns i=0; i<GARY_SIZE(pop); i++)
2122 clear_individual(pop[i]);
2126 struct placement **get_overlapping(struct placement *p)
2128 struct placement **buffer;
2129 GARY_INIT(buffer, 0);
2131 struct map_part **parts = get_map_parts(p);
2132 if (! parts) return NULL;
2134 for (uns i=0; i<GARY_SIZE(parts); i++)
2136 struct map_placement *mp = parts[i]->placement->next_in_map;
2139 if (p->variant_used >= 0)
2141 struct placement **p = GARY_PUSH(buffer);
2144 mp = mp->next_in_map;
2149 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu potentially overlapping placements\n", GARY_SIZE(buffer));
2154 struct placement **filter(struct placement **list, bool **pred_ptr)
2156 bool *pred = *pred_ptr; // As GARY can't be passed directly
2157 struct placement **filtered;
2158 GARY_INIT(filtered, 0);
2160 for (uns i=0; i<GARY_SIZE(list); i++)
2162 if (pred[list[i]->request->ind])
2165 struct placement **p = GARY_PUSH(filtered);
2172 int flip(int a, int b)
2174 return (random() % 2 ? a : b);
2177 double randdouble(void)
2179 return ((double) rand() / (double) RAND_MAX);
2182 void init_individual(struct individual *individual)
2184 GARY_INIT(individual->placements, num_requests);
2185 GARY_INIT(individual->map, 0);
2186 for (uns j=0; j<num_map_parts; j++)
2188 GARY_PUSH(individual->map);
2189 struct map_part *part = xmalloc(sizeof(struct map_part));
2190 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
2191 part->placement = mp;
2193 mp->placement = &dummy_placement;
2194 mp->next_in_map = mp->prev_in_map = NULL;
2195 mp->next_in_placement = mp->prev_in_placement = NULL;
2196 individual->map[j] = part;
2198 individual->penalty = 0;
2201 void copy_individual(struct individual *src, struct individual *dest)
2203 init_individual(dest);
2204 dest->penalty = src->penalty;
2206 for (uns i=0; i<GARY_SIZE(src->placements); i++)
2208 dest->placements[i] = src->placements[i];
2209 dest->placements[i].map_links = NULL;
2210 dest->placements[i].individual = dest;
2212 update_map_parts_create(&dest->placements[i]);