From: Florian Forster Date: Fri, 1 Feb 2008 19:05:09 +0000 (+0100) Subject: src/sn-evolution.c: Installed a SIGINT handler to shutdown cleanly upon sigint. X-Git-Tag: v1.0.0~123 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=948834fa53e183dff7705b5ba5870beab8591fb8;p=sort-networks.git src/sn-evolution.c: Installed a SIGINT handler to shutdown cleanly upon sigint. The best solution is then written to STDOUT before exiting. --- diff --git a/src/Makefile b/src/Makefile index 4cf6a22..e86008d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ CC = gcc -#CFLAGS = -Wall -Werror -std=c99 -O3 -CFLAGS = -Wall -Werror -std=c99 -O0 -g +CFLAGS = -Wall -Werror -std=c99 -O3 +#CFLAGS = -Wall -Werror -std=c99 -O0 -g all: sn-cut sn-evolution sn-merge sn-show diff --git a/src/sn-evolution.c b/src/sn-evolution.c index 4638b37..a110a5a 100644 --- a/src/sn-evolution.c +++ b/src/sn-evolution.c @@ -1,3 +1,6 @@ +#define _ISOC99_SOURCE +#define _POSIX_C_SOURCE 200112L + #include #include #include @@ -6,6 +9,7 @@ #include #include #include +#include #include #include "sn_network.h" @@ -17,13 +21,18 @@ struct population_entry_s }; typedef struct population_entry_s population_entry_t; -static int iterations_num = 10000; -static int max_population_size = 64; +static int iterations_num = 1000000; +static int max_population_size = 128; static int inputs_num = 16; static population_entry_t *population = NULL; int population_size = 0; +static void sigint_handler (int signal) +{ + iterations_num = 0; +} /* void sigint_handler */ + static int init_random (void) { int fd; @@ -147,6 +156,10 @@ static int create_offspring (void) sn_network_cut_at (n, pos, dir); } + sn_network_compress (n); + + assert (SN_NETWORK_INPUT_NUM (n) == inputs_num); + insert_into_population (n); return (0); @@ -158,7 +171,7 @@ static int start_evolution (void) for (i = 0; i < iterations_num; i++) { - if ((i % 100) == 0) + if ((i % 1000) == 0) population_print_stats (i); create_offspring (); @@ -169,11 +182,17 @@ static int start_evolution (void) int main (int argc, char **argv) { + struct sigaction sigint_action; + if (argc != 2) exit_usage (argv[0]); init_random (); + memset (&sigint_action, '\0', sizeof (sigint_action)); + sigint_action.sa_handler = sigint_handler; + sigaction (SIGINT, &sigint_action, NULL); + population = (population_entry_t *) malloc (max_population_size * sizeof (population_entry_t)); if (population == NULL) @@ -198,13 +217,21 @@ int main (int argc, char **argv) start_evolution (); - if (0) { + { int i; + int best_rate = -1; + int best_index = -1; + for (i = 0; i < population_size; i++) { - sn_network_show (population[i].network); - printf ("=============\n"); + if ((best_rate == -1) || (best_rate > population[i].rating)) + { + best_rate = population[i].rating; + best_index = i; + } } + + sn_network_show (population[best_index].network); } return (0);