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