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
37 #include <sys/types.h>
47 #include <population.h>
49 #include "sn_network.h"
50 #include "sn_random.h"
52 #if !defined(__GNUC__) || !__GNUC__
53 # define __attribute__(x) /**/
56 static uint64_t iteration_counter = 0;
57 static int inputs_num = 16;
58 static int inputs_num_is_power_of_two = 1;
60 static char *initial_input_file = NULL;
61 static char *best_output_file = NULL;
63 static int stats_interval = 0;
65 static int max_population_size = 128;
66 static population_t *population;
68 static int evolution_threads_num = 4;
70 static int do_loop = 0;
72 static void sigint_handler (int signal __attribute__((unused)))
75 } /* void sigint_handler */
77 static void exit_usage (const char *name)
79 printf ("Usage: %s [options]\n"
81 "Valid options are:\n"
82 " -i <file> Initial input file (REQUIRED)\n"
83 " -o <file> Write the current best solution to <file>\n"
84 " -p <num> Size of the population (default: 128)\n"
85 " -P <peer> Send individuals to <peer> (may be repeated)\n"
86 " -t <num> Number of threads (default: 4)\n"
90 } /* void exit_usage */
92 int read_options (int argc, char **argv)
96 while ((option = getopt (argc, argv, "i:o:p:P:s:t:h")) != -1)
102 if (initial_input_file != NULL)
103 free (initial_input_file);
104 initial_input_file = strdup (optarg);
110 if (best_output_file != NULL)
111 free (best_output_file);
112 best_output_file = strdup (optarg);
118 int tmp = atoi (optarg);
121 max_population_size = tmp;
122 population_set_size (population, (size_t) max_population_size);
131 status = population_add_peer (population, optarg, /* port = */ NULL);
134 fprintf (stderr, "population_add_peer failed with status %i.\n",
142 int tmp = atoi (optarg);
144 stats_interval = tmp;
150 int tmp = atoi (optarg);
152 evolution_threads_num = tmp;
158 exit_usage (argv[0]);
163 } /* int read_options */
165 static int rate_network (const sn_network_t *n)
170 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
171 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
173 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
174 rate += SN_STAGE_COMP_NUM (s);
178 } /* int rate_network */
180 static int mutate_network (sn_network_t *n)
182 sn_network_t *n_copy;
185 int comparator_index;
188 n_copy = sn_network_clone (n);
191 fprintf (stderr, "mutate_network: sn_network_clone failed.\n");
195 stage_index = sn_bounded_random (0, SN_NETWORK_STAGE_NUM (n_copy) - 1);
196 s = SN_NETWORK_STAGE_GET (n_copy, stage_index);
198 comparator_index = sn_bounded_random (0, SN_STAGE_COMP_NUM (s) - 1);
199 sn_stage_comparator_remove (s, comparator_index);
201 status = sn_network_brute_force_check (n_copy);
203 sn_network_destroy (n_copy);
207 else if (status > 0) /* Mutated network does not sort anymore. */
210 /* We saved one comparator \o/ Let's do the same change on the original
212 s = SN_NETWORK_STAGE_GET (n, stage_index);
213 sn_stage_comparator_remove (s, comparator_index);
216 } /* int mutate_network */
218 static int create_offspring (void)
224 p0 = population_get_random (population);
225 p1 = population_get_random (population);
230 /* combine the two parents */
231 n = sn_network_combine (p0, p1);
233 sn_network_destroy (p0);
234 sn_network_destroy (p1);
236 /* randomly chose an input and do a min- or max-cut. */
237 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
240 enum sn_network_cut_dir_e dir;
242 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
243 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
245 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
247 sn_network_cut_at (n, pos, dir);
250 /* compress the network to get a compact representation */
251 sn_network_compress (n);
253 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
255 if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1))
258 population_insert (population, n);
260 sn_network_destroy (n);
263 } /* int create_offspring */
265 static void *evolution_thread (void *arg __attribute__((unused)))
270 /* XXX: Not synchronized! */
275 } /* int start_evolution */
277 static int evolution_start (int threads_num)
279 pthread_t threads[threads_num]; /* C99 ftw! */
282 for (i = 0; i < threads_num; i++)
286 status = pthread_create (&threads[i], /* attr = */ NULL,
287 evolution_thread, /* arg = */ NULL);
290 fprintf (stderr, "evolution_start: pthread_create[%i] failed "
310 iter = iteration_counter;
312 n = population_get_fittest (population);
314 rating = rate_network (n);
316 stages_num = SN_NETWORK_STAGE_NUM (n);
318 for (i = 0; i < stages_num; i++)
322 s = SN_NETWORK_STAGE_GET (n, i);
323 comparators_num += SN_STAGE_COMP_NUM (s);
326 sn_network_destroy (n);
328 printf ("Best after approximately %i iterations: "
329 "%i comparators in %i stages. Rating: %i.\n",
330 iter, comparators_num, stages_num, rating);
334 for (i = 0; i < threads_num; i++)
338 pthread_join (threads[i], /* value_ptr = */ NULL);
342 } /* int evolution_start */
344 int main (int argc, char **argv)
346 struct sigaction sigint_action;
347 struct sigaction sigterm_action;
349 population = population_create ((pi_rate_f) rate_network,
350 (pi_copy_f) sn_network_clone,
351 (pi_free_f) sn_network_destroy);
352 if (population == NULL)
354 fprintf (stderr, "population_create failed.\n");
358 population_set_serialization (population,
359 (pi_serialize_f) sn_network_serialize,
360 (pi_unserialize_f) sn_network_unserialize);
362 read_options (argc, argv);
363 if (initial_input_file == NULL)
364 exit_usage (argv[0]);
366 memset (&sigint_action, '\0', sizeof (sigint_action));
367 sigint_action.sa_handler = sigint_handler;
368 sigaction (SIGINT, &sigint_action, NULL);
370 memset (&sigterm_action, '\0', sizeof (sigterm_action));
371 sigterm_action.sa_handler = sigint_handler;
372 sigaction (SIGTERM, &sigterm_action, NULL);
374 population_start_listen_thread (population, NULL, NULL);
380 n = sn_network_read_file (initial_input_file);
383 printf ("n == NULL\n");
387 inputs_num = SN_NETWORK_INPUT_NUM(n);
389 /* Determine if `inputs_num' is a power of two. If so, more merge
390 * algorithms can be used. */
392 inputs_num_is_power_of_two = 1;
398 inputs_num_is_power_of_two = 1;
400 inputs_num_is_power_of_two = 0;
406 population_insert (population, n);
407 sn_network_destroy (n);
410 printf ("Current configuration:\n"
411 " Initial network: %s\n"
412 " Number of inputs: %3i\n"
413 " Population size: %3i\n"
414 "=======================\n",
415 initial_input_file, inputs_num, max_population_size);
417 evolution_start (evolution_threads_num);
419 printf ("Exiting after %llu iterations.\n",
420 (unsigned long long) iteration_counter);
425 n = population_get_fittest (population);
428 if (best_output_file != NULL)
429 sn_network_write_file (n, best_output_file);
431 sn_network_destroy (n);
435 population_destroy (population);
440 /* vim: set shiftwidth=2 softtabstop=2 : */