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