]> mj.ucw.cz Git - leo.git/blob - labeller.c
7b2454315126cfdf6e970b970f45a68ebf58fed6
[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     for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
927     {
928       for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
929       {
930         if (bitmap[row * page_width_int + col] == 1) total += 1;
931         bitmap[row * page_width_int + col] = 1;
932       }
933     }
934   }
935   if (dbg_overlaps >= VERBOSITY_GENERAL)
936     printf("There were %d collisions during bitmap dump\n", total);
937
938   FILE *fd_dump = fopen("dump.pbm", "w");
939   fprintf(fd_dump, "P1\n");
940   fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
941   for (int i=0; i<page_height_int; i++)
942   {
943     for (int j=0; j<page_width_int; j++)
944     {
945       fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
946     }
947     fprintf(fd_dump, "\n");
948   }
949   fclose(fd_dump);
950
951   free(bitmap);
952 }
953
954 void dump_individual(struct individual *individual)
955 {
956   printf("*** Individual dump\n");
957   printf("(There are %d requests)\n", num_requests);
958
959   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
960   {
961     struct placement *p = &(individual->placements[i]);
962
963     switch (p->request->type)
964     {
965       case REQUEST_POINT:
966         printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
967         break;
968       case REQUEST_LINE: ;
969         struct request_line *rl = (struct request_line *) p->request;
970         printf("Line: ");
971         dump_label(rl->sections[0].segments[0].label);
972         break;
973       case REQUEST_SECTION: ;
974         printf("*");
975         break;
976       case REQUEST_SEGMENT: ;
977         if (p->variant_used >= 0)
978           printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
979         else
980           printf("Segment not placed\n");
981         break;
982       case REQUEST_AREA: ;
983         struct request_area *ra = (struct request_area *) p->request;
984         printf("Area label ");
985         dump_label(ra->label);
986         printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
987         break;
988       default:
989         ASSERT(p->request->type != 0);
990     }
991   }
992   printf("\nTotal penalty: %d\n", individual->penalty);
993 }
994
995 void plan_individual(struct individual *individual)
996 {
997   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
998   {
999     struct symbol *s = NULL;
1000     z_index_t zindex = 0;
1001     if (individual->placements[i].variant_used < 0) continue;
1002     switch (individual->placements[i].request->type)
1003     {
1004       case REQUEST_POINT: ;
1005         struct request_point *rp = (struct request_point *) individual->placements[i].request;
1006         s = rp->sym;
1007         zindex = rp->zindex;
1008         break;
1009       case REQUEST_SEGMENT: ;
1010         struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
1011         s = rs->label;
1012         zindex = rs->zindex;
1013         break;
1014       case REQUEST_LINE: ;
1015         break;
1016       case REQUEST_AREA: ;
1017         struct request_area *ra = (struct request_area *) individual->placements[i].request;
1018         s = ra->label;
1019         zindex = ra->zindex;
1020         break;
1021       default:
1022         ASSERT(individual->placements[i].request != REQUEST_INVALID);
1023         continue;
1024     }
1025
1026   if (dbg_plan >= VERBOSITY_PLACEMENT)
1027     printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1028
1029     if (s) switch (s->type)
1030     {
1031       case SYMBOLIZER_POINT: ;
1032         struct sym_point *sp = (struct sym_point *) s;
1033         sp->x = individual->placements[i].x;
1034         sp->y = individual->placements[i].y;
1035         sym_plan((struct symbol *) sp, zindex);
1036         break;
1037       case SYMBOLIZER_ICON: ;
1038         struct sym_icon *si = (struct sym_icon *) s;
1039         si->sir.x = individual->placements[i].x;
1040         si->sir.y = individual->placements[i].y;
1041         sym_plan((struct symbol *) si, zindex);
1042         break;
1043       case SYMBOLIZER_TEXT: ;
1044         struct sym_text *st = (struct sym_text *) s;
1045         st->x = individual->placements[i].x;
1046         st->y = individual->placements[i].y;
1047         st->next_duplicate = NULL;
1048         if (dbg_plan >= VERBOSITY_PLACEMENT)
1049           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);
1050         sym_plan((struct symbol *) st, zindex);
1051         break;
1052       default:
1053         ASSERT(s->type != SYMBOLIZER_INVALID);
1054     }
1055   }
1056 }
1057
1058 void dump_penalties(struct individual **population)
1059 {
1060   for (int i=0; i<conf_pop_size; i++)
1061   {
1062     printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1063   }
1064 }
1065
1066 void compute_sizes(void)
1067 {
1068   page_width_int = floor(page_width);
1069   page_height_int = floor(page_height);
1070
1071   num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
1072   num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
1073   num_map_parts = num_map_parts_row * num_map_parts_col;
1074
1075   breed_pop_size = conf_breed_pop_size * conf_pop_size;
1076   breed_rbest_size = conf_breed_rbest * conf_pop_size;
1077   if (dbg_evolution >= VERBOSITY_GENERAL)
1078   {
1079     printf("Breeding parameters:\n");
1080     printf(" %d individuals are created\n", breed_pop_size);
1081     printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1082   }
1083
1084   mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1085   mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1086   if (dbg_evolution >= VERBOSITY_GENERAL)
1087   {
1088     printf("Mutation parameters:\n");
1089     printf(" %d individuals are created\n", mutate_pop_size);
1090     printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1091   }
1092
1093   elite_pop_size = conf_elite_pop_size * conf_pop_size;
1094   if (dbg_evolution >= VERBOSITY_GENERAL)
1095   {
1096     printf("Elitism parameters:\n");
1097     printf(" %d best individuals are copied\n", elite_pop_size);
1098   }
1099
1100   if (breed_pop_size + mutate_pop_size + elite_pop_size != conf_pop_size)
1101   {
1102     if (conf_fit_size)
1103     {
1104       elite_pop_size += conf_pop_size - (breed_pop_size + mutate_pop_size + elite_pop_size);
1105     }
1106     else
1107     {
1108       fprintf(stderr, "Breeding + mutation + elitism won't create correct number of individuals\n");
1109       fprintf(stderr, "Please fix conf_breed_pop_size, conf_mutate_pop_size and conf_elite_pop_size parameters\n");
1110       exit(2);
1111     }
1112   }
1113 }
1114
1115 void labeller_label(void)
1116 {
1117   make_graph();
1118   label_graph();
1119   bfs_wrapper();
1120   make_segments();
1121
1122   GARY_INIT(population1, conf_pop_size);
1123   GARY_INIT(population2, conf_pop_size);
1124   make_population();
1125   rank_population();
1126   qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1127
1128   if (dbg_evolution >= VERBOSITY_GENERAL)
1129   {
1130     printf("Penalties after initialization\n");
1131     dump_penalties(population1);
1132   }
1133
1134   while (! shall_terminate())
1135   {
1136     iteration++;
1137     if (dbg_evolution)
1138       printf("\n*** Iteration %d ***\n", iteration);
1139
1140     breed();
1141     mutate();
1142     elite();
1143
1144     struct individual **swp = population1;
1145     population1 = population2;
1146     population2 = swp;
1147     pop2_ind = 0;
1148     clear_population(population2);
1149
1150     rank_population();
1151
1152     if (dbg_evolution >= VERBOSITY_GENERAL)
1153       printf("Sorting population\n");
1154     qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1155
1156     if (dbg_evolution >= VERBOSITY_GENERAL)
1157     {
1158       printf("Penalties after sort\n");
1159       dump_penalties(population1);
1160     }
1161
1162     old_best = population1[0]->penalty;
1163   }
1164
1165   if (dbg_overlaps >= VERBOSITY_GENERAL)
1166     dump_bitmaps(population1[0]);
1167
1168   plan_individual(population1[0]);
1169
1170   labeller_cleanup();
1171
1172   return;
1173 }
1174
1175 void labeller_cleanup(void)
1176 {
1177   hash_cleanup();
1178   GARY_FREE(requests_point);
1179   GARY_FREE(requests_line);
1180   GARY_FREE(requests_area);
1181 }
1182
1183 void make_population(void)
1184 {
1185   for (int i=0; i<conf_pop_size; i++)
1186   {
1187     num_placements = 0; // FIXME: This IS a terrible HACK
1188     struct individual *i2 = ep_alloc(ep_individuals);
1189     init_individual(i2);
1190     population2[i] = i2;
1191
1192     if (dbg_init >= VERBOSITY_INDIVIDUAL)
1193       printf("Making individual %d\n", i);
1194     struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1195     population1[i] = individual;
1196
1197     int p = 0;
1198     for (uns j=0; j<GARY_SIZE(requests_point); j++)
1199     {
1200       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1201     }
1202
1203     for (uns j=0; j<GARY_SIZE(requests_line); j++)
1204     {
1205       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1206
1207       for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1208       {
1209         init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1210
1211         for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1212         {
1213           init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1214         }
1215       }
1216     }
1217
1218     for (uns j=0; j<GARY_SIZE(requests_area); j++)
1219     {
1220       init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1221     }
1222
1223     hide_segment_labels(individual);
1224
1225     ASSERT(p == num_requests);
1226   }
1227 }
1228
1229 bool shall_terminate(void)
1230 {
1231   switch (conf_term_cond)
1232   {
1233     case TERM_COND_PENALTY:
1234       return (population1[0]->penalty < conf_penalty_bound);
1235     case TERM_COND_STAGNATION:
1236       return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1237     case TERM_COND_ITERATIONS:
1238       return (iteration >= conf_iteration_limit);
1239     default:
1240       fprintf(stderr, "Warning: No termination condition is set, terminating\n");
1241       return 1;
1242   }
1243 }
1244
1245 void breed(void)
1246 {
1247   int i=0;
1248
1249   struct individual **breed_buffer;
1250   while (i < breed_pop_size)
1251   {
1252     int parent1 = randint(0, breed_rbest_size);
1253     int parent2 = randint(0, breed_rbest_size);
1254     if (dbg_breeding >= VERBOSITY_INDIVIDUAL)
1255       printf("Will breed %d and %d\n", parent1, parent2);
1256
1257     breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1258     population2[pop2_ind++] = breed_buffer[0];
1259     population2[pop2_ind++] = breed_buffer[1];
1260     free(breed_buffer);
1261     i += 2;
1262   }
1263
1264   return;
1265 }
1266
1267 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1268 {
1269   struct individual **buffer = malloc(2*sizeof(struct individual));
1270   struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1271   struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1272
1273   bool *processed;
1274   GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1275
1276   for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1277   {
1278     if (! processed[parent1->placements[i].ind])
1279     {
1280       if (dbg_breeding >= VERBOSITY_PLACEMENT)
1281         printf("Creating symbol closure for placement %u\n", i);
1282
1283       struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1284       int x = randint(0, 2);
1285
1286       if (x == 0)
1287       {
1288         if (dbg_breeding >= VERBOSITY_PLACEMENT)
1289           printf("Copying parent->child 1->1 and 2->2\n");
1290         copy_symbols(clos_symbols, parent1, child1, &processed);
1291         copy_symbols(clos_symbols, parent2, child2, &processed);
1292       }
1293       else
1294       {
1295         if (dbg_breeding >= VERBOSITY_PLACEMENT)
1296           printf("Copying parent->child 2->1 and 1->2\n");
1297         copy_symbols(clos_symbols, parent2, child1, &processed);
1298         copy_symbols(clos_symbols, parent1, child2, &processed);
1299       }
1300
1301       GARY_FREE(clos_symbols);
1302     }
1303   }
1304
1305   GARY_FREE(processed);
1306
1307   if (conf_mutate_children)
1308   {
1309     if (randdouble() < conf_mutate_children_prob) perform_mutation(child1);
1310     if (randdouble() < conf_mutate_children_prob) perform_mutation(child2);
1311   }
1312
1313   buffer[0] = child1;
1314   buffer[1] = child2;
1315   return buffer;
1316 }
1317
1318 void mutate(void)
1319 {
1320   for (int i=0; i < mutate_pop_size; i++)
1321   {
1322     if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1323       printf("Creating %d-th individual by mutation\n", i);
1324     int ind = randint(0, mutate_rbest_size);
1325     if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1326       printf("Mutating %d-th individual of original population\n", ind);
1327     population2[pop2_ind] = ep_alloc(ep_individuals);
1328     copy_individual(population1[ind], population2[pop2_ind]);
1329     if (dbg_mutation >= VERBOSITY_INDIVIDUAL)
1330       printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1331     perform_mutation(population2[pop2_ind]);
1332     pop2_ind++;
1333   }
1334 }
1335
1336 void perform_mutation(struct individual *individual)
1337 {
1338   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1339   {
1340     double x = randdouble();
1341     double acc = 0;
1342
1343     if (x <= acc + conf_mutate_move_bound)
1344     {
1345       if (dbg_mutation >= VERBOSITY_PLACEMENT)
1346         printf("Mutation: Moving symbol in placement %u\n", i);
1347       move_symbol(&(individual->placements[i]));
1348       continue;
1349     }
1350     acc += conf_mutate_move_bound;
1351
1352     if (x <= acc + conf_mutate_regen_bound)
1353     {
1354       gen_coords(&(individual->placements[i]));
1355       continue;
1356     }
1357     acc += conf_mutate_regen_bound;
1358
1359     if (x <= acc + conf_mutate_chvar_bound)
1360     {
1361       struct placement *p = &(individual->placements[i]);
1362       switch (p->request->type)
1363       {
1364         case REQUEST_POINT:
1365         case REQUEST_SEGMENT:
1366         case REQUEST_AREA:
1367           // Does nothing when there are 0 variants... does it mind?
1368           p->variant_used = randint(-1, GARY_SIZE(p->request->variants));
1369           break;
1370         case REQUEST_SECTION:
1371           p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments));
1372           break;
1373         default:
1374           ;
1375       }
1376     }
1377   }
1378
1379   hide_segment_labels(individual);
1380 }
1381
1382 void elite(void)
1383 {
1384   for (int i=0; i<elite_pop_size; i++)
1385   {
1386     population2[pop2_ind] = ep_alloc(ep_individuals);
1387     copy_individual(population1[i], population2[pop2_ind++]);
1388   }
1389 }
1390
1391 int overlaps(struct placement *p1, struct placement *p2)
1392 {
1393   if (p1->request->type != REQUEST_POINT &&
1394       p1->request->type != REQUEST_SEGMENT &&
1395       p1->request->type != REQUEST_AREA)
1396     return 0;
1397
1398   if (p2->request->type != REQUEST_POINT &&
1399       p2->request->type != REQUEST_SEGMENT &&
1400       p2->request->type != REQUEST_AREA)
1401     return 0;
1402
1403   if (p1->variant_used == -1 || p2->variant_used == -1)
1404     return 0;
1405
1406   struct variant *v1, *v2;
1407
1408   v1 = &(p1->request->variants[p1->variant_used]);
1409   v2 = &(p2->request->variants[p2->variant_used]);
1410
1411   // FIXME: This doesn't fully respect offset which it probably should
1412   int p1x = p1->x; int p1y = p1->y;
1413   int p2x = p2->x; int p2y = p2->y;
1414
1415   int overlap = 0;
1416   for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1417     for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1418     {
1419       if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1420           v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1421         overlap++;
1422     }
1423
1424   return overlap;
1425 }
1426
1427 int get_overlap(struct placement *p)
1428 {
1429   if (p->variant_used == -1) return 0;
1430
1431   struct map_part **parts = get_map_parts(p);
1432   if (! parts)
1433   {
1434     if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1435       printf("Placement of request %d seems not to be placed\n", p->request->ind);
1436     return 0;
1437   }
1438
1439   struct placement **others;
1440   bool *planned;
1441
1442   GARY_INIT_ZERO(planned, num_requests);
1443   planned[p->request->ind] = 1;
1444   GARY_INIT(others, 0);
1445
1446   for (uns i=0; i<GARY_SIZE(parts); i++)
1447   {
1448     struct map_placement *mp = parts[i]->placement->next_in_map;
1449     while (mp)
1450     {
1451       if (! planned[mp->placement->request->ind])
1452       {
1453         struct placement **p = GARY_PUSH(others);
1454         *p = mp->placement;
1455         planned[mp->placement->request->ind] = true;
1456       }
1457       mp = mp->next_in_map;
1458     }
1459   }
1460
1461   int overlap = 0;
1462   for (uns i=0; i<GARY_SIZE(others); i++)
1463   {
1464     overlap += overlaps(p, others[i]);
1465   }
1466
1467   GARY_FREE(planned);
1468   GARY_FREE(parts);
1469   GARY_FREE(others);
1470
1471   if (dbg_overlaps >= VERBOSITY_PLACEMENT)
1472     printf("Placement of request %d add %d to overlaps\n", p->request->ind, overlap);
1473
1474   if (p->x < 0) overlap += 0 - p->x;
1475   if (p->x + p->request->variants[p->variant_used].width > page_width_int)
1476     overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
1477
1478   if (p->y < 0) overlap += 0 - p->y;
1479   if (p->y + p->request->variants[p->variant_used].height > page_height_int)
1480     overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
1481
1482   return overlap;
1483 }
1484
1485 int individual_overlap(struct individual *individual)
1486 {
1487   int overlap = 0;
1488
1489   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1490   {
1491     overlap += get_overlap(&individual->placements[i]);
1492   }
1493
1494   return overlap;
1495 }
1496
1497 double get_distance(struct placement *p)
1498 {
1499   if (p->variant_used < 0) return 0;
1500   struct variant *v = &p->request->variants[p->variant_used];
1501
1502   double dx, dy, distance;
1503   switch (p->request->type)
1504   {
1505     case REQUEST_POINT: ;
1506       struct request_point *rp = (struct request_point *) p->request;
1507       dx = rp->x + v->offset_x - p->x;
1508       dy = rp->y + v->offset_y - p->y;
1509       distance = sqrt(dx*dx + dy*dy);
1510       if (dbg_rank >= VERBOSITY_PLACEMENT)
1511         printf("Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1512       break;
1513     case REQUEST_AREA: ;
1514       struct request_area *ra = (struct request_area *) p->request;
1515       dx = ra->cx + v->offset_x - p->x;
1516       dy = ra->cy + v->offset_y - p->y;
1517       distance = sqrt(dx*dx + dy*dy);
1518       if (dbg_rank >= VERBOSITY_PLACEMENT)
1519         printf("Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1520       break;
1521     default:
1522       return 0;
1523   }
1524
1525   if (dbg_rank >= VERBOSITY_PLACEMENT)
1526     printf("Placement %d has distance %.2f\n", p->ind, distance);
1527   return distance;
1528 }
1529
1530 double individual_distances(struct individual *individual)
1531 {
1532   int distances = 0;
1533
1534   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1535   {
1536     distances += get_distance(&individual->placements[i]);
1537   }
1538
1539   return distances;
1540 }
1541
1542 double get_omittment(struct placement *p)
1543 {
1544   if (p->variant_used >= 0) return 0;
1545
1546   // FIX ME :)
1547   switch (p->request->type)
1548   {
1549     case REQUEST_POINT:
1550     case REQUEST_AREA:
1551       return 10;
1552       break;
1553     default:
1554       return 0;
1555   }
1556 }
1557
1558 double individual_omittment(struct individual *individual)
1559 {
1560   int omittment = 0;
1561
1562   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1563   {
1564     omittment += get_omittment(&individual->placements[i]);
1565   }
1566
1567   return omittment;
1568 }
1569
1570 int cmp_individual(const void *a, const void *b)
1571 {
1572   struct individual **ia = (struct individual **) a;
1573   struct individual **ib = (struct individual **) b;
1574
1575   return (*ia)->penalty - (*ib)->penalty;
1576 }
1577
1578 void rank_population(void)
1579 {
1580   int penalty;
1581
1582   for (int i=0; i<conf_pop_size; i++)
1583   {
1584     if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1585       printf("Individual %d\n", i);
1586     population1[i]->penalty = 0;
1587
1588     penalty = individual_omittment(population1[i]);
1589     if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1590       printf("Increasing penalty by %d for omittment\n", penalty);
1591     population1[i]->penalty += penalty;
1592
1593     penalty = individual_overlap(population1[i]);
1594     if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1595       printf("Increasing penalty by %d for overlap\n", penalty);
1596     population1[i]->penalty += penalty;
1597
1598     penalty = individual_distances(population1[i]);
1599     if (dbg_rank >= VERBOSITY_INDIVIDUAL)
1600       printf("Increasing penalty by %d for distances\n", penalty);
1601     population1[i]->penalty += penalty;
1602   }
1603 }
1604
1605 struct map_part **get_map_parts(struct placement *p)
1606 {
1607   if (p->variant_used < 0) return NULL;
1608
1609   struct map_part **buffer;
1610   GARY_INIT(buffer, 0);
1611
1612   if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1613     printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1614
1615   struct variant v;
1616   switch (p->request->type)
1617   {
1618     case REQUEST_POINT:
1619     case REQUEST_SEGMENT:
1620     case REQUEST_AREA:
1621       v = p->request->variants[p->variant_used];
1622       break;
1623     default:
1624       if (dbg_map_parts >= VERBOSITY_ALL)
1625         printf("Skipping unsupported request type (%d)\n", p->request->type);
1626       return NULL;
1627   }
1628
1629   if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1630     printf("Bitmap is %d x %d\n", v.width, v.height);
1631
1632   int x_min = max2(0, p->x) / conf_map_part_width;
1633   // CHECK ME: Is rounding needed?
1634   int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1635   int y_min = max2(0, p->y) / conf_map_part_height;
1636   // CHECK ME: Is rounding needed?
1637   int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1638
1639   if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1640     printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1641
1642   for (int y=y_min; y<=y_max; y++)
1643     for (int x=x_min; x<=x_max; x++)
1644     {
1645       struct map_part **m = GARY_PUSH(buffer);
1646       if (dbg_map_parts >= VERBOSITY_ALL)
1647         printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1648       *m = p->individual->map[y * num_map_parts_row + x];
1649     }
1650
1651   if (dbg_map_parts >= VERBOSITY_PLACEMENT)
1652     printf("Returning %u map parts potentially containing the symbol\n", GARY_SIZE(buffer));
1653
1654   return buffer;
1655 }
1656
1657 void update_map_parts_delete(struct placement *p)
1658 {
1659   struct map_placement *mp = p->map_links;
1660   while (mp)
1661   {
1662     mp->prev_in_map->next_in_map = mp->next_in_map;
1663     if (mp->next_in_map)
1664       mp->next_in_map->prev_in_map = mp->prev_in_map;
1665
1666     struct map_placement *tmp = mp;
1667     mp = mp->next_in_placement;
1668     free(tmp);
1669   }
1670   p->map_links = NULL;
1671 }
1672
1673 void update_map_parts_create(struct placement *p)
1674 {
1675   struct map_part **parts = get_map_parts(p);
1676   if (parts == NULL) return;
1677
1678   for (uns i=0; i<GARY_SIZE(parts); i++)
1679   {
1680     struct map_placement *mp = malloc(sizeof(struct map_placement));
1681     mp->placement = p;
1682     mp->part = parts[i];
1683
1684     mp->next_in_map = parts[i]->placement->next_in_map;
1685     mp->prev_in_map = parts[i]->placement;
1686     parts[i]->placement->next_in_map = mp;
1687     if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
1688
1689     mp->next_in_placement = p->map_links;
1690     mp->prev_in_placement = NULL;
1691     p->map_links = mp;
1692   }
1693
1694   GARY_FREE(parts);
1695 }
1696
1697 void update_map_parts(struct placement *p)
1698 {
1699   update_map_parts_delete(p);
1700   update_map_parts_create(p);
1701 }
1702
1703 void gen_coords(struct placement *p)
1704 {
1705   switch(p->request->type)
1706   {
1707     case REQUEST_POINT:
1708       gen_coords_point(p);
1709       break;
1710     case REQUEST_AREA:
1711       gen_coords_area(p);
1712       break;
1713     case REQUEST_SEGMENT:
1714       gen_coords_segment(p);
1715       break;
1716     case REQUEST_LINE:
1717       if (dbg_movement)
1718         printf("Not yet implemented\n");
1719       break;
1720     default:
1721       if (dbg_movement >= VERBOSITY_ALL)
1722         printf("Testing request type\n");
1723       ASSERT(p->request->type != REQUEST_INVALID);
1724   }
1725
1726   update_map_parts(p);
1727 }
1728
1729 double gen_movement(void)
1730 {
1731   double m = (random() % 100000) / 10000;
1732   m = pow(m, 1.0/3) * flip(1, -1);
1733   if (dbg_movement >= VERBOSITY_ALL)
1734     printf("Movement %.2f\n", m);
1735   return m;
1736 }
1737
1738 double gen_movement_uniform(void)
1739 {
1740   return (move_max - move_min) * randdouble() * flip(1, -1);
1741 }
1742
1743 void gen_coords_point(struct placement *p)
1744 {
1745   p->x = p->x + gen_movement();
1746 }
1747
1748 void gen_coords_segment(struct placement *p)
1749 {
1750   struct request_segment *rs = (struct request_segment *) p->request;
1751   int a = flip(1, 2);
1752   p->x = (a == 1 ? rs->x1 : rs->x2);
1753   p->y = (a == 1 ? rs->y1 : rs->y2);
1754 }
1755
1756 void gen_coords_area(struct placement *p)
1757 {
1758   struct request_area *ra = (struct request_area *) p->request;
1759
1760   p->x = p->x + gen_movement();
1761   p->y = p->y + gen_movement();
1762
1763   if (dbg_movement >= VERBOSITY_PLACEMENT)
1764     printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1765 }
1766
1767 int randint(int min, int max)
1768 {
1769   if (min == max) return min;
1770   int r = random();
1771   return min + (r % (max - min));
1772 }
1773
1774 struct placement **get_closure(struct placement *placement)
1775 {
1776   struct placement **closure;
1777   GARY_INIT(closure, 0);
1778   bool *chosen = malloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1779   for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1780   chosen[placement->request->ind] = 1;
1781
1782   struct placement **p = GARY_PUSH(closure); *p = placement;
1783
1784   uns first = 0;
1785   while (first < GARY_SIZE(closure))
1786   {
1787     if (dbg_breeding >= VERBOSITY_ALL)
1788       printf("Iterating, first is %d of current %u\n", first, GARY_SIZE(closure));
1789     struct placement **overlapping = get_overlapping(placement);
1790     if (! overlapping) { first++; continue; }
1791
1792     struct placement **filtered = filter(overlapping, &chosen);
1793     if (dbg_breeding >= VERBOSITY_ALL)
1794       printf("There are %u new overlapping symbols\n", GARY_SIZE(filtered));
1795     GARY_FREE(overlapping);
1796     overlapping = filtered;
1797     for (uns j=0; j<GARY_SIZE(overlapping); j++)
1798     {
1799       if (! chosen[overlapping[j]->request->ind])
1800       {
1801         if (overlaps(*p, overlapping[j]))
1802         {
1803           p = GARY_PUSH(closure); *p = overlapping[j];
1804           if (dbg_breeding >= VERBOSITY_ALL)
1805             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);
1806           chosen[overlapping[j]->request->ind] = 1;
1807         }
1808       }
1809     }
1810     GARY_FREE(overlapping);
1811     first++;
1812   }
1813
1814   free(chosen);
1815
1816   return closure;
1817 }
1818
1819 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1820 {
1821   bool *processed = *processed_ptr;
1822   if (dbg_breeding >= VERBOSITY_ALL)
1823     printf("Will copy %u symbols\n", GARY_SIZE(closure));
1824
1825   for (uns i=0; i<GARY_SIZE(closure); i++)
1826   {
1827     processed[closure[i]->ind] = 1;
1828     int ind = closure[i]->ind;
1829     child->placements[ind] = parent->placements[ind];
1830     child->placements[ind].individual = child;
1831     child->placements[ind].processed = 0;
1832     child->placements[ind].map_links = NULL;
1833     update_map_parts(&child->placements[ind]);
1834   }
1835 }
1836
1837 void move_symbol(struct placement *p)
1838 {
1839   switch (p->request->type)
1840   {
1841     case REQUEST_POINT:
1842     case REQUEST_AREA:
1843       move_symbol_point(p);
1844       break;
1845     case REQUEST_SEGMENT:
1846       move_symbol_segment(p);
1847       break;
1848     default:
1849       ASSERT(p->request->type != REQUEST_INVALID);
1850   }
1851 }
1852
1853 void move_symbol_point(struct placement *p)
1854 {
1855   p->x += gen_movement_uniform();
1856   p->y += gen_movement_uniform();
1857 }
1858
1859 void move_symbol_segment(struct placement *p)
1860 {
1861   double m = gen_movement_uniform();
1862   // CHECK ME
1863   p->x += m;
1864   p->y += m * ((struct request_segment *) p->request)->slope;
1865 }
1866
1867 void hide_segment_labels(struct individual *individual)
1868 {
1869   // BEWARE: This fully depends on current genetic encoding
1870
1871   int used = -1, num = -1;
1872   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1873   {
1874     switch (individual->placements[i].request->type)
1875     {
1876       case REQUEST_SECTION:
1877         used = individual->placements[i].variant_used;
1878         num = 0;
1879         break;
1880       case REQUEST_SEGMENT:
1881         if (num == used)
1882           individual->placements[i].variant_used = 0;
1883         else
1884           individual->placements[i].variant_used = -1;
1885         num++;
1886         break;
1887       default:
1888         ;
1889     }
1890   }
1891 }
1892
1893 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1894 {
1895   p->ind = num_placements++;
1896   p->request = r;
1897   p->processed = 0;
1898   p->x = p->y = 0; // To prevent valgrind from complaining
1899   p->variant_used = 0;
1900   p->map_links = NULL;
1901   p->individual = individual;
1902   switch (r->type)
1903   {
1904     case REQUEST_POINT: ;
1905       struct request_point *rp = (struct request_point *) r;
1906       p->x = rp->x;
1907       p->y = rp->y;
1908       break;
1909     case REQUEST_LINE: ;
1910       break;
1911     case REQUEST_SECTION: ;
1912       struct request_section *rls = (struct request_section *) r;
1913       p->variant_used = randint(0, rls->num_segments);
1914       break;
1915     case REQUEST_SEGMENT: ;
1916       struct request_segment *rs = (struct request_segment *) r;
1917       p->x = rs->x2;
1918       p->y = rs->y2;
1919       break;
1920     case REQUEST_AREA: ;
1921       struct request_area *ra = (struct request_area *) r;
1922       p->x = ra->cx;
1923       p->y = ra->cy;
1924       p->variant_used = 0;
1925       break;
1926     default:
1927       ASSERT(p->request->type != REQUEST_INVALID);
1928   }
1929
1930   gen_coords(p);
1931   if (dbg_init >= VERBOSITY_PLACEMENT)
1932     printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1933 }
1934
1935 void reset_individual_map(struct individual *i)
1936 {
1937   for (uns j=0; j<num_map_parts; j++)
1938   {
1939     struct map_placement *mp = i->map[j]->placement;
1940     while (mp)
1941     {
1942       struct map_placement *tmp = mp;
1943       mp = mp->next_in_map;
1944       free(tmp);
1945     }
1946
1947     free(i->map[j]);
1948     struct map_part *part = malloc(sizeof(struct map_part));
1949     part->ind = j;
1950
1951     mp = malloc(sizeof(struct map_placement));
1952     part->placement = mp;
1953     mp->placement = &dummy_placement;
1954     mp->next_in_map = mp->prev_in_map = NULL;
1955     mp->next_in_placement = mp->prev_in_placement = NULL;
1956     i->map[j] = part;
1957   }
1958 }
1959
1960 void update_individual(struct individual *individual)
1961 {
1962   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1963   {
1964     update_map_parts_delete(&individual->placements[i]);
1965   }
1966 }
1967
1968 void clear_individual(struct individual *individual)
1969 {
1970   for (uns j=0; j<num_map_parts; j++)
1971   {
1972     struct map_placement *mp = individual->map[j]->placement;
1973     while (mp)
1974     {
1975       struct map_placement *tmp = mp;
1976       mp = mp->next_in_map;
1977       free(tmp);
1978     }
1979
1980     free(individual->map[j]);
1981   }
1982
1983   GARY_FREE(individual->map);
1984   GARY_FREE(individual->placements);
1985   ep_free(ep_individuals, individual);
1986 }
1987
1988 void clear_population(struct individual **pop)
1989 {
1990   for (uns i=0; i<GARY_SIZE(pop); i++)
1991   {
1992     clear_individual(pop[i]);
1993   }
1994 }
1995
1996 struct placement **get_overlapping(struct placement *p)
1997 {
1998   struct placement **buffer;
1999   GARY_INIT(buffer, 0);
2000
2001   struct map_part **parts = get_map_parts(p);
2002   if (! parts) return NULL;
2003
2004   for (uns i=0; i<GARY_SIZE(parts); i++)
2005   {
2006     struct map_placement *mp = parts[i]->placement->next_in_map;
2007     while (mp)
2008     {
2009       if (p->variant_used >= 0)
2010       {
2011         struct placement **p = GARY_PUSH(buffer);
2012         *p = mp->placement;
2013       }
2014       mp = mp->next_in_map;
2015     }
2016   }
2017   GARY_FREE(parts);
2018
2019   if (dbg_map_parts >= VERBOSITY_PLACEMENT)
2020     printf("Returning %u potentially overlapping placements\n", GARY_SIZE(buffer));
2021
2022   return buffer;
2023 }
2024
2025 struct placement **filter(struct placement **list, bool **pred_ptr)
2026 {
2027   bool *pred = *pred_ptr; // As GARY can't be passed directly
2028   struct placement **filtered;
2029   GARY_INIT(filtered, 0);
2030
2031   for (uns i=0; i<GARY_SIZE(list); i++)
2032   {
2033     if (pred[list[i]->request->ind])
2034       continue;
2035
2036     struct placement **p = GARY_PUSH(filtered);
2037     *p = list[i];
2038   }
2039
2040   return filtered;
2041 }
2042
2043 int flip(int a, int b)
2044 {
2045   return (random() % 2 ? a : b);
2046 }
2047
2048 double randdouble(void)
2049 {
2050   return ((double) rand() / (double) RAND_MAX);
2051 }
2052
2053 void init_individual(struct individual *individual)
2054 {
2055   GARY_INIT(individual->placements, num_requests);
2056   GARY_INIT(individual->map, 0);
2057   for (uns j=0; j<num_map_parts; j++)
2058   {
2059     GARY_PUSH(individual->map);
2060     struct map_part *part = malloc(sizeof(struct map_part));
2061     struct map_placement *mp = malloc(sizeof(struct map_placement));
2062     part->placement = mp;
2063     part->ind = j;
2064     mp->placement = &dummy_placement;
2065     mp->next_in_map = mp->prev_in_map = NULL;
2066     mp->next_in_placement = mp->prev_in_placement = NULL;
2067     individual->map[j] = part;
2068   }
2069   individual->penalty = 0;
2070 }
2071
2072 void copy_individual(struct individual *src, struct individual *dest)
2073 {
2074   init_individual(dest);
2075   dest->penalty = src->penalty;
2076
2077   for (uns i=0; i<GARY_SIZE(src->placements); i++)
2078   {
2079     dest->placements[i] = src->placements[i];
2080     dest->placements[i].map_links = NULL;
2081     dest->placements[i].individual = dest;
2082
2083     update_map_parts_create(&dest->placements[i]);
2084   }
2085 }