population_set_replacement_method: New function.
[libpopulation.git] / src / libpopulation.c
index fb99db1..c27fd9b 100644 (file)
@@ -107,6 +107,7 @@ struct population_s
 
 #define POPULATION_FLAG_LISTEN   0x01
 #define POPULATION_FLAG_SHUTDOWN 0x02
+#define POPULATION_FLAG_EXPLORE  0x10
   int flags;
   pthread_t listen_thread_id;
 
@@ -489,6 +490,27 @@ int population_set_serialization (population_t *p, /* {{{ */
   return (0);
 } /* }}} int population_set_serialization */
 
+int population_set_replacement_method (population_t *p, int method) /* {{{ */
+{
+  int status = 0;
+
+  if (p == NULL)
+    return (EINVAL);
+
+  pthread_mutex_lock (&p->lock);
+
+  if (method == POPULATION_REPLACEMENT_EXPLOIT)
+    p->flags &= ~POPULATION_FLAG_EXPLORE;
+  else if (method == POPULATION_REPLACEMENT_EXPLORE)
+    p->flags |= POPULATION_FLAG_EXPLORE;
+  else
+    status = EINVAL;
+
+  pthread_mutex_unlock (&p->lock);
+
+  return (0);
+} /* }}} int population_set_replacement_method */
+
 int population_add_peer (population_t *p, const char *node, /* {{{ */
     const char *port)
 {
@@ -747,6 +769,10 @@ int population_insert (population_t *p, void *pi_orig) /* {{{ */
 
     chance = (int) (((double) (chance_j + chance_pi))
         * (rand() / (RAND_MAX + 1.0)));
+
+    if (p->flags & POPULATION_FLAG_EXPLORE)
+      chance *= .5;
+
     if (chance < chance_j) /* j looses ;) */
     {
       void *temp0;