]> mj.ucw.cz Git - leo.git/blobdiff - labeller.c
Labelling: Variants may have offset
[leo.git] / labeller.c
index 7465f86cefb976c07965542a757e77f86cbb6c92..345b70cee807a813adbd80afcaff491b891291bf 100644 (file)
@@ -48,6 +48,11 @@ int dbg_graph = 0;
 int dbg_bfs = 0;
 int dbg_map_parts = 0;
 int dbg_movement = 0;
+int dbg_init = 0;
+int dbg_overlaps = 0;
+int dbg_rank = 0;
+int dbg_evolution = 0;
+int dbg_mutation = 0;
 
 int page_width_int;
 int page_height_int;
@@ -70,16 +75,20 @@ int conf_breed_pop_size_perc = 20;
 int conf_breed_perc = 50;                      // Percentage of new pop created by breeding
 
 bool conf_mutate_children = 1;
-int conf_mutate_children_prob = 0.3;
+double conf_mutate_children_prob = 0.3;
 
-int conf_mutate_rbest_perc = 60;
-int conf_mutate_pop_size_perc = 20;
+double conf_mutate_rbest = 1;
+double conf_mutate_pop_size = 0.9;
 
-int conf_mutate_move_bound = 0.2;
-int conf_mutate_regen_bound = 0.1;
-int conf_mutate_chvar_bound = 0.1;
+double conf_mutate_move_bound = 1.0;
+double conf_mutate_regen_bound = 0.0;
+double conf_mutate_chvar_bound = 0.0;
 
-int conf_elite_perc = 5;
+int mutate_pop_size;
+int mutate_rbest_size;
+
+double conf_elite_pop_size = 0.1;
+int elite_pop_size;
 
 double conf_max_section_length = 100;
 double conf_max_section_overlay = 10;
@@ -91,9 +100,10 @@ int pop2_ind;
 int conf_part_size = 50;
 
 int move_min = 0;
-int move_max = 1;
+int move_max = 5;
 
 int num_requests = 0;
+int num_placements = 0;
 
 int conf_map_part_width = 5;
 int conf_map_part_height = 5;
@@ -107,6 +117,10 @@ void join_edge(struct graph_edge *e, int dir);
 void bfs(uns longline);
 void make_segments(void);
 
+int overlaps(struct placement *p1, struct placement *p2);
+int get_overlap(struct placement *p);
+int individual_overlap(struct individual *individual);
+
 void make_population(void);
 bool shall_terminate(void);
 void breed(void);
@@ -115,6 +129,8 @@ void elite(void);
 void rank_population(void);
 void plan_individual(struct individual *individual);
 
+int cmp_individual(const void *a, const void *b);
+
 void make_bitmap(struct variant *v, struct symbol *sym);
 void make_bitmap_icon(struct variant *v, struct sym_icon *si);
 void make_bitmap_point(struct variant *v, struct sym_point *sp);
@@ -135,8 +151,10 @@ void dump_longlines(void);
 void dump_linelabel_requests(void);
 void dump_individual(struct individual *individual);
 void print_label(struct symbol *sym);
+void dump_penalties(struct individual **population);
 
 double gen_movement(void);
+double gen_movement_uniform(void);
 void gen_coords(struct placement *p);
 void gen_coords_point(struct placement *p);
 void gen_coords_segment(struct placement *p);
@@ -163,6 +181,7 @@ struct placement **get_closure(struct placement *placement, struct individual *p
 void copy_symbols(struct placement **closure, struct individual *parent, struct individual *child);
 void move_symbol(struct placement *p);
 void move_symbol_point(struct placement *p);
+void move_symbol_segment(struct placement *p);
 
 struct placement **get_overlapping(struct placement *p);
 void filter(struct placement **list, bool *pred);
@@ -233,6 +252,8 @@ void labeller_init(void)
 
 void make_bitmap(struct variant *v, struct symbol *sym)
 {
+  v->offset_x = v->offset_y = 0;
+
   switch (sym->type)
   {
     case SYMBOLIZER_POINT:
@@ -251,16 +272,16 @@ void make_bitmap(struct variant *v, struct symbol *sym)
 
 void make_bitmap_icon(struct variant *v, struct sym_icon *si)
 {
-  v->width = si->sir.icon->width;
-  v->height = si->sir.icon->height;
-  v->bitmap = malloc((int) ceil(v->width * v->height * sizeof(bool)));
+  v->width = si->sir.width + 1;
+  v->height = si->sir.height + 1;
+  v->bitmap = malloc(v->width * v->height * sizeof(bool));
   for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
 }
 
 void make_bitmap_point(struct variant *v, struct sym_point *sp)
 {
-  v->width = v->height = sp->size;
-  v->bitmap = malloc(sp->size*sp->size * sizeof(bool));
+  v->width = v->height = sp->size + 1;
+  v->bitmap = malloc(v->width * v->height * sizeof(bool));
   // FIXME: Okay, memset would be much nicer here
   for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
 }
@@ -279,7 +300,8 @@ void make_bitmap_label(struct variant *v, struct sym_text *text)
 
 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
 {
-printf("Adding point\n");
+  if (dbg_requests)
+    printf("Adding point\n");
   if (object->type != OSM_TYPE_NODE)
   {
     printf("Warning: Point label requested on non-point object\n");
@@ -305,20 +327,14 @@ printf("Adding point\n");
   struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
   r->x = n->x;
   r->y = n->y;
+  make_bitmap(v, sym);
   switch (sym->type)
   {
     case SYMBOLIZER_ICON:
-      make_bitmap_icon(v, (struct sym_icon *) sym);
+      // FIXME: Really?
       r->x = ((struct sym_icon *)sym)->sir.x;
       r->y = ((struct sym_icon *)sym)->sir.y;
       break;
-    case SYMBOLIZER_POINT:
-      make_bitmap_point(v, (struct sym_point *) sym);
-      break;
-    case SYMBOLIZER_TEXT: ;
-      struct sym_text *st = (struct sym_text *) sym;
-      struct osm_node *n = (struct osm_node *) object;
-      make_bitmap_label(v, st);
     default:
       // FIXME
       return;
@@ -345,7 +361,8 @@ void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t
     return;
   }
 
-  printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
+  if (dbg_requests)
+    printf("[LAB] Labelling way %ju on %u\n", o->id, zindex);
   struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
   ll->way = (struct osm_way *) o;
   ll->label = sym;
@@ -354,7 +371,8 @@ void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t
 
 void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
 {
-printf("Adding area on %u\n", zindex);
+  if (dbg_requests)
+    printf("Adding area on %u\n", zindex);
   struct request_area *r = GARY_PUSH(requests_area);
 
   r->request.type = REQUEST_AREA;
@@ -368,19 +386,7 @@ printf("Adding area on %u\n", zindex);
 
   GARY_INIT(r->request.variants, 0);
   struct variant *v = GARY_PUSH(r->request.variants);
-  switch (sym->type)
-  {
-    case SYMBOLIZER_ICON:
-      printf("DEBUG: Icon label\n");
-      make_bitmap_icon(v, (struct sym_icon *) sym);
-      break;
-    case SYMBOLIZER_TEXT:
-      printf("DEBUG: Text label\n");
-      make_bitmap_label(v, (struct sym_text *) sym);
-    default:
-      // FIXME
-      ;
-  }
+  make_bitmap(v, sym);
 }
 
 void make_graph(void)
@@ -680,6 +686,7 @@ struct request_line *make_new_line(void)
   rl->request.ind = num_requests++;
   rl->request.type = REQUEST_LINE;
   GARY_INIT(rl->sections, 0);
+  GARY_INIT(rl->request.variants, 0);
 
   return rl;
 }
@@ -691,6 +698,7 @@ struct request_section *make_new_section(struct request_line *rl)
   rls->request.type = REQUEST_SECTION;
   rls->num_segments = 0;
   GARY_INIT(rls->segments, 0);
+  GARY_INIT(rls->request.variants, 0);
 
   return rls;
 }
@@ -703,9 +711,9 @@ struct request_segment *make_new_segment(struct request_section *rls, struct sym
   rs->request.ind = num_requests++;
   rs->request.type = REQUEST_SEGMENT;
 
-  struct variant *v = malloc(sizeof(struct variant));
+  GARY_INIT(rs->request.variants, 0);
+  struct variant *v = GARY_PUSH(rs->request.variants);
   make_bitmap(v, sym);
-  rs->request.variants = v;
 
   return rs;
 }
@@ -812,8 +820,7 @@ void make_segments(void)
       rs->x2 = e->n2->o->x;
       rs->y2 = e->n2->o->y;
 
-      // FIXME: Set text rotation
-      rs->angle = atan2(rs->x2 - rs->x1, rs->y2 - rs->y1);
+      rs->slope = (rs->y2 - rs->y1) / (rs->x2 - rs->x1);
       rs->zindex = e->zindex;
 
       cur_length += e->length;
@@ -873,7 +880,6 @@ void dump_bitmaps(struct individual *individual)
 
   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
   {
-fprintf(stderr, "%d-th placement\n", i);
     if (individual->placements[i].variant_used == -1) continue;
 
     struct placement *p = &(individual->placements[i]);
@@ -887,19 +893,14 @@ fprintf(stderr, "%d-th placement\n", i);
         v = &(p->request->variants[p->variant_used]);
         break;
       default:
-        printf("Testing request type (dump_bitmaps): %d\n", p->request->type);
         ASSERT(p->request->type != REQUEST_INVALID);
         continue;
     }
 
-    printf("Got after with %d-th placement of request type %d\n", i, p->request->type);
-
-    printf("Rendering %d-th label %d x %d (w x h)\n", i, v->width, v->height);
     for (int row = max2(p->y, 0); row < min2(p->y + v->height, page_height_int); row++)
     {
       for (int col = max2(p->x, 0); col < min2(p->x + v->width, page_width_int); col++)
       {
-        printf("Writing to %d\n", row*page_width_int + col);
         bitmap[row * page_width_int + col] = 1;
       }
     }
@@ -1021,7 +1022,14 @@ if (dbg_plan)
         ASSERT(s->type != SYMBOLIZER_INVALID);
     }
   }
+}
 
+void dump_penalties(struct individual **population)
+{
+  for (int i=0; i<conf_pop_size; i++)
+  {
+    printf("Individual %d has penalty %d\n", i, population[i]->penalty);
+  }
 }
 
 void labeller_label(void)
@@ -1036,20 +1044,53 @@ printf("Having %u point requests, %u line requests and %u area requests\n", GARY
   GARY_INIT(population1, conf_pop_size);
   GARY_INIT(population2, conf_pop_size);
   make_population();
+  rank_population();
+  qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
+
+  if (dbg_evolution)
+    dump_penalties(population1);
 
   printf("Dealing with %d requests\n", num_requests);
 
-/*
+  mutate_pop_size = conf_mutate_pop_size * conf_pop_size;
+  mutate_rbest_size = conf_mutate_rbest * conf_pop_size;
+  if (dbg_evolution)
+  {
+    printf("Mutation parameters:\n");
+    printf(" %d individuals are created\n", mutate_pop_size);
+    printf(" %d best individuals in old population are considered\n", mutate_rbest_size);
+  }
+
+  elite_pop_size = conf_elite_pop_size * conf_pop_size;
+  if (dbg_evolution)
+  {
+    printf("Elitism parameters:\n");
+    printf(" %d best individuals are copied\n", elite_pop_size);
+  }
+
   while (! shall_terminate())
   {
     iteration++;
+    if (dbg_evolution)
+      printf("*** Iteration %d ***\n", iteration);
+
+    mutate();
+    elite();
 
     struct individual **swp = population1;
     population1 = population2;
     population2 = swp;
     pop2_ind = 0;
+
+    if (dbg_evolution)
+      dump_penalties(population1);
+
+    rank_population();
+    qsort(population1, conf_pop_size, sizeof(struct individual *), cmp_individual);
+
+    if (dbg_evolution)
+      dump_penalties(population1);
   }
-*/
 
   plan_individual(population1[0]);
 
@@ -1066,7 +1107,13 @@ void make_population(void)
 {
   for (int i=0; i<conf_pop_size; i++)
   {
-    printf("Making individual %d\n", i);
+    num_placements = 0; // FIXME: This IS a terrible HACK
+    struct individual *i2 = ep_alloc(ep_individuals);
+    init_individual(i2);
+    population2[i] = i2;
+
+    if (dbg_init)
+      printf("Making individual %d\n", i);
     struct individual *individual = ep_alloc(ep_individuals); init_individual(individual);
     population1[i] = individual;
 
@@ -1199,12 +1246,16 @@ struct individual **perform_crossover(struct individual *parent1, struct individ
 
 void mutate(void)
 {
-  int i = 0;
-  int conf_mutate_pop_size = conf_mutate_pop_size_perc * conf_pop_size;
-  while (i < conf_mutate_rbest_perc * conf_pop_size)
+  for (int i=0; i < mutate_pop_size; i++)
   {
-    int ind = randint(1, conf_mutate_pop_size);
-    copy_individual(population2[pop2_ind], population1[ind]);
+    if (dbg_mutation)
+      printf("%d\n", i);
+    int ind = randint(1, mutate_rbest_size);
+    if (dbg_mutation)
+      printf("Mutating %d-th individual of original population\n", ind);
+    copy_individual(population1[ind], population2[pop2_ind]);
+    if (dbg_mutation)
+      printf("Individual %d in pop2 inited from individual %d in pop1\n", pop2_ind, ind);
     perform_mutation(population2[pop2_ind]);
     pop2_ind++;
   }
@@ -1214,11 +1265,13 @@ void perform_mutation(struct individual *individual)
 {
   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
   {
-    int x = randint(1, 1000);
-    int acc = 0;
+    double x = randdouble();
+    double acc = 0;
 
     if (x <= acc + conf_mutate_move_bound)
     {
+      if (dbg_mutation)
+        printf("Mutation: Moving symbol in placement %u\n", i);
       move_symbol(&(individual->placements[i]));
       continue;
     }
@@ -1233,9 +1286,20 @@ void perform_mutation(struct individual *individual)
 
     if (x <= acc + conf_mutate_chvar_bound)
     {
-      if (0) // if num_variants > 1
+      struct placement *p = &(individual->placements[i]);
+      switch (p->request->type)
       {
-        // FIXME: assign new variant
+        case REQUEST_POINT:
+        case REQUEST_SEGMENT:
+        case REQUEST_AREA:
+          // Does nothing when there are 0 variants... does it mind?
+          p->variant_used = randint(0, GARY_SIZE(p->request->variants) - 1);
+          break;
+        case REQUEST_SECTION:
+          p->variant_used = randint(0, GARY_SIZE(((struct request_section *) p->request)->segments)-1);
+          break;
+        default:
+          ;
       }
     }
   }
@@ -1243,15 +1307,127 @@ void perform_mutation(struct individual *individual)
 
 void elite(void)
 {
-  for (int i=0; i<conf_elite_perc * conf_pop_size; i++)
+  for (int i=0; i<elite_pop_size; i++)
   {
-    population2[pop2_ind++] = population1[0];
+    copy_individual(population1[i], population2[pop2_ind++]);
   }
 }
 
+int overlaps(struct placement *p1, struct placement *p2)
+{
+  if (p1->request->type != REQUEST_POINT &&
+      p1->request->type != REQUEST_SEGMENT &&
+      p1->request->type != REQUEST_AREA)
+    return 0;
+
+  if (p2->request->type != REQUEST_POINT &&
+      p2->request->type != REQUEST_SEGMENT &&
+      p2->request->type != REQUEST_AREA)
+    return 0;
+
+  if (p1->variant_used == -1 || p2->variant_used == -1)
+    return 0;
+
+  struct variant *v1, *v2;
+
+  v1 = &(p1->request->variants[p1->variant_used]);
+  v2 = &(p2->request->variants[p2->variant_used]);
+
+  int p1x = p1->x; int p1y = p1->y;
+  int p2x = p2->x; int p2y = p2->y;
+
+  int overlap = 0;
+  for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
+    for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
+    {
+      if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
+          v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
+        overlap++;
+    }
+
+  return overlap;
+}
+
+int get_overlap(struct placement *p)
+{
+  struct map_part **parts = get_map_parts(p);
+  if (! parts)
+  {
+    if (dbg_overlaps)
+      printf("Placement of request %d seems not to be placed\n", p->request->ind);
+    return 0;
+  }
+
+  struct placement **others;
+  bool *planned;
+
+  GARY_INIT_ZERO(planned, num_requests);
+  planned[p->request->ind] = 1;
+  GARY_INIT(others, 0);
+
+  for (uns i=0; i<GARY_SIZE(parts); i++)
+  {
+    struct map_placement *mp = parts[i]->placement->next;
+    while (mp)
+    {
+      if (! planned[mp->placement->request->ind])
+      {
+        struct placement **p = GARY_PUSH(others);
+        *p = mp->placement;
+        planned[mp->placement->request->ind] = true;
+      }
+      mp = mp->next;
+    }
+  }
+
+  int overlap = 0;
+  for (uns i=0; i<GARY_SIZE(others); i++)
+  {
+    overlap += overlaps(p, others[i]);
+  }
+
+  GARY_FREE(planned);
+  GARY_FREE(parts);
+  GARY_FREE(others);
+
+  return overlap;
+}
+
+int individual_overlap(struct individual *individual)
+{
+  int overlap = 0;
+
+  for (uns i=0; i<GARY_SIZE(individual->placements); i++)
+  {
+    overlap += get_overlap(&individual->placements[i]);
+  }
+
+  return overlap;
+}
+
+int cmp_individual(const void *a, const void *b)
+{
+  struct individual **ia = (struct individual **) a;
+  struct individual **ib = (struct individual **) b;
+
+  return (*ia)->penalty - (*ib)->penalty;
+}
+
 void rank_population(void)
 {
-  // FIXME
+  int penalty;
+
+  for (int i=0; i<conf_pop_size; i++)
+  {
+    if (dbg_rank)
+      printf("Individual %d\n", i);
+    population1[i]->penalty = 0;
+
+    penalty = individual_overlap(population1[i]);
+    if (dbg_rank)
+      printf("Increasing penalty by %d for overlap\n", penalty);
+    population1[i]->penalty += penalty;
+  }
 }
 
 struct map_part **get_map_parts(struct placement *p)
@@ -1302,7 +1478,8 @@ void update_map_parts(struct placement *p)
   while (ml)
   {
     struct map_placement *mp = ml->mp;
-    mp->prev = mp->next;
+
+    mp->prev->next = mp->next;
     if (mp->next)
       mp->next->prev = mp->prev;
     free(mp);
@@ -1311,6 +1488,7 @@ void update_map_parts(struct placement *p)
     ml = ml->next;
     free(tmp);
   }
+  p->map_links = NULL;
 
   struct map_part **parts = get_map_parts(p);
   if (parts == NULL) return;
@@ -1321,6 +1499,7 @@ void update_map_parts(struct placement *p)
     mp->placement = p;
 
     mp->next = parts[i]->placement->next;
+    mp->prev = parts[i]->placement;
     parts[i]->placement->next = mp;
     if (mp->next) mp->next->prev = mp;
 
@@ -1361,13 +1540,18 @@ void gen_coords(struct placement *p)
 
 double gen_movement(void)
 {
-  double m = (random() % 1000000) / 10000;
+  double m = (random() % 100000) / 10000;
   m = pow(m, 1.0/3) * flip(1, -1);
   if (dbg_movement)
     printf("Movement %.2f\n", m);
   return m;
 }
 
+double gen_movement_uniform(void)
+{
+  return (move_max - move_min) * randdouble() * flip(1, -1);
+}
+
 void gen_coords_point(struct placement *p)
 {
   p->x = p->x + gen_movement();
@@ -1388,7 +1572,8 @@ void gen_coords_area(struct placement *p)
   p->x = p->x + gen_movement();
   p->y = p->y + gen_movement();
 
-  printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
+  if (dbg_movement)
+    printf("Moved label to [%.2f; %.2f] from [%.2f; %.2f]\n", p->x, p->y, ra->cx, ra->cy);
 }
 
 struct map_part **get_parts(struct placement *symbol, struct individual *individual)
@@ -1464,12 +1649,12 @@ void move_symbol(struct placement *p)
   switch (p->request->type)
   {
     case REQUEST_POINT:
+    case REQUEST_AREA:
       move_symbol_point(p);
-    case REQUEST_LINE:
+      break;
     case REQUEST_SEGMENT:
-    case REQUEST_AREA:
-      if (dbg_movement)
-        printf("Not yet implemented\n");
+      move_symbol_segment(p);
+      break;
     default:
       ASSERT(p->request->type != REQUEST_INVALID);
   }
@@ -1477,8 +1662,16 @@ void move_symbol(struct placement *p)
 
 void move_symbol_point(struct placement *p)
 {
-  p->x += (double) (move_min + randdouble()) * flip(1, -1);
-  p->y += (double) (move_min + randdouble()) * flip(1, -1);
+  p->x += gen_movement_uniform();
+  p->y += gen_movement_uniform();
+}
+
+void move_symbol_segment(struct placement *p)
+{
+  double m = gen_movement_uniform();
+  // CHECK ME
+  p->x += m;
+  p->y += m * ((struct request_segment *) p->request)->slope;
 }
 
 void hide_segment_labels(struct individual *individual)
@@ -1510,6 +1703,7 @@ void hide_segment_labels(struct individual *individual)
 void init_placement(struct placement *p, struct individual *individual, struct request *r)
 {
   // FIXME
+  p->ind = num_placements++;
   p->request = r;
   p->processed = 0;
   p->x = p->y = 0; // To prevent valgrind from complaining
@@ -1546,7 +1740,8 @@ void init_placement(struct placement *p, struct individual *individual, struct r
   }
 
   gen_coords(p);
-//  printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
+  if (dbg_init)
+    printf("Inited placement to [%.2f; %.2f]\n", p->x, p->y);
 }
 
 void init_individual(struct individual *i)
@@ -1563,6 +1758,9 @@ void init_individual(struct individual *i)
     mp->next = mp->prev = NULL;
   }
   i->penalty = 0; // FIXME
+
+  if (dbg_init)
+    printf("Individual inited, has %u map parts\n", GARY_SIZE(i->map));
 }
 
 struct placement **get_overlapping(struct placement *p UNUSED)
@@ -1584,8 +1782,7 @@ int flip(int a, int b)
 
 double randdouble(void)
 {
-  // FIXME: How the hell shall double in range <0, 1> be generated? O:)
-  return 0.5;
+  return ((double) rand() / (double) RAND_MAX);
 }
 
 void cleanup(void)
@@ -1598,10 +1795,19 @@ void cleanup(void)
 
 void copy_individual(struct individual *src, struct individual *dest)
 {
-  src->penalty = dest->penalty;
+  dest->penalty = src->penalty;
   GARY_INIT(dest->placements, GARY_SIZE(src->placements));
   for (uns i=0; i<GARY_SIZE(src->placements); i++)
   {
     dest->placements[i] = src->placements[i];
+    dest->placements[i].map_links = NULL;
+  }
+  for (uns j=0; j<num_map_parts; j++)
+  {
+    struct map_part *part = GARY_PUSH(dest->map);
+    GARY_INIT(part->placement, 0);
+    struct map_placement *mp = GARY_PUSH(part->placement);
+    mp->placement = &dummy_placement;
+    mp->next = mp->prev = NULL;
   }
 }