Global: collectd → libsortnetwork
[sort-networks.git] / src / sn-evolution2.c
index 62a0969..c18c30d 100644 (file)
@@ -1,6 +1,6 @@
 /**
- * collectd - src/sn-evolution.c
- * Copyright (C) 2008,2009  Florian octo Forster
+ * libsortnetwork - src/sn-evolution.c
+ * Copyright (C) 2008-2010  Florian octo Forster
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -16,7 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <ff at octo.it>
  **/
 
 #ifndef _ISOC99_SOURCE
@@ -72,6 +72,10 @@ static int evolution_threads_num = 4;
 
 static int do_loop = 0;
 
+static int weight_overall = 50;
+static int weight_fails = 2;
+static int weight_stages = 1;
+
 static void sigint_handler (int signal)
 {
   do_loop++;
@@ -247,7 +251,7 @@ static int rate_network (sn_network_t *n) /* {{{ */
   } /* while (42) */
 
   /* All tests successfull */
-  return (SN_NETWORK_STAGE_NUM (n) + patterns_failed);
+  return (weight_overall + (weight_stages * SN_NETWORK_STAGE_NUM (n)) + (weight_fails * patterns_failed));
 } /* }}} int rate_network */
 
 static sn_comparator_t get_random_comparator (void) /* {{{ */
@@ -265,22 +269,20 @@ static sn_comparator_t get_random_comparator (void) /* {{{ */
 static int mutate_network (sn_comparator_t *comparators, int comparators_num)
 {
   static int old_comparators_num = -1;
-  static int cut_off = 1000000;
+  static double mutate_probability = 0.0;
 
   int i;
 
   if (old_comparators_num != comparators_num)
   {
-    double p;
-
-    p = powl (0.5, (1.0 / ((double) comparators_num)));
-    cut_off = (int) (((double) 1000000) * p);
-
+    /* Probability that NO mutation takes place: 1/3 */
+    mutate_probability = powl (1.0 / 3.0, (1.0 / ((double) comparators_num)));
     old_comparators_num = comparators_num;
   }
 
+  /* `mutate_probability' actually holds 1-p, i. e. it is close to one */
   for (i = 0; i < comparators_num; i++)
-    if (sn_bounded_random (0, 1000000) >= cut_off)
+    if (sn_double_random () >= mutate_probability)
       comparators[i] = get_random_comparator ();
 
   return (0);
@@ -458,7 +460,7 @@ static int evolution_start (int threads_num)
       printf ("Best after approximately %i iterations: "
          "%i comparators in %i stages. Rating: %i (%i not sorted).\n",
          iter, comparators_num, stages_num, rating,
-         rating - stages_num);
+         (rating - (weight_overall + (weight_stages * stages_num))) / weight_fails);
     }
   }