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 */
91 /* Optional serialization */
92 pi_serialize_f serialize;
93 pi_unserialize_f unserialize;
97 individual_t *individuals;
98 size_t individuals_num;
102 * Constructor and destructor
104 population_t *population_create (pi_rate_f rate, pi_copy_f copy, /* {{{ */
110 p = (population_t *) malloc (sizeof (population_t));
114 memset (p, 0, sizeof (*p));
115 pthread_mutex_init (&p->lock, /* attr = */ NULL);
121 p->fittest.ptr = NULL;
122 p->fittest.rating = -1;
124 p->individuals = malloc (32 * sizeof (p->individuals[0]));
125 if (p->individuals == NULL)
130 memset (p->individuals, 0, 32 * sizeof (p->individuals[0]));
131 p->individuals_num = 32;
133 for (i = 0; i < p->individuals_num; i++)
135 p->individuals[i].ptr = NULL;
136 p->individuals[i].rating = -1;
140 } /* }}} population_t *population_create */
142 void population_destroy (population_t *p) /* {{{ */
147 if (p->fittest.ptr != NULL)
148 p->free (p->fittest.ptr);
149 p->fittest.ptr = NULL;
150 p->fittest.rating = -1;
152 if (p->individuals_num > 0)
156 for (i = 0; i < p->individuals_num; i++)
158 if (p->individuals[i].ptr != NULL)
159 p->free (p->individuals[i].ptr);
160 p->individuals[i].ptr = NULL;
161 p->individuals[i].rating = -1;
164 free (p->individuals);
165 p->individuals = NULL;
166 p->individuals_num = 0;
169 memset (p, 0, sizeof (*p));
171 } /* }}} void population_destroy */
173 int population_set_size (population_t *p, /* {{{ */
174 size_t population_size)
182 pthread_mutex_lock (&p->lock);
184 if (p->individuals_num == population_size)
186 pthread_mutex_unlock (&p->lock);
190 for (i = population_size; i < p->individuals_num; i++)
192 p->free (p->individuals[i].ptr);
193 p->individuals[i].ptr = NULL;
194 p->individuals[i].rating = -1;
197 temp = (individual_t *) realloc (p->individuals,
198 population_size * sizeof (p->individuals[0]));
201 pthread_mutex_unlock (&p->lock);
204 p->individuals = temp;
206 for (i = p->individuals_num; i < population_size; i++)
208 p->individuals[i].ptr = NULL;
209 p->individuals[i].rating = -1;
212 p->individuals_num = population_size;
214 pthread_mutex_unlock (&p->lock);
219 int population_set_serialization (population_t *p,
220 pi_serialize_f serialize, pi_unserialize_f unserialize) /* {{{ */
225 pthread_mutex_lock (&p->lock);
227 p->serialize = serialize;
228 p->unserialize = unserialize;
230 pthread_mutex_unlock (&p->lock);
232 } /* }}} int population_set_serialization */
234 void *population_get_random (population_t *p) /* {{{ */
239 pthread_mutex_lock (&p->lock);
241 if (p->individuals_num < 1)
243 pthread_mutex_unlock (&p->lock);
249 i = (size_t) (((double) p->individuals_num)
250 * (rand() / (RAND_MAX + 1.0)));
251 if (p->individuals[i].ptr == NULL)
254 ret = p->copy (p->individuals[i].ptr);
257 pthread_mutex_unlock (&p->lock);
260 } /* }}} void *population_pick_random */
262 void *population_get_fittest (population_t *p) /* {{{ */
269 pthread_mutex_lock (&p->lock);
271 if (p->fittest.ptr == NULL)
273 pthread_mutex_unlock (&p->lock);
277 ret = p->copy (p->fittest.ptr);
279 pthread_mutex_unlock (&p->lock);
282 } /* }}} void *population_get_fittest */
284 int population_insert (population_t *p, void *pi_orig) /* {{{ */
297 pi = p->copy (pi_orig);
300 fprintf (stderr, "population_insert: p->copy failed.\n");
304 pi_rating = p->rate (pi);
306 pthread_mutex_lock (&p->lock);
308 /* Keep track of the all time best. */
309 if ((p->fittest.ptr == NULL) || (p->fittest.rating > pi_rating))
316 if (p->fittest.ptr != NULL)
317 p->free (p->fittest.ptr);
318 p->fittest.ptr = temp;
319 p->fittest.rating = pi_rating;
323 if (p->individuals_num <= 0)
325 pthread_mutex_unlock (&p->lock);
330 num_tries = (int) ceil (log (p->individuals_num) / log (2.0));
331 for (i = 0; i < num_tries; i++)
335 j = (size_t) (((double) p->individuals_num) * (rand() / (RAND_MAX + 1.0)));
337 if (p->individuals[j].ptr == NULL)
339 p->individuals[j].ptr = pi;
340 p->individuals[j].rating = pi_rating;
345 if (pi_rating < p->individuals[j].rating)
350 temp0 = p->individuals[j].ptr;
351 p->individuals[j].ptr = pi;
354 temp1 = p->individuals[j].rating;
355 p->individuals[j].rating = pi_rating;
360 pthread_mutex_unlock (&p->lock);
369 } /* }}} int population_insert */
371 /* vim: set sw=2 sts=2 et fdm=marker : */