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
43 #include "sn_network.h"
44 #include "sn_random.h"
46 #if !defined(__GNUC__) || !__GNUC__
47 # define __attribute__(x) /**/
50 static int inputs_num = 16;
52 static char *initial_input_file = NULL;
53 static char *best_output_file = NULL;
55 static sn_network_t *best_solution = NULL;
56 static int best_rating = INT_MAX;
58 static _Bool do_loop = 1;
60 static void sigint_handler (int signal __attribute__((unused)))
63 } /* void sigint_handler */
65 static void exit_usage (const char *name, int status)
67 printf ("Usage: %s [options]\n"
69 "Valid options are:\n"
70 " -i <file> Initial input file (REQUIRED)\n"
71 " -o <file> Write the current best solution to <file>\n"
72 " -h Display usage information (this message)\n"
76 } /* void exit_usage */
78 int read_options (int argc, char **argv)
82 while ((option = getopt (argc, argv, "i:o:p:P:s:t:h")) != -1)
88 if (initial_input_file != NULL)
89 free (initial_input_file);
90 initial_input_file = strdup (optarg);
96 if (best_output_file != NULL)
97 free (best_output_file);
98 best_output_file = strdup (optarg);
104 exit_usage (argv[0], EXIT_SUCCESS);
109 } /* int read_options */
111 static int rate_network (const sn_network_t *n)
116 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
117 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
119 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
120 rate += SN_STAGE_COMP_NUM (s);
124 } /* int rate_network */
126 static sn_network_t *get_next (sn_network_t *n)
129 sn_network_t *nright;
135 nleft = sn_network_clone (n);
136 nright = sn_network_clone (n);
138 assert (nleft != NULL);
139 assert (nright != NULL);
141 /* combine the two parents */
142 nret = sn_network_combine (nleft, nright);
144 sn_network_destroy (nleft);
145 sn_network_destroy (nright);
147 /* randomly chose an input and do a min- or max-cut. */
148 while (SN_NETWORK_INPUT_NUM (nret) > inputs_num)
151 enum sn_network_cut_dir_e dir;
153 pos = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (nret) - 1);
154 dir = (sn_bounded_random (0, 1) == 0) ? DIR_MIN : DIR_MAX;
156 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (nret)));
158 sn_network_cut_at (nret, pos, dir);
161 /* compress the network to get a compact representation */
162 sn_network_compress (nret);
164 assert (SN_NETWORK_INPUT_NUM (nret) == inputs_num);
166 } /* sn_network_t *get_next */
168 static void random_walk (sn_network_t *n)
170 uint64_t iteration_counter = 0;
174 sn_network_t *next = get_next (n);
179 fprintf (stderr, "get_next() failed.\n");
183 rating = rate_network (next);
184 if (rating < best_rating)
186 printf ("New best rating (%i) after %"PRIu64" iterations.\n",
187 rating, iteration_counter);
189 best_rating = rating;
190 sn_network_destroy (best_solution);
191 best_solution = sn_network_clone (next);
194 sn_network_destroy (n);
197 } /* while (do_loop) */
199 sn_network_destroy (n);
201 printf ("Exiting after %"PRIu64" iterations.\n", iteration_counter);
202 } /* void random_walk */
204 int main (int argc, char **argv)
206 sn_network_t *start_network;
208 struct sigaction sigint_action;
209 struct sigaction sigterm_action;
211 read_options (argc, argv);
212 if (initial_input_file == NULL)
213 exit_usage (argv[0], EXIT_FAILURE);
215 memset (&sigint_action, '\0', sizeof (sigint_action));
216 sigint_action.sa_handler = sigint_handler;
217 sigaction (SIGINT, &sigint_action, NULL);
219 memset (&sigterm_action, '\0', sizeof (sigterm_action));
220 sigterm_action.sa_handler = sigint_handler;
221 sigaction (SIGTERM, &sigterm_action, NULL);
223 start_network = sn_network_read_file (initial_input_file);
224 if (start_network == NULL)
226 printf ("start_network == NULL\n");
230 inputs_num = SN_NETWORK_INPUT_NUM(start_network);
232 printf ("Current configuration:\n"
233 " Initial network: %s\n"
234 " Number of inputs: %3i\n"
235 "=======================\n",
236 initial_input_file, inputs_num);
238 random_walk (start_network);
239 start_network = NULL;
241 if (best_solution != NULL)
243 if (best_output_file != NULL)
244 sn_network_write_file (best_solution, best_output_file);
246 printf ("=== Best solution (rating: %i) ===\n", best_rating);
247 sn_network_normalize (best_solution);
248 sn_network_show (best_solution);
249 sn_network_destroy (best_solution);
256 /* vim: set shiftwidth=2 softtabstop=2 : */