2 * libsortnetwork - src/sn-count-markov.c
3 * Copyright (C) 2008-2011 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
46 #include "sn_network.h"
47 #include "sn_random.h"
49 #if !defined(__GNUC__) || !__GNUC__
50 # define __attribute__(x) /**/
53 static int inputs_num = 16;
55 static char *initial_input_file = NULL;
57 static GTree *all_networks = NULL;
58 static uint64_t cycles_num = 0;
60 static _Bool do_loop = 1;
61 static _Bool do_output_network = 0;
62 static uint64_t max_iterations = 0;
64 static void sigint_handler (__attribute__((unused)) int signal) /* {{{ */
67 } /* }}} void sigint_handler */
69 static void sighup_handler (__attribute__((unused)) int signal) /* {{{ */
71 do_output_network = 1;
72 } /* }}} void sighup_handler */
74 static int output_network (sn_network_t *n) /* {{{ */
81 fh = fopen ("/dev/tty", "w");
85 sn_network_show_fh (n, fh);
89 } /* }}} int output_network */
91 static gint compare_hashval (gconstpointer p0, gconstpointer p1) /* {{{ */
93 const uint64_t *i0 = (gconstpointer) p0;
94 const uint64_t *i1 = (gconstpointer) p1;
98 else if ((*i0) > (*i1))
102 } /* }}} gint account_network_compare */
104 static int account_network (const sn_network_t *n, uint64_t iteration) /* {{{ */
114 printf ("# iteration cyclelength\n");
116 key = sn_network_get_hashval (n);
119 value_ptr = (uint64_t *) g_tree_lookup (all_networks, (gconstpointer) key_ptr);
120 if (value_ptr != NULL)
122 uint64_t cycle_length = iteration - (*value_ptr);
123 printf ("%"PRIu64" %"PRIu64"\n", iteration, cycle_length);
126 *value_ptr = iteration;
130 key_ptr = malloc (sizeof (*key_ptr));
135 value_ptr = malloc (sizeof (*value_ptr));
136 if (value_ptr == NULL)
141 *value_ptr = iteration;
143 g_tree_insert (all_networks, (gpointer) key_ptr, (gpointer) value_ptr);
145 } /* }}} int account_network */
147 static void exit_usage (const char *name, int status)
149 printf ("Usage: %s [options]\n"
151 "Valid options are:\n"
152 " -i <file> Initial input file (REQUIRED)\n"
153 " -o <file> Write the current best solution to <file>\n"
154 " -n <number> Maximum number of steps (iterations) to perform\n"
155 " -h Display usage information (this message)\n"
159 } /* void exit_usage */
161 int read_options (int argc, char **argv)
165 while ((option = getopt (argc, argv, "i:o:n:h")) != -1)
171 if (initial_input_file != NULL)
172 free (initial_input_file);
173 initial_input_file = strdup (optarg);
180 max_iterations = (uint64_t) strtoull (optarg,
185 fprintf (stderr, "Parsing integer argument failed: %s\n",
187 exit_usage (argv[0], EXIT_FAILURE);
194 exit_usage (argv[0], EXIT_SUCCESS);
199 } /* int read_options */
201 static sn_network_t *get_next (sn_network_t *n)
204 sn_network_t *nright;
210 nleft = sn_network_clone (n);
211 nright = sn_network_clone (n);
213 assert (nleft != NULL);
214 assert (nright != NULL);
216 /* combine the two parents */
217 nret = sn_network_combine (nleft, nright);
219 sn_network_destroy (nleft);
220 sn_network_destroy (nright);
222 /* randomly chose an input and do a min- or max-cut. */
223 while (SN_NETWORK_INPUT_NUM (nret) > inputs_num)
226 enum sn_network_cut_dir_e dir;
228 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (nret) - 1);
229 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
231 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (nret)));
233 sn_network_cut_at (nret, pos, dir);
236 /* Make sure one network is always represented in the same way. */
237 sn_network_unify (nret);
239 assert (SN_NETWORK_INPUT_NUM (nret) == inputs_num);
241 } /* sn_network_t *get_next */
243 static void random_walk (sn_network_t *n)
245 uint64_t iteration_counter = 0;
249 sn_network_t *next = get_next (n);
253 fprintf (stderr, "get_next() failed.\n");
257 account_network (next, iteration_counter);
259 if (do_output_network)
261 output_network (next);
262 do_output_network = 0;
265 sn_network_destroy (n);
269 if ((max_iterations > 0) && (iteration_counter >= max_iterations))
271 } /* while (do_loop) */
273 sn_network_destroy (n);
275 printf ("# Exiting after %"PRIu64" iterations.\n", iteration_counter);
276 } /* void random_walk */
278 int main (int argc, char **argv)
280 sn_network_t *start_network;
282 struct sigaction sigint_action;
283 struct sigaction sigterm_action;
284 struct sigaction sighup_action;
286 read_options (argc, argv);
287 if (initial_input_file == NULL)
288 exit_usage (argv[0], EXIT_FAILURE);
290 memset (&sigint_action, 0, sizeof (sigint_action));
291 sigint_action.sa_handler = sigint_handler;
292 sigaction (SIGINT, &sigint_action, NULL);
294 memset (&sigterm_action, '\0', sizeof (sigterm_action));
295 sigterm_action.sa_handler = sigint_handler;
296 sigaction (SIGTERM, &sigterm_action, NULL);
298 memset (&sighup_action, 0, sizeof (sighup_action));
299 sighup_action.sa_handler = sighup_handler;
300 sigaction (SIGHUP, &sighup_action, NULL);
302 all_networks = g_tree_new (compare_hashval);
303 if (all_networks == NULL)
305 fprintf (stderr, "g_tree_new() failed.\n");
309 start_network = sn_network_read_file (initial_input_file);
310 if (start_network == NULL)
312 printf ("start_network == NULL\n");
316 inputs_num = SN_NETWORK_INPUT_NUM(start_network);
318 random_walk (start_network);
319 start_network = NULL;
321 printf ("# %"PRIu64" cycles were detected.\n", cycles_num);
327 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */