From 19978bd155793d6cf67051ade1d83d6a101a1c9a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 5 Oct 2009 12:02:33 +0000 Subject: simplify, use global vars --- weasel.c | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/weasel.c b/weasel.c index fc386c6..0358471 100644 --- a/weasel.c +++ b/weasel.c @@ -28,10 +28,8 @@ pthread_t *threads; pthread_mutex_t mutexsum; pthread_attr_t attr; -struct intargs { - int mutationrate; - int population; -}; +int population = 1000; +int mutationrate = 100; /* 1/n */ #if 0 const char defaim[] = "METHINKS IT IS LIKE A WEASEL"; @@ -70,14 +68,14 @@ calculatefitness(char *genom, int length) } void -initpopulation(int length, int number) +initpopulation(int length) { int i, n; - generation = calloc(number, sizeof(struct creature *)); + generation = calloc(population, sizeof(struct creature *)); assert(generation); - for (n = 0; n < number; n++) { + for (n = 0; n < population; n++) { cp = malloc(sizeof(struct creature)); assert(cp); cp->genom = malloc(length + 1); @@ -99,12 +97,12 @@ printcreature(struct creature *c) } void -printpopulation(int maximal, int number) +printpopulation(int maximal) { int n; printw("%1.3f\t%s\n\n", 1.0, aim); - for (n = 0; n < number; n++) { + for (n = 0; n < population; n++) { printcreature(generation[n]); if (n >= maximal) break; @@ -114,7 +112,7 @@ printpopulation(int maximal, int number) } struct creature * -pickrandom(int population) +pickrandom() { int n; @@ -150,13 +148,8 @@ cmp(const void *u, const void *v) } void * -intercourse(void *args) +intercourse() { - - struct intargs *ia = args; - int population = ia->population; - int mutationsrate = ia->mutationrate; - struct creature *c[3]; int i; @@ -167,7 +160,7 @@ intercourse(void *args) qsort(c, 3, sizeof(struct creature *), cmp); for (i = 0; i < c[2]->length; i++) { - if (random() % mutationsrate) + if (random() % mutationrate) c[2]->genom[i] = c[random() % 2]->genom[i]; else c[2]->genom[i] = rndchar(); @@ -183,14 +176,14 @@ intercourse(void *args) } int -success(int number) +success() { struct creature *best = NULL; float fittest = 0; int n; /* find best */ - for (n = 0; n < number; n++) { + for (n = 0; n < population; n++) { cp = generation[n]; if (cp->fitness > fittest) { fittest = cp->fitness; @@ -219,10 +212,7 @@ usage(void) int main(int argc, char **argv) { - struct intargs args; extern int LINES; - int population = 1000; - int mutationrate = 100; /* 1/n */ int nthreads = population / 5; int i, c, t; @@ -262,10 +252,8 @@ main(int argc, char **argv) initscr(); srandom(time(NULL)); - initpopulation(strlen(aim), population); + initpopulation(strlen(aim)); - args.population = population; - args.mutationrate = mutationrate; if (!nthreads) nthreads = 1; threads = calloc(nthreads, sizeof(pthread_t)); @@ -277,9 +265,9 @@ main(int argc, char **argv) for (i = 0;; i++) { move(0, 0); for (t = 0; t < nthreads; t++) - pthread_create(&threads[t], &attr, intercourse, (void *)&args); - printpopulation(LINES - 8, population); - if (success(population)) + pthread_create(&threads[t], &attr, intercourse, NULL); + printpopulation(LINES - 8); + if (success()) break; refresh(); } -- cgit v1.2.3