sn-evolution: Use the `libpopulation' library instead of `sn_population'.
[sort-networks.git] / src / sn-evolution.c
index af48642..64da702 100644 (file)
@@ -41,8 +41,9 @@
 
 #include <pthread.h>
 
+#include <population.h>
+
 #include "sn_network.h"
-#include "sn_population.h"
 #include "sn_random.h"
 
 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
@@ -58,7 +59,7 @@ static char *best_output_file = NULL;
 static int stats_interval = 0;
 
 static int max_population_size = 128;
-static sn_population_t *population;
+static population_t *population;
 
 static int do_loop = 0;
 
@@ -129,7 +130,6 @@ int read_options (int argc, char **argv)
   return (0);
 } /* int read_options */
 
-#if 0
 static int rate_network (const sn_network_t *n)
 {
   int rate;
@@ -144,94 +144,44 @@ static int rate_network (const sn_network_t *n)
 
   return (rate);
 } /* int rate_network */
-#endif
 
-#if 0
-static int population_print_stats (int iterations)
+static int mutate_network (sn_network_t *n)
 {
-  int best = -1;
-  int total = 0;
-  int i;
-
-  for (i = 0; i < population_size; i++)
+  sn_network_t *n_copy;
+  int stage_index;
+  sn_stage_t *s;
+  int comparator_index;
+  int status;
+
+  n_copy = sn_network_clone (n);
+  if (n_copy == NULL)
   {
-    if ((best == -1) || (best > population[i].rating))
-      best = population[i].rating;
-    total += population[i].rating;
+    fprintf (stderr, "mutate_network: sn_network_clone failed.\n");
+    return (-1);
   }
 
-  printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
-      iterations, best, ((double) total) / ((double) population_size));
+  stage_index = sn_bounded_random (0, SN_NETWORK_STAGE_NUM (n_copy) - 1);
+  s = SN_NETWORK_STAGE_GET (n_copy, stage_index);
 
-  return (0);
-} /* int population_print_stats */
-#endif
-
-#if 0
-static int insert_into_population (sn_network_t *n)
-{
-  int rating;
-  int worst_rating;
-  int worst_index;
-  int best_rating;
-  int nmemb;
-  int i;
+  comparator_index = sn_bounded_random (0, SN_STAGE_COMP_NUM (s) - 1);
+  sn_stage_comparator_remove (s, comparator_index);
 
-  rating = rate_network (n);
-
-  if (population_size < max_population_size)
-  {
-    population[population_size].network = n;
-    population[population_size].rating  = rating;
-    population_size++;
-    return (0);
-  }
-
-  worst_rating = -1;
-  worst_index  = -1;
-  best_rating  = -1;
-  for (i = 0; i < olymp_size; i++)
-  {
-    if (population[i].rating > worst_rating)
-    {
-      worst_rating = population[i].rating;
-      worst_index  = i;
-    }
-    if ((population[i].rating < best_rating)
-       || (best_rating == -1))
-      best_rating = population[i].rating;
-  }
+  status = sn_network_brute_force_check (n_copy);
+  
+  sn_network_destroy (n_copy);
 
-  if (rating < best_rating)
-  {
-    if (best_output_file != NULL)
-    {
-      printf ("Writing network with rating %i to %s\n",
-         rating, best_output_file);
-      sn_network_write_file (n, best_output_file);
-    }
-    else
-    {
-      printf ("New best solution has rating %i\n",
-         rating);
-    }
-  }
-
-  nmemb = max_population_size - (worst_index + 1);
-
-  sn_network_destroy (population[worst_index].network);
-  population[worst_index].network = NULL;
-
-  memmove (population + worst_index,
-      population + (worst_index + 1),
-      nmemb * sizeof (population_entry_t));
+  if (status < 0)
+    return (-1);
+  else if (status > 0) /* Mutated network does not sort anymore. */
+    return (1);
 
-  population[max_population_size - 1].network = n;
-  population[max_population_size - 1].rating  = rating;
+  /* We saved one comparator \o/ Let's do the same change on the original
+   * network. */
+  s = SN_NETWORK_STAGE_GET (n, stage_index);
+  sn_stage_comparator_remove (s, comparator_index);
 
   return (0);
-} /* int insert_into_population */
-#endif
+} /* int mutate_network */
 
 static int create_offspring (void)
 {
@@ -239,8 +189,8 @@ static int create_offspring (void)
   sn_network_t *p1;
   sn_network_t *n;
 
-  p0 = sn_population_pop (population);
-  p1 = sn_population_pop (population);
+  p0 = population_get_random (population);
+  p1 = population_get_random (population);
 
   assert (p0 != NULL);
   assert (p1 != NULL);
@@ -270,7 +220,10 @@ static int create_offspring (void)
 
   assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
 
-  sn_population_push (population, n);
+  if (sn_bounded_random (0, 100) <= 1)
+    mutate_network (n);
+
+  population_insert (population, n);
 
   sn_network_destroy (n);
 
@@ -316,11 +269,18 @@ static int evolution_start (int threads_num)
     status = sleep (1);
     if (status == 0)
     {
-      int best_rating;
+      sn_network_t *n;
+      int rating;
+
       i = iteration_counter;
 
-      best_rating = sn_population_best_rating (population);
-      printf ("After approximately %i iterations: Currently best rating: %i\n", i, best_rating);
+      n = population_get_fittest (population);
+      rating = rate_network (n);
+      sn_network_destroy (n);
+
+      printf ("After approximately %i iterations: "
+         "Currently best rating: %i\n",
+         i, rating);
     }
   }
 
@@ -337,6 +297,7 @@ static int evolution_start (int threads_num)
 int main (int argc, char **argv)
 {
   struct sigaction sigint_action;
+  struct sigaction sigterm_action;
 
   read_options (argc, argv);
   if (initial_input_file == NULL)
@@ -346,10 +307,16 @@ int main (int argc, char **argv)
   sigint_action.sa_handler = sigint_handler;
   sigaction (SIGINT, &sigint_action, NULL);
 
-  population = sn_population_create (max_population_size);
+  memset (&sigterm_action, '\0', sizeof (sigterm_action));
+  sigterm_action.sa_handler = sigint_handler;
+  sigaction (SIGTERM, &sigterm_action, NULL);
+
+  population = population_create ((pi_rate_f) rate_network,
+      (pi_copy_f) sn_network_clone,
+      (pi_free_f) sn_network_destroy);
   if (population == NULL)
   {
-    fprintf (stderr, "sn_population_create failed.\n");
+    fprintf (stderr, "population_create failed.\n");
     return (1);
   }
 
@@ -365,7 +332,7 @@ int main (int argc, char **argv)
 
     inputs_num = SN_NETWORK_INPUT_NUM(n);
 
-    sn_population_push (population, n);
+    population_insert (population, n);
     sn_network_destroy (n);
   }
 
@@ -384,7 +351,7 @@ int main (int argc, char **argv)
   {
     sn_network_t *n;
 
-    n = sn_population_best (population);
+    n = population_get_fittest (population);
     if (n != NULL)
     {
       if (best_output_file != NULL)
@@ -395,7 +362,7 @@ int main (int argc, char **argv)
     }
   }
 
-  sn_population_destroy (population);
+  population_destroy (population);
 
   return (0);
 } /* int main */