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 inputs_num = 16;
28 static population_entry_t *population = NULL;
29 int population_size = 0;
31 static void sigint_handler (int signal)
34 } /* void sigint_handler */
36 static int init_random (void)
41 fd = open ("/dev/random", O_RDONLY);
48 read (fd, (void *) &r, sizeof (r));
54 } /* int init_random */
56 static int bounded_random (int upper_bound)
58 double r = ((double) rand ()) / ((double) RAND_MAX);
59 return ((int) (r * upper_bound));
62 static void exit_usage (const char *name)
64 printf ("%s <file0>\n", name);
66 } /* void exit_usage */
68 static int rate_network (const sn_network_t *n)
73 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
74 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
76 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
77 rate += SN_STAGE_COMP_NUM (s);
81 } /* int rate_network */
83 static int population_print_stats (int iterations)
89 for (i = 0; i < population_size; i++)
91 if ((best == -1) || (best > population[i].rating))
92 best = population[i].rating;
93 total += population[i].rating;
96 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
97 iterations, best, ((double) total) / ((double) population_size));
100 } /* int population_print_stats */
102 static int insert_into_population (sn_network_t *n)
107 rating = rate_network (n);
109 if (population_size < max_population_size)
111 population[population_size].network = n;
112 population[population_size].rating = rating;
117 for (i = 0; i < population_size; i++)
118 if (population[i].rating > rating)
121 if (i < population_size)
123 sn_network_destroy (population[i].network);
124 population[i].network = n;
125 population[i].rating = rating;
129 sn_network_destroy (n);
133 } /* int insert_into_population */
135 static int create_offspring (void)
141 p0 = bounded_random (population_size);
142 p1 = bounded_random (population_size);
144 n = sn_network_combine (population[p0].network, population[p1].network);
146 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
149 enum sn_network_cut_dir_e dir;
151 pos = bounded_random (SN_NETWORK_INPUT_NUM (n));
152 dir = (bounded_random (2) == 0) ? DIR_MIN : DIR_MAX;
154 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
156 sn_network_cut_at (n, pos, dir);
159 sn_network_compress (n);
161 assert (SN_NETWORK_INPUT_NUM (n) == inputs_num);
163 insert_into_population (n);
166 } /* int create_offspring */
168 static int start_evolution (void)
172 for (i = 0; i < iterations_num; i++)
175 population_print_stats (i);
181 } /* int start_evolution */
183 int main (int argc, char **argv)
185 struct sigaction sigint_action;
188 exit_usage (argv[0]);
192 memset (&sigint_action, '\0', sizeof (sigint_action));
193 sigint_action.sa_handler = sigint_handler;
194 sigaction (SIGINT, &sigint_action, NULL);
196 population = (population_entry_t *) malloc (max_population_size
197 * sizeof (population_entry_t));
198 if (population == NULL)
200 printf ("Malloc failed.\n");
203 memset (population, '\0', max_population_size
204 * sizeof (population_entry_t));
207 sn_network_t *n = sn_network_read_file (argv[1]);
210 printf ("n == NULL\n");
213 population[0].network = n;
214 population[0].rating = rate_network (n);
225 for (i = 0; i < population_size; i++)
227 if ((best_rate == -1) || (best_rate > population[i].rating))
229 best_rate = population[i].rating;
234 sn_network_show (population[best_index].network);
240 /* vim: set shiftwidth=2 softtabstop=2 : */