2 * collectd - src/sn-evolution.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>
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 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
51 char *strdup (const char *s);
53 static uint64_t iteration_counter = 0;
54 static int inputs_num = 16;
56 static char *initial_input_file = NULL;
57 static char *best_output_file = NULL;
59 static int stats_interval = 0;
61 static int max_population_size = 128;
62 static population_t *population;
64 static int do_loop = 0;
66 static void sigint_handler (int signal)
69 } /* void sigint_handler */
71 static void exit_usage (const char *name)
73 printf ("Usage: %s [options]\n"
75 "Valid options are:\n"
76 " -i <file> Initial input file (REQUIRED)\n"
77 " -o <file> Write the current best solution to <file>\n"
78 " -p <num> Size of the population (default: 128)\n"
82 } /* void exit_usage */
84 int read_options (int argc, char **argv)
88 while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1)
94 if (initial_input_file != NULL)
95 free (initial_input_file);
96 initial_input_file = strdup (optarg);
102 if (best_output_file != NULL)
103 free (best_output_file);
104 best_output_file = strdup (optarg);
110 int tmp = atoi (optarg);
112 max_population_size = tmp;
118 int tmp = atoi (optarg);
120 stats_interval = tmp;
126 exit_usage (argv[0]);
131 } /* int read_options */
133 static int rate_network (const sn_network_t *n)
138 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
139 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
141 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
142 rate += SN_STAGE_COMP_NUM (s);
146 } /* int rate_network */
148 static int mutate_network (sn_network_t *n)
150 sn_network_t *n_copy;
153 int comparator_index;
156 n_copy = sn_network_clone (n);
159 fprintf (stderr, "mutate_network: sn_network_clone failed.\n");
163 stage_index = sn_bounded_random (0, SN_NETWORK_STAGE_NUM (n_copy) - 1);
164 s = SN_NETWORK_STAGE_GET (n_copy, stage_index);
166 comparator_index = sn_bounded_random (0, SN_STAGE_COMP_NUM (s) - 1);
167 sn_stage_comparator_remove (s, comparator_index);
169 status = sn_network_brute_force_check (n_copy);
171 sn_network_destroy (n_copy);
175 else if (status > 0) /* Mutated network does not sort anymore. */
178 /* We saved one comparator \o/ Let's do the same change on the original
180 s = SN_NETWORK_STAGE_GET (n, stage_index);
181 sn_stage_comparator_remove (s, comparator_index);
184 } /* int mutate_network */
186 static int create_offspring (void)
192 p0 = population_get_random (population);
193 p1 = population_get_random (population);
198 /* combine the two parents */
199 n = sn_network_combine (p0, p1);
201 sn_network_destroy (p0);
202 sn_network_destroy (p1);
204 /* randomly chose an input and do a min- or max-cut. */
205 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
208 enum sn_network_cut_dir_e dir;
210 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
211 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
213 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
215 sn_network_cut_at (n, pos, dir);
218 /* compress the network to get a compact representation */
219 sn_network_compress (n);
221 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
223 if (sn_bounded_random (0, 100) <= 1)
226 population_insert (population, n);
228 sn_network_destroy (n);
231 } /* int create_offspring */
233 static void *evolution_thread (void *arg)
238 /* XXX: Not synchronized! */
243 } /* int start_evolution */
245 static int evolution_start (int threads_num)
247 pthread_t threads[threads_num]; /* C99 ftw! */
250 for (i = 0; i < threads_num; i++)
254 status = pthread_create (&threads[i], /* attr = */ NULL,
255 evolution_thread, /* arg = */ NULL);
258 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
275 i = iteration_counter;
277 n = population_get_fittest (population);
278 rating = rate_network (n);
279 sn_network_destroy (n);
281 printf ("After approximately %i iterations: "
282 "Currently best rating: %i\n",
287 for (i = 0; i < threads_num; i++)
291 pthread_join (threads[i], /* value_ptr = */ NULL);
295 } /* int evolution_start */
297 int main (int argc, char **argv)
299 struct sigaction sigint_action;
300 struct sigaction sigterm_action;
302 read_options (argc, argv);
303 if (initial_input_file == NULL)
304 exit_usage (argv[0]);
306 memset (&sigint_action, '\0', sizeof (sigint_action));
307 sigint_action.sa_handler = sigint_handler;
308 sigaction (SIGINT, &sigint_action, NULL);
310 memset (&sigterm_action, '\0', sizeof (sigterm_action));
311 sigterm_action.sa_handler = sigint_handler;
312 sigaction (SIGTERM, &sigterm_action, NULL);
314 population = population_create ((pi_rate_f) rate_network,
315 (pi_copy_f) sn_network_clone,
316 (pi_free_f) sn_network_destroy);
317 if (population == NULL)
319 fprintf (stderr, "population_create failed.\n");
326 n = sn_network_read_file (initial_input_file);
329 printf ("n == NULL\n");
333 inputs_num = SN_NETWORK_INPUT_NUM(n);
335 population_insert (population, n);
336 sn_network_destroy (n);
339 printf ("Current configuration:\n"
340 " Initial network: %s\n"
341 " Number of inputs: %3i\n"
342 " Population size: %3i\n"
343 "=======================\n",
344 initial_input_file, inputs_num, max_population_size);
348 printf ("Exiting after %llu iterations.\n",
349 (unsigned long long) iteration_counter);
354 n = population_get_fittest (population);
357 if (best_output_file != NULL)
358 sn_network_write_file (n, best_output_file);
361 sn_network_destroy (n);
365 population_destroy (population);
370 /* vim: set shiftwidth=2 softtabstop=2 : */