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 "sn_network.h"
45 #include "sn_population.h"
46 #include "sn_random.h"
48 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
50 char *strdup (const char *s);
52 static uint64_t iteration_counter = 0;
53 static int inputs_num = 16;
55 static char *initial_input_file = NULL;
56 static char *best_output_file = NULL;
58 static int stats_interval = 0;
60 static int max_population_size = 128;
61 static sn_population_t *population;
63 static int do_loop = 0;
65 static void sigint_handler (int signal)
68 } /* void sigint_handler */
70 static void exit_usage (const char *name)
72 printf ("Usage: %s [options]\n"
74 "Valid options are:\n"
75 " -i <file> Initial input file (REQUIRED)\n"
76 " -o <file> Write the current best solution to <file>\n"
77 " -p <num> Size of the population (default: 128)\n"
81 } /* void exit_usage */
83 int read_options (int argc, char **argv)
87 while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1)
93 if (initial_input_file != NULL)
94 free (initial_input_file);
95 initial_input_file = strdup (optarg);
101 if (best_output_file != NULL)
102 free (best_output_file);
103 best_output_file = strdup (optarg);
109 int tmp = atoi (optarg);
111 max_population_size = tmp;
117 int tmp = atoi (optarg);
119 stats_interval = tmp;
125 exit_usage (argv[0]);
130 } /* 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 */
150 static int population_print_stats (int iterations)
156 for (i = 0; i < population_size; i++)
158 if ((best == -1) || (best > population[i].rating))
159 best = population[i].rating;
160 total += population[i].rating;
163 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
164 iterations, best, ((double) total) / ((double) population_size));
167 } /* int population_print_stats */
171 static int insert_into_population (sn_network_t *n)
180 rating = rate_network (n);
182 if (population_size < max_population_size)
184 population[population_size].network = n;
185 population[population_size].rating = rating;
193 for (i = 0; i < olymp_size; i++)
195 if (population[i].rating > worst_rating)
197 worst_rating = population[i].rating;
200 if ((population[i].rating < best_rating)
201 || (best_rating == -1))
202 best_rating = population[i].rating;
205 if (rating < best_rating)
207 if (best_output_file != NULL)
209 printf ("Writing network with rating %i to %s\n",
210 rating, best_output_file);
211 sn_network_write_file (n, best_output_file);
215 printf ("New best solution has rating %i\n",
220 nmemb = max_population_size - (worst_index + 1);
222 sn_network_destroy (population[worst_index].network);
223 population[worst_index].network = NULL;
225 memmove (population + worst_index,
226 population + (worst_index + 1),
227 nmemb * sizeof (population_entry_t));
229 population[max_population_size - 1].network = n;
230 population[max_population_size - 1].rating = rating;
233 } /* int insert_into_population */
236 static int create_offspring (void)
242 p0 = sn_population_pop (population);
243 p1 = sn_population_pop (population);
248 /* combine the two parents */
249 n = sn_network_combine (p0, p1);
251 sn_network_destroy (p0);
252 sn_network_destroy (p1);
254 /* randomly chose an input and do a min- or max-cut. */
255 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
258 enum sn_network_cut_dir_e dir;
260 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
261 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
263 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
265 sn_network_cut_at (n, pos, dir);
268 /* compress the network to get a compact representation */
269 sn_network_compress (n);
271 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
273 sn_population_push (population, n);
275 sn_network_destroy (n);
278 } /* int create_offspring */
280 static void *evolution_thread (void *arg)
285 /* XXX: Not synchronized! */
290 } /* int start_evolution */
292 static int evolution_start (int threads_num)
294 pthread_t threads[threads_num]; /* C99 ftw! */
297 for (i = 0; i < threads_num; i++)
301 status = pthread_create (&threads[i], /* attr = */ NULL,
302 evolution_thread, /* arg = */ NULL);
305 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
320 i = iteration_counter;
322 best_rating = sn_population_best_rating (population);
323 printf ("After approximately %i iterations: Currently best rating: %i\n", i, best_rating);
327 for (i = 0; i < threads_num; i++)
331 pthread_join (threads[i], /* value_ptr = */ NULL);
335 } /* int evolution_start */
337 int main (int argc, char **argv)
339 struct sigaction sigint_action;
341 read_options (argc, argv);
342 if (initial_input_file == NULL)
343 exit_usage (argv[0]);
345 memset (&sigint_action, '\0', sizeof (sigint_action));
346 sigint_action.sa_handler = sigint_handler;
347 sigaction (SIGINT, &sigint_action, NULL);
349 population = sn_population_create (max_population_size);
350 if (population == NULL)
352 fprintf (stderr, "sn_population_create failed.\n");
359 n = sn_network_read_file (initial_input_file);
362 printf ("n == NULL\n");
366 inputs_num = SN_NETWORK_INPUT_NUM(n);
368 sn_population_push (population, n);
369 sn_network_destroy (n);
372 printf ("Current configuration:\n"
373 " Initial network: %s\n"
374 " Number of inputs: %3i\n"
375 " Population size: %3i\n"
376 "=======================\n",
377 initial_input_file, inputs_num, max_population_size);
381 printf ("Exiting after %llu iterations.\n",
382 (unsigned long long) iteration_counter);
387 n = sn_population_best (population);
390 if (best_output_file != NULL)
391 sn_network_write_file (n, best_output_file);
394 sn_network_destroy (n);
398 sn_population_destroy (population);
403 /* vim: set shiftwidth=2 softtabstop=2 : */