2 * libsortnetwork - src/sn-evolution.c
3 * Copyright (C) 2008-2010 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 <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200809L
29 # define _XOPEN_SOURCE 700
38 #include <sys/types.h>
48 #include <population.h>
50 #include "sn_network.h"
51 #include "sn_random.h"
53 #if !defined(__GNUC__) || !__GNUC__
54 # define __attribute__(x) /**/
57 static uint64_t iteration_counter = 0;
58 static int inputs_num = 16;
59 static int inputs_num_is_power_of_two = 1;
61 static char *initial_input_file = NULL;
62 static char *best_output_file = NULL;
64 static int stats_interval = 0;
66 static int max_population_size = 128;
67 static population_t *population;
74 } selected_merger = MERGER_ODDEVEN;
76 static int evolution_threads_num = 4;
78 static int do_loop = 0;
80 static void sigint_handler (int signal __attribute__((unused)))
83 } /* void sigint_handler */
85 static void exit_usage (const char *name)
87 printf ("Usage: %s [options]\n"
89 "Valid options are:\n"
90 " -i <file> Initial input file (REQUIRED)\n"
91 " -o <file> Write the current best solution to <file>\n"
92 " -p <num> Size of the population (default: 128)\n"
93 " -P <peer> Send individuals to <peer> (may be repeated)\n"
94 " -t <num> Number of threads (default: 4)\n"
95 " -m <merger> Specify the merging network to use.\n"
96 " Available: \"oddeven\", \"bitonic\", \"random\"\n"
100 } /* void exit_usage */
102 int read_options (int argc, char **argv)
106 while ((option = getopt (argc, argv, "i:o:p:P:s:t:m:h")) != -1)
112 if (initial_input_file != NULL)
113 free (initial_input_file);
114 initial_input_file = strdup (optarg);
120 if (best_output_file != NULL)
121 free (best_output_file);
122 best_output_file = strdup (optarg);
128 int tmp = atoi (optarg);
131 max_population_size = tmp;
132 population_set_size (population, (size_t) max_population_size);
141 status = population_add_peer (population, optarg, /* port = */ NULL);
144 fprintf (stderr, "population_add_peer failed with status %i.\n",
152 int tmp = atoi (optarg);
154 stats_interval = tmp;
160 int tmp = atoi (optarg);
162 evolution_threads_num = tmp;
168 if (strcasecmp ("oddeven", optarg) == 0)
169 selected_merger = MERGER_ODDEVEN;
170 else if (strcasecmp ("bitonic", optarg) == 0)
171 selected_merger = MERGER_BITONIC;
172 else if (strcasecmp ("random", optarg) == 0)
173 selected_merger = MERGER_RANDOM;
175 fprintf (stderr, "Not a valid merging strategy: \"%s\"\n", optarg);
181 exit_usage (argv[0]);
186 } /* int read_options */
188 static int rate_network (const sn_network_t *n)
193 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
194 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
196 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
197 rate += SN_STAGE_COMP_NUM (s);
201 } /* int rate_network */
204 static int mutate_network (sn_network_t *n)
206 sn_network_t *n_copy;
209 int comparator_index;
212 n_copy = sn_network_clone (n);
215 fprintf (stderr, "mutate_network: sn_network_clone failed.\n");
219 stage_index = sn_bounded_random (0, SN_NETWORK_STAGE_NUM (n_copy) - 1);
220 s = SN_NETWORK_STAGE_GET (n_copy, stage_index);
222 comparator_index = sn_bounded_random (0, SN_STAGE_COMP_NUM (s) - 1);
223 sn_stage_comparator_remove (s, comparator_index);
225 status = sn_network_brute_force_check (n_copy);
227 sn_network_destroy (n_copy);
231 else if (status > 0) /* Mutated network does not sort anymore. */
234 /* We saved one comparator \o/ Let's do the same change on the original
236 s = SN_NETWORK_STAGE_GET (n, stage_index);
237 sn_stage_comparator_remove (s, comparator_index);
240 } /* int mutate_network */
243 static int create_offspring (void)
249 p0 = population_get_random (population);
250 p1 = population_get_random (population);
255 /* combine the two parents */
256 if ((selected_merger == MERGER_ODDEVEN)
257 || ((selected_merger == MERGER_RANDOM)
258 && (sn_bounded_random (0, 1) == 0)))
259 n = sn_network_combine_odd_even_merge (p0, p1);
261 n = sn_network_combine_bitonic_merge (p0, p1);
263 sn_network_destroy (p0);
264 sn_network_destroy (p1);
266 /* randomly chose an input and do a min- or max-cut. */
267 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
270 enum sn_network_cut_dir_e dir;
272 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
273 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
275 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
277 sn_network_cut_at (n, pos, dir);
280 /* compress the network to get a compact representation */
281 sn_network_compress (n);
283 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
286 if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1))
290 population_insert (population, n);
292 sn_network_destroy (n);
295 } /* int create_offspring */
297 static void *evolution_thread (void *arg __attribute__((unused)))
302 /* XXX: Not synchronized! */
307 } /* int start_evolution */
309 static int evolution_start (int threads_num)
311 pthread_t threads[threads_num]; /* C99 ftw! */
314 for (i = 0; i < threads_num; i++)
318 status = pthread_create (&threads[i], /* attr = */ NULL,
319 evolution_thread, /* arg = */ NULL);
322 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
342 iter = iteration_counter;
344 n = population_get_fittest (population);
346 rating = rate_network (n);
348 stages_num = SN_NETWORK_STAGE_NUM (n);
350 for (i = 0; i < stages_num; i++)
354 s = SN_NETWORK_STAGE_GET (n, i);
355 comparators_num += SN_STAGE_COMP_NUM (s);
358 sn_network_destroy (n);
360 printf ("Best after approximately %i iterations: "
361 "%i comparators in %i stages. Rating: %i.\n",
362 iter, comparators_num, stages_num, rating);
366 for (i = 0; i < threads_num; i++)
370 pthread_join (threads[i], /* value_ptr = */ NULL);
374 } /* int evolution_start */
376 int main (int argc, char **argv)
378 struct sigaction sigint_action;
379 struct sigaction sigterm_action;
381 population = population_create ((pi_rate_f) rate_network,
382 (pi_copy_f) sn_network_clone,
383 (pi_free_f) sn_network_destroy);
384 if (population == NULL)
386 fprintf (stderr, "population_create failed.\n");
390 population_set_serialization (population,
391 (pi_serialize_f) sn_network_serialize,
392 (pi_unserialize_f) sn_network_unserialize);
394 read_options (argc, argv);
395 if (initial_input_file == NULL)
396 exit_usage (argv[0]);
398 memset (&sigint_action, '\0', sizeof (sigint_action));
399 sigint_action.sa_handler = sigint_handler;
400 sigaction (SIGINT, &sigint_action, NULL);
402 memset (&sigterm_action, '\0', sizeof (sigterm_action));
403 sigterm_action.sa_handler = sigint_handler;
404 sigaction (SIGTERM, &sigterm_action, NULL);
406 population_start_listen_thread (population, NULL, NULL);
412 n = sn_network_read_file (initial_input_file);
415 printf ("n == NULL\n");
419 inputs_num = SN_NETWORK_INPUT_NUM(n);
421 /* Determine if `inputs_num' is a power of two. If so, more merge
422 * algorithms can be used. */
424 inputs_num_is_power_of_two = 1;
430 inputs_num_is_power_of_two = 1;
432 inputs_num_is_power_of_two = 0;
438 population_insert (population, n);
439 sn_network_destroy (n);
442 printf ("Current configuration:\n"
443 " Initial network: %s\n"
444 " Number of inputs: %3i\n"
445 " Population size: %3i\n"
446 "=======================\n",
447 initial_input_file, inputs_num, max_population_size);
449 evolution_start (evolution_threads_num);
451 printf ("Exiting after %llu iterations.\n",
452 (unsigned long long) iteration_counter);
457 n = population_get_fittest (population);
460 if (best_output_file != NULL)
461 sn_network_write_file (n, best_output_file);
463 sn_network_destroy (n);
467 population_destroy (population);
472 /* vim: set shiftwidth=2 softtabstop=2 : */