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 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex);
150 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex);
152 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
153 void perform_mutation(struct individual *individual);
154 void init_placement(struct placement *p, struct individual *individual, struct request *r);
155 void init_individual(struct individual *i);
156 void copy_individual(struct individual *src, struct individual *dest);
157 int cmp_individual(const void *a, const void *b);
159 void clear_individual(struct individual *individual);
160 void clear_population(struct individual **pop);
162 void make_bitmap(struct variant *v, struct symbol *sym);
163 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
164 void make_bitmap_point(struct variant *v, struct sym_point *sp);
165 void make_bitmap_label(struct variant *v, struct sym_text *text);
167 double gen_movement(void);
168 double gen_movement_uniform(void);
169 void move_symbol(struct placement *p);
170 void move_symbol_point(struct placement *p);
171 void move_symbol_segment(struct placement *p);
172 void hide_segment_labels(struct individual *individual);
174 void gen_coords(struct placement *p);
175 void gen_coords_point(struct placement *p);
176 void gen_coords_segment(struct placement *p);
177 void gen_coords_area(struct placement *p);
179 struct map_part **get_map_parts(struct placement *p);
180 void update_map_parts(struct placement *p);
181 void update_map_parts_delete(struct placement *p);
182 void update_map_parts_create(struct placement *p);
183 struct placement **get_closure(struct placement *placement);
184 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
185 struct placement **get_overlapping(struct placement *p);
186 struct placement **filter(struct placement **list, bool **pred_ptr);
189 void make_graph(void);
190 void label_graph(void);
191 void bfs_wrapper(void);
192 void bfs(uns longline);
193 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
194 void make_segments(void);
196 void cut_edge(struct graph_edge *e, double dist);
197 struct request_line *make_new_line(void);
198 struct request_section *make_new_section(struct request_line *rl);
199 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
201 void dump_bitmaps(struct individual *individual);
202 void dump_graph(void);
203 void dump_longlines(void);
204 void dump_linelabel_requests(void);
205 void dump_individual(struct individual *individual);
206 void dump_label(struct symbol *sym);
207 void dump_penalties(struct individual **population);
208 void dump_placements(struct individual *individual);
209 void dump_placement_links(struct placement *p);
210 void dump_part_links(struct map_part *part);
211 void dump_links(struct individual *individual);
213 int randint(int min, int max);
214 int flip(int a, int b);
215 double randdouble(void);
217 int max2(int a, int b);
218 int min2(int a, int b);
219 int max4(int a, int b, int c, int d);
220 int min4(int a, int b, int c, int d);
222 struct placement dummy_placement;
224 int max2(int a, int b)
226 return (a > b ? a : b);
229 int min2(int a, int b)
231 return (a < b ? a : b);
234 int max4(int a, int b, int c, int d)
236 return max2(max2(a, b), max2(c, d));
239 int min4(int a, int b, int c, int d)
241 return min2(min2(a, b), min2(c, d));
244 void dump_label(struct symbol *sym)
248 case SYMBOLIZER_TEXT: ;
249 struct sym_text *st = (struct sym_text *) sym;
250 printf("%s\n", osm_val_decode(st->text));
257 void labeller_conf(void)
259 cf_declare_section("Evolution", &evolution_cf, 0);
262 void labeller_init(void)
264 GARY_INIT(requests_point, 0);
265 GARY_INIT(requests_line, 0);
266 GARY_INIT(requests_area, 0);
267 GARY_INIT(buffer_line, 0);
268 GARY_INIT(buffer_linelabel, 0);
269 ep_individuals = ep_new(sizeof(struct individual), 1);
274 void make_bitmap(struct variant *v, struct symbol *sym)
276 v->offset_x = v->offset_y = 0;
280 case SYMBOLIZER_POINT:
281 make_bitmap_point(v, (struct sym_point *) sym);
283 case SYMBOLIZER_ICON:
284 make_bitmap_icon(v, (struct sym_icon *) sym);
286 case SYMBOLIZER_TEXT:
287 make_bitmap_label(v, (struct sym_text *) sym);
290 ASSERT(sym->type != SYMBOLIZER_INVALID);
294 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
296 v->width = si->sir.width + 1;
297 v->height = si->sir.height + 1;
298 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
299 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
302 void make_bitmap_point(struct variant *v, struct sym_point *sp)
304 v->width = v->height = sp->size + 1;
305 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
306 // FIXME: Okay, memset would be much nicer here
307 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
310 void make_bitmap_label(struct variant *v, struct sym_text *text)
312 int tw = ceil(text->tw);
313 int th = ceil(text->th);
315 double rotate_rad = text->rotate / (-180 / M_PI);
317 // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th]
318 // They could be declared before but should not be initialized in code
319 // as reassigning x-coordinate affects computation of y-cordinate
323 int lrx = tw * cos(rotate_rad);
324 int lry = tw * sin(rotate_rad);
326 int ulx = th * sin(rotate_rad);
327 int uly = th * cos(rotate_rad);
329 int urx = tw * cos(rotate_rad) + th * sin(rotate_rad);
330 int ury = tw * sin(rotate_rad) + th * cos(rotate_rad);
332 int min_x = min4(llx, lrx, ulx, urx);
333 int min_y = min4(lly, lry, uly, ury);
334 int max_x = max4(llx, lrx, ulx, urx);
335 int max_y = max4(lly, lry, uly, ury);
337 v->width = max_x - min_x + 1;
338 v->height = max_y - min_y + 1;
339 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
340 memset(v->bitmap, 0, v->width * v->height * sizeof(bool));
342 for (int i=0; i<th; i++)
344 for (int j=0; j<tw; j++)
346 int nx = j*cos(rotate_rad) + i*sin(rotate_rad);
347 int ny = j*sin(rotate_rad) + i*cos(rotate_rad);
348 v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1;
353 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
355 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding point\n");
356 if (object->type != OSM_TYPE_NODE)
358 printf("Warning: Point label requested on non-point object\n");
362 struct request_point *r = GARY_PUSH(requests_point);
364 r->request.type = REQUEST_POINT;
365 r->request.ind = num_requests++;
374 GARY_INIT(r->request.variants, 0);
376 struct variant *v = GARY_PUSH(r->request.variants);
378 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
384 case SYMBOLIZER_ICON:
386 r->x = ((struct sym_icon *)sym)->sir.x;
387 r->y = ((struct sym_icon *)sym)->sir.y;
394 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
397 void labeller_notify_line(struct symbol *sym, z_index_t zindex)
399 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding line on %u\n", zindex);
400 struct buffer_line *b = GARY_PUSH(buffer_line);
401 b->line = (struct sym_line *) sym;
405 void labeller_add_label(struct symbol *sym, struct osm_object *o, z_index_t zindex)
410 if (osm_way_cyclic_p((struct osm_way *) o))
411 labeller_add_arealabel(sym, o, zindex);
413 labeller_add_linelabel(sym, o, zindex);
420 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
422 if (o->type != OSM_TYPE_WAY)
424 printf("Linelabel request on object which is not way\n");
428 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Labelling way %ju on %u\n", o->id, zindex);
429 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
430 ll->way = (struct osm_way *) o;
435 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
437 DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding area on %u\n", zindex);
438 struct request_area *r = GARY_PUSH(requests_area);
440 r->request.type = REQUEST_AREA;
441 r->request.ind = num_requests++;
443 r->o = (struct osm_multipolygon *) o;
447 osm_obj_center(o, &(r->cx), &(r->cy));
449 GARY_INIT(r->request.variants, 0);
450 struct variant *v = GARY_PUSH(r->request.variants);
454 void make_graph(void)
457 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
459 DEBUG(dbg_graph, VERBOSITY_GENERAL, "Extracting nodes, will iterate over %zu ways\n", GARY_SIZE(buffer_line));
460 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
462 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
463 struct graph_node *g_prev = NULL;
464 struct osm_node *o_prev = NULL;
466 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
468 // FIXME: Shall osm_object's type be checked here?
469 struct osm_node *o_node = (struct osm_node *) ref->o;
471 struct graph_node *g_node = hash_find(ref->o->id);
474 g_node = hash_new(ref->o->id);
475 GARY_INIT(g_node->edges, 0);
477 g_node->id = ref->o->id;
478 g_node->num = num_nodes++;
488 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge));
489 e->num = num_edges++;
490 e->id = buffer_line[i].line->s.o->id;
491 e->color = buffer_line[i].line->color;
492 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
498 e->longline = (uns) -1;
499 e->line = buffer_line[i].line;
503 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
505 edge = GARY_PUSH(g_node->edges);
514 void dump_graph(void)
516 HASH_FOR_ALL(hash, node)
518 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
519 for (uns i=0; i<GARY_SIZE(node->edges); i++)
521 struct graph_edge *e = node->edges[i];
522 printf("\t edge (%d) #%ju to ", e->num, e->id);
523 if (node->edges[i]->n1->id == node->id)
524 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
525 else if (node->edges[i]->n2->id == node->id)
526 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
529 // This shouldn't ever happen
530 printf("BEWARE! Edge is associated with a node it doesn't belongs to!\n");
535 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));
536 else if ((node->edges[i]->label)) printf("Labelled\n");
538 printf(" colored %d;", node->edges[i]->color);
539 printf(" length %.2f", node->edges[i]->length);
546 void label_graph(void)
548 DEBUG(dbg_graph, VERBOSITY_GENERAL, "There are %zu line labels requested\n", GARY_SIZE(buffer_linelabel));
549 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
551 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
552 DEBUG(dbg_graph, VERBOSITY_INDIVIDUAL, "Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
553 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
555 DEBUG(dbg_graph, VERBOSITY_PLACEMENT, "Looking for node %ju\n", ref->o->id);
556 struct graph_node *n = hash_find(ref->o->id);
559 printf("BEWARE! Requested node couldn't be found.\n");
563 DEBUG(dbg_graph, VERBOSITY_ALL, "Searching among %zu edges\n", GARY_SIZE(n->edges));
564 for (uns j=0; j<GARY_SIZE(n->edges); j++)
566 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
568 DEBUG(dbg_graph, VERBOSITY_ALL, "Labelling node %ju\n", n->id);
569 n->edges[j]->label = buffer_linelabel[i].label;
570 n->edges[j]->zindex = buffer_linelabel[i].zindex;
578 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
580 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
581 struct graph_edge *candidate = NULL;
583 for (uns i=0; i<GARY_SIZE(node->edges); i++)
585 struct graph_edge *other = node->edges[i];
586 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
588 if ((uns) other->visited != e->longline) {
589 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "Pushing new edge %d / %ju\n", other->num, other->id);
590 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
592 other->visited = e->longline;
595 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
596 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
599 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
600 (e->label) && (other->label) &&
601 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
602 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
604 if (! candidate || (other->length > candidate->length))
611 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "New line in longline %u\n", e->longline);
612 struct graph_edge *other = candidate;
613 other->longline = e->longline;
615 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
616 ((dir == DIR_FWD) && (other->n2->id == node->id)))
618 struct graph_node *swp = other->n2;
619 other->n2 = other->n1;
628 longlines[other->longline].first = other;
641 void bfs(uns longline)
643 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "BFS called for longline %u\n", longline);
644 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "%zu longlines exist\n", GARY_SIZE(longlines));
646 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
648 struct graph_edge *cur = bfs_queue[i];
649 DEBUG(dbg_bfs, VERBOSITY_PLACEMENT, "Exploring new edge %d; %zu remaining\n", cur->num, GARY_SIZE(bfs_queue));
651 cur->visited = longline;
653 if (cur->longline == (uns) -1)
656 if (cur->dir == DIR_UNSET)
658 cur->dir = DIR_CENTER;
659 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
660 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
667 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
670 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
680 void bfs_wrapper(void)
682 GARY_INIT(bfs_queue, 0);
683 GARY_INIT(longlines, 0);
685 HASH_FOR_ALL(hash, node)
687 for (uns i=0; i<GARY_SIZE(node->edges); i++)
689 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
691 GARY_PUSH(longlines);
692 longlines[GARY_SIZE(longlines)-1].first = node->edges[i];
694 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Running new BFS\n");
695 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Creating longline %zu\n", GARY_SIZE(longlines)-1);
697 GARY_RESIZE(bfs_queue, 0);
698 struct graph_edge **e = GARY_PUSH(bfs_queue);
700 node->edges[i]->longline = GARY_SIZE(longlines)-1;
701 bfs(node->edges[i]->longline);
703 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
704 DEBUG(dbg_bfs, VERBOSITY_INDIVIDUAL, "Planned %zu edges\n", GARY_SIZE(bfs_queue));
710 GARY_FREE(bfs_queue);
713 void dump_longlines(void)
715 printf("*** Longlines dump\n");
716 for (uns i=0; i<GARY_SIZE(longlines); i++)
718 printf("Longline %u:", i);
719 struct graph_edge *e = longlines[i].first;
720 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
721 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
726 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
727 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
734 struct request_line *make_new_line(void)
736 struct request_line *rl = GARY_PUSH(requests_line);
737 rl->request.ind = num_requests++;
738 rl->request.type = REQUEST_LINE;
739 GARY_INIT(rl->sections, 0);
740 GARY_INIT(rl->request.variants, 0);
745 struct request_section *make_new_section(struct request_line *rl)
747 struct request_section *rls = GARY_PUSH(rl->sections);
748 rls->request.ind = num_requests++;
749 rls->request.type = REQUEST_SECTION;
750 rls->num_segments = 0;
751 GARY_INIT(rls->segments, 0);
752 GARY_INIT(rls->request.variants, 0);
757 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
759 struct request_segment *rs = GARY_PUSH(rls->segments);
762 rs->request.ind = num_requests++;
763 rs->request.type = REQUEST_SEGMENT;
765 GARY_INIT(rs->request.variants, 0);
768 struct variant *v = GARY_PUSH(rs->request.variants);
775 void cut_edge(struct graph_edge *e, double dist)
777 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);
779 struct graph_edge *new = xmalloc(sizeof(struct graph_edge));
783 switch (e->label->type)
785 case SYMBOLIZER_TEXT:
786 new->label = xmalloc(sizeof(struct sym_text));
787 *((struct sym_text *) new->label) = *((struct sym_text *) e->label);
793 struct osm_node *n1 = e->n1->o;
794 struct osm_node *n2 = e->n2->o;
796 if ((n1->x == n2->x) && (n1->y == n2->y))
798 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
799 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Won't cut point\n");
803 struct osm_node *n11 = xmalloc(sizeof(struct osm_node));
804 struct graph_node *gn = xmalloc(sizeof(struct graph_node));
806 double vsize = hypot(n1->x - n2->x, n1->y - n2->y);
807 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
808 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
810 e->n2 = new->n1 = gn;
812 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
813 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
817 void make_segments(void)
819 for (uns i=0; i<GARY_SIZE(longlines); i++)
821 // Skip lines which are not labelled
822 if (! (longlines[i].first && longlines[i].first->label))
825 struct request_line *request = make_new_line();
826 struct request_section *rls = make_new_section(request);
827 struct request_segment *rs = NULL;
829 struct graph_edge *e = longlines[i].first;
830 double cur_length = 0;
832 struct sym_text *st = NULL;
833 if (e->label->type == SYMBOLIZER_TEXT)
835 st = (struct sym_text *) e->label;
839 // FIXME: Should other label types be supported in future?
840 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Warning: Skipping line\n");
844 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "New longline\n");
850 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "BEWARE: Edge cycle\n");
855 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);
857 if (st && (e->length < st->tw))
860 DEBUG(dbg_segments, VERBOSITY_PLACEMENT, "Warning: Skipping segment\n");
864 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
866 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);
867 // HACK to prevent cutting to 0 lenght
868 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
871 rs = make_new_segment(rls, NULL);
872 rs->label = xmalloc(sizeof(struct sym_text));
873 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
875 rs->x1 = e->n1->o->x;
876 rs->y1 = e->n1->o->y;
877 rs->x2 = e->n2->o->x;
878 rs->y2 = e->n2->o->y;
880 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
881 ((struct sym_text *) rs->label)->rotate = atan(rs->slope) * (-180 / M_PI);
882 struct variant *v = GARY_PUSH(rs->request.variants);
883 make_bitmap(v, rs->label);
885 rs->zindex = e->zindex;
887 cur_length += e->length;
888 if (cur_length > conf_max_section_length)
890 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);
892 rls = make_new_section(request);
899 if (request->sections[0].num_segments == 0)
901 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "WARNING: Longline without any segment, skipped\n");
903 struct request_section *rls = &request->sections[0];
904 GARY_FREE(rls->segments);
905 GARY_FREE(rls->request.variants);
907 struct request_line *rl = &requests_line[GARY_SIZE(requests_line)-1];
908 GARY_FREE(rl->sections);
909 GARY_FREE(rl->request.variants);
911 GARY_POP(requests_line);
917 void dump_linelabel_requests(void)
919 for (uns i=0; i<GARY_SIZE(requests_line); i++)
921 if (requests_line[i].sections[0].num_segments == 0)
923 DEBUG(dbg_segments, VERBOSITY_INDIVIDUAL, "Beware: Longline without any segment\n");
927 printf("Request for linelabel, %zu sections\n", GARY_SIZE(requests_line[i].sections));
928 dump_label(requests_line[i].sections[0].segments[0].label);
929 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
931 printf("%u section, %zu segments [%d]\n", j, GARY_SIZE(requests_line[i].sections[j].segments), requests_line[i].sections[j].request.ind);
932 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
934 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
935 printf("[%.2f; %.2f] -- [%.2f; %.2f]\t\t[%d]\n", rs->x1, rs->y1, rs->x2, rs->y2, rs->request.ind);
942 void dump_bitmaps(struct individual *individual)
944 bool *bitmap = xmalloc(page_width_int * page_height_int * sizeof(bool));
945 printf("Bitmap size is %d\n", page_width_int * page_height_int);
946 for (int i=0; i<page_height_int; i++)
947 for (int j=0; j<page_width_int; j++)
948 bitmap[i*page_width_int + j] = 0;
951 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
953 if (individual->placements[i].variant_used == -1) continue;
955 struct placement *p = &(individual->placements[i]);
956 struct variant *v = NULL;
958 switch (p->request->type)
960 case REQUEST_SEGMENT: ;
961 case REQUEST_POINT: ;
963 v = &(p->request->variants[p->variant_used]);
966 ASSERT(p->request->type != REQUEST_INVALID);
970 int base_x = p->x; int base_y = p->y;
971 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
973 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
975 if (v->bitmap[dr * v->width + dc])
977 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
978 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
983 DEBUG(dbg_overlaps, VERBOSITY_GENERAL, "There were %d collisions during bitmap dump\n", total);
985 FILE *fd_dump = fopen("dump.pbm", "w");
986 fprintf(fd_dump, "P1\n");
987 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
988 for (int i=0; i<page_height_int; i++)
990 for (int j=0; j<page_width_int; j++)
992 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
994 fprintf(fd_dump, "\n");
1001 void dump_individual(struct individual *individual)
1003 printf("*** Individual dump\n");
1004 printf("(There are %d requests)\n", num_requests);
1006 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1008 struct placement *p = &(individual->placements[i]);
1010 switch (p->request->type)
1013 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
1015 case REQUEST_LINE: ;
1016 struct request_line *rl = (struct request_line *) p->request;
1018 dump_label(rl->sections[0].segments[0].label);
1020 case REQUEST_SECTION: ;
1023 case REQUEST_SEGMENT: ;
1024 if (p->variant_used >= 0)
1025 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
1027 printf("Segment not placed\n");
1029 case REQUEST_AREA: ;
1030 struct request_area *ra = (struct request_area *) p->request;
1031 printf("Area label ");
1032 dump_label(ra->label);
1033 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
1036 ASSERT(p->request->type != 0);
1039 printf("\nTotal penalty: %d\n", individual->penalty);
1042 void dump_placements(struct individual *individual)
1044 printf("*** Individual placements dump\n");
1046 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1048 struct placement *p = &individual->placements[i];
1049 printf("Placement %d:\tPlacet at [%.2f; %.2f] using variant %d\n", p->ind, p->x, p->y, p->variant_used);
1053 void plan_individual(struct individual *individual)
1055 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1057 struct symbol *s = NULL;
1058 z_index_t zindex = 0;
1060 if (individual->placements[i].variant_used < 0)
1062 printf("Skipping placement of request %d\n", individual->placements[i].request->ind);
1065 if (individual->placements[i].variant_used < 0) continue;
1066 switch (individual->placements[i].request->type)
1068 case REQUEST_POINT: ;
1069 struct request_point *rp = (struct request_point *) individual->placements[i].request;
1071 zindex = rp->zindex;
1073 case REQUEST_SEGMENT: ;
1074 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1076 zindex = rs->zindex;
1078 case REQUEST_LINE: ;
1080 case REQUEST_AREA: ;
1081 struct request_area *ra = (struct request_area *) individual->placements[i].request;
1083 zindex = ra->zindex;
1086 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1090 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);
1092 if (s) switch (s->type)
1094 case SYMBOLIZER_POINT: ;
1095 struct sym_point *sp = (struct sym_point *) s;
1096 sp->x = individual->placements[i].x;
1097 sp->y = individual->placements[i].y;
1098 sym_plan((struct symbol *) sp, zindex);
1100 case SYMBOLIZER_ICON: ;
1101 struct sym_icon *si = (struct sym_icon *) s;
1102 si->sir.x = individual->placements[i].x;
1103 si->sir.y = individual->placements[i].y;
1104 sym_plan((struct symbol *) si, zindex);
1106 case SYMBOLIZER_TEXT: ;
1107 struct sym_text *st = (struct sym_text *) s;
1108 st->x = individual->placements[i].x;
1109 st->y = individual->placements[i].y;
1110 st->next_duplicate = NULL;
1111 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);
1112 sym_plan((struct symbol *) st, zindex);
1115 ASSERT(s->type != SYMBOLIZER_INVALID);
1120 void dump_penalties(struct individual **population)
1122 for (int i=0; i<conf_pop_size; i++)
1124 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1128 void dump_placement_links(struct placement *p)
1130 struct map_placement *mp = p->map_links;
1134 printf(" %d", mp->part->ind);
1135 mp = mp->next_in_placement;
1141 void dump_part_links(struct map_part *part)
1143 struct map_placement *mp = part->placement->next_in_map;
1147 printf(" %d", mp->placement->ind);
1148 mp = mp->next_in_map;
1154 void dump_links(struct individual *individual)
1156 printf("Dumping links in individual\n");
1157 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1159 dump_placement_links(&individual->placements[i]);
1163 void compute_sizes(void)
1165 page_width_int = floor(page_width);
1166 page_height_int = floor(page_height);
1168 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1169 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1170 num_map_parts = num_map_parts_row * num_map_parts_col;
1172 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1173 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1174 if (dbg_evolution >= VERBOSITY_GENERAL)
1176 printf("Breeding parameters:\n");
1177 printf(" %d individuals are created\n", breed_pop_size);
1178 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1181 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1182 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1183 if (dbg_evolution >= VERBOSITY_GENERAL)
1185 printf("Mutation parameters:\n");
1186 printf(" %d individuals are created\n", mutate_pop_size);
1187 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1190 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1191 if (dbg_evolution >= VERBOSITY_GENERAL)
1193 printf("Elitism parameters:\n");
1194 printf(" %d best individuals are copied\n", elite_pop_size);
1197 if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1201 elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1205 fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1206 fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1212 void labeller_label(void)
1214 //srandom(29011992);
1220 printf("Will deal with %d requests\n", num_requests);
1222 GARY_INIT(population1, conf_pop_size);
1223 GARY_INIT(population2, conf_pop_size);
1226 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1228 if (dbg_evolution >= VERBOSITY_GENERAL)
1230 printf("Penalties after initialization\n");
1231 dump_penalties(population1);
1234 while (! shall_terminate())
1238 printf("\n*** Iteration %d ***\n", iteration);
1244 struct individual **swp = population1;
1245 population1 = population2;
1248 clear_population(population2);
1252 if (dbg_evolution >= VERBOSITY_INDIVIDUAL)
1254 printf("Penalties before sort\n");
1255 dump_penalties(population1);
1258 DEBUG(dbg_evolution, VERBOSITY_GENERAL, "Sorting population\n");
1259 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1261 if (dbg_evolution >= VERBOSITY_GENERAL)
1263 printf("Penalties after sort\n");
1264 dump_penalties(population1);
1267 old_best = population1[0]->penalty;
1270 if (dbg_overlaps >= VERBOSITY_GENERAL)
1271 dump_bitmaps(population1[0]);
1273 plan_individual(population1[0]);
1278 void labeller_cleanup(void)
1281 GARY_FREE(longlines);
1283 GARY_FREE(requests_point);
1284 GARY_FREE(requests_area);
1286 for (uns i=0; i<GARY_SIZE(requests_line); i++)
1288 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
1290 GARY_FREE(requests_line[i].sections[j].segments);
1292 GARY_FREE(requests_line[i].sections);
1294 GARY_FREE(requests_line);
1297 void make_population(void)
1299 for (int i=0; i<conf_pop_size; i++)
1301 num_placements = 0; // FIXME: This IS a terrible HACK
1302 struct individual *i2 = ep_alloc(ep_individuals);
1303 init_individual(i2);
1304 population2[i] = i2;
1306 DEBUG(dbg_init, VERBOSITY_INDIVIDUAL, "Making individual %d\n", i);
1307 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1308 population1[i] = individual;
1311 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1313 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1316 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1318 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1320 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1322 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1324 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1326 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1331 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1333 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1336 hide_segment_labels(individual);
1338 ASSERT(p == num_requests);
1342 bool shall_terminate(void)
1344 switch (conf_term_cond)
1346 case TERM_COND_PENALTY:
1347 return (population1[0]->penalty < conf_penalty_bound);
1348 case TERM_COND_STAGNATION:
1349 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1350 case TERM_COND_ITERATIONS:
1351 return (iteration >= conf_iteration_limit);
1353 fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1362 struct individual **breed_buffer;
1363 while (i < breed_pop_size)
1365 int parent1 = randint(0, breed_rbest_size);
1366 int parent2 = randint(0, breed_rbest_size);
1367 DEBUG(dbg_breeding, VERBOSITY_INDIVIDUAL, "Will breed %d and %d\n", parent1, parent2);
1369 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1370 population2[pop2_ind++] = breed_buffer[0];
1371 population2[pop2_ind++] = breed_buffer[1];
1377 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1379 struct individual **buffer = xmalloc(2*sizeof(struct individual));
1380 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1381 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1384 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1386 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1388 if (! processed[parent1->placements[i].ind])
1390 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Creating symbol closure for placement %u\n", i);
1392 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1393 int x = randint(0, 2);
1397 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Copying parent->child 1->1 and 2->2\n");
1398 copy_symbols(clos_symbols, parent1, child1, &processed);
1399 copy_symbols(clos_symbols, parent2, child2, &processed);
1403 DEBUG(dbg_breeding, VERBOSITY_PLACEMENT, "Copying parent->child 2->1 and 1->2\n");
1404 copy_symbols(clos_symbols, parent2, child1, &processed);
1405 copy_symbols(clos_symbols, parent1, child2, &processed);
1408 GARY_FREE(clos_symbols);
1412 GARY_FREE(processed);
1414 if (conf_mutate_children)
1416 if (randdouble() < conf_mutate_children_prob) perform_mutation(child1);
1417 else hide_segment_labels(child1);
1419 if (randdouble() < conf_mutate_children_prob) perform_mutation(child2);
1420 else hide_segment_labels(child2);
1430 for (int i=0; i < mutate_pop_size; i++)
1432 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Creating %d-th individual by mutation\n", i);
1433 int ind = randint(0, mutate_rbest_size);
1434 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Mutating %d-th individual of original population\n", ind);
1435 population2[pop2_ind] = ep_alloc(ep_individuals);
1436 copy_individual(population1[ind], population2[pop2_ind]);
1437 DEBUG(dbg_mutation, VERBOSITY_INDIVIDUAL, "Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1438 perform_mutation(population2[pop2_ind]);
1443 void perform_mutation(struct individual *individual)
1445 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1447 double x = randdouble();
1450 if (x <= acc + conf_mutate_move_bound)
1452 DEBUG(dbg_mutation, VERBOSITY_PLACEMENT, "Mutation: Moving symbol in placement %u\n", i);
1453 move_symbol(&(individual->placements[i]));
1456 acc += conf_mutate_move_bound;
1458 if (x <= acc + conf_mutate_regen_bound)
1460 gen_coords(&(individual->placements[i]));
1463 acc += conf_mutate_regen_bound;
1465 if (x <= acc + conf_mutate_chvar_bound)
1467 struct placement *p = &(individual->placements[i]);
1468 switch (p->request->type)
1472 // Does nothing when there are 0 variants... does it mind?
1473 p->variant_used = randint(-1, GARY_SIZE(p->request->variants));
1475 case REQUEST_SEGMENT:
1476 p->variant_used = randint(0, GARY_SIZE(p->request->variants));
1478 case REQUEST_SECTION:
1479 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments));
1487 hide_segment_labels(individual);
1492 for (int i=0; i<elite_pop_size; i++)
1494 DEBUG(dbg_evolution, VERBOSITY_INDIVIDUAL, "Iteration %d: Copying individual #%d from pop 1 to #%d in pop 2\n", iteration, i, pop2_ind);
1495 population2[pop2_ind] = ep_alloc(ep_individuals);
1496 copy_individual(population1[i], population2[pop2_ind++]);
1500 int overlaps(struct placement *p1, struct placement *p2)
1502 if (p1->request->type != REQUEST_POINT &&
1503 p1->request->type != REQUEST_SEGMENT &&
1504 p1->request->type != REQUEST_AREA)
1507 if (p2->request->type != REQUEST_POINT &&
1508 p2->request->type != REQUEST_SEGMENT &&
1509 p2->request->type != REQUEST_AREA)
1512 if (p1->variant_used == -1 || p2->variant_used == -1)
1515 struct variant *v1, *v2;
1517 v1 = &(p1->request->variants[p1->variant_used]);
1518 v2 = &(p2->request->variants[p2->variant_used]);
1520 // FIXME: This doesn't fully respect offset which it probably should
1521 int p1x = p1->x; int p1y = p1->y;
1522 int p2x = p2->x; int p2y = p2->y;
1525 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1526 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1528 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1529 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1536 int get_overlap(struct placement *p, int **planned_ptr, int iteration)
1538 int *planned = *planned_ptr;
1540 if (p->variant_used == -1) return 0;
1542 struct map_part **parts = get_map_parts(p);
1545 DEBUG(dbg_overlaps, VERBOSITY_PLACEMENT, "Placement of request %d seems not to be placed\n", p->request->ind);
1549 struct placement **others;
1551 planned[p->request->ind] = iteration;
1552 GARY_INIT(others, 0);
1554 //printf("Iterating over parts of placement %d (at [%.2f; %.2f] / %d)\n", p->ind, p->x, p->y, p->variant_used);
1555 for (uns i=0; i<GARY_SIZE(parts); i++)
1557 //printf("%d:\t", parts[i]->ind);
1558 //dump_part_links(parts[i]);
1559 struct map_placement *mp = parts[i]->placement->next_in_map;
1562 if (planned[mp->placement->request->ind] != iteration)
1564 struct placement **p = GARY_PUSH(others);
1566 planned[mp->placement->request->ind] = iteration;
1568 mp = mp->next_in_map;
1573 for (uns i=0; i<GARY_SIZE(others); i++)
1575 overlap += overlaps(p, others[i]);
1581 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1583 printf("Placement %d (of request %d) add %d to overlaps", p->ind, p->request->ind, overlap);
1584 dump_placement_links(p);
1587 if (p->x < 0) overlap += 0 - p->x;
1588 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1589 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1591 if (p->y < 0) overlap += 0 - p->y;
1592 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1593 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1598 int individual_overlap(struct individual *individual)
1603 GARY_INIT_ZERO(planned, GARY_SIZE(individual->placements));
1605 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1607 overlap += get_overlap(&individual->placements[i], &planned, i+1);
1612 // printf("Total overlap is %d\n", overlap);
1617 double get_distance(struct placement *p)
1619 if (p->variant_used < 0) return 0;
1620 struct variant *v = &p->request->variants[p->variant_used];
1622 double dx, dy, distance;
1623 switch (p->request->type)
1625 case REQUEST_POINT: ;
1626 struct request_point *rp = (struct request_point *) p->request;
1627 dx = rp->x + v->offset_x - p->x;
1628 dy = rp->y + v->offset_y - p->y;
1629 distance = hypot(dx, dy);
1630 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1632 case REQUEST_SEGMENT: ;
1633 struct request_segment *rs = (struct request_segment *) p->request;
1634 struct sym_text *st = (struct sym_text *) rs->label;
1636 double width = p->request->variants[p->variant_used].width;
1637 double rotated_x = p->x + width * sin(st->rotate / (-180 / M_PI));
1638 double rotated_y = p->y + width * cos(st->rotate / (-180 / M_PI));
1640 if (rs->x1 < rs->x2)
1647 else if (rotated_x > rs->x2)
1649 dx = rotated_x - rs->x2;
1650 dy = rotated_y - rs->y2;
1664 else if (rotated_x > rs->x1)
1666 dx = rotated_x - rs->x1;
1667 dy = rotated_y - rs->y1;
1675 distance = hypot(dx, dy);
1677 case REQUEST_AREA: ;
1678 struct request_area *ra = (struct request_area *) p->request;
1679 dx = ra->cx + v->offset_x - p->x;
1680 dy = ra->cy + v->offset_y - p->y;
1681 distance = hypot(dx, dy);
1682 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1688 DEBUG(dbg_rank, VERBOSITY_PLACEMENT, "Placement %d has distance %.2f\n", p->ind, distance);
1692 double individual_distances(struct individual *individual)
1696 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1698 distances += get_distance(&individual->placements[i]);
1704 double get_omittment(struct placement *p)
1706 if (p->variant_used >= 0) return 0;
1709 switch (p->request->type)
1720 double individual_omittment(struct individual *individual)
1724 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1726 omittment += get_omittment(&individual->placements[i]);
1732 int cmp_individual(const void *a, const void *b)
1734 struct individual **ia = (struct individual **) a;
1735 struct individual **ib = (struct individual **) b;
1737 return (*ia)->penalty - (*ib)->penalty;
1740 void rank_population(void)
1744 for (int i=0; i<conf_pop_size; i++)
1746 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Individual %d\n", i);
1747 population1[i]->penalty = 0;
1749 penalty = individual_omittment(population1[i]);
1750 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for omittment\n", penalty);
1751 population1[i]->penalty += penalty;
1753 penalty = individual_overlap(population1[i]);
1754 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for overlap\n", penalty);
1755 population1[i]->penalty += penalty;
1757 penalty = individual_distances(population1[i]);
1758 DEBUG(dbg_rank, VERBOSITY_INDIVIDUAL, "Increasing penalty by %d for distances\n", penalty);
1759 population1[i]->penalty += penalty;
1763 struct map_part **get_map_parts(struct placement *p)
1765 if (p->variant_used < 0) return NULL;
1767 struct map_part **buffer;
1768 GARY_INIT(buffer, 0);
1770 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);
1773 switch (p->request->type)
1776 case REQUEST_SEGMENT:
1778 v = p->request->variants[p->variant_used];
1781 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Skipping unsupported request type (%d)\n", p->request->type);
1785 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Bitmap is %d x %d\n", v.width, v.height);
1787 int x_min = max2(0, p->x) / conf_map_part_width;
1788 // CHECK ME: Is rounding needed?
1789 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1790 int y_min = max2(0, p->y) / conf_map_part_height;
1791 // CHECK ME: Is rounding needed?
1792 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1794 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1796 for (int y=y_min; y<=y_max; y++)
1797 for (int x=x_min; x<=x_max; x++)
1799 struct map_part **m = GARY_PUSH(buffer);
1800 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Asking for %d of %zu\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1801 *m = p->individual->map[y * num_map_parts_row + x];
1804 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1809 void update_map_parts_delete(struct placement *p)
1811 struct map_placement *mp = p->map_links;
1814 mp->prev_in_map->next_in_map = mp->next_in_map;
1815 if (mp->next_in_map)
1816 mp->next_in_map->prev_in_map = mp->prev_in_map;
1818 struct map_placement *tmp = mp;
1819 mp = mp->next_in_placement;
1822 p->map_links = NULL;
1825 void update_map_parts_create(struct placement *p)
1827 struct map_part **parts = get_map_parts(p);
1828 if (parts == NULL) return;
1830 for (uns i=0; i<GARY_SIZE(parts); i++)
1832 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
1834 mp->part = parts[i];
1836 mp->next_in_map = parts[i]->placement->next_in_map;
1837 mp->prev_in_map = parts[i]->placement;
1838 parts[i]->placement->next_in_map = mp;
1839 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1841 mp->next_in_placement = p->map_links;
1842 mp->prev_in_placement = NULL;
1849 void update_map_parts(struct placement *p)
1851 update_map_parts_delete(p);
1852 update_map_parts_create(p);
1855 void gen_coords(struct placement *p)
1857 switch(p->request->type)
1860 gen_coords_point(p);
1865 case REQUEST_SEGMENT:
1866 gen_coords_segment(p);
1870 printf("Not yet implemented\n");
1873 DEBUG(dbg_movement, VERBOSITY_ALL, "Testing request type\n");
1874 ASSERT(p->request->type != REQUEST_INVALID);
1877 update_map_parts(p);
1880 double gen_movement(void)
1882 double m = (random() % 100000) / 10000;
1883 m = pow(m, 1.0/3) * flip(1, -1);
1884 DEBUG(dbg_movement, VERBOSITY_ALL, "Movement %.2f\n", m);
1888 double gen_movement_uniform(void)
1890 return (move_max - move_min) * randdouble() * flip(1, -1);
1893 void gen_coords_point(struct placement *p)
1895 p->x = p->x + gen_movement();
1898 void gen_coords_segment(struct placement *p)
1900 struct request_segment *rs = (struct request_segment *) p->request;
1901 p->x = (rs->x1 + rs->x2) / 2;
1902 p->y = (rs->y1 + rs->y2) / 2;
1905 void gen_coords_area(struct placement *p)
1907 struct request_area *ra = (struct request_area *) p->request;
1909 p->x = p->x + gen_movement();
1910 p->y = p->y + gen_movement();
1912 DEBUG(dbg_movement, VERBOSITY_PLACEMENT, "Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1915 int randint(int min, int max)
1917 if (min == max) return min;
1919 return min + (r % (max - min));
1922 struct placement **get_closure(struct placement *placement)
1924 struct placement **closure;
1925 GARY_INIT(closure, 0);
1926 bool *chosen = xmalloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1927 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1928 chosen[placement->request->ind] = 1;
1930 struct placement **p = GARY_PUSH(closure); *p = placement;
1933 while (first < GARY_SIZE(closure))
1935 DEBUG(dbg_breeding, VERBOSITY_ALL, "Iterating, first is %u of current %zu\n", first, GARY_SIZE(closure));
1936 struct placement **overlapping = get_overlapping(placement);
1937 if (! overlapping) { first++; continue; }
1939 struct placement **filtered = filter(overlapping, &chosen);
1940 DEBUG(dbg_breeding, VERBOSITY_ALL, "There are %zu new overlapping symbols\n", GARY_SIZE(filtered));
1941 GARY_FREE(overlapping);
1942 overlapping = filtered;
1943 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1945 if (! chosen[overlapping[j]->request->ind])
1947 if (overlaps(*p, overlapping[j]))
1949 p = GARY_PUSH(closure); *p = overlapping[j];
1950 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);
1951 chosen[overlapping[j]->request->ind] = 1;
1955 GARY_FREE(overlapping);
1964 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1966 bool *processed = *processed_ptr;
1967 DEBUG(dbg_breeding, VERBOSITY_ALL, "Will copy %zu symbols\n", GARY_SIZE(closure));
1969 for (uns i=0; i<GARY_SIZE(closure); i++)
1971 processed[closure[i]->ind] = 1;
1972 int ind = closure[i]->ind;
1973 child->placements[ind] = parent->placements[ind];
1974 child->placements[ind].individual = child;
1975 child->placements[ind].processed = 0;
1976 child->placements[ind].map_links = NULL;
1977 update_map_parts(&child->placements[ind]);
1981 void move_symbol(struct placement *p)
1983 switch (p->request->type)
1987 move_symbol_point(p);
1989 case REQUEST_SEGMENT:
1990 move_symbol_segment(p);
1993 ASSERT(p->request->type != REQUEST_INVALID);
1997 void move_symbol_point(struct placement *p)
1999 p->x += gen_movement_uniform();
2000 p->y += gen_movement_uniform();
2003 void move_symbol_segment(struct placement *p)
2005 double m = gen_movement_uniform();
2008 p->y += m * ((struct request_segment *) p->request)->slope;
2011 void hide_segment_labels(struct individual *individual)
2013 // BEWARE: This fully depends on current genetic encoding
2015 int used = -1, num = -1;
2016 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
2018 switch (individual->placements[i].request->type)
2020 case REQUEST_SECTION:
2021 used = individual->placements[i].variant_used;
2024 case REQUEST_SEGMENT:
2026 individual->placements[i].variant_used = 0;
2028 individual->placements[i].variant_used = -1;
2037 void init_placement(struct placement *p, struct individual *individual, struct request *r)
2039 p->ind = num_placements++;
2042 p->x = p->y = 0; // To prevent valgrind from complaining
2043 p->variant_used = 0;
2044 p->map_links = NULL;
2045 p->individual = individual;
2048 case REQUEST_POINT: ;
2049 struct request_point *rp = (struct request_point *) r;
2053 case REQUEST_LINE: ;
2055 case REQUEST_SECTION: ;
2056 struct request_section *rls = (struct request_section *) r;
2057 p->variant_used = randint(0, rls->num_segments);
2059 case REQUEST_SEGMENT: ;
2060 struct request_segment *rs = (struct request_segment *) r;
2064 case REQUEST_AREA: ;
2065 struct request_area *ra = (struct request_area *) r;
2068 p->variant_used = 0;
2071 ASSERT(p->request->type != REQUEST_INVALID);
2075 DEBUG(dbg_init, VERBOSITY_PLACEMENT, "Inited placement to [%.2f; %.2f]\n", p->x, p->y);
2079 void reset_individual_map(struct individual *i)
2081 for (uns j=0; j<num_map_parts; j++)
2083 struct map_placement *mp = i->map[j]->placement;
2086 struct map_placement *tmp = mp;
2087 mp = mp->next_in_map;
2092 struct map_part *part = xmalloc(sizeof(struct map_part));
2095 mp = xmalloc(sizeof(struct map_placement));
2096 part->placement = mp;
2097 mp->placement = &dummy_placement;
2098 mp->next_in_map = mp->prev_in_map = NULL;
2099 mp->next_in_placement = mp->prev_in_placement = NULL;
2106 void update_individual(struct individual *individual)
2108 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
2110 update_map_parts_delete(&individual->placements[i]);
2115 void clear_individual(struct individual *individual)
2117 for (uns j=0; j<num_map_parts; j++)
2119 struct map_placement *mp = individual->map[j]->placement;
2122 struct map_placement *tmp = mp;
2123 mp = mp->next_in_map;
2127 free(individual->map[j]);
2130 GARY_FREE(individual->map);
2131 GARY_FREE(individual->placements);
2132 ep_free(ep_individuals, individual);
2135 void clear_population(struct individual **pop)
2137 for (uns i=0; i<GARY_SIZE(pop); i++)
2139 clear_individual(pop[i]);
2143 struct placement **get_overlapping(struct placement *p)
2145 struct placement **buffer;
2146 GARY_INIT(buffer, 0);
2148 struct map_part **parts = get_map_parts(p);
2149 if (! parts) return NULL;
2151 for (uns i=0; i<GARY_SIZE(parts); i++)
2153 struct map_placement *mp = parts[i]->placement->next_in_map;
2156 if (p->variant_used >= 0)
2158 struct placement **p = GARY_PUSH(buffer);
2161 mp = mp->next_in_map;
2166 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu potentially overlapping placements\n", GARY_SIZE(buffer));
2171 struct placement **filter(struct placement **list, bool **pred_ptr)
2173 bool *pred = *pred_ptr; // As GARY can't be passed directly
2174 struct placement **filtered;
2175 GARY_INIT(filtered, 0);
2177 for (uns i=0; i<GARY_SIZE(list); i++)
2179 if (pred[list[i]->request->ind])
2182 struct placement **p = GARY_PUSH(filtered);
2189 int flip(int a, int b)
2191 return (random() % 2 ? a : b);
2194 double randdouble(void)
2196 return ((double) rand() / (double) RAND_MAX);
2199 void init_individual(struct individual *individual)
2201 GARY_INIT(individual->placements, num_requests);
2202 GARY_INIT(individual->map, 0);
2203 for (uns j=0; j<num_map_parts; j++)
2205 GARY_PUSH(individual->map);
2206 struct map_part *part = xmalloc(sizeof(struct map_part));
2207 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
2208 part->placement = mp;
2210 mp->placement = &dummy_placement;
2211 mp->next_in_map = mp->prev_in_map = NULL;
2212 mp->next_in_placement = mp->prev_in_placement = NULL;
2213 individual->map[j] = part;
2215 individual->penalty = 0;
2218 void copy_individual(struct individual *src, struct individual *dest)
2220 init_individual(dest);
2221 dest->penalty = src->penalty;
2223 for (uns i=0; i<GARY_SIZE(src->placements); i++)
2225 dest->placements[i] = src->placements[i];
2226 dest->placements[i].map_links = NULL;
2227 dest->placements[i].individual = dest;
2229 update_map_parts_create(&dest->placements[i]);