11 #include "sn_network.h"
13 struct population_entry_s
15 sn_network_t *network;
18 typedef struct population_entry_s population_entry_t;
20 static int iterations_num = 10000;
21 static int max_population_size = 64;
22 static int inputs_num = 16;
24 static population_entry_t *population = NULL;
25 int population_size = 0;
27 static int init_random (void)
32 fd = open ("/dev/random", O_RDONLY);
39 read (fd, (void *) &r, sizeof (r));
45 } /* int init_random */
47 static int bounded_random (int upper_bound)
49 double r = ((double) rand ()) / ((double) RAND_MAX);
50 return ((int) (r * upper_bound));
53 static void exit_usage (const char *name)
55 printf ("%s <file0>\n", name);
57 } /* void exit_usage */
59 static int rate_network (const sn_network_t *n)
64 rate = SN_NETWORK_STAGE_NUM (n) * SN_NETWORK_INPUT_NUM (n);
65 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
67 sn_stage_t *s = SN_NETWORK_STAGE_GET (n, i);
68 rate += SN_STAGE_COMP_NUM (s);
72 } /* int rate_network */
74 static int population_print_stats (int iterations)
80 for (i = 0; i < population_size; i++)
82 if ((best == -1) || (best > population[i].rating))
83 best = population[i].rating;
84 total += population[i].rating;
87 printf ("Iterations: %6i; Best: %i; Average: %.2f;\n",
88 iterations, best, ((double) total) / ((double) population_size));
91 } /* int population_print_stats */
93 static int insert_into_population (sn_network_t *n)
98 rating = rate_network (n);
100 if (population_size < max_population_size)
102 population[population_size].network = n;
103 population[population_size].rating = rating;
108 for (i = 0; i < population_size; i++)
109 if (population[i].rating > rating)
112 if (i < population_size)
114 sn_network_destroy (population[i].network);
115 population[i].network = n;
116 population[i].rating = rating;
120 sn_network_destroy (n);
124 } /* int insert_into_population */
126 static int create_offspring (void)
132 p0 = bounded_random (population_size);
133 p1 = bounded_random (population_size);
135 n = sn_network_combine (population[p0].network, population[p1].network);
137 while (SN_NETWORK_INPUT_NUM (n) > inputs_num)
140 enum sn_network_cut_dir_e dir;
142 pos = bounded_random (SN_NETWORK_INPUT_NUM (n));
143 dir = (bounded_random (2) == 0) ? DIR_MIN : DIR_MAX;
145 assert ((pos >= 0) && (pos < SN_NETWORK_INPUT_NUM (n)));
147 sn_network_cut_at (n, pos, dir);
150 insert_into_population (n);
153 } /* int create_offspring */
155 static int start_evolution (void)
159 for (i = 0; i < iterations_num; i++)
162 population_print_stats (i);
168 } /* int start_evolution */
170 int main (int argc, char **argv)
173 exit_usage (argv[0]);
177 population = (population_entry_t *) malloc (max_population_size
178 * sizeof (population_entry_t));
179 if (population == NULL)
181 printf ("Malloc failed.\n");
184 memset (population, '\0', max_population_size
185 * sizeof (population_entry_t));
188 sn_network_t *n = sn_network_read_file (argv[1]);
191 printf ("n == NULL\n");
194 population[0].network = n;
195 population[0].rating = rate_network (n);
203 for (i = 0; i < population_size; i++)
205 sn_network_show (population[i].network);
206 printf ("=============\n");
213 /* vim: set shiftwidth=2 softtabstop=2 : */