aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-30 19:26:29 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-30 19:26:29 +0000
commit436c01d5e722dadaa459ae2ec13fb82e72c0784a (patch)
treeaa0a3d720d26b39d76f240636e81c465d7a0702a
parent486179e39baea48095e3f75529451ccb6b8e5321 (diff)
beatify output
-rw-r--r--weasel.c59
1 files changed, 39 insertions, 20 deletions
diff --git a/weasel.c b/weasel.c
index 07527b5..6784c1c 100644
--- a/weasel.c
+++ b/weasel.c
@@ -26,12 +26,13 @@
#include <time.h>
#include <unistd.h>
+enum { Red = 1, Green, Cyan, Blue };
+
pthread_mutex_t mutexsum;
pthread_attr_t attr;
int population = 1000;
int mutationrate = 100; /* 1/n */
-int display = 100;
#if 0
const char defaim[] = "METHINKS IT IS LIKE A WEASEL";
@@ -113,7 +114,18 @@ initpopulation(int length)
void
printcreature(struct creature *c)
{
- printw("%1.3f\t%s\t%d\n", c->fitness, c->genom, c->locked);
+ if (c->locked)
+ attron(COLOR_PAIR(Red));
+ else if (c->fitness > 0.95)
+ ;
+ else if (c->fitness > 0.85)
+ attron(COLOR_PAIR(Green));
+ else if (c->fitness > 0.70)
+ attron(COLOR_PAIR(Cyan));
+ else if (c->fitness > 0.47)
+ attron(COLOR_PAIR(Blue));
+ printw("%1.3f\t%s\n", c->fitness, c->genom);
+ attroff(COLOR_PAIR(Red)|COLOR_PAIR(Blue)|COLOR_PAIR(Cyan)|COLOR_PAIR(Green));
}
void
@@ -124,11 +136,13 @@ printpopulation(struct creature *best)
move(0, 0);
printw("%1.3f\t%s\n\n", 1.0, aim);
+
for (n = 0; n < population; n++) {
printcreature(generation[n]);
if (n >= LINES - 8)
break;
}
+
printw("\n");
printcreature(best);
clrtobot();
@@ -140,7 +154,6 @@ pickrandom()
{
int hasone = 0;
- pthread_mutex_lock(&mutexsum);
while (!hasone) {
cp = generation[random() % population];
if (!cp->locked) {
@@ -148,7 +161,6 @@ pickrandom()
hasone = 1;
}
}
- pthread_mutex_unlock(&mutexsum);
return cp;
}
@@ -173,10 +185,12 @@ intercourse()
struct creature *c[3];
int i;
+ pthread_mutex_lock(&mutexsum);
for (i = 0; i < 3; i++) {
c[i] = pickrandom(population);
assert(c[i]);
}
+ pthread_mutex_unlock(&mutexsum);
qsort(c, 3, sizeof(struct creature *), cmp);
for (i = 0; i < c[2]->length; i++) {
@@ -218,7 +232,7 @@ usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [a <alphabet>] [-m <mutatationrate>] [-p <population>] [-d <display>] [target string]\n",
+ fprintf(stderr, "usage: %s [-n] [-a <alphabet>] [-m <mutatationrate>] [-p <population>] target string]\n",
__progname);
exit(1);
@@ -231,11 +245,12 @@ main(int argc, char **argv)
struct creature *best;
pthread_t *threads;
int i, c, n, nthreads;
+ int display = 1;
alphabet = defalpha;
nthreads = ncpu();
- while ((c = getopt(argc, argv, "a:m:p:d:t:")) != -1)
+ while ((c = getopt(argc, argv, "a:m:np:t:")) != -1)
switch (c) {
case 'a':
alphabet = strdup(optarg);
@@ -246,18 +261,15 @@ main(int argc, char **argv)
usage();
mutationrate = i;
break;
+ case 'n':
+ display = 0;
+ break;
case 'p':
i = atoi(optarg);
if (i < 3)
usage();
population = i;
break;
- case 'd':
- i = atoi(optarg);
- if (i <= 0)
- usage();
- display = i;
- break;
case 't':
i = atoi(optarg);
if (i <= 0)
@@ -281,28 +293,35 @@ main(int argc, char **argv)
srandom(time(NULL));
initpopulation(strlen(aim));
+ if (has_colors())
+ start_color();
+ init_pair(Red, COLOR_RED, COLOR_BLACK);
+ init_pair(Green, COLOR_GREEN, COLOR_BLACK);
+ init_pair(Cyan, COLOR_CYAN, COLOR_BLACK);
+ init_pair(Blue, COLOR_BLUE, COLOR_BLACK);
+
pthread_mutex_init(&mutexsum, NULL);
pthread_attr_init(&attr);
- /* pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); */
+ // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (i = 0; !success(&best); i++) {
for (n = 0; n < nthreads; n++)
pthread_create(&threads[n], &attr, intercourse, NULL);
for (n = 0; n < nthreads; n++)
pthread_join(threads[n], NULL);
- if (i > display) {
- display += i;
+ if (display)
printpopulation(best);
- }
}
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&mutexsum);
- printpopulation(best);
- printw("\nhalted after %d generations with population of %d\n",
- display * i / population, population);
- refresh();
+ if (display) {
+ printpopulation(best);
+ printw("\nhalted after %d generations with population of %d\n",
+ nthreads * i / population, population);
+ refresh();
+ }
endwin();
return 0;