sn-evolution: Add the "-m" option.
[sort-networks.git] / src / sn-evolution.c
index 2f3fba3..9f63506 100644 (file)
 # define _ISOC99_SOURCE
 #endif
 #ifndef _POSIX_C_SOURCE
-# define _POSIX_C_SOURCE 200112L
+# define _POSIX_C_SOURCE 200809L
+#endif
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 700
 #endif
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
+#include <strings.h>
 
 #include <sys/types.h>
 #include <sys/stat.h>
 # define __attribute__(x) /**/
 #endif
 
-/* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
- * */
-char *strdup (const char *s);
-
 static uint64_t iteration_counter = 0;
 static int inputs_num = 16;
 static int inputs_num_is_power_of_two = 1;
@@ -66,6 +66,13 @@ static int stats_interval = 0;
 static int max_population_size = 128;
 static population_t *population;
 
+static enum 
+{
+  MERGER_ODDEVEN,
+  MERGER_BITONIC,
+  MERGER_RANDOM
+} selected_merger = MERGER_ODDEVEN;
+
 static int evolution_threads_num = 4;
 
 static int do_loop = 0;
@@ -85,6 +92,8 @@ static void exit_usage (const char *name)
       "  -p <num>      Size of the population (default: 128)\n"
       "  -P <peer>     Send individuals to <peer> (may be repeated)\n"
       "  -t <num>      Number of threads (default: 4)\n"
+      "  -m <merger>   Specify the merging network to use.\n"
+      "                Available: \"oddeven\", \"bitonic\", \"random\"\n"
       "\n",
       name);
   exit (1);
@@ -94,7 +103,7 @@ int read_options (int argc, char **argv)
 {
   int option;
 
-  while ((option = getopt (argc, argv, "i:o:p:P:s:t:h")) != -1)
+  while ((option = getopt (argc, argv, "i:o:p:P:s:t:m:h")) != -1)
   {
     switch (option)
     {
@@ -154,6 +163,19 @@ int read_options (int argc, char **argv)
        break;
       }
 
+      case 'm':
+      {
+       if (strcasecmp ("oddeven", optarg) == 0)
+         selected_merger = MERGER_ODDEVEN;
+       else if (strcasecmp ("bitonic", optarg) == 0)
+         selected_merger = MERGER_BITONIC;
+       else if (strcasecmp ("random", optarg) == 0)
+         selected_merger = MERGER_RANDOM;
+       else
+         fprintf (stderr, "Not a valid merging strategy: \"%s\"\n", optarg);
+       break;
+      }
+
       case 'h':
       default:
        exit_usage (argv[0]);
@@ -178,6 +200,7 @@ static int rate_network (const sn_network_t *n)
   return (rate);
 } /* int rate_network */
 
+#if 0
 static int mutate_network (sn_network_t *n)
 {
   sn_network_t *n_copy;
@@ -215,6 +238,7 @@ static int mutate_network (sn_network_t *n)
 
   return (0);
 } /* int mutate_network */
+#endif
 
 static int create_offspring (void)
 {
@@ -229,7 +253,12 @@ static int create_offspring (void)
   assert (p1 != NULL);
 
   /* combine the two parents */
-  n = sn_network_combine (p0, p1, inputs_num_is_power_of_two);
+  if ((selected_merger == MERGER_ODDEVEN)
+      || ((selected_merger == MERGER_RANDOM)
+       && (sn_bounded_random (0, 1) == 0)))
+    n = sn_network_combine_odd_even_merge (p0, p1);
+  else
+    n = sn_network_combine_bitonic_merge (p0, p1);
 
   sn_network_destroy (p0);
   sn_network_destroy (p1);
@@ -253,8 +282,10 @@ static int create_offspring (void)
 
   assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
 
+#if 0
   if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1))
     mutate_network (n);
+#endif
 
   population_insert (population, n);