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