2 * libevolve - src/evolve.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
37 #define _POSIX_C_SOURCE 200112L
39 /* Single UNIX needed for strdup. */
44 #define _XOPEN_SOURCE 500
61 #include "population.h"
79 /* Callback functions */
85 size_t individuals_num;
89 * Constructor and destructor
91 population_t *population_create (pi_rate_f rate, pi_copy_f copy, /* {{{ */
96 p = (population_t *) malloc (sizeof (population_t));
100 memset (p, 0, sizeof (*p));
101 pthread_mutex_init (&p->lock, /* attr = */ NULL);
107 p->individuals = (void **) malloc (32 * sizeof (void *));
108 if (p->individuals == NULL)
113 memset (p->individuals, 0, 32 * sizeof (void *));
114 p->individuals_num = 32;
117 } /* }}} population_t *population_create */
119 void population_destroy (population_t *p) /* {{{ */
124 if (p->individuals_num > 0)
128 for (i = 0; i < p->individuals_num; i++)
130 p->free (p->individuals[i]);
131 p->individuals[i] = NULL;
134 free (p->individuals);
135 p->individuals = NULL;
136 p->individuals_num = 0;
139 memset (p, 0, sizeof (*p));
141 } /* }}} void population_destroy */
143 int population_set_size (population_t *p, /* {{{ */
144 size_t population_size)
152 pthread_mutex_lock (&p->lock);
154 if (p->individuals_num == population_size)
156 pthread_mutex_unlock (&p->lock);
160 for (i = population_size; i < p->individuals_num; i++)
162 p->free (p->individuals[i]);
163 p->individuals[i] = NULL;
166 temp = (void **) realloc (p->individuals, population_size * sizeof (void *));
169 pthread_mutex_unlock (&p->lock);
172 p->individuals = temp;
174 for (i = p->individuals_num; i < population_size; i++)
175 p->individuals[i] = NULL;
177 p->individuals_num = population_size;
179 pthread_mutex_unlock (&p->lock);
184 void *population_get_random (population_t *p) /* {{{ */
189 pthread_mutex_lock (&p->lock);
191 if (p->individuals_num < 1)
193 pthread_mutex_unlock (&p->lock);
199 i = (size_t) (((double) p->individuals_num)
200 * (rand() / (RAND_MAX + 1.0)));
201 if (p->individuals[i] == NULL)
204 ret = p->copy (p->individuals[i]);
207 pthread_mutex_unlock (&p->lock);
210 } /* }}} void *population_pick_random */
212 void *population_get_fittest (population_t *p) /* {{{ */
221 pthread_mutex_lock (&p->lock);
223 best_rating = INT_MAX;
224 for (i = 0; i < p->individuals_num; i++)
228 if (p->individuals[i] == NULL)
231 rating = p->rate (p->individuals[i]);
232 if (rating < best_rating)
236 temp = p->copy (p->individuals[i]);
242 best_rating = rating;
247 pthread_mutex_unlock (&p->lock);
250 } /* }}} void *population_get_fittest */
252 int population_insert (population_t *p, void *pi_orig) /* {{{ */
265 pi = p->copy (pi_orig);
268 fprintf (stderr, "population_insert: p->copy failed.\n");
272 pi_rating = p->rate (pi);
274 pthread_mutex_lock (&p->lock);
276 if (p->individuals_num <= 0)
278 pthread_mutex_unlock (&p->lock);
283 num_tries = (int) ceil (log (p->individuals_num) / log (2.0));
284 for (i = 0; i < num_tries; i++)
289 j = (size_t) (((double) p->individuals_num) * (rand() / (RAND_MAX + 1.0)));
291 if (p->individuals[j] == NULL)
293 p->individuals[j] = pi;
298 j_rating = p->rate (p->individuals[j]);
300 if (pi_rating < j_rating)
304 temp = p->individuals[j];
305 p->individuals[j] = pi;
308 pi_rating = j_rating;
312 pthread_mutex_unlock (&p->lock);
321 } /* }}} int population_insert */
323 /* vim: set sw=2 sts=2 et fdm=marker : */