]> mj.ucw.cz Git - leo.git/blob - labeller.c
Labelling: Comparison of individuals
[leo.git] / labeller.c
1 #include <errno.h>
2
3 #include <ucw/lib.h>
4 #include <ucw/gary.h>
5 #include <ucw/mempool.h>
6 #include <ucw/eltpool.h>
7
8 #include "leo.h"
9 #include "sym.h"
10 #include "map.h"
11 #include "labeller.h"
12
13 #define HASH_NODE struct graph_node
14 #define HASH_PREFIX(x) hash_##x
15 #define HASH_KEY_ATOMIC id
16 #define HASH_WANT_FIND
17 #define HASH_WANT_NEW
18 #define HASH_WANT_CLEANUP
19 #include <ucw/hashtable.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <fcntl.h>
25
26 #define BLOCK_SIZE 4096
27
28 //struct mempool *mpool_requests;
29
30 static struct request_point *requests_point;
31 static struct request_line *requests_line;
32 static struct request_area *requests_area;
33
34 static struct graph_edge **bfs_queue;
35 static struct longline *longlines; int num_longlines;
36 static struct buffer_line *buffer_line;
37 static struct buffer_linelabel *buffer_linelabel;
38
39 struct eltpool *ep_individuals;
40
41 struct individual **population1;
42 struct individual **population2;
43
44 int dbg_segments = 0;
45 int dbg_plan = 0;
46 int dbg_requests = 0;
47 int dbg_graph = 0;
48 int dbg_bfs = 0;
49 int dbg_map_parts = 0;
50 int dbg_movement = 0;
51 int dbg_init = 0;
52 int dbg_overlaps = 0;
53 int dbg_rank = 0;
54
55 int page_width_int;
56 int page_height_int;
57
58 int num_edges_dbg;
59 int num_nodes;
60 int num_edges = 0;
61 int dbg_num_hits = 0;
62
63 int conf_pop_size = 50;
64
65 int conf_penalty_bound = 0;
66 int conf_stagnation_bound = 0;
67 int conf_iteration_limit = 4;
68
69 int conf_term_cond = TERM_COND_ITERATIONS;
70
71 int conf_breed_rbest_perc = 80;
72 int conf_breed_pop_size_perc = 20;
73 int conf_breed_perc = 50;                       // Percentage of new pop created by breeding
74
75 bool conf_mutate_children = 1;
76 int conf_mutate_children_prob = 0.3;
77
78 int conf_mutate_rbest_perc = 60;
79 int conf_mutate_pop_size_perc = 20;
80
81 int conf_mutate_move_bound = 0.2;
82 int conf_mutate_regen_bound = 0.1;
83 int conf_mutate_chvar_bound = 0.1;
84
85 int conf_elite_perc = 5;
86
87 double conf_max_section_length = 100;
88 double conf_max_section_overlay = 10;
89
90 int old_best = 0; // FIXME: Shall be int max
91 int iteration = 0;
92 int pop2_ind;
93
94 int conf_part_size = 50;
95
96 int move_min = 0;
97 int move_max = 1;
98
99 int num_requests = 0;
100
101 int conf_map_part_width = 5;
102 int conf_map_part_height = 5;
103 int num_map_parts_row;
104 int num_map_parts_col;
105 int num_map_parts;
106
107 void make_graph(void);
108 void label_graph(void);
109 void join_edge(struct graph_edge *e, int dir);
110 void bfs(uns longline);
111 void make_segments(void);
112
113 int overlaps(struct placement *p1, struct placement *p2);
114 int get_overlap(struct placement *p);
115 int individual_overlap(struct individual *individual);
116
117 void make_population(void);
118 bool shall_terminate(void);
119 void breed(void);
120 void mutate(void);
121 void elite(void);
122 void rank_population(void);
123 void plan_individual(struct individual *individual);
124
125 int cmp_individual(const void *a, const void *b);
126
127 void make_bitmap(struct variant *v, struct symbol *sym);
128 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
129 void make_bitmap_point(struct variant *v, struct sym_point *sp);
130 void make_bitmap_label(struct variant *v, struct sym_text *text);
131
132 void cut_edge(struct graph_edge *e, double dist);
133 struct request_line *make_new_line(void);
134 struct request_section *make_new_section(struct request_line *rl);
135 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
136
137 void dump_bitmaps(struct individual *individual);
138 void dump_graph(void);
139 void bfs2(void);
140 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
141 void bfs_wrapper(void);
142 void oldbfs(void);
143 void dump_longlines(void);
144 void dump_linelabel_requests(void);
145 void dump_individual(struct individual *individual);
146 void print_label(struct symbol *sym);
147
148 double gen_movement(void);
149 void gen_coords(struct placement *p);
150 void gen_coords_point(struct placement *p);
151 void gen_coords_segment(struct placement *p);
152 void gen_coords_area(struct placement *p);
153
154 struct map_part **get_map_parts(struct placement *p);
155 void update_map_parts(struct placement *p);
156
157 void make_segments_old(void);
158
159 void labeller_cleanup(void);
160
161 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
162 void perform_mutation(struct individual *individual);
163
164 void hide_segment_labels(struct individual *individual);
165 void init_placement(struct placement *p, struct individual *individual, struct request *r);
166 void init_individual(struct individual *i);
167 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
168
169 int randint(int min, int max);
170
171 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2);
172 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
173 void move_symbol(struct placement *p);
174 void move_symbol_point(struct placement *p);
175
176 struct placement **get_overlapping(struct placement *p);
177 void filter(struct placement **list, bool *pred);
178
179 int flip(int a, int b);
180 double randdouble(void);
181
182 void cleanup(void);
183
184 void copy_individual(struct individual *src, struct individual *dest);
185
186 int max2(int a, int b);
187 int min2(int a, int b);
188 int max4(int a, int b, int c, int d);
189 int min4(int a, int b, int c, int d);
190
191 struct placement dummy_placement;
192
193 int max2(int a, int b)
194 {
195   return (a > b ? a : b);
196 }
197
198 int min2(int a, int b)
199 {
200   return (a < b ? a : b);
201 }
202
203 int max4(int a, int b, int c, int d)
204 {
205   return max2(max2(a, b), max2(c, d));
206 }
207
208 int min4(int a, int b, int c, int d)
209 {
210   return min2(min2(a, b), min2(c, d));
211 }
212
213 void print_label(struct symbol *sym)
214 {
215   switch (sym->type)
216   {
217     case SYMBOLIZER_TEXT: ;
218       struct sym_text *st = (struct sym_text *) sym;
219       printf("%s\n", osm_val_decode(st->text));
220     default:
221       // FIXME
222       ;
223   }
224 }
225
226 void labeller_init(void)
227 {
228   GARY_INIT(requests_point, 0);
229   GARY_INIT(requests_line, 0);
230   GARY_INIT(requests_area, 0);
231   GARY_INIT(buffer_line, 0);
232   GARY_INIT(buffer_linelabel, 0);
233   ep_individuals = ep_new(sizeof(struct individual), 1);
234
235   page_width_int = floor(page_width);
236   page_height_int = floor(page_height);
237
238   num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
239   num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
240   num_map_parts = num_map_parts_row * num_map_parts_col;
241 }
242
243 void make_bitmap(struct variant *v, struct symbol *sym)
244 {
245   switch (sym->type)
246   {
247     case SYMBOLIZER_POINT:
248       make_bitmap_point(v, (struct sym_point *) sym);
249       break;
250     case SYMBOLIZER_ICON:
251       make_bitmap_icon(v, (struct sym_icon *) sym);
252       break;
253     case SYMBOLIZER_TEXT:
254       make_bitmap_label(v, (struct sym_text *) sym);
255       break;
256     default:
257       ASSERT(sym->type != SYMBOLIZER_INVALID);
258   }
259 }
260
261 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
262 {
263   v->width = si->sir.width + 1;
264   v->height = si->sir.height + 1;
265   v->bitmap = malloc(v->width * v->height * sizeof(bool));
266   for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
267 }
268
269 void make_bitmap_point(struct variant *v, struct sym_point *sp)
270 {
271   v->width = v->height = sp->size + 1;
272   v->bitmap = malloc(v->width * v->height * sizeof(bool));
273   // FIXME: Okay, memset would be much nicer here
274   for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
275 }
276
277 void make_bitmap_label(struct variant *v, struct sym_text *text)
278 {
279   v->width = ceil(text->tw);
280   v->height = ceil(text->th);
281   v->bitmap = malloc(v->width * v->height * sizeof(bool));
282   for (int i=0; i<v->height; i++)
283     for (int j=0; j<v->width; j++)
284     {
285       v->bitmap[i*v->width + j] = 1;
286     }
287 }
288
289 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
290 {
291   if (dbg_requests)
292     printf("Adding point\n");
293   if (object->type != OSM_TYPE_NODE)
294   {
295     printf("Warning: Point label requested on non-point object\n");
296     return;
297   }
298
299   struct request_point *r = GARY_PUSH(requests_point);
300
301   r->request.type = REQUEST_POINT;
302   r->request.ind = num_requests++;
303
304   r->sym = sym;
305   r->zindex = zindex;
306
307   r->offset_x = 0;
308   r->offset_y = 0;
309
310   r->num_variants = 1;
311   GARY_INIT(r->request.variants, 0);
312
313   struct variant *v = GARY_PUSH(r->request.variants);
314
315   struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
316   r->x = n->x;
317   r->y = n->y;
318   switch (sym->type)
319   {
320     case SYMBOLIZER_ICON:
321       make_bitmap_icon(v, (struct sym_icon *) sym);
322       r->x = ((struct sym_icon *)sym)->sir.x;
323       r->y = ((struct sym_icon *)sym)->sir.y;
324       break;
325     case SYMBOLIZER_POINT:
326       make_bitmap_point(v, (struct sym_point *) sym);
327       break;
328     case SYMBOLIZER_TEXT: ;
329       struct sym_text *st = (struct sym_text *) sym;
330       struct osm_node *n = (struct osm_node *) object;
331       make_bitmap_label(v, st);
332     default:
333       // FIXME
334       return;
335   }
336
337 //  printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
338 }
339
340 void labeller_add_line(struct symbol *sym, z_index_t zindex)
341 {
342   if (dbg_requests)
343     printf("Adding line on %u\n", zindex);
344   struct buffer_line *b = GARY_PUSH(buffer_line);
345   b->line = (struct sym_line *) sym;
346   b->zindex = zindex;
347   sym_plan(sym, zindex);
348 }
349
350 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
351 {
352   if (o->type != OSM_TYPE_WAY)
353   {
354     // FIXME
355     return;
356   }
357
358   if (dbg_requests)
359     printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
360   struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
361   ll->way = (struct osm_way *) o;
362   ll->label = sym;
363   ll->zindex = zindex;
364 }
365
366 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
367 {
368   if (dbg_requests)
369     printf("Adding area on %u\n", zindex);
370   struct request_area *r = GARY_PUSH(requests_area);
371
372   r->request.type = REQUEST_AREA;
373   r->request.ind = num_requests++;
374
375   r->o = (struct osm_multipolygon *) o;
376   r->zindex = zindex;
377   r->label = sym;
378
379   osm_obj_center(o, &(r->cx), &(r->cy));
380
381   GARY_INIT(r->request.variants, 0);
382   struct variant *v = GARY_PUSH(r->request.variants);
383   switch (sym->type)
384   {
385     case SYMBOLIZER_ICON:
386       if (dbg_requests)
387         printf("DEBUG: Icon label\n");
388       make_bitmap_icon(v, (struct sym_icon *) sym);
389       break;
390     case SYMBOLIZER_TEXT:
391       if (dbg_requests)
392         printf("DEBUG: Text label\n");
393       make_bitmap_label(v, (struct sym_text *) sym);
394     default:
395       // FIXME
396       ;
397   }
398 }
399
400 void make_graph(void)
401 {
402   hash_init();
403   struct mempool *mp_edges = mp_new(BLOCK_SIZE);
404
405   printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
406   for (uns i=0; i<GARY_SIZE(buffer_line); i++)
407   {
408     struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
409     struct graph_node *g_prev = NULL;
410     struct osm_node *o_prev = NULL;
411
412     CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
413     {
414       // FIXME: Shall osm_object's type be checked here?
415       struct osm_node *o_node = (struct osm_node *) ref->o;
416
417       struct graph_node *g_node = hash_find(ref->o->id);
418       if (!g_node)
419       {
420         g_node = hash_new(ref->o->id);
421         GARY_INIT(g_node->edges, 0);
422         g_node->o = o_node;
423         g_node->id = ref->o->id;
424         g_node->num = num_nodes++;
425       }
426
427       if (! g_prev)
428       {
429         g_prev = g_node;
430         o_prev = o_node;
431         continue;
432       }
433
434       struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
435       e->num = num_edges++;
436       e->id = buffer_line[i].line->s.o->id;
437       e->color = buffer_line[i].line->color;
438       e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
439       e->visited = -1;
440       e->prev = NULL;
441       e->next = NULL;
442       e->n1 = g_prev;
443       e->n2 = g_node;
444       e->longline = (uns) -1;
445       e->line = buffer_line[i].line;
446       e->dir = DIR_UNSET;
447       e->label = NULL;
448
449       struct graph_edge **edge = GARY_PUSH(g_prev->edges);
450       *edge = e;
451       edge = GARY_PUSH(g_node->edges);
452       *edge = e;
453
454       g_prev = g_node;
455       o_prev = o_node;
456     }
457   }
458
459   printf("Made graph with %d edges\n", num_edges_dbg);
460 }
461
462 void dump_graph(void)
463 {
464   HASH_FOR_ALL(hash, node)
465   {
466     printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
467     for (uns i=0; i<GARY_SIZE(node->edges); i++)
468     {
469       struct graph_edge *e = node->edges[i];
470       printf("\t edge (%d) #%ju to ", e->num, e->id);
471       if (node->edges[i]->n1->id == node->id)
472         printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
473       else if (node->edges[i]->n2->id == node->id)
474         printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
475       else
476         printf("BEWARE! BEWARE! BEWARE!\n");
477
478       printf("\t\t");
479       if ((node->edges[i]->label)) printf("Labelled\n");
480       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));
481       printf(" colored %d;", node->edges[i]->color);
482       printf("   length %.2f", node->edges[i]->length);
483       printf("\n");
484     }
485   }
486   HASH_END_FOR;
487 }
488
489 void label_graph(void)
490 {
491   if (dbg_graph)
492     printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
493   for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
494   {
495     if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
496     if (dbg_graph)
497       printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
498     CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
499     {
500       if (dbg_graph)
501         printf("Looking for node %ju\n", ref->o->id);
502       struct graph_node *n = hash_find(ref->o->id);
503       if (n == NULL)
504       {
505         // FIXME: What shall be done?
506       }
507       else
508       {
509         if (dbg_graph)
510           printf("Searching among %u edges\n", GARY_SIZE(n->edges));
511         for (uns j=0; j<GARY_SIZE(n->edges); j++)
512         {
513           if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
514           {
515             if (dbg_graph)
516               printf("Labelling node %ju\n", n->id);
517             n->edges[j]->label = buffer_linelabel[i].label;
518             n->edges[j]->zindex = buffer_linelabel[i].zindex;
519           }
520         }
521       }
522     }
523   }
524 }
525
526 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
527 {
528   if (dbg_bfs)
529     printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
530   struct graph_edge *candidate = NULL;
531
532   for (uns i=0; i<GARY_SIZE(node->edges); i++)
533   {
534     struct graph_edge *other = node->edges[i];
535     if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
536
537     if ((uns) other->visited != e->longline) {
538     if (dbg_bfs)
539       printf("Pushing new edge %d / %ju\n", other->num, other->id);
540     struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
541     *e_ptr = other;
542     other->visited = e->longline;
543     }
544
545     if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
546         ((other->n2->id == node->id) && (other->n1->id == anode->id)))
547         continue;
548
549     if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
550         (e->label) && (other->label) &&
551         (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
552         (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
553     {
554       if (! candidate || (other->length > candidate->length))
555       candidate = other;
556     }
557   }
558
559   if (candidate)
560   {
561     if (dbg_bfs)
562       printf("New line in longline %u\n", e->longline);
563     struct graph_edge *other = candidate;
564       other->longline = e->longline;
565       other->dir = dir;
566       if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
567           ((dir == DIR_FWD) && (other->n2->id == node->id)))
568       {
569         struct graph_node *swp = other->n2;
570         other->n2 = other->n1;
571         other->n1 = swp;
572       }
573
574       switch (dir)
575       {
576         case DIR_BWD:
577           e->prev = other;
578           other->next = e;
579           longlines[other->longline].first = other;
580           break;
581         case DIR_FWD:
582           e->next = other;
583           other->prev = e;
584           break;
585         default:
586           printf("Oops\n");
587           ASSERT(0);
588       }
589   }
590 }
591
592 void bfs(uns longline)
593 {
594 if (dbg_bfs)
595   printf("BFS called for longline %u\n", longline);
596 if (dbg_bfs)
597   printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
598   for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
599   {
600     struct graph_edge *cur = bfs_queue[i];
601     if (dbg_bfs)
602       printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
603     //ASSERT(! cur->visited);
604
605     cur->visited = longline;
606
607     if (cur->longline == (uns) -1)
608       continue;
609
610     if (cur->dir == DIR_UNSET)
611     {
612       cur->dir = DIR_CENTER;
613       bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
614       bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
615     }
616     else
617     {
618       switch (cur->dir)
619       {
620         case DIR_BWD:
621           bfs_edge(cur, cur->n1, cur->n2, cur->dir);
622           break;
623         case DIR_FWD:
624           bfs_edge(cur, cur->n2, cur->n1, cur->dir);
625           break;
626         default:
627           // FIXME
628           ;
629       }
630     }
631   }
632 }
633
634 void bfs_wrapper(void)
635 {
636   GARY_INIT(bfs_queue, 0);
637   GARY_INIT(longlines, 0);
638
639   HASH_FOR_ALL(hash, node)
640   {
641     for (uns i=0; i<GARY_SIZE(node->edges); i++)
642     {
643       if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
644       {
645         GARY_PUSH(longlines);
646         longlines[num_longlines].first = node->edges[i];
647         if (dbg_bfs)
648           printf("Running new BFS\n");
649         if (dbg_bfs)
650           printf("Creating longline %u\n", num_longlines);
651         GARY_RESIZE(bfs_queue, 0);
652         struct graph_edge **e = GARY_PUSH(bfs_queue);
653         *e = node->edges[i];
654         node->edges[i]->longline = num_longlines;
655         bfs(node->edges[i]->longline);
656         //dump_longlines();
657         if (dbg_bfs)
658           printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
659         if (dbg_bfs)
660           printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
661         num_longlines++;
662       }
663     }
664   }
665   HASH_END_FOR;
666
667   GARY_FREE(bfs_queue);
668 }
669
670 void dump_longlines(void)
671 {
672 printf("*** Longlines dump\n");
673   for (uns i=0; i<GARY_SIZE(longlines); i++)
674   {
675 printf("Longline %u:", i);
676     struct graph_edge *e = longlines[i].first;
677 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
678   printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
679 printf("\n");
680
681     while (e)
682     {
683       printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
684              e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
685
686       e = e->next;
687     }
688   }
689 }
690
691 struct request_line *make_new_line(void)
692 {
693   struct request_line *rl = GARY_PUSH(requests_line);
694   rl->request.ind = num_requests++;
695   rl->request.type = REQUEST_LINE;
696   GARY_INIT(rl->sections, 0);
697
698   return rl;
699 }
700
701 struct request_section *make_new_section(struct request_line *rl)
702 {
703   struct request_section *rls = GARY_PUSH(rl->sections);
704   rls->request.ind = num_requests++;
705   rls->request.type = REQUEST_SECTION;
706   rls->num_segments = 0;
707   GARY_INIT(rls->segments, 0);
708
709   return rls;
710 }
711
712 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
713 {
714   struct request_segment *rs = GARY_PUSH(rls->segments);
715   rls->num_segments++;
716
717   rs->request.ind = num_requests++;
718   rs->request.type = REQUEST_SEGMENT;
719
720   struct variant *v = malloc(sizeof(struct variant));
721   make_bitmap(v, sym);
722   rs->request.variants = v;
723
724   return rs;
725 }
726
727 void cut_edge(struct graph_edge *e, double dist)
728 {
729   if (dbg_segments)
730     printf("Cutting [%.2f; %.2f] -- [%.2f; %.2f] to dist %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, dist);
731
732   struct graph_edge *new = malloc(sizeof(struct graph_edge));
733   *new = *e;
734   e->next = new;
735
736   // FIXME? Create new label for new edge, don't only copy pointer?
737
738   struct osm_node *n1 = e->n1->o;
739   struct osm_node *n2 = e->n2->o;
740
741   // FIXME
742   if ((n1->x == n2->x) && (n1->y == n2->y))
743   {
744     printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
745     printf("Won't cut point\n");
746     return;
747   }
748
749   struct osm_node *n11 = malloc(sizeof(struct osm_node));
750   struct graph_node *gn = malloc(sizeof(struct graph_node));
751   gn->o = n11;
752   double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
753   n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
754   n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
755
756   e->n2 = new->n1 = gn;
757
758   e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
759   new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
760   new->visited = 0;
761 }
762
763 void make_segments(void)
764 {
765   for (uns i=0; i<GARY_SIZE(longlines); i++)
766   {
767     // Skip lines which are not labelled
768     if (! (longlines[i].first && longlines[i].first->label))
769       continue;
770
771     struct request_line *request = make_new_line();
772     struct request_section *rls = make_new_section(request);
773     struct request_segment *rs = NULL;
774
775     struct graph_edge *e = longlines[i].first;
776     double cur_length = 0;
777
778     struct sym_text *st = NULL;
779     if (e->label->type == SYMBOLIZER_TEXT)
780     {
781       st = (struct sym_text *) e->label;
782     }
783     else
784     {
785       if (dbg_segments)
786         printf("Warning: Skipping line\n");
787       continue;
788       // FIXME;
789     }
790
791     if (dbg_segments)
792       printf("New longline\n");
793     while (e)
794     {
795       if (e->visited < 0)
796       {
797         printf("BEWARE: Edge cycle\n");
798         break;
799       }
800       e->visited = -1; // FIXME
801
802       if (dbg_segments)
803         printf("Taking edge from [%.2f; %.2f] to [%.2f; %.2f] of length %.2f\n", e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->length);
804
805       if (st && (e->length < st->tw))
806       {
807         e = e->next;
808         //printf("Warning: Skipping segment\n");
809         continue;
810       }
811
812       if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
813       {
814         if (dbg_segments)
815           printf("Edge too long, length is %.2f; %.2f - %.2f = %.2f\n", e->length, conf_max_section_length, cur_length, conf_max_section_length - cur_length);
816         // HACK to prevent cutting to 0 lenght
817         cut_edge(e, max2(conf_max_section_length - cur_length, 2));
818       }
819
820       rs = make_new_segment(rls, e->label);
821       rs->label = malloc(sizeof(struct sym_text));
822       *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
823
824       rs->x1 = e->n1->o->x;
825       rs->y1 = e->n1->o->y;
826       rs->x2 = e->n2->o->x;
827       rs->y2 = e->n2->o->y;
828
829       // FIXME: Set text rotation
830       rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
831       rs->zindex = e->zindex;
832
833       cur_length += e->length;
834       if (cur_length > conf_max_section_length)
835       {
836         if (dbg_segments)
837           printf("Making new section, new length would be %f, allowed is %.2f / %.2f\n", cur_length + e->length, conf_max_section_length, conf_max_section_overlay);
838
839         rls = make_new_section(request);
840         cur_length = 0;
841       }
842
843       e = e->next;
844     }
845
846     if (request->sections[0].num_segments == 0)
847     {
848       // FIXME
849       printf("WARNING: 0 segment section\n");
850       GARY_POP(requests_line);
851       num_requests -= 2;
852     }
853   }
854 }
855
856 void dump_linelabel_requests(void)
857 {
858   for (uns i=0; i<GARY_SIZE(requests_line); i++)
859   {
860     if (requests_line[i].sections[0].num_segments == 0)
861     {
862       printf("HEY!\n");
863       continue;
864     }
865     printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
866     print_label(requests_line[i].sections[0].segments[0].label);
867     for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
868     {
869       printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
870       for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
871       {
872         struct request_segment *rs = &requests_line[i].sections[j].segments[k];
873         printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
874       }
875     }
876     printf("\n");
877   }
878 }
879
880 void dump_bitmaps(struct individual *individual)
881 {
882   bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
883   printf("Bitmap size is %d\n", page_width_int * page_height_int);
884   for (int i=0; i<page_height_int; i++)
885     for (int j=0; j<page_width_int; j++)
886       bitmap[i*page_width_int + j] = 0;
887
888   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
889   {
890     if (individual->placements[i].variant_used == -1) continue;
891
892     struct placement *p = &(individual->placements[i]);
893     struct variant *v = NULL;
894
895     switch (p->request->type)
896     {
897       case REQUEST_SEGMENT: ;
898       case REQUEST_POINT: ;
899       case REQUEST_AREA: ;
900         v = &(p->request->variants[p->variant_used]);
901         break;
902       default:
903         ASSERT(p->request->type != REQUEST_INVALID);
904         continue;
905     }
906
907     for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
908     {
909       for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
910       {
911         bitmap[row * page_width_int + col] = 1;
912       }
913     }
914   }
915
916   errno = 0;
917   FILE *fd_dump = fopen("dump.pbm", "w");
918   fprintf(fd_dump, "P1\n");
919   fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
920   for (int i=0; i<page_height_int; i++)
921   {
922     for (int j=0; j<page_width_int; j++)
923     {
924       fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
925     }
926     fprintf(fd_dump, "\n");
927   }
928   fclose(fd_dump);
929 }
930
931 void dump_individual(struct individual *individual)
932 {
933 printf("*** Dumping INDIVIDUAL ***\n");
934 printf("(There are %d requests)\n", num_requests);
935   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
936   {
937     struct placement *p = &(individual->placements[i]);
938
939     switch (p->request->type)
940     {
941       case REQUEST_POINT:
942         printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
943         break;
944       case REQUEST_LINE: ;
945         struct request_line *rl = (struct request_line *) p->request;
946         printf("Line: ");
947         print_label(rl->sections[0].segments[0].label);
948         break;
949       case REQUEST_SECTION: ;
950         printf("*");
951         break;
952       case REQUEST_SEGMENT: ;
953         if (p->variant_used >= 0)
954           printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
955         else
956           printf("Segment not placed\n");
957         break;
958       case REQUEST_AREA: ;
959         struct request_area *ra = (struct request_area *) p->request;
960         printf("Area label ");
961         print_label(ra->label);
962         printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
963         break;
964       default:
965         printf("Testing request type (dump_individual)\n");
966         ASSERT(p->request->type != 0);
967     }
968   }
969   printf("\nTotal penalty: %d\n", individual->penalty);
970 }
971
972 void plan_individual(struct individual *individual)
973 {
974   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
975   {
976     struct symbol *s = NULL;
977     z_index_t zindex = 0;
978     if (individual->placements[i].variant_used < 0) continue;
979     switch (individual->placements[i].request->type)
980     {
981       case REQUEST_POINT: ;
982         struct request_point *rp = (struct request_point *) individual->placements[i].request;
983         s = rp->sym;
984         zindex = rp->zindex;
985         break;
986       case REQUEST_SEGMENT: ;
987         struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
988         s = rs->label;
989         zindex = rs->zindex;
990         break;
991       case REQUEST_LINE: ;
992         break;
993       case REQUEST_AREA: ;
994         struct request_area *ra = (struct request_area *) individual->placements[i].request;
995         s = ra->label;
996         zindex = ra->zindex;
997         break;
998       default:
999         ASSERT(individual->placements[i].request != REQUEST_INVALID);
1000         continue;
1001     }
1002
1003 if (dbg_plan)
1004   printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1005
1006     if (s) switch (s->type)
1007     {
1008       case SYMBOLIZER_POINT: ;
1009         struct sym_point *sp = (struct sym_point *) s;
1010         sp->x = individual->placements[i].x;
1011         sp->y = individual->placements[i].y;
1012         sym_plan((struct symbol *) sp, zindex);
1013         break;
1014       case SYMBOLIZER_ICON: ;
1015         struct sym_icon *si = (struct sym_icon *) s;
1016         si->sir.x = individual->placements[i].x;
1017         si->sir.y = individual->placements[i].y;
1018         sym_plan((struct symbol *) si, zindex);
1019         break;
1020       case SYMBOLIZER_TEXT: ;
1021         struct sym_text *st = (struct sym_text *) s;
1022         st->x = individual->placements[i].x;
1023         st->y = individual->placements[i].y;
1024         st->next_duplicate = NULL;
1025         if (dbg_plan) printf("Planning text %s at [%.2f; %.2f] on %u, with rotation %.2f\n", osm_val_decode(st->text), st->x, st->y, zindex, st->rotate);
1026         sym_plan((struct symbol *) st, zindex);
1027         break;
1028       default:
1029         ASSERT(s->type != SYMBOLIZER_INVALID);
1030     }
1031   }
1032
1033 }
1034
1035 void labeller_label(void)
1036 {
1037   make_graph();
1038   label_graph();
1039   bfs_wrapper();
1040   make_segments();
1041
1042 printf("Having %u point requests, %u line requests and %u area requests\n", GARY_SIZE(requests_point), GARY_SIZE(requests_line), GARY_SIZE(requests_area));
1043
1044   GARY_INIT(population1, conf_pop_size);
1045   GARY_INIT(population2, conf_pop_size);
1046   make_population();
1047
1048   printf("Dealing with %d requests\n", num_requests);
1049
1050 /*
1051   while (! shall_terminate())
1052   {
1053     iteration++;
1054
1055     struct individual **swp = population1;
1056     population1 = population2;
1057     population2 = swp;
1058     pop2_ind = 0;
1059   }
1060 */
1061
1062   plan_individual(population1[0]);
1063
1064   labeller_cleanup();
1065
1066   return;
1067 }
1068
1069 void labeller_cleanup(void)
1070 {
1071 }
1072
1073 void make_population(void)
1074 {
1075   for (int i=0; i<conf_pop_size; i++)
1076   {
1077     if (dbg_init)
1078       printf("Making individual %d\n", i);
1079     struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1080     population1[i] = individual;
1081
1082     int p = 0;
1083     for (uns j=0; j<GARY_SIZE(requests_point); j++)
1084     {
1085       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1086     }
1087
1088     for (uns j=0; j<GARY_SIZE(requests_line); j++)
1089     {
1090       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1091
1092       for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1093       {
1094         init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1095
1096         for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1097         {
1098           init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1099         }
1100       }
1101     }
1102
1103     for (uns j=0; j<GARY_SIZE(requests_area); j++)
1104     {
1105       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1106     }
1107
1108     hide_segment_labels(individual);
1109
1110     ASSERT(p == num_requests);
1111   }
1112 }
1113
1114 bool shall_terminate(void)
1115 {
1116   switch (conf_term_cond)
1117   {
1118     case TERM_COND_PENALTY:
1119       return (population1[0]->penalty < conf_penalty_bound);
1120     case TERM_COND_STAGNATION:
1121       return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1122     case TERM_COND_ITERATIONS:
1123       return (iteration >= conf_iteration_limit);
1124     default:
1125       // FIXME: Warn the user that no condition is set
1126       return 1;
1127   }
1128 }
1129
1130 void breed(void)
1131 {
1132   int acc = 0;
1133   int i=0;
1134   printf("%.2f\n", ((double) conf_breed_pop_size_perc/100));
1135   int conf_breed_pop_size = ((double) conf_breed_pop_size_perc/100) * conf_pop_size;
1136   struct individual **breed_buffer;
1137   while (i < conf_breed_pop_size)
1138   {
1139   printf("%d < %d, breeding\n", i, conf_breed_pop_size);
1140     int parent1 = randint(1, conf_breed_pop_size);
1141     int parent2 = randint(1, conf_breed_pop_size);
1142     printf("Will breed %d and %d, chosen of %d best of %d population (intended to be %d)\n", parent1, parent2, conf_breed_pop_size, GARY_SIZE(population1), conf_pop_size);
1143     breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1144     population2[pop2_ind++] = breed_buffer[0];
1145     population2[pop2_ind++] = breed_buffer[1];
1146     free(breed_buffer);
1147     i++;
1148   }
1149
1150   acc += conf_breed_rbest_perc;
1151
1152   return; // FIXME: DEBUG HACK
1153
1154   int remaining = (1 - acc) * (conf_pop_size * conf_breed_perc);
1155   int step = remaining / conf_pop_size;
1156   for (; i<conf_pop_size; i += 2)
1157   {
1158     printf("Asking for %d and %d of %d\n", i*step, i*(step+1), conf_pop_size);
1159     breed_buffer = perform_crossover(population1[i*step], population1[i*step+1]);
1160     population2[pop2_ind++] = breed_buffer[0];
1161     population2[pop2_ind++] = breed_buffer[1];
1162   }
1163
1164   // FIXME: Could there be one missing individual?
1165 }
1166
1167 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1168 {
1169   struct individual **buffer = malloc(2*sizeof(struct individual));
1170   struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1171   struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1172
1173   printf("Performing crossover\n");
1174
1175   for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1176   {
1177     printf("%dth placement out of %d\n", i, num_requests);
1178     if (! parent1->placements[i].processed)
1179     {
1180       struct placement **clos_symbols = get_closure(&(parent1->placements[i]), parent1, parent2);
1181       int x = randint(1, 2);
1182
1183       if (x == 1)
1184       {
1185         copy_symbols(clos_symbols, parent1, child1);
1186         copy_symbols(clos_symbols, parent2, child2);
1187       }
1188       else
1189       {
1190         copy_symbols(clos_symbols, parent2, child1);
1191         copy_symbols(clos_symbols, parent1, child2);
1192       }
1193       printf("Symbols copied; %lld\n", GARY_SIZE(clos_symbols));
1194       GARY_FREE(clos_symbols);
1195     }
1196
1197     if (conf_mutate_children)
1198     {
1199       if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1200       if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1201     }
1202   }
1203
1204   buffer[0] = child1;
1205   buffer[1] = child2;
1206   return buffer;
1207 }
1208
1209 void mutate(void)
1210 {
1211   int i = 0;
1212   int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
1213   while (i < conf_mutate_rbest_perc * conf_pop_size)
1214   {
1215     int ind = randint(1, conf_mutate_pop_size);
1216     copy_individual(population2[pop2_ind], population1[ind]);
1217     perform_mutation(population2[pop2_ind]);
1218     pop2_ind++;
1219   }
1220 }
1221
1222 void perform_mutation(struct individual *individual)
1223 {
1224   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1225   {
1226     int x = randint(1, 1000);
1227     int acc = 0;
1228
1229     if (x <= acc + conf_mutate_move_bound)
1230     {
1231       move_symbol(&(individual->placements[i]));
1232       continue;
1233     }
1234     acc += conf_mutate_move_bound;
1235
1236     if (x <= acc + conf_mutate_regen_bound)
1237     {
1238       gen_coords(&(individual->placements[i]));
1239       continue;
1240     }
1241     acc += conf_mutate_regen_bound;
1242
1243     if (x <= acc + conf_mutate_chvar_bound)
1244     {
1245       if (0) // if num_variants > 1
1246       {
1247         // FIXME: assign new variant
1248       }
1249     }
1250   }
1251 }
1252
1253 void elite(void)
1254 {
1255   for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
1256   {
1257     population2[pop2_ind++] = population1[0];
1258   }
1259 }
1260
1261 int overlaps(struct placement *p1, struct placement *p2)
1262 {
1263   if (p1->request->type != REQUEST_POINT &&
1264       p1->request->type != REQUEST_SEGMENT &&
1265       p1->request->type != REQUEST_AREA)
1266     return 0;
1267
1268   if (p2->request->type != REQUEST_POINT &&
1269       p2->request->type != REQUEST_SEGMENT &&
1270       p2->request->type != REQUEST_AREA)
1271     return 0;
1272
1273   if (p1->variant_used == -1 || p2->variant_used == -1)
1274     return 0;
1275
1276   struct variant *v1, *v2;
1277
1278   v1 = &(p1->request->variants[p1->variant_used]);
1279   v2 = &(p2->request->variants[p2->variant_used]);
1280
1281   int p1x = p1->x; int p1y = p1->y;
1282   int p2x = p2->x; int p2y = p2->y;
1283
1284   int overlap = 0;
1285   for (int y=max2(p1y, p2y); y<=min2(p1y+v1->height, p2y+v2->height); y++)
1286     for (int x=max2(p1x, p2x); x<=min2(p1x+v1->width, p2x+v2->width); x++)
1287     {
1288       if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1289           v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1290         overlap++;
1291     }
1292
1293   return overlap;
1294 }
1295
1296 int get_overlap(struct placement *p)
1297 {
1298   struct map_part **parts = get_map_parts(p);
1299   if (! parts)
1300   {
1301     if (dbg_overlaps)
1302       printf("Placement of request %d seems not to be placed\n", p->request->ind);
1303     return 0;
1304   }
1305
1306   struct placement **others;
1307   bool *planned;
1308
1309   GARY_INIT_ZERO(planned, num_requests);
1310   planned[p->request->ind] = 1;
1311   GARY_INIT(others, 0);
1312
1313   for (uns i=0; i<GARY_SIZE(parts); i++)
1314   {
1315     struct map_placement *mp = parts[i]->placement->next;
1316     while (mp)
1317     {
1318       if (! planned[mp->placement->request->ind])
1319       {
1320         struct placement **p = GARY_PUSH(others);
1321         *p = mp->placement;
1322         planned[mp->placement->request->ind] = true;
1323       }
1324       mp = mp->next;
1325     }
1326   }
1327
1328   int overlap = 0;
1329   for (uns i=0; i<GARY_SIZE(others); i++)
1330   {
1331     overlap += overlaps(p, others[i]);
1332   }
1333
1334   GARY_FREE(planned);
1335   GARY_FREE(parts);
1336   GARY_FREE(others);
1337
1338   return overlap;
1339 }
1340
1341 int individual_overlap(struct individual *individual)
1342 {
1343   int overlap = 0;
1344
1345   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1346   {
1347     overlap += get_overlap(&individual->placements[i]);
1348   }
1349
1350   return overlap;
1351 }
1352
1353 int cmp_individual(const void *a, const void *b)
1354 {
1355   struct individual **ia = (struct individual **) a;
1356   struct individual **ib = (struct individual **) b;
1357
1358   return (*ia)->penalty - (*ib)->penalty;
1359 }
1360
1361 void rank_population(void)
1362 {
1363   int penalty;
1364
1365   for (int i=0; i<conf_pop_size; i++)
1366   {
1367     if (dbg_rank)
1368       printf("Individual %d\n", i);
1369     population1[i]->penalty = 0;
1370
1371     penalty = individual_overlap(population1[i]);
1372     if (dbg_rank)
1373       printf("Increasing penalty by %d for overlap\n", penalty);
1374     population1[i]->penalty += penalty;
1375   }
1376 }
1377
1378 struct map_part **get_map_parts(struct placement *p)
1379 {
1380   if (p->variant_used < 0) return NULL;
1381
1382   struct map_part **buffer;
1383   GARY_INIT(buffer, 0);
1384
1385   if (dbg_map_parts)
1386     printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1387
1388   struct variant v;
1389   switch (p->request->type)
1390   {
1391     case REQUEST_POINT:
1392     case REQUEST_SEGMENT:
1393     case REQUEST_AREA:
1394       v = p->request->variants[p->variant_used];
1395       break;
1396     default:
1397       return NULL;
1398   }
1399
1400   int x_min = max2(0, p->x) / conf_map_part_width;
1401   int x_max = min2(page_width_int, (p->x + v.width + conf_map_part_width - 1)) / conf_map_part_width;
1402   int y_min = max2(0, p->y) / conf_map_part_height;
1403   int y_max = min2(page_height_int, (p->y + v.height + conf_map_part_height - 1)) / conf_map_part_height;
1404
1405   if (dbg_map_parts)
1406     printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1407
1408   for (int y=y_min; y<=y_max; y++)
1409     for (int x=x_min; x<=x_max; x++)
1410     {
1411       struct map_part **m = GARY_PUSH(buffer);
1412       if (dbg_map_parts)
1413         printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1414       *m = p->individual->map[y * num_map_parts_row + x];
1415     }
1416
1417   return buffer;
1418 }
1419
1420 void update_map_parts(struct placement *p)
1421 {
1422   struct placement_link *ml = p->map_links;
1423   while (ml)
1424   {
1425     struct map_placement *mp = ml->mp;
1426     mp->prev = mp->next;
1427     if (mp->next)
1428       mp->next->prev = mp->prev;
1429     free(mp);
1430
1431     struct placement_link *tmp = ml;
1432     ml = ml->next;
1433     free(tmp);
1434   }
1435
1436   struct map_part **parts = get_map_parts(p);
1437   if (parts == NULL) return;
1438
1439   for (uns i=0; i<GARY_SIZE(parts); i++)
1440   {
1441     struct map_placement *mp = malloc(sizeof(struct map_placement));
1442     mp->placement = p;
1443
1444     mp->next = parts[i]->placement->next;
1445     parts[i]->placement->next = mp;
1446     if (mp->next) mp->next->prev = mp;
1447
1448     struct placement_link *ml = malloc(sizeof(struct placement_link));
1449     ml->mp = mp;
1450     ml->next = p->map_links;
1451     p->map_links = ml;
1452   }
1453
1454   GARY_FREE(parts);
1455 }
1456
1457 void gen_coords(struct placement *p)
1458 {
1459   switch(p->request->type)
1460   {
1461     case REQUEST_POINT:
1462       gen_coords_point(p);
1463       break;
1464     case REQUEST_AREA:
1465       gen_coords_area(p);
1466       break;
1467     case REQUEST_SEGMENT:
1468       gen_coords_segment(p);
1469       break;
1470     case REQUEST_LINE:
1471       if (dbg_movement)
1472         printf("Not yet implemented\n");
1473       break;
1474     default:
1475       if (dbg_movement)
1476         printf("Testing request type\n");
1477       ASSERT(p->request->type != REQUEST_INVALID);
1478   }
1479
1480   update_map_parts(p);
1481 }
1482
1483 double gen_movement(void)
1484 {
1485   double m = (random() % 1000000) / 10000;
1486   m = pow(m, 1.0/3) * flip(1, -1);
1487   if (dbg_movement)
1488     printf("Movement %.2f\n", m);
1489   return m;
1490 }
1491
1492 void gen_coords_point(struct placement *p)
1493 {
1494   p->x = p->x + gen_movement();
1495 }
1496
1497 void gen_coords_segment(struct placement *p)
1498 {
1499   struct request_segment *rs = (struct request_segment *) p->request;
1500   int a = flip(1, 2);
1501   p->x = (a == 1 ? rs->x1 : rs->x2);
1502   p->y = (a == 1 ? rs->y1 : rs->y2);
1503 }
1504
1505 void gen_coords_area(struct placement *p)
1506 {
1507   struct request_area *ra = (struct request_area *) p->request;
1508
1509   p->x = p->x + gen_movement();
1510   p->y = p->y + gen_movement();
1511
1512   if (dbg_movement)
1513     printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1514 }
1515
1516 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1517 {
1518   struct map_part **buffer;
1519   GARY_INIT(buffer, 0);
1520   int x_min = symbol->x / conf_part_size;
1521   int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1522   int y_min = symbol->y / conf_part_size;
1523   int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1524
1525   for (int x=x_min; x < x_max; x++)
1526     for (int y=y_min; y < y_max; y++)
1527     {
1528       struct map_part *m = GARY_PUSH(buffer);
1529       *m = individual->map[x][y];
1530     }
1531
1532   return buffer;
1533 }
1534
1535 int randint(int min, int max)
1536 {
1537   if (min == max) return min;
1538   int r = random();
1539   //printf("Returning %d + (%d %% (%d - %d)) = %d + %d %% %d = %d + %d = %d\n", min, r, max, min, min, r, max-min, min, r%(max-min), min+(r%(max-min)));
1540   return min + (r % (max - min));
1541   return (r * (max - min));
1542 }
1543
1544 struct placement **get_closure(struct placement *placement, struct individual *parent1, struct individual *parent2 UNUSED)
1545 {
1546   printf("Getting closure\n");
1547   struct placement **closure;
1548   GARY_INIT(closure, 0);
1549   bool *chosen = malloc(GARY_SIZE(parent1->placements) * sizeof(bool));
1550   chosen[placement->request->ind] = 1;
1551
1552   struct placement **p = GARY_PUSH(closure); *p = placement;
1553
1554   uns first = 0;
1555   while (first < GARY_SIZE(closure))
1556   {
1557     printf("Iterating, first is %d\n", first);
1558     struct placement **overlapping = get_overlapping(placement);
1559     filter(overlapping, chosen);
1560     for (uns j=0; j<GARY_SIZE(overlapping); j++)
1561     {
1562       p = GARY_PUSH(closure); *p = overlapping[j];
1563       chosen[overlapping[j]->request->ind] = 1;
1564     }
1565     GARY_FREE(overlapping);
1566     first++;
1567   }
1568
1569   return closure;
1570 }
1571
1572 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child)
1573 {
1574   //printf("%d\n", child->penalty);
1575   //printf("Closure size: %lld\n", GARY_SIZE(closure));
1576   for (uns i=0; i<GARY_SIZE(closure); i++)
1577   {
1578     int ind = closure[i]->request->ind;
1579     child->placements[ind] = parent->placements[ind];
1580     child->placements[ind].processed = 0;
1581   }
1582 }
1583
1584 void move_symbol(struct placement *p)
1585 {
1586   switch (p->request->type)
1587   {
1588     case REQUEST_POINT:
1589       move_symbol_point(p);
1590     case REQUEST_LINE:
1591     case REQUEST_SEGMENT:
1592     case REQUEST_AREA:
1593       if (dbg_movement)
1594         printf("Not yet implemented\n");
1595     default:
1596       ASSERT(p->request->type != REQUEST_INVALID);
1597   }
1598 }
1599
1600 void move_symbol_point(struct placement *p)
1601 {
1602   p->x += (double) (move_min + randdouble()) * flip(1, -1);
1603   p->y += (double) (move_min + randdouble()) * flip(1, -1);
1604 }
1605
1606 void hide_segment_labels(struct individual *individual)
1607 {
1608   // BEWARE: This fully depends on current genetic encoding
1609
1610   int used = -1, num = -1;
1611   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1612   {
1613     switch (individual->placements[i].request->type)
1614     {
1615       case REQUEST_SECTION:
1616         used = individual->placements[i].variant_used;
1617         num = 0;
1618         break;
1619       case REQUEST_SEGMENT:
1620         if (num == used)
1621           individual->placements[i].variant_used = 0;
1622         else
1623           individual->placements[i].variant_used = -1;
1624         num++;
1625         break;
1626       default:
1627         ;
1628     }
1629   }
1630 }
1631
1632 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1633 {
1634   // FIXME
1635   p->request = r;
1636   p->processed = 0;
1637   p->x = p->y = 0; // To prevent valgrind from complaining
1638   p->variant_used = 0;
1639   p->map_links = NULL;
1640   p->individual = individual;
1641   switch (r->type)
1642   {
1643     case REQUEST_POINT: ;
1644       struct request_point *rp = (struct request_point *) r;
1645       p->x = rp->x;
1646       p->y = rp->y;
1647       break;
1648     case REQUEST_LINE: ;
1649       break;
1650     case REQUEST_SECTION: ;
1651       struct request_section *rls = (struct request_section *) r;
1652       p->variant_used = randint(0, rls->num_segments);
1653       break;
1654     case REQUEST_SEGMENT: ;
1655       struct request_segment *rs = (struct request_segment *) r;
1656       p->x = rs->x2;
1657       p->y = rs->y2;
1658       break;
1659     case REQUEST_AREA: ;
1660       struct request_area *ra = (struct request_area *) r;
1661       p->x = ra->cx;
1662       p->y = ra->cy;
1663       p->variant_used = 0;
1664       break;
1665     default:
1666       ASSERT(p->request->type != REQUEST_INVALID);
1667       printf("Valid request: %d\n", p->request->type);
1668   }
1669
1670   gen_coords(p);
1671   if (dbg_init)
1672     printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1673 }
1674
1675 void init_individual(struct individual *i)
1676 {
1677 //printf("Initing individual\n");
1678   GARY_INIT(i->placements, num_requests);
1679   GARY_INIT(i->map, 0);
1680   for (uns j=0; j<num_map_parts; j++)
1681   {
1682     struct map_part *part = GARY_PUSH(i->map);
1683     GARY_INIT(part->placement, 0);
1684     struct map_placement *mp = GARY_PUSH(part->placement);
1685     mp->placement = &dummy_placement;
1686     mp->next = mp->prev = NULL;
1687   }
1688   i->penalty = 0; // FIXME
1689
1690   if (dbg_init)
1691     printf("Individual inited, has %u map parts\n", GARY_SIZE(i->map));
1692 }
1693
1694 struct placement **get_overlapping(struct placement *p UNUSED)
1695 {
1696   struct placement **buffer;
1697   GARY_INIT(buffer, 0);
1698   return buffer;
1699 }
1700
1701 void filter(struct placement **list UNUSED, bool *pred UNUSED)
1702 {
1703   // FIXME
1704 }
1705
1706 int flip(int a, int b)
1707 {
1708   return (random() % 2 ? a : b);
1709 }
1710
1711 double randdouble(void)
1712 {
1713   // FIXME: How the hell shall double in range <0, 1> be generated? O:)
1714   return 0.5;
1715 }
1716
1717 void cleanup(void)
1718 {
1719   hash_cleanup();
1720   GARY_FREE(requests_point);
1721   GARY_FREE(requests_line);
1722   GARY_FREE(requests_area);
1723 }
1724
1725 void copy_individual(struct individual *src, struct individual *dest)
1726 {
1727   src->penalty = dest->penalty;
1728   GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1729   for (uns i=0; i<GARY_SIZE(src->placements); i++)
1730   {
1731     dest->placements[i] = src->placements[i];
1732   }
1733 }