2 * collectd - src/sn-evolution.c
3 * Copyright (C) 2008-2010 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 <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
34 #include <sys/types.h>
44 #include <population.h>
46 #include "sn_network.h"
47 #include "sn_random.h"
49 #if !defined(__GNUC__) || !__GNUC__
50 # define __attribute__(x) /**/
53 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
55 char *strdup (const char *s);
57 static uint64_t iteration_counter = 0;
58 static int inputs_num = 16;
59 static int inputs_num_is_power_of_two = 1;
61 static char *initial_input_file = NULL;
62 static char *best_output_file = NULL;
64 static int stats_interval = 0;
66 static int max_population_size = 128;
67 static population_t *population;
69 static int evolution_threads_num = 4;
71 static int do_loop = 0;
73 static void sigint_handler (int signal __attribute__((unused)))
76 } /* void sigint_handler */
78 static void exit_usage (const char *name)
80 printf ("Usage: %s [options]\n"
82 "Valid options are:\n"
83 " -i <file> Initial input file (REQUIRED)\n"
84 " -o <file> Write the current best solution to <file>\n"
85 " -p <num> Size of the population (default: 128)\n"
86 " -P <peer> Send individuals to <peer> (may be repeated)\n"
87 " -t <num> Number of threads (default: 4)\n"
91 } /* void exit_usage */
93 int read_options (int argc, char **argv)
97 while ((option = getopt (argc, argv, "i:o:p:P:s:t:h")) != -1)
103 if (initial_input_file != NULL)
104 free (initial_input_file);
105 initial_input_file = strdup (optarg);
111 if (best_output_file != NULL)
112 free (best_output_file);
113 best_output_file = strdup (optarg);
119 int tmp = atoi (optarg);
122 max_population_size = tmp;
123 population_set_size (population, (size_t) max_population_size);
132 status = population_add_peer (population, optarg, /* port = */ NULL);
135 fprintf (stderr, "population_add_peer failed with status %i.\n",
143 int tmp = atoi (optarg);
145 stats_interval = tmp;
151 int tmp = atoi (optarg);
153 evolution_threads_num = tmp;
159 exit_usage (argv[0]);
164 } /* int read_options */
166 static int rate_network (const sn_network_t *n)
171 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
172 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
174 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
175 rate += SN_STAGE_COMP_NUM (s);
179 } /* int rate_network */
181 static int mutate_network (sn_network_t *n)
183 sn_network_t *n_copy;
186 int comparator_index;
189 n_copy = sn_network_clone (n);
192 fprintf (stderr, "mutate_network: sn_network_clone failed.\n");
196 stage_index = sn_bounded_random (0, SN_NETWORK_STAGE_NUM (n_copy) - 1);
197 s = SN_NETWORK_STAGE_GET (n_copy, stage_index);
199 comparator_index = sn_bounded_random (0, SN_STAGE_COMP_NUM (s) - 1);
200 sn_stage_comparator_remove (s, comparator_index);
202 status = sn_network_brute_force_check (n_copy);
204 sn_network_destroy (n_copy);
208 else if (status > 0) /* Mutated network does not sort anymore. */
211 /* We saved one comparator \o/ Let's do the same change on the original
213 s = SN_NETWORK_STAGE_GET (n, stage_index);
214 sn_stage_comparator_remove (s, comparator_index);
217 } /* int mutate_network */
219 static int create_offspring (void)
225 p0 = population_get_random (population);
226 p1 = population_get_random (population);
231 /* combine the two parents */
232 n = sn_network_combine (p0, p1, inputs_num_is_power_of_two);
234 sn_network_destroy (p0);
235 sn_network_destroy (p1);
237 /* randomly chose an input and do a min- or max-cut. */
238 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
241 enum sn_network_cut_dir_e dir;
243 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
244 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
246 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
248 sn_network_cut_at (n, pos, dir);
251 /* compress the network to get a compact representation */
252 sn_network_compress (n);
254 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
256 if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1))
259 population_insert (population, n);
261 sn_network_destroy (n);
264 } /* int create_offspring */
266 static void *evolution_thread (void *arg __attribute__((unused)))
271 /* XXX: Not synchronized! */
276 } /* int start_evolution */
278 static int evolution_start (int threads_num)
280 pthread_t threads[threads_num]; /* C99 ftw! */
283 for (i = 0; i < threads_num; i++)
287 status = pthread_create (&threads[i], /* attr = */ NULL,
288 evolution_thread, /* arg = */ NULL);
291 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
311 iter = iteration_counter;
313 n = population_get_fittest (population);
315 rating = rate_network (n);
317 stages_num = SN_NETWORK_STAGE_NUM (n);
319 for (i = 0; i < stages_num; i++)
323 s = SN_NETWORK_STAGE_GET (n, i);
324 comparators_num += SN_STAGE_COMP_NUM (s);
327 sn_network_destroy (n);
329 printf ("Best after approximately %i iterations: "
330 "%i comparators in %i stages. Rating: %i.\n",
331 iter, comparators_num, stages_num, rating);
335 for (i = 0; i < threads_num; i++)
339 pthread_join (threads[i], /* value_ptr = */ NULL);
343 } /* int evolution_start */
345 int main (int argc, char **argv)
347 struct sigaction sigint_action;
348 struct sigaction sigterm_action;
350 population = population_create ((pi_rate_f) rate_network,
351 (pi_copy_f) sn_network_clone,
352 (pi_free_f) sn_network_destroy);
353 if (population == NULL)
355 fprintf (stderr, "population_create failed.\n");
359 population_set_serialization (population,
360 (pi_serialize_f) sn_network_serialize,
361 (pi_unserialize_f) sn_network_unserialize);
363 read_options (argc, argv);
364 if (initial_input_file == NULL)
365 exit_usage (argv[0]);
367 memset (&sigint_action, '\0', sizeof (sigint_action));
368 sigint_action.sa_handler = sigint_handler;
369 sigaction (SIGINT, &sigint_action, NULL);
371 memset (&sigterm_action, '\0', sizeof (sigterm_action));
372 sigterm_action.sa_handler = sigint_handler;
373 sigaction (SIGTERM, &sigterm_action, NULL);
375 population_start_listen_thread (population, NULL, NULL);
381 n = sn_network_read_file (initial_input_file);
384 printf ("n == NULL\n");
388 inputs_num = SN_NETWORK_INPUT_NUM(n);
390 /* Determine if `inputs_num' is a power of two. If so, more merge
391 * algorithms can be used. */
393 inputs_num_is_power_of_two = 1;
399 inputs_num_is_power_of_two = 1;
401 inputs_num_is_power_of_two = 0;
407 population_insert (population, n);
408 sn_network_destroy (n);
411 printf ("Current configuration:\n"
412 " Initial network: %s\n"
413 " Number of inputs: %3i\n"
414 " Population size: %3i\n"
415 "=======================\n",
416 initial_input_file, inputs_num, max_population_size);
418 evolution_start (evolution_threads_num);
420 printf ("Exiting after %llu iterations.\n",
421 (unsigned long long) iteration_counter);
426 n = population_get_fittest (population);
429 if (best_output_file != NULL)
430 sn_network_write_file (n, best_output_file);
432 sn_network_destroy (n);
436 population_destroy (population);
441 /* vim: set shiftwidth=2 softtabstop=2 : */