X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsn-evolution.c;h=86bdfba7cd292d0a394a143da939ebb834237d70;hb=79789cfc25a65312d8b91000abc7eb9209322555;hp=3c11adb61b057441c5ae2ae657cae6ba26273ff0;hpb=6afcf3afbef32c7f16b12872b5aabcd7a6467786;p=sort-networks.git diff --git a/src/sn-evolution.c b/src/sn-evolution.c index 3c11adb..86bdfba 100644 --- a/src/sn-evolution.c +++ b/src/sn-evolution.c @@ -1,6 +1,6 @@ /** * collectd - src/sn-evolution.c - * Copyright (C) 2008 Florian octo Forster + * Copyright (C) 2008,2009 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -52,6 +52,7 @@ char *strdup (const char *s); static uint64_t iteration_counter = 0; static int inputs_num = 16; +static int inputs_num_is_power_of_two = 1; static char *initial_input_file = NULL; static char *best_output_file = NULL; @@ -224,7 +225,7 @@ static int create_offspring (void) assert (p1 != NULL); /* combine the two parents */ - n = sn_network_combine (p0, p1); + n = sn_network_combine (p0, p1, inputs_num_is_power_of_two); sn_network_destroy (p0); sn_network_destroy (p1); @@ -248,7 +249,7 @@ static int create_offspring (void) assert (SN_NETWORK_INPUT_NUM (n) == inputs_num); - if (sn_bounded_random (0, 100) <= 1) + if ((SN_NETWORK_INPUT_NUM (n) <= 16) && (sn_bounded_random (0, 100) <= 1)) mutate_network (n); population_insert (population, n); @@ -298,17 +299,32 @@ static int evolution_start (int threads_num) if (status == 0) { sn_network_t *n; + int stages_num; + int comparators_num; int rating; + int iter; - i = iteration_counter; + iter = iteration_counter; n = population_get_fittest (population); + rating = rate_network (n); + + stages_num = SN_NETWORK_STAGE_NUM (n); + comparators_num = 0; + for (i = 0; i < stages_num; i++) + { + sn_stage_t *s; + + s = SN_NETWORK_STAGE_GET (n, i); + comparators_num += SN_STAGE_COMP_NUM (s); + } + sn_network_destroy (n); - printf ("After approximately %i iterations: " - "Currently best rating: %i\n", - i, rating); + printf ("Best after approximately %i iterations: " + "%i comparators in %i stages. Rating: %i.\n", + iter, comparators_num, stages_num, rating); } } @@ -356,6 +372,7 @@ int main (int argc, char **argv) { sn_network_t *n; + int tmp; n = sn_network_read_file (initial_input_file); if (n == NULL) @@ -366,6 +383,23 @@ int main (int argc, char **argv) inputs_num = SN_NETWORK_INPUT_NUM(n); + /* Determine if `inputs_num' is a power of two. If so, more merge + * algorithms can be used. */ + tmp = inputs_num; + inputs_num_is_power_of_two = 1; + while (tmp > 0) + { + if ((tmp % 2) != 0) + { + if (tmp == 1) + inputs_num_is_power_of_two = 1; + else + inputs_num_is_power_of_two = 0; + break; + } + tmp = tmp >> 1; + } + population_insert (population, n); sn_network_destroy (n); }