aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-15 23:08:42 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-15 23:08:42 +0000
commit842779f5c6cfd8e5e0ee302d358adf0a540d764a (patch)
tree71c9f798eb824682345a001f40c5aa8780508e5d
parent9e5f7ef55ced908b24c0ac98b018006cb04cbd40 (diff)
simplify
-rw-r--r--weasel.c58
1 files 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 <time.h>
#include <unistd.h>
-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 <mutatationrate>] [-p <population>] [-t <threads>] [target string]\n",
+ fprintf(stderr, "usage: %s [-m <mutatationrate>] [-p <population>] [-d <display>] [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();