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 #define _ISOC99_SOURCE
23 #define _POSIX_C_SOURCE 200112L
30 #include <sys/types.h>
40 #include "sn_network.h"
41 #include "sn_population.h"
42 #include "sn_random.h"
44 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
46 char *strdup (const char *s);
48 static uint64_t iteration_counter = 0;
49 static int inputs_num = 16;
51 static char *initial_input_file = NULL;
52 static char *best_output_file = NULL;
54 static int stats_interval = 0;
56 static int max_population_size = 128;
57 static sn_population_t *population;
59 static int do_loop = 0;
61 static void sigint_handler (int signal)
64 } /* void sigint_handler */
66 static void exit_usage (const char *name)
68 printf ("Usage: %s [options]\n"
70 "Valid options are:\n"
71 " -i <file> Initial input file (REQUIRED)\n"
72 " -o <file> Write the current best solution to <file>\n"
73 " -p <num> Size of the population (default: 128)\n"
77 } /* void exit_usage */
79 int read_options (int argc, char **argv)
83 while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1)
89 if (initial_input_file != NULL)
90 free (initial_input_file);
91 initial_input_file = strdup (optarg);
97 if (best_output_file != NULL)
98 free (best_output_file);
99 best_output_file = strdup (optarg);
105 int tmp = atoi (optarg);
107 max_population_size = tmp;
113 int tmp = atoi (optarg);
115 stats_interval = tmp;
121 exit_usage (argv[0]);
126 } /* int read_options */
129 static int rate_network (const sn_network_t *n)
134 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
135 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
137 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
138 rate += SN_STAGE_COMP_NUM (s);
142 } /* int rate_network */
146 static int population_print_stats (int iterations)
152 for (i = 0; i < population_size; i++)
154 if ((best == -1) || (best > population[i].rating))
155 best = population[i].rating;
156 total += population[i].rating;
159 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
160 iterations, best, ((double) total) / ((double) population_size));
163 } /* int population_print_stats */
167 static int insert_into_population (sn_network_t *n)
176 rating = rate_network (n);
178 if (population_size < max_population_size)
180 population[population_size].network = n;
181 population[population_size].rating = rating;
189 for (i = 0; i < olymp_size; i++)
191 if (population[i].rating > worst_rating)
193 worst_rating = population[i].rating;
196 if ((population[i].rating < best_rating)
197 || (best_rating == -1))
198 best_rating = population[i].rating;
201 if (rating < best_rating)
203 if (best_output_file != NULL)
205 printf ("Writing network with rating %i to %s\n",
206 rating, best_output_file);
207 sn_network_write_file (n, best_output_file);
211 printf ("New best solution has rating %i\n",
216 nmemb = max_population_size - (worst_index + 1);
218 sn_network_destroy (population[worst_index].network);
219 population[worst_index].network = NULL;
221 memmove (population + worst_index,
222 population + (worst_index + 1),
223 nmemb * sizeof (population_entry_t));
225 population[max_population_size - 1].network = n;
226 population[max_population_size - 1].rating = rating;
229 } /* int insert_into_population */
232 static int create_offspring (void)
238 p0 = sn_population_pop (population);
239 p1 = sn_population_pop (population);
244 /* combine the two parents */
245 n = sn_network_combine (p0, p1);
247 sn_network_destroy (p0);
248 sn_network_destroy (p1);
250 /* randomly chose an input and do a min- or max-cut. */
251 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
254 enum sn_network_cut_dir_e dir;
256 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
257 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
259 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
261 sn_network_cut_at (n, pos, dir);
264 /* compress the network to get a compact representation */
265 sn_network_compress (n);
267 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
269 sn_population_push (population, n);
271 sn_network_destroy (n);
274 } /* int create_offspring */
276 static void *evolution_thread (void *arg)
281 /* XXX: Not synchronized! */
286 } /* int start_evolution */
288 static int evolution_start (int threads_num)
290 pthread_t threads[threads_num]; /* C99 ftw! */
293 for (i = 0; i < threads_num; i++)
297 status = pthread_create (&threads[i], /* attr = */ NULL,
298 evolution_thread, /* arg = */ NULL);
301 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
316 i = iteration_counter;
318 best_rating = sn_population_best_rating (population);
319 printf ("After approximately %i iterations: Currently best rating: %i\n", i, best_rating);
323 for (i = 0; i < threads_num; i++)
327 pthread_join (threads[i], /* value_ptr = */ NULL);
331 } /* int evolution_start */
333 int main (int argc, char **argv)
335 struct sigaction sigint_action;
337 read_options (argc, argv);
338 if (initial_input_file == NULL)
339 exit_usage (argv[0]);
341 memset (&sigint_action, '\0', sizeof (sigint_action));
342 sigint_action.sa_handler = sigint_handler;
343 sigaction (SIGINT, &sigint_action, NULL);
345 population = sn_population_create (max_population_size);
346 if (population == NULL)
348 fprintf (stderr, "sn_population_create failed.\n");
355 n = sn_network_read_file (initial_input_file);
358 printf ("n == NULL\n");
362 inputs_num = SN_NETWORK_INPUT_NUM(n);
364 sn_population_push (population, n);
365 sn_network_destroy (n);
368 printf ("Current configuration:\n"
369 " Initial network: %s\n"
370 " Number of inputs: %3i\n"
371 " Population size: %3i\n"
372 "=======================\n",
373 initial_input_file, inputs_num, max_population_size);
377 printf ("Exiting after %llu iterations.\n",
378 (unsigned long long) iteration_counter);
383 n = sn_population_best (population);
386 if (best_output_file != NULL)
387 sn_network_write_file (n, best_output_file);
390 sn_network_destroy (n);
394 sn_population_destroy (population);
399 /* vim: set shiftwidth=2 softtabstop=2 : */