From da78e9d3abd8a803a5c8d662b8f8f1cbadb3c956 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 10 May 2010 11:50:43 +0200 Subject: [PATCH] population_set_replacement_method: New function. --- src/libpopulation.c | 26 ++++++++++++++++++++++++++ src/population.h | 4 ++++ 2 files changed, 30 insertions(+) diff --git a/src/libpopulation.c b/src/libpopulation.c index fb99db1..c27fd9b 100644 --- a/src/libpopulation.c +++ b/src/libpopulation.c @@ -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; diff --git a/src/population.h b/src/population.h index 8dc7c73..cc513d4 100644 --- a/src/population.h +++ b/src/population.h @@ -35,6 +35,10 @@ int population_set_size (population_t *p, size_t population_size); int population_set_serialization (population_t *p, pi_serialize_f serialize, pi_unserialize_f unserialize); +#define POPULATION_REPLACEMENT_EXPLOIT 1 +#define POPULATION_REPLACEMENT_EXPLORE 2 +int population_set_replacement_method (population_t *p, int method); + #define POPULATION_DEFAULT_PORT "46835" int population_add_peer (population_t *p, const char *node, const char *port); int population_start_listen_thread (population_t *p, -- 2.11.0