aboutsummaryrefslogtreecommitdiff
path: root/weasel.c
blob: ca70e32aa44cb914a360d0180d32d104c157f0a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* $Id$ */
/*
 * Copyright (c) 2009 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>
#include <assert.h>
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>

/* const char aim[] = "METHINKS IT IS LIKE A WEASEL"; */
const char aim[] = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S BACK 1234567890 TIMES.";
const char alphabet[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~";
extern int LINES;
int mutationrate = 100;		/* 1/n */

TAILQ_HEAD(creatures, creature) generation;
struct creature {
	char	*genom;
	int	length;
	float	fitness;
	TAILQ_ENTRY(creature) link;
} *cp;

char
rndchar()
{
	return alphabet[arc4random() % (sizeof(alphabet) - 1)];
}

void
initpopulation(int length, int number)
{
	int i;

	TAILQ_INIT(&generation);

	while (number--) {
		cp = malloc(sizeof(struct creature));
		assert(cp);
		cp->genom = malloc(length + 1);
		assert(cp->genom);
		cp->length = length;
		for (i = 0; i < length; i++)
			cp->genom[i] = rndchar();
		cp->genom[i] = '\0';
		cp->fitness = 0.0;
		TAILQ_INSERT_HEAD(&generation, cp, link);
	}
}

void
sortfitness()
{
	struct creature *next;

	for (cp = TAILQ_FIRST(&generation); cp; cp = next) {
		next = TAILQ_NEXT(cp, link);
		if (next && next->fitness > cp->fitness) {
			TAILQ_REMOVE(&generation, next, link);
			TAILQ_INSERT_BEFORE(cp, next, link);
			next = TAILQ_NEXT(cp, link);
			sortfitness();		/* FIXME */
		}
	}
}

void
calculatefitness()
{
	int i, good;

	TAILQ_FOREACH(cp, &generation, link) {
		good = 0;
		for (i = 0; i < cp->length; i++)
			if (aim[i] == cp->genom[i])
				good++;
		cp->fitness = good / (float)cp->length;
	}
}

void
intercourse(int probability)
{
	int i, length;
	struct creature *parent[2], *child, *next;

	parent[0] = TAILQ_FIRST(&generation);
	parent[1] = TAILQ_NEXT(parent[0], link);
	length = strlen(parent[0]->genom);

	/* produce children */
	TAILQ_FOREACH(cp, &generation, link) {
		child = malloc(sizeof(struct creature));
		assert(child);
		child->genom = malloc(length + 1);
		assert(child->genom);
		child->length = length;

		/* recombine and mutate */
		for (i = 0; i < length; i++) {
			if (arc4random() % probability == 0) {
				child->genom[i] = rndchar();
			} else {
				child->genom[i] = parent[arc4random() % 2]->genom[i];
			}
		}
		child->genom[i] = '\0';
		TAILQ_INSERT_HEAD(&generation, child, link);
	}

	/* remove old generation */
	for (cp = parent[0]; cp; cp = next) {
		next = TAILQ_NEXT(cp, link);
		TAILQ_REMOVE(&generation, cp, link);
		free(cp->genom);
		free(cp);
	}
}

void
printpopulation()
{
	move(0, 0);
	printw("%1.3f\t%s\n\n", 1.0, aim);
	TAILQ_FOREACH(cp, &generation, link)
		printw("%1.3f\t%s\n", cp->fitness, cp->genom);
	refresh();
}

int
main()
{
	int i;

	struct creature *first;
	initscr();
	setterm("vt220");
	initpopulation(strlen(aim), LINES - 5);

	for (i = 0;; i++) {
		intercourse(mutationrate);
		calculatefitness();
		sortfitness();
		first = TAILQ_FIRST(&generation);
		printpopulation();
		if (strcmp(first->genom, aim) == 0)
			break;
	}

	printw("\nhalted after %d generations\n", i);
	refresh();
	endwin();

	return 0;
}