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