aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-11-30 21:51:59 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-11-30 21:51:59 +0000
commit97b25404cff3d3df2984d19191af1fb53b2a7960 (patch)
treee481b6ce1c097f06c5e2ec03afa93be4dbafe992
parent4711fcec5bc1af344bc3be0239df964046c54104 (diff)
average coloring
-rw-r--r--weasel.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/weasel.c b/weasel.c
index 0f2b1f0..b2fb6f4 100644
--- a/weasel.c
+++ b/weasel.c
@@ -31,7 +31,9 @@ pthread_attr_t attr;
int population = 1000;
int mutationrate = 100; /* 1/n */
-int bad;
+float average;
+
+enum { NORM, GOOD, BAD };
#if 0
const char defaim[] = "METHINKS IT IS LIKE A WEASEL";
@@ -113,19 +115,18 @@ initpopulation(int length)
void
printcreature(struct creature *c)
{
- //double lg[] = { 0.30, 0.48, 0.60, 0.70, 0.78, 0.85, 0.90, 0.95 };
- double lg[] = { 0.30, 0.60, 0.80, 0.90 };
- int i, n;
+ int color = NORM;
- for (i = 0, n = 1; i < sizeof(lg)/sizeof(lg[0]); i++)
- if (c->fitness > lg[i])
- n++;
+ if (c->locked)
+ color = BAD;
+ else if (c->fitness > average)
+ color = GOOD;
- attron(COLOR_PAIR(c->locked ? bad : n));
+ attron(COLOR_PAIR(color));
printw("%1.3f\t%s\n", c->fitness, c->genom);
- attroff(COLOR_PAIR(c->locked ? bad : n));
+ attroff(COLOR_PAIR(color));
}
void
@@ -215,6 +216,8 @@ success(struct creature **best)
float fittest = 0;
int n;
+ average = 0;
+
/* find best */
for (n = 0; n < population; n++) {
cp = generation[n];
@@ -222,8 +225,11 @@ success(struct creature **best)
fittest = cp->fitness;
*best = cp;
}
+ average += cp->fitness;
}
+ average /= population;
+
return (*best)->fitness == 1.0;
}
@@ -239,20 +245,6 @@ usage(void)
}
int
-initcolors(void)
-{
- short c[] = { COLOR_WHITE, COLOR_CYAN, COLOR_YELLOW, COLOR_GREEN, COLOR_WHITE };
- int i;
-
- for (i = 0; i < sizeof(c)/sizeof(c[0]); i++)
- init_pair(i + 1, c[i], COLOR_BLACK);
-
- init_pair(++i, COLOR_BLACK, COLOR_RED);
-
- return i;
-}
-
-int
main(int argc, char **argv)
{
struct creature *best;
@@ -308,7 +300,10 @@ main(int argc, char **argv)
if (has_colors())
start_color();
- bad = initcolors();
+
+ init_pair(NORM, COLOR_WHITE, COLOR_BLACK);
+ init_pair(GOOD, COLOR_GREEN, COLOR_BLACK);
+ init_pair(BAD, COLOR_BLACK, COLOR_RED);
pthread_mutex_init(&mutexsum, NULL);
pthread_attr_init(&attr);