5 #include <ucw/mempool.h>
6 #include <ucw/eltpool.h>
13 #define HASH_NODE struct graph_node
14 #define HASH_PREFIX(x) hash_##x
15 #define HASH_KEY_ATOMIC id
16 #define HASH_WANT_FIND
18 #define HASH_WANT_CLEANUP
19 #include <ucw/hashtable.h>
26 #define BLOCK_SIZE 4096
28 //struct mempool *mpool_requests;
30 static struct request_point *requests_point;
31 static struct request_line *requests_line;
32 static struct request_area *requests_area;
34 static struct graph_edge **bfs_queue;
35 static struct longline *longlines; int num_longlines;
36 static struct buffer_line *buffer_line;
37 static struct buffer_linelabel *buffer_linelabel;
39 struct eltpool *ep_individuals;
41 struct individual **population1;
42 struct individual **population2;
44 int dbg_segments = VERBOSITY_NONE;
45 int dbg_plan = VERBOSITY_NONE;
46 int dbg_requests = VERBOSITY_NONE;
47 int dbg_graph = VERBOSITY_NONE;
48 int dbg_bfs = VERBOSITY_NONE;
49 int dbg_map_parts = VERBOSITY_NONE;
50 int dbg_movement = VERBOSITY_NONE;
51 int dbg_init = VERBOSITY_NONE;
52 int dbg_overlaps = VERBOSITY_NONE;
53 int dbg_rank = VERBOSITY_NONE;
54 int dbg_evolution = VERBOSITY_NONE;
55 int dbg_mutation = VERBOSITY_NONE;
56 int dbg_breeding = VERBOSITY_NONE;
66 int conf_pop_size = 50;
68 int conf_penalty_bound = 0;
69 int conf_stagnation_bound = 0;
70 int conf_iteration_limit = 4;
72 int conf_term_cond = TERM_COND_ITERATIONS;
74 int conf_breed_rbest_perc = 80;
75 int conf_breed_pop_size_perc = 20;
76 int conf_breed_perc = 50; // Percentage of new pop created by breeding
78 bool conf_mutate_children = 1;
79 double conf_mutate_children_prob = 0.3;
81 double conf_mutate_rbest = 1;
82 double conf_mutate_pop_size = 0.9;
84 double conf_mutate_move_bound = 1.0;
85 double conf_mutate_regen_bound = 0.0;
86 double conf_mutate_chvar_bound = 0.0;
89 int mutate_rbest_size;
91 double conf_elite_pop_size = 0.1;
94 double conf_max_section_length = 100;
95 double conf_max_section_overlay = 10;
97 int old_best = 0; // FIXME: Shall be int max
101 int conf_part_size = 50;
106 int num_requests = 0;
107 int num_placements = 0;
109 int conf_map_part_width = 5;
110 int conf_map_part_height = 5;
111 uns num_map_parts_row;
112 uns num_map_parts_col;
115 void make_graph(void);
116 void label_graph(void);
117 void join_edge(struct graph_edge *e, int dir);
118 void bfs(uns longline);
119 void make_segments(void);
121 int overlaps(struct placement *p1, struct placement *p2);
122 int get_overlap(struct placement *p);
123 int individual_overlap(struct individual *individual);
125 double get_distance(struct placement *p);
126 double individual_distances(struct individual *individual);
128 void make_population(void);
129 bool shall_terminate(void);
133 void rank_population(void);
134 void plan_individual(struct individual *individual);
136 int cmp_individual(const void *a, const void *b);
138 void make_bitmap(struct variant *v, struct symbol *sym);
139 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
140 void make_bitmap_point(struct variant *v, struct sym_point *sp);
141 void make_bitmap_label(struct variant *v, struct sym_text *text);
143 void cut_edge(struct graph_edge *e, double dist);
144 struct request_line *make_new_line(void);
145 struct request_section *make_new_section(struct request_line *rl);
146 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym);
148 void dump_bitmaps(struct individual *individual);
149 void dump_graph(void);
151 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir);
152 void bfs_wrapper(void);
154 void dump_longlines(void);
155 void dump_linelabel_requests(void);
156 void dump_individual(struct individual *individual);
157 void print_label(struct symbol *sym);
158 void dump_penalties(struct individual **population);
160 double gen_movement(void);
161 double gen_movement_uniform(void);
162 void gen_coords(struct placement *p);
163 void gen_coords_point(struct placement *p);
164 void gen_coords_segment(struct placement *p);
165 void gen_coords_area(struct placement *p);
167 struct map_part **get_map_parts(struct placement *p);
168 void update_map_parts(struct placement *p);
170 void make_segments_old(void);
172 void labeller_cleanup(void);
174 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2);
175 void perform_mutation(struct individual *individual);
177 void hide_segment_labels(struct individual *individual);
178 void init_placement(struct placement *p, struct individual *individual, struct request *r);
179 void init_individual(struct individual *i);
180 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
182 int randint(int min, int max);
184 struct placement **get_closure(struct placement *placement);
185 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr);
186 void move_symbol(struct placement *p);
187 void move_symbol_point(struct placement *p);
188 void move_symbol_segment(struct placement *p);
190 struct placement **get_overlapping(struct placement *p);
191 struct placement **filter(struct placement **list, bool **pred_ptr);
193 int flip(int a, int b);
194 double randdouble(void);
198 void copy_individual(struct individual *src, struct individual *dest);
200 int max2(int a, int b);
201 int min2(int a, int b);
202 int max4(int a, int b, int c, int d);
203 int min4(int a, int b, int c, int d);
205 struct placement dummy_placement;
207 int max2(int a, int b)
209 return (a > b ? a : b);
212 int min2(int a, int b)
214 return (a < b ? a : b);
217 int max4(int a, int b, int c, int d)
219 return max2(max2(a, b), max2(c, d));
222 int min4(int a, int b, int c, int d)
224 return min2(min2(a, b), min2(c, d));
227 void print_label(struct symbol *sym)
231 case SYMBOLIZER_TEXT: ;
232 struct sym_text *st = (struct sym_text *) sym;
233 printf("%s\n", osm_val_decode(st->text));
240 void labeller_init(void)
242 GARY_INIT(requests_point, 0);
243 GARY_INIT(requests_line, 0);
244 GARY_INIT(requests_area, 0);
245 GARY_INIT(buffer_line, 0);
246 GARY_INIT(buffer_linelabel, 0);
247 ep_individuals = ep_new(sizeof(struct individual), 1);
249 page_width_int = floor(page_width);
250 page_height_int = floor(page_height);
252 num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
253 num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
254 num_map_parts = num_map_parts_row * num_map_parts_col;
257 void make_bitmap(struct variant *v, struct symbol *sym)
259 v->offset_x = v->offset_y = 0;
263 case SYMBOLIZER_POINT:
264 make_bitmap_point(v, (struct sym_point *) sym);
266 case SYMBOLIZER_ICON:
267 make_bitmap_icon(v, (struct sym_icon *) sym);
269 case SYMBOLIZER_TEXT:
270 make_bitmap_label(v, (struct sym_text *) sym);
273 ASSERT(sym->type != SYMBOLIZER_INVALID);
277 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
279 v->width = si->sir.width + 1;
280 v->height = si->sir.height + 1;
281 v->bitmap = malloc(v->width * v->height * sizeof(bool));
282 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
285 void make_bitmap_point(struct variant *v, struct sym_point *sp)
287 v->width = v->height = sp->size + 1;
288 v->bitmap = malloc(v->width * v->height * sizeof(bool));
289 // FIXME: Okay, memset would be much nicer here
290 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
293 void make_bitmap_label(struct variant *v, struct sym_text *text)
295 v->width = ceil(text->tw);
296 v->height = ceil(text->th);
297 v->bitmap = malloc(v->width * v->height * sizeof(bool));
298 for (int i=0; i<v->height; i++)
299 for (int j=0; j<v->width; j++)
301 v->bitmap[i*v->width + j] = 1;
305 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
308 printf("Adding point\n");
309 if (object->type != OSM_TYPE_NODE)
311 printf("Warning: Point label requested on non-point object\n");
315 struct request_point *r = GARY_PUSH(requests_point);
317 r->request.type = REQUEST_POINT;
318 r->request.ind = num_requests++;
327 GARY_INIT(r->request.variants, 0);
329 struct variant *v = GARY_PUSH(r->request.variants);
331 struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
337 case SYMBOLIZER_ICON:
339 r->x = ((struct sym_icon *)sym)->sir.x;
340 r->y = ((struct sym_icon *)sym)->sir.y;
347 // printf("Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
350 void labeller_add_line(struct symbol *sym, z_index_t zindex)
353 printf("Adding line on %u\n", zindex);
354 struct buffer_line *b = GARY_PUSH(buffer_line);
355 b->line = (struct sym_line *) sym;
357 sym_plan(sym, zindex);
360 void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
362 if (o->type != OSM_TYPE_WAY)
369 printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
370 struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
371 ll->way = (struct osm_way *) o;
376 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
379 printf("Adding area on %u\n", zindex);
380 struct request_area *r = GARY_PUSH(requests_area);
382 r->request.type = REQUEST_AREA;
383 r->request.ind = num_requests++;
385 r->o = (struct osm_multipolygon *) o;
389 osm_obj_center(o, &(r->cx), &(r->cy));
391 GARY_INIT(r->request.variants, 0);
392 struct variant *v = GARY_PUSH(r->request.variants);
396 void make_graph(void)
399 struct mempool *mp_edges = mp_new(BLOCK_SIZE);
401 printf("Extracting nodes, will iterate over %lld ways\n", GARY_SIZE(buffer_line));
402 for (uns i=0; i<GARY_SIZE(buffer_line); i++)
404 struct osm_way *way = (struct osm_way *) buffer_line[i].line->s.o;
405 struct graph_node *g_prev = NULL;
406 struct osm_node *o_prev = NULL;
408 CLIST_FOR_EACH(struct osm_ref *, ref, way->nodes)
410 // FIXME: Shall osm_object's type be checked here?
411 struct osm_node *o_node = (struct osm_node *) ref->o;
413 struct graph_node *g_node = hash_find(ref->o->id);
416 g_node = hash_new(ref->o->id);
417 GARY_INIT(g_node->edges, 0);
419 g_node->id = ref->o->id;
420 g_node->num = num_nodes++;
430 struct graph_edge *e = mp_alloc(mp_edges, sizeof(struct graph_edge)); num_edges_dbg++;
431 e->num = num_edges++;
432 e->id = buffer_line[i].line->s.o->id;
433 e->color = buffer_line[i].line->color;
434 e->length = hypot(abs(o_prev->x - o_node->x), abs(o_prev->y - o_node->y));
440 e->longline = (uns) -1;
441 e->line = buffer_line[i].line;
445 struct graph_edge **edge = GARY_PUSH(g_prev->edges);
447 edge = GARY_PUSH(g_node->edges);
455 printf("Made graph with %d edges\n", num_edges_dbg);
458 void dump_graph(void)
460 HASH_FOR_ALL(hash, node)
462 printf("* Node: (%d) #%ju [%.2f; %.2f]\n", node->num, node->id, node->o->x, node->o->y);
463 for (uns i=0; i<GARY_SIZE(node->edges); i++)
465 struct graph_edge *e = node->edges[i];
466 printf("\t edge (%d) #%ju to ", e->num, e->id);
467 if (node->edges[i]->n1->id == node->id)
468 printf("(%d) #%ju [%.2f; %.2f]\n", e->n2->num, e->n2->id, e->n2->o->x, e->n2->o->y);
469 else if (node->edges[i]->n2->id == node->id)
470 printf("(%d) #%ju [%.2f; %.2f]\n", e->n1->num, e->n1->id, e->n1->o->x, e->n1->o->y);
472 printf("BEWARE! BEWARE! BEWARE!\n");
475 if ((node->edges[i]->label)) printf("Labelled\n");
476 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));
477 printf(" colored %d;", node->edges[i]->color);
478 printf(" length %.2f", node->edges[i]->length);
485 void label_graph(void)
488 printf("There are %u line labels requested\n", GARY_SIZE(buffer_linelabel));
489 for (uns i=0; i<GARY_SIZE(buffer_linelabel); i++)
491 if (buffer_linelabel[i].label->type == SYMBOLIZER_TEXT)
493 printf("Labelling nodes of way %s\n", osm_val_decode(((struct sym_text *) buffer_linelabel[i].label)->text));
494 CLIST_FOR_EACH(struct osm_ref *, ref, buffer_linelabel[i].way->nodes)
497 printf("Looking for node %ju\n", ref->o->id);
498 struct graph_node *n = hash_find(ref->o->id);
501 // FIXME: What shall be done?
506 printf("Searching among %u edges\n", GARY_SIZE(n->edges));
507 for (uns j=0; j<GARY_SIZE(n->edges); j++)
509 if (n->edges[j]->id == buffer_linelabel[i].way->o.id)
512 printf("Labelling node %ju\n", n->id);
513 n->edges[j]->label = buffer_linelabel[i].label;
514 n->edges[j]->zindex = buffer_linelabel[i].zindex;
522 void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, enum edge_dir dir)
525 printf("BFS edge called for edge %d (going %d) in direction %d\n", e->num, e->dir, dir);
526 struct graph_edge *candidate = NULL;
528 for (uns i=0; i<GARY_SIZE(node->edges); i++)
530 struct graph_edge *other = node->edges[i];
531 if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
533 if ((uns) other->visited != e->longline) {
535 printf("Pushing new edge %d / %ju\n", other->num, other->id);
536 struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
538 other->visited = e->longline;
541 if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
542 ((other->n2->id == node->id) && (other->n1->id == anode->id)))
545 if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
546 (e->label) && (other->label) &&
547 (e->label->type == SYMBOLIZER_TEXT) && (other->label->type == SYMBOLIZER_TEXT) &&
548 (((struct sym_text *) e->label)->text == ((struct sym_text *) other->label)->text))
550 if (! candidate || (other->length > candidate->length))
558 printf("New line in longline %u\n", e->longline);
559 struct graph_edge *other = candidate;
560 other->longline = e->longline;
562 if (((dir == DIR_BWD) && (other->n1->id == node->id)) ||
563 ((dir == DIR_FWD) && (other->n2->id == node->id)))
565 struct graph_node *swp = other->n2;
566 other->n2 = other->n1;
575 longlines[other->longline].first = other;
588 void bfs(uns longline)
591 printf("BFS called for longline %u\n", longline);
593 printf("%d longlines are believed to exist, %d exist\n", num_longlines, GARY_SIZE(longlines));
594 for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
596 struct graph_edge *cur = bfs_queue[i];
598 printf("Exploring new edge %d; %d remaining\n", cur->num, GARY_SIZE(bfs_queue));
599 //ASSERT(! cur->visited);
601 cur->visited = longline;
603 if (cur->longline == (uns) -1)
606 if (cur->dir == DIR_UNSET)
608 cur->dir = DIR_CENTER;
609 bfs_edge(cur, cur->n1, cur->n2, DIR_BWD);
610 bfs_edge(cur, cur->n2, cur->n1, DIR_FWD);
617 bfs_edge(cur, cur->n1, cur->n2, cur->dir);
620 bfs_edge(cur, cur->n2, cur->n1, cur->dir);
630 void bfs_wrapper(void)
632 GARY_INIT(bfs_queue, 0);
633 GARY_INIT(longlines, 0);
635 HASH_FOR_ALL(hash, node)
637 for (uns i=0; i<GARY_SIZE(node->edges); i++)
639 if ((node->edges[i]->label) && (node->edges[i]->longline == (uns) -1))
641 GARY_PUSH(longlines);
642 longlines[num_longlines].first = node->edges[i];
644 printf("Running new BFS\n");
646 printf("Creating longline %u\n", num_longlines);
647 GARY_RESIZE(bfs_queue, 0);
648 struct graph_edge **e = GARY_PUSH(bfs_queue);
650 node->edges[i]->longline = num_longlines;
651 bfs(node->edges[i]->longline);
654 printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
656 printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
663 GARY_FREE(bfs_queue);
666 void dump_longlines(void)
668 printf("*** Longlines dump\n");
669 for (uns i=0; i<GARY_SIZE(longlines); i++)
671 printf("Longline %u:", i);
672 struct graph_edge *e = longlines[i].first;
673 if ((e->label) && (e->label->type == SYMBOLIZER_TEXT))
674 printf(" labelled %s", osm_val_decode(((struct sym_text *) e->label)->text));
679 printf("\t#%ju (%d): [%.2f; %.2f] -- [%.2f; %.2f] (dir %d)\n",
680 e->id, e->num, e->n1->o->x, e->n1->o->y, e->n2->o->x, e->n2->o->y, e->dir);
687 struct request_line *make_new_line(void)
689 struct request_line *rl = GARY_PUSH(requests_line);
690 rl->request.ind = num_requests++;
691 rl->request.type = REQUEST_LINE;
692 GARY_INIT(rl->sections, 0);
693 GARY_INIT(rl->request.variants, 0);
698 struct request_section *make_new_section(struct request_line *rl)
700 struct request_section *rls = GARY_PUSH(rl->sections);
701 rls->request.ind = num_requests++;
702 rls->request.type = REQUEST_SECTION;
703 rls->num_segments = 0;
704 GARY_INIT(rls->segments, 0);
705 GARY_INIT(rls->request.variants, 0);
710 struct request_segment *make_new_segment(struct request_section *rls, struct symbol *sym)
712 struct request_segment *rs = GARY_PUSH(rls->segments);
715 rs->request.ind = num_requests++;
716 rs->request.type = REQUEST_SEGMENT;
718 GARY_INIT(rs->request.variants, 0);
719 struct variant *v = GARY_PUSH(rs->request.variants);
725 void cut_edge(struct graph_edge *e, double dist)
728 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);
730 struct graph_edge *new = malloc(sizeof(struct graph_edge));
734 // FIXME? Create new label for new edge, don't only copy pointer?
736 struct osm_node *n1 = e->n1->o;
737 struct osm_node *n2 = e->n2->o;
740 if ((n1->x == n2->x) && (n1->y == n2->y))
742 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", n1->x, n1->y, n2->x, n2->y);
743 printf("Won't cut point\n");
747 struct osm_node *n11 = malloc(sizeof(struct osm_node));
748 struct graph_node *gn = malloc(sizeof(struct graph_node));
750 double vsize = sqrt(pow(n1->x - n2->x, 2) + pow(n1->y - n2->y, 2));
751 n11->x = n1->x + (n2->x - n1->x) / vsize * dist;
752 n11->y = n1->y + (n2->y - n1->y) / vsize * dist;
754 e->n2 = new->n1 = gn;
756 e->length = hypot(abs(n1->x - n11->x), abs(n1->y - n11->y));
757 new->length = hypot(abs(n11->x - n2->x), abs(n11->y - n2->y));
761 void make_segments(void)
763 for (uns i=0; i<GARY_SIZE(longlines); i++)
765 // Skip lines which are not labelled
766 if (! (longlines[i].first && longlines[i].first->label))
769 struct request_line *request = make_new_line();
770 struct request_section *rls = make_new_section(request);
771 struct request_segment *rs = NULL;
773 struct graph_edge *e = longlines[i].first;
774 double cur_length = 0;
776 struct sym_text *st = NULL;
777 if (e->label->type == SYMBOLIZER_TEXT)
779 st = (struct sym_text *) e->label;
784 printf("Warning: Skipping line\n");
790 printf("New longline\n");
795 printf("BEWARE: Edge cycle\n");
798 e->visited = -1; // FIXME
801 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);
803 if (st && (e->length < st->tw))
806 //printf("Warning: Skipping segment\n");
810 if (cur_length + e->length > conf_max_section_length + conf_max_section_overlay)
813 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);
814 // HACK to prevent cutting to 0 lenght
815 cut_edge(e, max2(conf_max_section_length - cur_length, 2));
818 rs = make_new_segment(rls, e->label);
819 rs->label = malloc(sizeof(struct sym_text));
820 *((struct sym_text *) rs->label) = *((struct sym_text *) e->label);
822 rs->x1 = e->n1->o->x;
823 rs->y1 = e->n1->o->y;
824 rs->x2 = e->n2->o->x;
825 rs->y2 = e->n2->o->y;
827 rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
828 rs->zindex = e->zindex;
830 cur_length += e->length;
831 if (cur_length > conf_max_section_length)
834 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);
836 rls = make_new_section(request);
843 if (request->sections[0].num_segments == 0)
846 printf("WARNING: 0 segment section\n");
847 GARY_POP(requests_line);
853 void dump_linelabel_requests(void)
855 for (uns i=0; i<GARY_SIZE(requests_line); i++)
857 if (requests_line[i].sections[0].num_segments == 0)
862 printf("Request for linelabel, %d sections\n", GARY_SIZE(requests_line[i].sections));
863 print_label(requests_line[i].sections[0].segments[0].label);
864 for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
866 printf("%d section, %d segments\n", j, GARY_SIZE(requests_line[i].sections[j].segments));
867 for (uns k=0; k<GARY_SIZE(requests_line[i].sections[j].segments); k++)
869 struct request_segment *rs = &requests_line[i].sections[j].segments[k];
870 printf("[%.2f; %.2f] -- [%.2f; %.2f]\n", rs->x1, rs->y1, rs->x2, rs->y2);
877 void dump_bitmaps(struct individual *individual)
879 bool *bitmap = malloc(page_width_int * page_height_int * sizeof(bool));
880 printf("Bitmap size is %d\n", page_width_int * page_height_int);
881 for (int i=0; i<page_height_int; i++)
882 for (int j=0; j<page_width_int; j++)
883 bitmap[i*page_width_int + j] = 0;
885 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
887 if (individual->placements[i].variant_used == -1) continue;
889 struct placement *p = &(individual->placements[i]);
890 struct variant *v = NULL;
892 switch (p->request->type)
894 case REQUEST_SEGMENT: ;
895 case REQUEST_POINT: ;
897 v = &(p->request->variants[p->variant_used]);
900 ASSERT(p->request->type != REQUEST_INVALID);
904 for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
906 for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
908 bitmap[row * page_width_int + col] = 1;
914 FILE *fd_dump = fopen("dump.pbm", "w");
915 fprintf(fd_dump, "P1\n");
916 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
917 for (int i=0; i<page_height_int; i++)
919 for (int j=0; j<page_width_int; j++)
921 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
923 fprintf(fd_dump, "\n");
928 void dump_individual(struct individual *individual)
930 printf("*** Dumping INDIVIDUAL ***\n");
931 printf("(There are %d requests)\n", num_requests);
932 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
934 struct placement *p = &(individual->placements[i]);
936 switch (p->request->type)
939 printf("Point at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_point *) p->request)->zindex);
942 struct request_line *rl = (struct request_line *) p->request;
944 print_label(rl->sections[0].segments[0].label);
946 case REQUEST_SECTION: ;
949 case REQUEST_SEGMENT: ;
950 if (p->variant_used >= 0)
951 printf("Segment placed at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_segment *) p->request)->zindex);
953 printf("Segment not placed\n");
956 struct request_area *ra = (struct request_area *) p->request;
957 printf("Area label ");
958 print_label(ra->label);
959 printf(" at [%.2f; %.2f] on %u\n", p->x, p->y, ((struct request_area *) p->request)->zindex);
962 printf("Testing request type (dump_individual)\n");
963 ASSERT(p->request->type != 0);
966 printf("\nTotal penalty: %d\n", individual->penalty);
969 void plan_individual(struct individual *individual)
971 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
973 struct symbol *s = NULL;
974 z_index_t zindex = 0;
975 if (individual->placements[i].variant_used < 0) continue;
976 switch (individual->placements[i].request->type)
978 case REQUEST_POINT: ;
979 struct request_point *rp = (struct request_point *) individual->placements[i].request;
983 case REQUEST_SEGMENT: ;
984 struct request_segment *rs = (struct request_segment *) individual->placements[i].request;
991 struct request_area *ra = (struct request_area *) individual->placements[i].request;
996 ASSERT(individual->placements[i].request != REQUEST_INVALID);
1001 printf("Will plan symbol at [%.2f; %.2f] on %u\n", individual->placements[i].x, individual->placements[i].y, zindex);
1003 if (s) switch (s->type)
1005 case SYMBOLIZER_POINT: ;
1006 struct sym_point *sp = (struct sym_point *) s;
1007 sp->x = individual->placements[i].x;
1008 sp->y = individual->placements[i].y;
1009 sym_plan((struct symbol *) sp, zindex);
1011 case SYMBOLIZER_ICON: ;
1012 struct sym_icon *si = (struct sym_icon *) s;
1013 si->sir.x = individual->placements[i].x;
1014 si->sir.y = individual->placements[i].y;
1015 sym_plan((struct symbol *) si, zindex);
1017 case SYMBOLIZER_TEXT: ;
1018 struct sym_text *st = (struct sym_text *) s;
1019 st->x = individual->placements[i].x;
1020 st->y = individual->placements[i].y;
1021 st->next_duplicate = NULL;
1022 if (dbg_plan) printf("Planning text %s at [%.2f; %.2f] on %u, with rotation %.2f\n", osm_val_decode(st->text), st->x, st->y, zindex, st->rotate);
1023 sym_plan((struct symbol *) st, zindex);
1026 ASSERT(s->type != SYMBOLIZER_INVALID);
1031 void dump_penalties(struct individual **population)
1033 for (int i=0; i<conf_pop_size; i++)
1035 printf("Individual %d has penalty %d\n", i, population[i]->penalty);
1039 void labeller_label(void)
1046 printf("Having %u point requests, %u line requests and %u area requests\n", GARY_SIZE(requests_point), GARY_SIZE(requests_line), GARY_SIZE(requests_area));
1048 GARY_INIT(population1, conf_pop_size);
1049 GARY_INIT(population2, conf_pop_size);
1052 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1055 dump_penalties(population1);
1058 breed_pop_size = conf_breed_pop_size * conf_pop_size;
1059 breed_rbest_size = conf_breed_rbest * conf_pop_size;
1062 printf("Breeding parameters:\n");
1063 printf(" %d individuals are created\n", breed_pop_size);
1064 printf(" %d best individuals in old population are considered\n", breed_rbest_size);
1067 mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
1068 mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
1071 printf("Mutation parameters:\n");
1072 printf(" %d individuals are created\n", mutate_pop_size);
1073 printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
1076 elite_pop_size = conf_elite_pop_size * conf_pop_size;
1079 printf("Elitism parameters:\n");
1080 printf(" %d best individuals are copied\n", elite_pop_size);
1083 while (! shall_terminate())
1087 printf("*** Iteration %d ***\n", iteration);
1093 struct individual **swp = population1;
1094 population1 = population2;
1099 dump_penalties(population1);
1102 qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
1105 dump_penalties(population1);
1108 plan_individual(population1[0]);
1115 void labeller_cleanup(void)
1119 void make_population(void)
1121 for (int i=0; i<conf_pop_size; i++)
1123 num_placements = 0; // FIXME: This IS a terrible HACK
1124 struct individual *i2 = ep_alloc(ep_individuals);
1125 init_individual(i2);
1126 population2[i] = i2;
1129 printf("Making individual %d\n", i);
1130 struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
1131 population1[i] = individual;
1134 for (uns j=0; j<GARY_SIZE(requests_point); j++)
1136 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
1139 for (uns j=0; j<GARY_SIZE(requests_line); j++)
1141 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
1143 for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
1145 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
1147 for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
1149 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
1154 for (uns j=0; j<GARY_SIZE(requests_area); j++)
1156 init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
1159 hide_segment_labels(individual);
1161 ASSERT(p == num_requests);
1165 bool shall_terminate(void)
1167 switch (conf_term_cond)
1169 case TERM_COND_PENALTY:
1170 return (population1[0]->penalty < conf_penalty_bound);
1171 case TERM_COND_STAGNATION:
1172 return (abs(old_best - population1[0]->penalty) < conf_stagnation_bound);
1173 case TERM_COND_ITERATIONS:
1174 return (iteration >= conf_iteration_limit);
1176 // FIXME: Warn the user that no condition is set
1185 struct individual **breed_buffer;
1186 while (i < breed_pop_size)
1188 int parent1 = randint(0, breed_rbest_size-1);
1189 int parent2 = randint(0, breed_rbest_size-1);
1190 if (dbg_breeding >= VERBOSITY_INDIVIDUAL)
1191 printf("Will breed %d and %d\n", parent1, parent2);
1193 breed_buffer = perform_crossover(population1[parent1], population1[parent2]);
1194 population2[pop2_ind++] = breed_buffer[0];
1195 population2[pop2_ind++] = breed_buffer[1];
1203 struct individual **perform_crossover(struct individual *parent1, struct individual *parent2)
1205 struct individual **buffer = malloc(2*sizeof(struct individual));
1206 struct individual *child1 = ep_alloc(ep_individuals); init_individual(child1);
1207 struct individual *child2 = ep_alloc(ep_individuals); init_individual(child2);
1210 GARY_INIT_ZERO(processed, GARY_SIZE(parent1->placements));
1212 for (uns i=0; i<GARY_SIZE(parent1->placements); i++)
1214 if (! processed[parent1->placements[i].ind])
1216 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1217 printf("Creating symbol closure for placement %u\n", i);
1219 struct placement **clos_symbols = get_closure(&(parent1->placements[i]));
1220 int x = randint(1, 2);
1224 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1225 printf("Copying parent->child 1->1 and 2->2\n");
1226 copy_symbols(clos_symbols, parent1, child1, &processed);
1227 copy_symbols(clos_symbols, parent2, child2, &processed);
1231 if (dbg_breeding >= VERBOSITY_PLACEMENT)
1232 printf("Copying parent->child 2->1 and 1->2\n");
1233 copy_symbols(clos_symbols, parent2, child1, &processed);
1234 copy_symbols(clos_symbols, parent1, child2, &processed);
1237 GARY_FREE(clos_symbols);
1241 GARY_FREE(processed);
1243 if (conf_mutate_children)
1245 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child1);
1246 if (randint(1, 1000) < conf_mutate_children_prob * 1000) perform_mutation(child2);
1256 for (int i=0; i < mutate_pop_size; i++)
1260 int ind = randint(1, mutate_rbest_size);
1262 printf("Mutating %d-th individual of original population\n", ind);
1263 copy_individual(population1[ind], population2[pop2_ind]);
1265 printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
1266 perform_mutation(population2[pop2_ind]);
1271 void perform_mutation(struct individual *individual)
1273 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1275 double x = randdouble();
1278 if (x <= acc + conf_mutate_move_bound)
1281 printf("Mutation: Moving symbol in placement %u\n", i);
1282 move_symbol(&(individual->placements[i]));
1285 acc += conf_mutate_move_bound;
1287 if (x <= acc + conf_mutate_regen_bound)
1289 gen_coords(&(individual->placements[i]));
1292 acc += conf_mutate_regen_bound;
1294 if (x <= acc + conf_mutate_chvar_bound)
1296 struct placement *p = &(individual->placements[i]);
1297 switch (p->request->type)
1300 case REQUEST_SEGMENT:
1302 // Does nothing when there are 0 variants... does it mind?
1303 p->variant_used = randint(0, GARY_SIZE(p->request->variants) - 1);
1305 case REQUEST_SECTION:
1306 p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments)-1);
1317 for (int i=0; i<elite_pop_size; i++)
1319 copy_individual(population1[i], population2[pop2_ind++]);
1323 int overlaps(struct placement *p1, struct placement *p2)
1325 if (p1->request->type != REQUEST_POINT &&
1326 p1->request->type != REQUEST_SEGMENT &&
1327 p1->request->type != REQUEST_AREA)
1330 if (p2->request->type != REQUEST_POINT &&
1331 p2->request->type != REQUEST_SEGMENT &&
1332 p2->request->type != REQUEST_AREA)
1335 if (p1->variant_used == -1 || p2->variant_used == -1)
1338 struct variant *v1, *v2;
1340 v1 = &(p1->request->variants[p1->variant_used]);
1341 v2 = &(p2->request->variants[p2->variant_used]);
1343 int p1x = p1->x; int p1y = p1->y;
1344 int p2x = p2->x; int p2y = p2->y;
1347 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
1348 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
1350 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
1351 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
1358 int get_overlap(struct placement *p)
1360 struct map_part **parts = get_map_parts(p);
1364 printf("Placement of request %d seems not to be placed\n", p->request->ind);
1368 struct placement **others;
1371 GARY_INIT_ZERO(planned, num_requests);
1372 planned[p->request->ind] = 1;
1373 GARY_INIT(others, 0);
1375 for (uns i=0; i<GARY_SIZE(parts); i++)
1377 struct map_placement *mp = parts[i]->placement->next;
1380 if (! planned[mp->placement->request->ind])
1382 struct placement **p = GARY_PUSH(others);
1384 planned[mp->placement->request->ind] = true;
1391 for (uns i=0; i<GARY_SIZE(others); i++)
1393 overlap += overlaps(p, others[i]);
1403 int individual_overlap(struct individual *individual)
1407 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1409 overlap += get_overlap(&individual->placements[i]);
1415 double get_distance(struct placement *p)
1417 if (p->variant_used < 0) return 0;
1418 struct variant *v = &p->request->variants[p->variant_used];
1420 double dx, dy, distance;
1421 switch (p->request->type)
1423 case REQUEST_POINT: ;
1424 struct request_point *rp = (struct request_point *) p->request;
1425 dx = rp->x + v->offset_x - p->x;
1426 dy = rp->y + v->offset_y - p->y;
1427 distance = sqrt(dx*dx + dy*dy);
1428 if (dbg_rank >= VERBOSITY_PLACEMENT)
1429 printf("Point placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, rp->x, rp->y);
1431 case REQUEST_AREA: ;
1432 struct request_area *ra = (struct request_area *) p->request;
1433 dx = ra->cx + v->offset_x - p->x;
1434 dy = ra->cy + v->offset_y - p->y;
1435 distance = sqrt(dx*dx + dy*dy);
1436 if (dbg_rank >= VERBOSITY_PLACEMENT)
1437 printf("Area placed at [%.2f; %.2f], expected at [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1443 if (dbg_rank >= VERBOSITY_PLACEMENT)
1444 printf("Placement %d has distance %.2f\n", p->ind, distance);
1448 double individual_distances(struct individual *individual)
1452 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1454 distances += get_distance(&individual->placements[i]);
1460 int cmp_individual(const void *a, const void *b)
1462 struct individual **ia = (struct individual **) a;
1463 struct individual **ib = (struct individual **) b;
1465 return (*ia)->penalty - (*ib)->penalty;
1468 void rank_population(void)
1472 for (int i=0; i<conf_pop_size; i++)
1475 printf("Individual %d\n", i);
1476 population1[i]->penalty = 0;
1478 penalty = individual_overlap(population1[i]);
1480 printf("Increasing penalty by %d for overlap\n", penalty);
1481 population1[i]->penalty += penalty;
1483 penalty = individual_distances(population1[i]);
1485 printf("Increasing penalty by %d for distances\n", penalty);
1486 population1[i]->penalty += penalty;
1490 struct map_part **get_map_parts(struct placement *p)
1492 if (p->variant_used < 0) return NULL;
1494 struct map_part **buffer;
1495 GARY_INIT(buffer, 0);
1498 printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
1501 switch (p->request->type)
1504 case REQUEST_SEGMENT:
1506 v = p->request->variants[p->variant_used];
1512 int x_min = max2(0, p->x) / conf_map_part_width;
1513 // CHECK ME: Is rounding needed?
1514 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
1515 int y_min = max2(0, p->y) / conf_map_part_height;
1516 // CHECK ME: Is rounding needed?
1517 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
1520 printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
1522 for (int y=y_min; y<=y_max; y++)
1523 for (int x=x_min; x<=x_max; x++)
1525 struct map_part **m = GARY_PUSH(buffer);
1527 printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
1528 *m = p->individual->map[y * num_map_parts_row + x];
1534 void update_map_parts(struct placement *p)
1536 struct placement_link *ml = p->map_links;
1539 struct map_placement *mp = ml->mp;
1541 mp->prev->next = mp->next;
1543 mp->next->prev = mp->prev;
1546 struct placement_link *tmp = ml;
1550 p->map_links = NULL;
1552 struct map_part **parts = get_map_parts(p);
1553 if (parts == NULL) return;
1555 for (uns i=0; i<GARY_SIZE(parts); i++)
1557 struct map_placement *mp = malloc(sizeof(struct map_placement));
1560 mp->next = parts[i]->placement->next;
1561 mp->prev = parts[i]->placement;
1562 parts[i]->placement->next = mp;
1563 if (mp->next) mp->next->prev = mp;
1565 struct placement_link *ml = malloc(sizeof(struct placement_link));
1567 ml->next = p->map_links;
1574 void gen_coords(struct placement *p)
1576 switch(p->request->type)
1579 gen_coords_point(p);
1584 case REQUEST_SEGMENT:
1585 gen_coords_segment(p);
1589 printf("Not yet implemented\n");
1593 printf("Testing request type\n");
1594 ASSERT(p->request->type != REQUEST_INVALID);
1597 update_map_parts(p);
1600 double gen_movement(void)
1602 double m = (random() % 100000) / 10000;
1603 m = pow(m, 1.0/3) * flip(1, -1);
1605 printf("Movement %.2f\n", m);
1609 double gen_movement_uniform(void)
1611 return (move_max - move_min) * randdouble() * flip(1, -1);
1614 void gen_coords_point(struct placement *p)
1616 p->x = p->x + gen_movement();
1619 void gen_coords_segment(struct placement *p)
1621 struct request_segment *rs = (struct request_segment *) p->request;
1623 p->x = (a == 1 ? rs->x1 : rs->x2);
1624 p->y = (a == 1 ? rs->y1 : rs->y2);
1627 void gen_coords_area(struct placement *p)
1629 struct request_area *ra = (struct request_area *) p->request;
1631 p->x = p->x + gen_movement();
1632 p->y = p->y + gen_movement();
1635 printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
1638 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
1640 struct map_part **buffer;
1641 GARY_INIT(buffer, 0);
1642 int x_min = symbol->x / conf_part_size;
1643 int x_max = (symbol->x /*+ symbol->bitmap->width*/ + conf_part_size - 1) / conf_part_size;
1644 int y_min = symbol->y / conf_part_size;
1645 int y_max = (symbol->y /*+ symbol->bitmap->height*/ + conf_part_size - 1) / conf_part_size;
1647 for (int x=x_min; x < x_max; x++)
1648 for (int y=y_min; y < y_max; y++)
1650 struct map_part *m = GARY_PUSH(buffer);
1651 *m = individual->map[x][y];
1657 int randint(int min, int max)
1659 if (min == max) return min;
1661 //printf("Returning %d + (%d %% (%d - %d)) = %d + %d %% %d = %d + %d = %d\n", min, r, max, min, min, r, max-min, min, r%(max-min), min+(r%(max-min)));
1662 return min + (r % (max - min));
1663 return (r * (max - min));
1666 struct placement **get_closure(struct placement *placement)
1668 struct placement **closure;
1669 GARY_INIT(closure, 0);
1670 bool *chosen = malloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
1671 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
1672 chosen[placement->request->ind] = 1;
1674 struct placement **p = GARY_PUSH(closure); *p = placement;
1677 while (first < GARY_SIZE(closure))
1679 if (dbg_breeding >= VERBOSITY_ALL)
1680 printf("Iterating, first is %d of current %u\n", first, GARY_SIZE(closure));
1681 struct placement **overlapping = get_overlapping(placement);
1682 if (! overlapping) { first++; continue; }
1684 struct placement **filtered = filter(overlapping, &chosen);
1685 if (dbg_breeding >= VERBOSITY_ALL)
1686 printf("There are %u new overlapping symbols\n", GARY_SIZE(filtered));
1687 GARY_FREE(overlapping);
1688 overlapping = filtered;
1689 for (uns j=0; j<GARY_SIZE(overlapping); j++)
1691 if (! chosen[overlapping[j]->request->ind])
1693 if (overlaps(*p, overlapping[j]))
1695 p = GARY_PUSH(closure); *p = overlapping[j];
1696 if (dbg_breeding >= VERBOSITY_ALL)
1697 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);
1698 chosen[overlapping[j]->request->ind] = 1;
1702 GARY_FREE(overlapping);
1709 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child, bool **processed_ptr)
1711 bool *processed = *processed_ptr;
1712 if (dbg_breeding >= VERBOSITY_ALL)
1713 printf("Will copy %u symbols\n", GARY_SIZE(closure));
1715 for (uns i=0; i<GARY_SIZE(closure); i++)
1717 processed[closure[i]->ind] = 1;
1718 int ind = closure[i]->ind;
1719 child->placements[ind] = parent->placements[ind];
1720 child->placements[ind].processed = 0;
1721 child->placements[ind].map_links = NULL;
1722 update_map_parts(&child->placements[ind]);
1726 void move_symbol(struct placement *p)
1728 switch (p->request->type)
1732 move_symbol_point(p);
1734 case REQUEST_SEGMENT:
1735 move_symbol_segment(p);
1738 ASSERT(p->request->type != REQUEST_INVALID);
1742 void move_symbol_point(struct placement *p)
1744 p->x += gen_movement_uniform();
1745 p->y += gen_movement_uniform();
1748 void move_symbol_segment(struct placement *p)
1750 double m = gen_movement_uniform();
1753 p->y += m * ((struct request_segment *) p->request)->slope;
1756 void hide_segment_labels(struct individual *individual)
1758 // BEWARE: This fully depends on current genetic encoding
1760 int used = -1, num = -1;
1761 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
1763 switch (individual->placements[i].request->type)
1765 case REQUEST_SECTION:
1766 used = individual->placements[i].variant_used;
1769 case REQUEST_SEGMENT:
1771 individual->placements[i].variant_used = 0;
1773 individual->placements[i].variant_used = -1;
1782 void init_placement(struct placement *p, struct individual *individual, struct request *r)
1785 p->ind = num_placements++;
1788 p->x = p->y = 0; // To prevent valgrind from complaining
1789 p->variant_used = 0;
1790 p->map_links = NULL;
1791 p->individual = individual;
1794 case REQUEST_POINT: ;
1795 struct request_point *rp = (struct request_point *) r;
1799 case REQUEST_LINE: ;
1801 case REQUEST_SECTION: ;
1802 struct request_section *rls = (struct request_section *) r;
1803 p->variant_used = randint(0, rls->num_segments);
1805 case REQUEST_SEGMENT: ;
1806 struct request_segment *rs = (struct request_segment *) r;
1810 case REQUEST_AREA: ;
1811 struct request_area *ra = (struct request_area *) r;
1814 p->variant_used = 0;
1817 ASSERT(p->request->type != REQUEST_INVALID);
1818 printf("Valid request: %d\n", p->request->type);
1823 printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
1826 void init_individual(struct individual *i)
1828 //printf("Initing individual\n");
1829 GARY_INIT(i->placements, num_requests);
1830 GARY_INIT(i->map, 0);
1831 for (uns j=0; j<num_map_parts; j++)
1833 struct map_part *part = GARY_PUSH(i->map);
1834 GARY_INIT(part->placement, 0);
1835 struct map_placement *mp = GARY_PUSH(part->placement);
1836 mp->placement = &dummy_placement;
1837 mp->next = mp->prev = NULL;
1839 i->penalty = 0; // FIXME
1842 printf("Individual inited, has %u map parts\n", GARY_SIZE(i->map));
1845 struct placement **get_overlapping(struct placement *p)
1847 struct placement **buffer;
1848 GARY_INIT(buffer, 0);
1850 struct map_part **parts = get_map_parts(p);
1851 if (! parts) return NULL;
1853 for (uns i=0; i<GARY_SIZE(parts); i++)
1855 struct map_placement *mp = parts[i]->placement->next;
1858 if (p->variant_used >= 0)
1860 struct placement **p = GARY_PUSH(buffer);
1869 printf("Returning %u potentially overlapping placements\n", GARY_SIZE(buffer));
1874 struct placement **filter(struct placement **list, bool **pred_ptr)
1876 bool *pred = *pred_ptr; // As GARY can't be passed directly
1877 struct placement **filtered;
1878 GARY_INIT(filtered, 0);
1880 for (uns i=0; i<GARY_SIZE(list); i++)
1882 if (pred[list[i]->request->ind])
1885 struct placement **p = GARY_PUSH(filtered);
1892 int flip(int a, int b)
1894 return (random() % 2 ? a : b);
1897 double randdouble(void)
1899 return ((double) rand() / (double) RAND_MAX);
1905 GARY_FREE(requests_point);
1906 GARY_FREE(requests_line);
1907 GARY_FREE(requests_area);
1910 void copy_individual(struct individual *src, struct individual *dest)
1912 dest->penalty = src->penalty;
1913 GARY_INIT(dest->placements, GARY_SIZE(src->placements));
1914 for (uns i=0; i<GARY_SIZE(src->placements); i++)
1916 dest->placements[i] = src->placements[i];
1917 dest->placements[i].map_links = NULL;
1919 for (uns j=0; j<num_map_parts; j++)
1921 struct map_part *part = GARY_PUSH(dest->map);
1922 GARY_INIT(part->placement, 0);
1923 struct map_placement *mp = GARY_PUSH(part->placement);
1924 mp->placement = &dummy_placement;
1925 mp->next = mp->prev = NULL;