2 * libsortnetwork - src/sn-markov.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
44 #include "sn_network.h"
45 #include "sn_random.h"
46 #include "histogram.h"
48 #if !defined(__GNUC__) || !__GNUC__
49 # define __attribute__(x) /**/
52 static int inputs_num = 16;
54 static char *initial_input_file = NULL;
55 static char *best_output_file = NULL;
57 static sn_network_t *best_solution = NULL;
58 static int best_rating = INT_MAX;
60 static _Bool do_loop = 1;
61 static uint64_t max_iterations = 0;
63 static histogram_t *histogram;
65 static void sigint_handler (int signal __attribute__((unused)))
68 } /* void sigint_handler */
70 static void exit_usage (const char *name, int status)
72 printf ("Usage: %s [options]\n"
74 "Valid options are:\n"
75 " -i <file> Initial input file (REQUIRED)\n"
76 " -o <file> Write the current best solution to <file>\n"
77 " -n <number> Maximum number of steps (iterations) to perform\n"
78 " -h Display usage information (this message)\n"
82 } /* void exit_usage */
84 int read_options (int argc, char **argv)
88 while ((option = getopt (argc, argv, "i:o:n:h")) != -1)
94 if (initial_input_file != NULL)
95 free (initial_input_file);
96 initial_input_file = strdup (optarg);
102 if (best_output_file != NULL)
103 free (best_output_file);
104 best_output_file = strdup (optarg);
111 max_iterations = (uint64_t) strtoull (optarg,
116 fprintf (stderr, "Parsing integer argument failed: %s\n",
118 exit_usage (argv[0], EXIT_FAILURE);
125 exit_usage (argv[0], EXIT_SUCCESS);
130 } /* int read_options */
132 static int rate_network (const sn_network_t *n)
136 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
137 rate += sn_network_get_comparator_num (n);
140 } /* int rate_network */
142 static sn_network_t *get_next (sn_network_t *n)
145 sn_network_t *nright;
151 nleft = sn_network_clone (n);
152 nright = sn_network_clone (n);
154 assert (nleft != NULL);
155 assert (nright != NULL);
157 /* combine the two parents */
158 nret = sn_network_combine (nleft, nright);
160 sn_network_destroy (nleft);
161 sn_network_destroy (nright);
163 /* randomly chose an input and do a min- or max-cut. */
164 while (SN_NETWORK_INPUT_NUM (nret) > inputs_num)
167 enum sn_network_cut_dir_e dir;
169 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (nret) - 1);
170 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
172 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (nret)));
174 sn_network_cut_at (nret, pos, dir);
177 /* compress the network to get a compact representation */
178 sn_network_compress (nret);
180 assert (SN_NETWORK_INPUT_NUM (nret) == inputs_num);
182 } /* sn_network_t *get_next */
184 static void random_walk (sn_network_t *n)
186 uint64_t iteration_counter = 0;
190 sn_network_t *next = get_next (n);
195 fprintf (stderr, "get_next() failed.\n");
199 rating = rate_network (next);
200 if (rating < best_rating)
202 printf ("New best rating (%i) after %"PRIu64" iterations.\n",
203 rating, iteration_counter);
205 best_rating = rating;
206 sn_network_destroy (best_solution);
207 best_solution = sn_network_clone (next);
210 hist_account (histogram, sn_network_get_comparator_num (next));
212 sn_network_destroy (n);
216 if ((max_iterations > 0) && (iteration_counter >= max_iterations))
218 } /* while (do_loop) */
220 sn_network_destroy (n);
222 printf ("Exiting after %"PRIu64" iterations.\n", iteration_counter);
223 } /* void random_walk */
225 int main (int argc, char **argv)
227 sn_network_t *start_network;
229 struct sigaction sigint_action;
230 struct sigaction sigterm_action;
232 histogram = hist_create ();
234 read_options (argc, argv);
235 if (initial_input_file == NULL)
236 exit_usage (argv[0], EXIT_FAILURE);
238 memset (&sigint_action, '\0', sizeof (sigint_action));
239 sigint_action.sa_handler = sigint_handler;
240 sigaction (SIGINT, &sigint_action, NULL);
242 memset (&sigterm_action, '\0', sizeof (sigterm_action));
243 sigterm_action.sa_handler = sigint_handler;
244 sigaction (SIGTERM, &sigterm_action, NULL);
246 start_network = sn_network_read_file (initial_input_file);
247 if (start_network == NULL)
249 printf ("start_network == NULL\n");
253 inputs_num = SN_NETWORK_INPUT_NUM(start_network);
255 printf ("Current configuration:\n"
256 " Initial network: %s\n"
257 " Number of inputs: %3i\n"
258 "=======================\n",
259 initial_input_file, inputs_num);
261 random_walk (start_network);
262 start_network = NULL;
264 hist_print (histogram);
265 hist_destroy (histogram);
268 if (best_solution != NULL)
270 if (best_output_file != NULL)
271 sn_network_write_file (best_solution, best_output_file);
273 printf ("=== Best solution (rating: %i) ===\n", best_rating);
274 sn_network_normalize (best_solution);
275 sn_network_show (best_solution);
276 sn_network_destroy (best_solution);
283 /* vim: set shiftwidth=2 softtabstop=2 : */