]> mj.ucw.cz Git - leo.git/blobdiff - labeller.c
Labelling: Variants may have offset
[leo.git] / labeller.c
index f0ddeffe26febbabb4cf8b079daf9947a1b4f3b9..345b70cee807a813adbd80afcaff491b891291bf 100644 (file)
@@ -51,6 +51,8 @@ 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;
@@ -73,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;
@@ -94,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;
@@ -144,6 +151,7 @@ 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);
@@ -244,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:
@@ -317,20 +327,14 @@ void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t
   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;
@@ -382,21 +386,7 @@ void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t
 
   GARY_INIT(r->request.variants, 0);
   struct variant *v = GARY_PUSH(r->request.variants);
-  switch (sym->type)
-  {
-    case SYMBOLIZER_ICON:
-      if (dbg_requests)
-        printf("DEBUG: Icon label\n");
-      make_bitmap_icon(v, (struct sym_icon *) sym);
-      break;
-    case SYMBOLIZER_TEXT:
-      if (dbg_requests)
-        printf("DEBUG: Text label\n");
-      make_bitmap_label(v, (struct sym_text *) sym);
-    default:
-      // FIXME
-      ;
-  }
+  make_bitmap(v, sym);
 }
 
 void make_graph(void)
@@ -830,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;
@@ -1033,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)
@@ -1048,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]);
 
@@ -1078,6 +1107,7 @@ void make_population(void)
 {
   for (int i=0; i<conf_pop_size; i++)
   {
+    num_placements = 0; // FIXME: This IS a terrible HACK
     struct individual *i2 = ep_alloc(ep_individuals);
     init_individual(i2);
     population2[i] = i2;
@@ -1216,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++;
   }
@@ -1231,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;
     }
@@ -1250,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:
+          ;
       }
     }
   }
@@ -1260,9 +1307,9 @@ 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++]);
   }
 }
 
@@ -1493,7 +1540,7 @@ 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);
@@ -1656,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