aboutsummaryrefslogtreecommitdiff
path: root/weasel.c
blob: 2a3a19633f3b81c3796959c4ec13a97cdbb6acde (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* $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>
#include <unistd.h>

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

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)];
}

float
calculatefitness(char *genom, int length)
{
	int i, good;

	good = 0;
	for (i = 0; i <length; i++)
		if (aim[i] == genom[i])
			good++;

	return good / (float)length;
}

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

	TAILQ_INIT(&generation);

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

void
printcreature(struct creature *c)
{
	printw("%1.3f\t%s\n", c->fitness, c->genom);
}

void
printpopulation(int maximal)
{
	printw("%1.3f\t%s\n\n", 1.0, aim);
	TAILQ_FOREACH(cp, &generation, link) {
		printcreature(cp);
		if (maximal-- <= 0)
			break;
	}
	printw("\n");
}

struct creature *
pickrandom(int population)
{
	int n = arc4random() % population;

	cp = TAILQ_FIRST(&generation);
	while (n-- && cp)
		cp = TAILQ_NEXT(cp, link);

	return cp;
}

int
cmp(const void *u, const void *v)
{
	struct creature * const *a = u;
	struct creature * const *b = v;

	return ((*a)->fitness <= (*b)->fitness ? 1 : -1);
}

void
intercourse(int population, int mutationsrate)
{
	struct creature *c[3], *child;
	int i, length;

	for (i = 0; i < 3; i++) {
		c[i] = pickrandom(population);
		assert(c[i]);
	}
	qsort(c, 3, sizeof(struct creature *), cmp);

	length = c[0]->length;
	child = malloc(sizeof(struct creature));
	assert(child);
	child->genom = malloc(length);
	assert(child->genom);

	for (i = 0; i < length; i++) {
		if (arc4random() % mutationsrate == 0)
			child->genom[i] = rndchar();
		else
			child->genom[i] = c[arc4random() % 2]->genom[i];
	}
	child->genom[i] = '\0';
	child->length = length;
	child->fitness = calculatefitness(child->genom, child->length);

#if 0
	printw("\n");
	printcreature(c[0]);
	printcreature(c[1]);
	printcreature(c[2]);
	printw("\n");
	printcreature(child);
	printw("\n");
	refresh();
#endif

	/* insert child & kill weakest */
	TAILQ_INSERT_TAIL(&generation, child, link);
	TAILQ_REMOVE(&generation, c[2], link);
	free(c[2]->genom);
	free(c[2]);
}

int
success()
{
	struct creature *best = NULL;
	float fittest = 0;

	/* find best */
	TAILQ_FOREACH(cp, &generation, link)
		if (cp->fitness > fittest) {
			fittest = cp->fitness;
			best = cp;
		}

	assert(best);
	printcreature(best);
	printw("\n");
		
	return !strcmp(best->genom, aim);
}

int
main()
{
	extern int LINES;
	int population = 1000;
	int mutationrate = 100;		/* 1/n */
	int i;

	initscr();
	setterm("vt220");
	initpopulation(strlen(aim), population);

	for (i = 0;; i++) {
		move(0, 0);
		intercourse(population, mutationrate);
		printpopulation(LINES - 8);
		if (success())
			break;
		refresh();
	}

	printw("halted after %d generations\n", i / population);
	refresh();
	endwin();

	return 0;
}