2 * collectd - src/sn-evolution.c
3 * Copyright (C) 2008,2009 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>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
34 #include <sys/types.h>
46 #include <population.h>
48 #include "sn_network.h"
49 #include "sn_random.h"
51 #define SNE_MIN(a,b) ((a) < (b) ? (a) : (b))
52 #define SNE_MAX(a,b) ((a) > (b) ? (a) : (b))
54 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
56 char *strdup (const char *s);
58 static uint64_t iteration_counter = 0;
59 static int inputs_num = -1;
61 static int comparators_num = -1;
63 static char *initial_input_file = NULL;
64 static char *best_output_file = NULL;
66 static int stats_interval = 0;
68 static int max_population_size = 128;
69 static population_t *population;
71 static int evolution_threads_num = 4;
73 static int do_loop = 0;
75 static void sigint_handler (int signal)
78 } /* void sigint_handler */
80 static void exit_usage (const char *name)
82 printf ("Usage: %s [options]\n"
84 "Valid options are:\n"
85 " -i <inputs> Number of inputs\n"
86 " -c <comparators> Number of comparators\n"
87 " -I <file> Initial input file\n"
88 " -o <file> Write the current best solution to <file>\n"
89 " -p <num> Size of the population (default: 128)\n"
90 " -P <peer> Send individuals to <peer> (may be repeated)\n"
91 " -t <num> Number of threads (default: 4)\n"
95 } /* void exit_usage */
97 int read_options (int argc, char **argv)
101 while ((option = getopt (argc, argv, "i:c:I:o:p:P:s:t:h")) != -1)
119 comparators_num = tmp;
125 if (initial_input_file != NULL)
126 free (initial_input_file);
127 initial_input_file = strdup (optarg);
133 if (best_output_file != NULL)
134 free (best_output_file);
135 best_output_file = strdup (optarg);
141 int tmp = atoi (optarg);
144 max_population_size = tmp;
145 population_set_size (population, (size_t) max_population_size);
154 status = population_add_peer (population, optarg, /* port = */ NULL);
157 fprintf (stderr, "population_add_peer failed with status %i.\n",
165 int tmp = atoi (optarg);
167 stats_interval = tmp;
173 int tmp = atoi (optarg);
175 evolution_threads_num = tmp;
181 exit_usage (argv[0]);
186 } /* int read_options */
188 static int rate_network (sn_network_t *n) /* {{{ */
190 int test_pattern[n->inputs_num];
191 int values[n->inputs_num];
193 int patterns_sorted = 0;
194 int patterns_failed = 0;
196 memset (test_pattern, 0, sizeof (test_pattern));
204 /* Copy the current pattern and let the network sort it */
205 memcpy (values, test_pattern, sizeof (values));
206 status = sn_network_sort (n, values);
210 /* Check if the array is now sorted. */
211 previous = values[0];
213 for (i = 1; i < n->inputs_num; i++)
215 if (previous > values[i])
221 previous = values[i];
227 /* Generate the next test pattern */
229 for (i = 0; i < n->inputs_num; i++)
231 if (test_pattern[i] == 0)
244 /* Break out of the while loop if we tested all possible patterns */
249 /* All tests successfull */
250 return (SN_NETWORK_STAGE_NUM (n) + patterns_failed);
251 } /* }}} int rate_network */
253 static sn_comparator_t get_random_comparator (void) /* {{{ */
257 c.min = sn_bounded_random (0, inputs_num - 1);
259 while (c.max == c.min)
260 c.max = sn_bounded_random (0, inputs_num - 1);
263 } /* }}} sn_comparator_t get_random_comparator */
265 static int mutate_network (sn_comparator_t *comparators, int comparators_num)
267 static int old_comparators_num = -1;
268 static double mutate_probability = 0.0;
272 if (old_comparators_num != comparators_num)
274 /* Probability that NO mutation takes place: 1/3 */
275 mutate_probability = powl (1.0 / 3.0, (1.0 / ((double) comparators_num)));
276 old_comparators_num = comparators_num;
279 /* `mutate_probability' actually holds 1-p, i. e. it is close to one */
280 for (i = 0; i < comparators_num; i++)
281 if (sn_double_random () >= mutate_probability)
282 comparators[i] = get_random_comparator ();
285 } /* int mutate_network */
287 static int create_offspring (void)
290 sn_comparator_t p0_comparators[comparators_num];
291 int p0_comparators_num;
294 sn_comparator_t p1_comparators[comparators_num];
295 int p1_comparators_num;
298 sn_comparator_t n_comparators[comparators_num];
299 int n_comparators_num;
303 p0 = population_get_random (population);
304 p1 = population_get_random (population);
309 p0_comparators_num = 0;
310 for (i = 0; i < SN_NETWORK_STAGE_NUM (p0); i++)
315 s = SN_NETWORK_STAGE_GET (p0, i);
317 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
321 c = SN_STAGE_COMP_GET (s, j);
322 p0_comparators[p0_comparators_num] = *c;
323 p0_comparators_num++;
326 while (p0_comparators_num < comparators_num)
328 p0_comparators[p0_comparators_num] = get_random_comparator ();
329 p0_comparators_num++;
331 sn_network_destroy (p0);
334 p1_comparators_num = 0;
335 for (i = 0; i < SN_NETWORK_STAGE_NUM (p1); i++)
340 s = SN_NETWORK_STAGE_GET (p1, i);
342 for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
346 c = SN_STAGE_COMP_GET (s, j);
347 p1_comparators[p1_comparators_num] = *c;
348 p1_comparators_num++;
351 while (p1_comparators_num < comparators_num)
353 p1_comparators[p1_comparators_num] = get_random_comparator ();
354 p1_comparators_num++;
356 sn_network_destroy (p1);
359 n_comparators_num = sn_bounded_random (SNE_MIN (p0_comparators_num, p1_comparators_num),
360 SNE_MAX (p0_comparators_num, p1_comparators_num));
362 for (i = 0; i < n_comparators_num; i++)
364 if (i >= p0_comparators_num)
365 n_comparators[i] = p1_comparators[i];
366 else if (i >= p1_comparators_num)
367 n_comparators[i] = p0_comparators[i];
368 else if (sn_bounded_random (0, 1) == 0)
369 n_comparators[i] = p1_comparators[i];
371 n_comparators[i] = p0_comparators[i];
375 mutate_network (n_comparators, n_comparators_num);
377 n = sn_network_create (inputs_num);
378 for (i = 0; i < n_comparators_num; i++)
379 sn_network_comparator_add (n, &n_comparators[i]);
381 /* compress the network to get a compact representation */
382 sn_network_compress (n);
384 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
386 population_insert (population, n);
388 sn_network_destroy (n);
391 } /* int create_offspring */
393 static void *evolution_thread (void *arg)
398 /* XXX: Not synchronized! */
403 } /* int start_evolution */
405 static int evolution_start (int threads_num)
407 pthread_t threads[threads_num]; /* C99 ftw! */
410 for (i = 0; i < threads_num; i++)
414 status = pthread_create (&threads[i], /* attr = */ NULL,
415 evolution_thread, /* arg = */ NULL);
418 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
438 iter = iteration_counter;
440 n = population_get_fittest (population);
442 rating = rate_network (n);
444 stages_num = SN_NETWORK_STAGE_NUM (n);
446 for (i = 0; i < stages_num; i++)
450 s = SN_NETWORK_STAGE_GET (n, i);
451 comparators_num += SN_STAGE_COMP_NUM (s);
454 sn_network_destroy (n);
456 printf ("Best after approximately %i iterations: "
457 "%i comparators in %i stages. Rating: %i (%i not sorted).\n",
458 iter, comparators_num, stages_num, rating,
459 rating - stages_num);
463 for (i = 0; i < threads_num; i++)
467 pthread_join (threads[i], /* value_ptr = */ NULL);
471 } /* int evolution_start */
473 int main (int argc, char **argv)
475 struct sigaction sigint_action;
476 struct sigaction sigterm_action;
478 population = population_create ((pi_rate_f) rate_network,
479 (pi_copy_f) sn_network_clone,
480 (pi_free_f) sn_network_destroy);
481 if (population == NULL)
483 fprintf (stderr, "population_create failed.\n");
487 population_set_serialization (population,
488 (pi_serialize_f) sn_network_serialize,
489 (pi_unserialize_f) sn_network_unserialize);
491 read_options (argc, argv);
493 if ((inputs_num <= 0) || (comparators_num <= 0))
494 exit_usage (argv[0]);
496 memset (&sigint_action, '\0', sizeof (sigint_action));
497 sigint_action.sa_handler = sigint_handler;
498 sigaction (SIGINT, &sigint_action, NULL);
500 memset (&sigterm_action, '\0', sizeof (sigterm_action));
501 sigterm_action.sa_handler = sigint_handler;
502 sigaction (SIGTERM, &sigterm_action, NULL);
504 population_start_listen_thread (population, NULL, NULL);
506 if (initial_input_file != NULL)
510 n = sn_network_read_file (initial_input_file);
513 fprintf (stderr, "Cannot read network from `%s'.\n",
518 if (n->inputs_num != inputs_num)
520 fprintf (stderr, "Network `%s' has %i inputs, but %i were configured "
521 "on the command line.\n",
522 initial_input_file, n->inputs_num, inputs_num);
526 population_insert (population, n);
527 sn_network_destroy (n);
529 else /* if (initial_input_file == NULL) */
534 n = sn_network_create (inputs_num);
535 for (i = 0; i < comparators_num; i++)
539 c = get_random_comparator ();
540 sn_network_comparator_add (n, &c);
543 population_insert (population, n);
544 sn_network_destroy (n);
547 printf ("Current configuration:\n"
548 " Number of inputs: %3i\n"
549 " Number of comparators: %3i\n"
550 " Population size: %3i\n"
551 "=======================\n",
552 inputs_num, comparators_num, max_population_size);
554 evolution_start (evolution_threads_num);
556 printf ("Exiting after %llu iterations.\n",
557 (unsigned long long) iteration_counter);
562 n = population_get_fittest (population);
565 if (best_output_file != NULL)
566 sn_network_write_file (n, best_output_file);
568 sn_network_destroy (n);
572 population_destroy (population);
577 /* vim: set shiftwidth=2 softtabstop=2 : */