2 #define _POSIX_C_SOURCE 200112L
15 #include "sn_network.h"
17 struct population_entry_s
19 sn_network_t *network;
22 typedef struct population_entry_s population_entry_t;
24 static int iterations_num = 1000000;
25 static int max_population_size = 128;
26 static int olymp_size = 96;
27 static int inputs_num = 16;
29 static population_entry_t *population = NULL;
30 int population_size = 0;
32 static void sigint_handler (int signal)
35 } /* void sigint_handler */
37 static int init_random (void)
42 fd = open ("/dev/random", O_RDONLY);
49 read (fd, (void *) &r, sizeof (r));
55 } /* int init_random */
57 static int bounded_random (int upper_bound)
59 double r = ((double) rand ()) / ((double) RAND_MAX);
60 return ((int) (r * upper_bound));
63 static void exit_usage (const char *name)
65 printf ("%s <file0>\n", name);
67 } /* void exit_usage */
69 static int rate_network (const sn_network_t *n)
74 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
75 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
77 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
78 rate += SN_STAGE_COMP_NUM (s);
82 } /* int rate_network */
84 static int population_print_stats (int iterations)
90 for (i = 0; i < population_size; i++)
92 if ((best == -1) || (best > population[i].rating))
93 best = population[i].rating;
94 total += population[i].rating;
97 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
98 iterations, best, ((double) total) / ((double) population_size));
101 } /* int population_print_stats */
103 static int insert_into_population (sn_network_t *n)
111 rating = rate_network (n);
113 if (population_size < max_population_size)
115 population[population_size].network = n;
116 population[population_size].rating = rating;
123 for (i = 0; i < olymp_size; i++)
124 if (population[i].rating > worst_rating)
126 worst_rating = population[i].rating;
130 nmemb = max_population_size - (worst_index + 1);
132 sn_network_destroy (population[worst_index].network);
133 population[worst_index].network = NULL;
135 memmove (population + worst_index,
136 population + (worst_index + 1),
137 nmemb * sizeof (population_entry_t));
139 population[max_population_size - 1].network = n;
140 population[max_population_size - 1].rating = rating;
143 } /* int insert_into_population */
145 static int create_offspring (void)
151 p0 = bounded_random (population_size);
152 p1 = bounded_random (population_size);
154 n = sn_network_combine (population[p0].network, population[p1].network);
156 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
159 enum sn_network_cut_dir_e dir;
161 pos = bounded_random (SN_NETWORK_INPUT_NUM (n));
162 dir = (bounded_random (2) == 0) ? DIR_MIN : DIR_MAX;
164 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
166 sn_network_cut_at (n, pos, dir);
169 sn_network_compress (n);
171 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
173 insert_into_population (n);
176 } /* int create_offspring */
178 static int start_evolution (void)
182 for (i = 0; i < iterations_num; i++)
185 population_print_stats (i);
191 } /* int start_evolution */
193 int main (int argc, char **argv)
195 struct sigaction sigint_action;
198 exit_usage (argv[0]);
202 memset (&sigint_action, '\0', sizeof (sigint_action));
203 sigint_action.sa_handler = sigint_handler;
204 sigaction (SIGINT, &sigint_action, NULL);
206 population = (population_entry_t *) malloc (max_population_size
207 * sizeof (population_entry_t));
208 if (population == NULL)
210 printf ("Malloc failed.\n");
213 memset (population, '\0', max_population_size
214 * sizeof (population_entry_t));
217 sn_network_t *n = sn_network_read_file (argv[1]);
220 printf ("n == NULL\n");
223 population[0].network = n;
224 population[0].rating = rate_network (n);
235 for (i = 0; i < population_size; i++)
237 if ((best_rate == -1) || (best_rate > population[i].rating))
239 best_rate = population[i].rating;
244 sn_network_show (population[best_index].network);
250 /* vim: set shiftwidth=2 softtabstop=2 : */