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