2 #define _POSIX_C_SOURCE 200112L
16 #include "sn_network.h"
18 struct population_entry_s
20 sn_network_t *network;
23 typedef struct population_entry_s population_entry_t;
25 static int iterations_num = INT_MAX;
26 static int max_population_size = 128;
27 static int olymp_size = 96;
28 static int inputs_num = 16;
30 static population_entry_t *population = NULL;
31 int population_size = 0;
33 static void sigint_handler (int signal)
36 } /* void sigint_handler */
38 static int init_random (void)
43 fd = open ("/dev/random", O_RDONLY);
50 read (fd, (void *) &r, sizeof (r));
56 } /* int init_random */
58 static int bounded_random (int upper_bound)
60 double r = ((double) rand ()) / ((double) RAND_MAX);
61 return ((int) (r * upper_bound));
64 static void exit_usage (const char *name)
66 printf ("%s <file0>\n", name);
68 } /* void exit_usage */
70 static int rate_network (const sn_network_t *n)
75 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
76 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
78 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
79 rate += SN_STAGE_COMP_NUM (s);
83 } /* int rate_network */
85 static int population_print_stats (int iterations)
91 for (i = 0; i < population_size; i++)
93 if ((best == -1) || (best > population[i].rating))
94 best = population[i].rating;
95 total += population[i].rating;
98 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
99 iterations, best, ((double) total) / ((double) population_size));
102 } /* int population_print_stats */
104 static int insert_into_population (sn_network_t *n)
112 rating = rate_network (n);
114 if (population_size < max_population_size)
116 population[population_size].network = n;
117 population[population_size].rating = rating;
124 for (i = 0; i < olymp_size; i++)
125 if (population[i].rating > worst_rating)
127 worst_rating = population[i].rating;
131 nmemb = max_population_size - (worst_index + 1);
133 sn_network_destroy (population[worst_index].network);
134 population[worst_index].network = NULL;
136 memmove (population + worst_index,
137 population + (worst_index + 1),
138 nmemb * sizeof (population_entry_t));
140 population[max_population_size - 1].network = n;
141 population[max_population_size - 1].rating = rating;
144 } /* int insert_into_population */
146 static int create_offspring (void)
152 p0 = bounded_random (population_size);
153 p1 = bounded_random (population_size);
155 n = sn_network_combine (population[p0].network, population[p1].network);
157 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
160 enum sn_network_cut_dir_e dir;
162 pos = bounded_random (SN_NETWORK_INPUT_NUM (n));
163 dir = (bounded_random (2) == 0) ? DIR_MIN : DIR_MAX;
165 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
167 sn_network_cut_at (n, pos, dir);
170 sn_network_compress (n);
172 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
174 insert_into_population (n);
177 } /* int create_offspring */
179 static int start_evolution (void)
183 for (i = 0; i < iterations_num; i++)
186 population_print_stats (i);
192 } /* int start_evolution */
194 int main (int argc, char **argv)
196 struct sigaction sigint_action;
199 exit_usage (argv[0]);
203 memset (&sigint_action, '\0', sizeof (sigint_action));
204 sigint_action.sa_handler = sigint_handler;
205 sigaction (SIGINT, &sigint_action, NULL);
207 population = (population_entry_t *) malloc (max_population_size
208 * sizeof (population_entry_t));
209 if (population == NULL)
211 printf ("Malloc failed.\n");
214 memset (population, '\0', max_population_size
215 * sizeof (population_entry_t));
218 sn_network_t *n = sn_network_read_file (argv[1]);
221 printf ("n == NULL\n");
224 population[0].network = n;
225 population[0].rating = rate_network (n);
236 for (i = 0; i < population_size; i++)
238 if ((best_rate == -1) || (best_rate > population[i].rating))
240 best_rate = population[i].rating;
245 sn_network_show (population[best_index].network);
251 for (i = 0; i < population_size; i++)
253 sn_network_destroy (population[i].network);
254 population[i].network = NULL;
264 /* vim: set shiftwidth=2 softtabstop=2 : */