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 uint64_t max_iterations = 0;
63 static void sigint_handler (int signal __attribute__((unused)))
66 } /* void sigint_handler */
68 static gint compare_hashval (gconstpointer p0, gconstpointer p1) /* {{{ */
70 const uint64_t *i0 = (gconstpointer) p0;
71 const uint64_t *i1 = (gconstpointer) p1;
75 else if ((*i0) > (*i1))
79 } /* }}} gint account_network_compare */
81 static int account_network (const sn_network_t *n, uint64_t iteration) /* {{{ */
91 printf ("# iteration cyclelength\n");
93 key = sn_network_get_hashval (n);
96 value_ptr = (uint64_t *) g_tree_lookup (all_networks, (gconstpointer) key_ptr);
97 if (value_ptr != NULL)
99 uint64_t cycle_length = iteration - (*value_ptr);
100 printf ("%"PRIu64" %"PRIu64"\n", iteration, cycle_length);
103 *value_ptr = iteration;
107 key_ptr = malloc (sizeof (*key_ptr));
112 value_ptr = malloc (sizeof (*value_ptr));
113 if (value_ptr == NULL)
118 *value_ptr = iteration;
120 g_tree_insert (all_networks, (gpointer) key_ptr, (gpointer) value_ptr);
122 } /* }}} int account_network */
124 static void exit_usage (const char *name, int status)
126 printf ("Usage: %s [options]\n"
128 "Valid options are:\n"
129 " -i <file> Initial input file (REQUIRED)\n"
130 " -o <file> Write the current best solution to <file>\n"
131 " -n <number> Maximum number of steps (iterations) to perform\n"
132 " -h Display usage information (this message)\n"
136 } /* void exit_usage */
138 int read_options (int argc, char **argv)
142 while ((option = getopt (argc, argv, "i:o:n:h")) != -1)
148 if (initial_input_file != NULL)
149 free (initial_input_file);
150 initial_input_file = strdup (optarg);
157 max_iterations = (uint64_t) strtoull (optarg,
162 fprintf (stderr, "Parsing integer argument failed: %s\n",
164 exit_usage (argv[0], EXIT_FAILURE);
171 exit_usage (argv[0], EXIT_SUCCESS);
176 } /* int read_options */
178 static sn_network_t *get_next (sn_network_t *n)
181 sn_network_t *nright;
187 nleft = sn_network_clone (n);
188 nright = sn_network_clone (n);
190 assert (nleft != NULL);
191 assert (nright != NULL);
193 /* combine the two parents */
194 nret = sn_network_combine (nleft, nright);
196 sn_network_destroy (nleft);
197 sn_network_destroy (nright);
199 /* randomly chose an input and do a min- or max-cut. */
200 while (SN_NETWORK_INPUT_NUM (nret) > inputs_num)
203 enum sn_network_cut_dir_e dir;
205 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (nret) - 1);
206 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
208 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (nret)));
210 sn_network_cut_at (nret, pos, dir);
213 /* Make sure one network is always represented in the same way. */
214 sn_network_unify (nret);
216 assert (SN_NETWORK_INPUT_NUM (nret) == inputs_num);
218 } /* sn_network_t *get_next */
220 static void random_walk (sn_network_t *n)
222 uint64_t iteration_counter = 0;
226 sn_network_t *next = get_next (n);
230 fprintf (stderr, "get_next() failed.\n");
234 account_network (next, iteration_counter);
236 sn_network_destroy (n);
240 if ((max_iterations > 0) && (iteration_counter >= max_iterations))
242 } /* while (do_loop) */
244 sn_network_destroy (n);
246 printf ("# Exiting after %"PRIu64" iterations.\n", iteration_counter);
247 } /* void random_walk */
249 int main (int argc, char **argv)
251 sn_network_t *start_network;
253 struct sigaction sigint_action;
254 struct sigaction sigterm_action;
256 read_options (argc, argv);
257 if (initial_input_file == NULL)
258 exit_usage (argv[0], EXIT_FAILURE);
260 memset (&sigint_action, '\0', sizeof (sigint_action));
261 sigint_action.sa_handler = sigint_handler;
262 sigaction (SIGINT, &sigint_action, NULL);
264 memset (&sigterm_action, '\0', sizeof (sigterm_action));
265 sigterm_action.sa_handler = sigint_handler;
266 sigaction (SIGTERM, &sigterm_action, NULL);
268 all_networks = g_tree_new (compare_hashval);
269 if (all_networks == NULL)
271 fprintf (stderr, "g_tree_new() failed.\n");
275 start_network = sn_network_read_file (initial_input_file);
276 if (start_network == NULL)
278 printf ("start_network == NULL\n");
282 inputs_num = SN_NETWORK_INPUT_NUM(start_network);
284 random_walk (start_network);
285 start_network = NULL;
287 printf ("# %"PRIu64" cycles were detected.\n", cycles_num);
293 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */