From 842779f5c6cfd8e5e0ee302d358adf0a540d764a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 15 Oct 2009 23:08:42 +0000 Subject: simplify --- weasel.c | 58 ++++++++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/weasel.c b/weasel.c index 1c2e90e..427fc9c 100644 --- a/weasel.c +++ b/weasel.c @@ -24,12 +24,12 @@ #include #include -pthread_t *threads; pthread_mutex_t mutexsum; pthread_attr_t attr; int population = 1000; int mutationrate = 100; /* 1/n */ +int display = 1000; #if 0 const char defaim[] = "METHINKS IT IS LIKE A WEASEL"; @@ -97,18 +97,21 @@ printcreature(struct creature *c) } void -printpopulation(int maximal) +printpopulation(void) { + extern int LINES; int n; + move(0, 0); printw("%1.3f\t%s\n\n", 1.0, aim); for (n = 0; n < population; n++) { printcreature(generation[n]); - if (n >= maximal) + if (n >= LINES - 8) break; } printw("\n"); clrtobot(); + refresh(); } struct creature * @@ -198,7 +201,7 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-m ] [-p ] [-t ] [target string]\n", + fprintf(stderr, "usage: %s [-m ] [-p ] [-d ] [target string]\n", __progname); exit(1); @@ -208,33 +211,28 @@ usage(void) int main(int argc, char **argv) { - extern int LINES; - int nthreads = population / 5; - int i, c, t; + pthread_t dummy; + int i, c, n; - while ((c = getopt(argc, argv, "m:p:t:")) != -1) + while ((c = getopt(argc, argv, "m:p:d:")) != -1) switch (c) { case 'm': i = atoi(optarg); - if (i > 0) - mutationrate = i; - else + if (i <= 0) usage(); + mutationrate = i; break; case 'p': i = atoi(optarg); - if (i >= 3) - population = i; - else + if (i < 3) usage(); - nthreads = population / 5; + population = i; break; - case 't': + case 'd': i = atoi(optarg); - if (i >= 1) - nthreads = i; - else + if (i <= 0) usage(); + display = i; break; default: usage(); @@ -250,30 +248,22 @@ main(int argc, char **argv) srandom(time(NULL)); initpopulation(strlen(aim)); - if (!nthreads) - nthreads = 1; - threads = calloc(nthreads, sizeof(pthread_t)); - assert(threads); pthread_mutex_init(&mutexsum, NULL); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - for (i = 0;; i++) { - move(0, 0); - for (t = 0; t < nthreads; t++) - pthread_create(&threads[t], &attr, intercourse, NULL); - printpopulation(LINES - 8); - if (success()) - break; - refresh(); + for (i = 0; !success(); i++) { + for (n = 0; n < display; n++) + pthread_create(&dummy, &attr, intercourse, NULL); + printpopulation(); } pthread_attr_destroy(&attr); pthread_mutex_destroy(&mutexsum); - free(threads); - printw("\nhalted after %d generations with population of %d (%d births, %d simultaneous)\n", - nthreads * i / population, population, nthreads * i, nthreads); + printpopulation(); + printw("\nhalted after %d generations with population of %d\n", + display * i / population, population); refresh(); endwin(); -- cgit v1.2.3