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"
80 typedef struct individual_s individual_t;
86 /* Callback functions */
93 individual_t *individuals;
94 size_t individuals_num;
98 * Constructor and destructor
100 population_t *population_create (pi_rate_f rate, pi_copy_f copy, /* {{{ */
106 p = (population_t *) malloc (sizeof (population_t));
110 memset (p, 0, sizeof (*p));
111 pthread_mutex_init (&p->lock, /* attr = */ NULL);
117 p->fittest.ptr = NULL;
118 p->fittest.rating = -1;
120 p->individuals = malloc (32 * sizeof (p->individuals[0]));
121 if (p->individuals == NULL)
126 memset (p->individuals, 0, 32 * sizeof (p->individuals[0]));
127 p->individuals_num = 32;
129 for (i = 0; i < p->individuals_num; i++)
131 p->individuals[i].ptr = NULL;
132 p->individuals[i].rating = -1;
136 } /* }}} population_t *population_create */
138 void population_destroy (population_t *p) /* {{{ */
143 if (p->fittest.ptr != NULL)
144 p->free (p->fittest.ptr);
145 p->fittest.ptr = NULL;
146 p->fittest.rating = -1;
148 if (p->individuals_num > 0)
152 for (i = 0; i < p->individuals_num; i++)
154 if (p->individuals[i].ptr != NULL)
155 p->free (p->individuals[i].ptr);
156 p->individuals[i].ptr = NULL;
157 p->individuals[i].rating = -1;
160 free (p->individuals);
161 p->individuals = NULL;
162 p->individuals_num = 0;
165 memset (p, 0, sizeof (*p));
167 } /* }}} void population_destroy */
169 int population_set_size (population_t *p, /* {{{ */
170 size_t population_size)
178 pthread_mutex_lock (&p->lock);
180 if (p->individuals_num == population_size)
182 pthread_mutex_unlock (&p->lock);
186 for (i = population_size; i < p->individuals_num; i++)
188 p->free (p->individuals[i].ptr);
189 p->individuals[i].ptr = NULL;
190 p->individuals[i].rating = -1;
193 temp = (individual_t *) realloc (p->individuals,
194 population_size * sizeof (p->individuals[0]));
197 pthread_mutex_unlock (&p->lock);
200 p->individuals = temp;
202 for (i = p->individuals_num; i < population_size; i++)
204 p->individuals[i].ptr = NULL;
205 p->individuals[i].rating = -1;
208 p->individuals_num = population_size;
210 pthread_mutex_unlock (&p->lock);
215 void *population_get_random (population_t *p) /* {{{ */
220 pthread_mutex_lock (&p->lock);
222 if (p->individuals_num < 1)
224 pthread_mutex_unlock (&p->lock);
230 i = (size_t) (((double) p->individuals_num)
231 * (rand() / (RAND_MAX + 1.0)));
232 if (p->individuals[i].ptr == NULL)
235 ret = p->copy (p->individuals[i].ptr);
238 pthread_mutex_unlock (&p->lock);
241 } /* }}} void *population_pick_random */
243 void *population_get_fittest (population_t *p) /* {{{ */
250 pthread_mutex_lock (&p->lock);
252 if (p->fittest.ptr == NULL)
254 pthread_mutex_unlock (&p->lock);
258 ret = p->copy (p->fittest.ptr);
260 pthread_mutex_unlock (&p->lock);
263 } /* }}} void *population_get_fittest */
265 int population_insert (population_t *p, void *pi_orig) /* {{{ */
278 pi = p->copy (pi_orig);
281 fprintf (stderr, "population_insert: p->copy failed.\n");
285 pi_rating = p->rate (pi);
287 pthread_mutex_lock (&p->lock);
289 /* Keep track of the all time best. */
290 if ((p->fittest.ptr == NULL) || (p->fittest.rating > pi_rating))
297 if (p->fittest.ptr != NULL)
298 p->free (p->fittest.ptr);
299 p->fittest.ptr = temp;
300 p->fittest.rating = pi_rating;
304 if (p->individuals_num <= 0)
306 pthread_mutex_unlock (&p->lock);
311 num_tries = (int) ceil (log (p->individuals_num) / log (2.0));
312 for (i = 0; i < num_tries; i++)
316 j = (size_t) (((double) p->individuals_num) * (rand() / (RAND_MAX + 1.0)));
318 if (p->individuals[j].ptr == NULL)
320 p->individuals[j].ptr = pi;
321 p->individuals[j].rating = pi_rating;
326 if (pi_rating < p->individuals[j].rating)
331 temp0 = p->individuals[j].ptr;
332 p->individuals[j].ptr = pi;
335 temp1 = p->individuals[j].rating;
336 p->individuals[j].rating = pi_rating;
341 pthread_mutex_unlock (&p->lock);
350 } /* }}} int population_insert */
352 /* vim: set sw=2 sts=2 et fdm=marker : */