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>
38 #include "sn_network.h"
39 #include "sn_population.h"
40 #include "sn_random.h"
42 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
44 char *strdup (const char *s);
46 static uint64_t iteration_counter = 0;
47 static int inputs_num = 16;
49 static char *initial_input_file = NULL;
50 static char *best_output_file = NULL;
52 static int stats_interval = 0;
54 static int max_population_size = 128;
55 static sn_population_t *population;
57 static int do_loop = 0;
59 static void sigint_handler (int signal)
62 } /* void sigint_handler */
64 static void exit_usage (const char *name)
66 printf ("Usage: %s [options]\n"
68 "Valid options are:\n"
69 " -i <file> Initial input file (REQUIRED)\n"
70 " -o <file> Write the current best solution to <file>\n"
71 " -p <num> Size of the population (default: 128)\n"
75 } /* void exit_usage */
77 int read_options (int argc, char **argv)
81 while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1)
87 if (initial_input_file != NULL)
88 free (initial_input_file);
89 initial_input_file = strdup (optarg);
95 if (best_output_file != NULL)
96 free (best_output_file);
97 best_output_file = strdup (optarg);
103 int tmp = atoi (optarg);
105 max_population_size = tmp;
111 int tmp = atoi (optarg);
113 stats_interval = tmp;
119 exit_usage (argv[0]);
124 } /* int read_options */
127 static int rate_network (const sn_network_t *n)
132 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
133 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
135 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
136 rate += SN_STAGE_COMP_NUM (s);
140 } /* int rate_network */
144 static int population_print_stats (int iterations)
150 for (i = 0; i < population_size; i++)
152 if ((best == -1) || (best > population[i].rating))
153 best = population[i].rating;
154 total += population[i].rating;
157 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
158 iterations, best, ((double) total) / ((double) population_size));
161 } /* int population_print_stats */
165 static int insert_into_population (sn_network_t *n)
174 rating = rate_network (n);
176 if (population_size < max_population_size)
178 population[population_size].network = n;
179 population[population_size].rating = rating;
187 for (i = 0; i < olymp_size; i++)
189 if (population[i].rating > worst_rating)
191 worst_rating = population[i].rating;
194 if ((population[i].rating < best_rating)
195 || (best_rating == -1))
196 best_rating = population[i].rating;
199 if (rating < best_rating)
201 if (best_output_file != NULL)
203 printf ("Writing network with rating %i to %s\n",
204 rating, best_output_file);
205 sn_network_write_file (n, best_output_file);
209 printf ("New best solution has rating %i\n",
214 nmemb = max_population_size - (worst_index + 1);
216 sn_network_destroy (population[worst_index].network);
217 population[worst_index].network = NULL;
219 memmove (population + worst_index,
220 population + (worst_index + 1),
221 nmemb * sizeof (population_entry_t));
223 population[max_population_size - 1].network = n;
224 population[max_population_size - 1].rating = rating;
227 } /* int insert_into_population */
230 static int create_offspring (void)
236 p0 = sn_population_pop (population);
237 p1 = sn_population_pop (population);
242 /* combine the two parents */
243 n = sn_network_combine (p0, p1);
245 sn_network_destroy (p0);
246 sn_network_destroy (p1);
248 /* randomly chose an input and do a min- or max-cut. */
249 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
252 enum sn_network_cut_dir_e dir;
254 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
255 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
257 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
259 sn_network_cut_at (n, pos, dir);
262 /* compress the network to get a compact representation */
263 sn_network_compress (n);
265 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
267 sn_population_push (population, n);
269 sn_network_destroy (n);
272 } /* int create_offspring */
274 static int start_evolution (void)
283 i = iteration_counter;
287 int rating = sn_population_best_rating (population);
288 printf ("After %10llu iterations: Best rating: %4i\n", i, rating);
293 } /* int start_evolution */
295 int main (int argc, char **argv)
297 struct sigaction sigint_action;
299 read_options (argc, argv);
300 if (initial_input_file == NULL)
301 exit_usage (argv[0]);
303 memset (&sigint_action, '\0', sizeof (sigint_action));
304 sigint_action.sa_handler = sigint_handler;
305 sigaction (SIGINT, &sigint_action, NULL);
307 population = sn_population_create (max_population_size);
308 if (population == NULL)
310 fprintf (stderr, "sn_population_create failed.\n");
317 n = sn_network_read_file (initial_input_file);
320 printf ("n == NULL\n");
324 inputs_num = SN_NETWORK_INPUT_NUM(n);
326 sn_population_push (population, n);
327 sn_network_destroy (n);
330 printf ("Current configuration:\n"
331 " Initial network: %s\n"
332 " Number of inputs: %3i\n"
333 " Population size: %3i\n"
334 "=======================\n",
335 initial_input_file, inputs_num, max_population_size);
339 printf ("Exiting after %llu iterations.\n", iteration_counter);
344 n = sn_population_best (population);
347 if (best_output_file != NULL)
348 sn_network_write_file (n, best_output_file);
351 sn_network_destroy (n);
355 sn_population_destroy (population);
360 /* vim: set shiftwidth=2 softtabstop=2 : */