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