2 #define _POSIX_C_SOURCE 200112L
17 #include "sn_network.h"
18 #include "sn_population.h"
19 #include "sn_random.h"
21 /* Yes, this is ugly, but the GNU libc doesn't export it with the above flags.
23 char *strdup (const char *s);
25 static uint64_t iteration_counter = 0;
26 static int inputs_num = 16;
28 static char *initial_input_file = NULL;
29 static char *best_output_file = NULL;
31 static int stats_interval = 0;
33 static int max_population_size = 128;
34 static sn_population_t *population;
36 static int do_loop = 0;
38 static void sigint_handler (int signal)
41 } /* void sigint_handler */
43 static void exit_usage (const char *name)
45 printf ("Usage: %s [options]\n"
47 "Valid options are:\n"
48 " -i <file> Initial input file (REQUIRED)\n"
49 " -o <file> Write the current best solution to <file>\n"
50 " -p <num> Size of the population (default: 128)\n"
54 } /* void exit_usage */
56 int read_options (int argc, char **argv)
60 while ((option = getopt (argc, argv, "i:o:p:P:s:h")) != -1)
66 if (initial_input_file != NULL)
67 free (initial_input_file);
68 initial_input_file = strdup (optarg);
74 if (best_output_file != NULL)
75 free (best_output_file);
76 best_output_file = strdup (optarg);
82 int tmp = atoi (optarg);
84 max_population_size = tmp;
90 int tmp = atoi (optarg);
103 } /* int read_options */
106 static int rate_network (const sn_network_t *n)
111 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
112 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
114 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
115 rate += SN_STAGE_COMP_NUM (s);
119 } /* int rate_network */
123 static int population_print_stats (int iterations)
129 for (i = 0; i < population_size; i++)
131 if ((best == -1) || (best > population[i].rating))
132 best = population[i].rating;
133 total += population[i].rating;
136 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
137 iterations, best, ((double) total) / ((double) population_size));
140 } /* int population_print_stats */
144 static int insert_into_population (sn_network_t *n)
153 rating = rate_network (n);
155 if (population_size < max_population_size)
157 population[population_size].network = n;
158 population[population_size].rating = rating;
166 for (i = 0; i < olymp_size; i++)
168 if (population[i].rating > worst_rating)
170 worst_rating = population[i].rating;
173 if ((population[i].rating < best_rating)
174 || (best_rating == -1))
175 best_rating = population[i].rating;
178 if (rating < best_rating)
180 if (best_output_file != NULL)
182 printf ("Writing network with rating %i to %s\n",
183 rating, best_output_file);
184 sn_network_write_file (n, best_output_file);
188 printf ("New best solution has rating %i\n",
193 nmemb = max_population_size - (worst_index + 1);
195 sn_network_destroy (population[worst_index].network);
196 population[worst_index].network = NULL;
198 memmove (population + worst_index,
199 population + (worst_index + 1),
200 nmemb * sizeof (population_entry_t));
202 population[max_population_size - 1].network = n;
203 population[max_population_size - 1].rating = rating;
206 } /* int insert_into_population */
209 static int create_offspring (void)
215 p0 = sn_population_pop (population);
216 p1 = sn_population_pop (population);
221 /* combine the two parents */
222 n = sn_network_combine (p0, p1);
224 sn_network_destroy (p0);
225 sn_network_destroy (p1);
227 /* randomly chose an input and do a min- or max-cut. */
228 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
231 enum sn_network_cut_dir_e dir;
233 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
234 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
236 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
238 sn_network_cut_at (n, pos, dir);
241 /* compress the network to get a compact representation */
242 sn_network_compress (n);
244 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
246 sn_population_push (population, n);
248 sn_network_destroy (n);
251 } /* int create_offspring */
253 static int start_evolution (void)
262 i = iteration_counter;
265 if ((stats_interval > 0) && ((i % stats_interval) == 0))
266 population_print_stats (i);
271 } /* int start_evolution */
273 int main (int argc, char **argv)
275 struct sigaction sigint_action;
277 read_options (argc, argv);
278 if (initial_input_file == NULL)
279 exit_usage (argv[0]);
281 memset (&sigint_action, '\0', sizeof (sigint_action));
282 sigint_action.sa_handler = sigint_handler;
283 sigaction (SIGINT, &sigint_action, NULL);
285 population = sn_population_create (max_population_size);
286 if (population == NULL)
288 fprintf (stderr, "sn_population_create failed.\n");
295 n = sn_network_read_file (initial_input_file);
298 printf ("n == NULL\n");
302 inputs_num = SN_NETWORK_INPUT_NUM(n);
304 sn_population_push (population, n);
305 sn_network_destroy (n);
308 printf ("Current configuration:\n"
309 " Initial network: %s\n"
310 " Number of inputs: %3i\n"
311 " Population size: %3i\n"
312 "=======================\n",
313 initial_input_file, inputs_num, max_population_size);
317 printf ("Exiting after %llu iterations.\n", iteration_counter);
322 n = sn_population_best (population);
325 if (best_output_file != NULL)
326 sn_network_write_file (n, best_output_file);
329 sn_network_destroy (n);
333 sn_population_destroy (population);
338 /* vim: set shiftwidth=2 softtabstop=2 : */