RosettaCodeData/Task/Evolutionary-algorithm/Aime/evolutionary-algorithm.aime
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

83 lines
1.2 KiB
Text

integer
fitness(data t, data b)
{
integer f, i;
f = 0;
i = b_length(t);
while (i) {
i -= 1;
f += sign(b_character(t, i) ^ b_character(b, i));
}
return f;
}
void
mutate(data c, data b, data u)
{
integer i, l;
l = b_length(b);
i = 0;
while (i < l) {
if (drand(15)) {
b_append(c, b_character(b, i));
} else {
b_append(c, b_character(u, drand(26)));
}
i += 1;
}
}
integer
main(void)
{
data b, t, u;
integer f, i, l;
b_cast(t, "METHINK IT IS LIKE A WEASEL");
b_cast(u, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
l = b_length(t);
i = l;
while (i) {
i -= 1;
b_append(b, b_character(u, drand(26)));
}
f = fitness(t, b);
while (f) {
data n;
integer a;
o_winteger(-4, f);
o_text(b_string(b));
o_newline();
n = b;
i = 32;
while (i) {
data c;
i -= 1;
mutate(c, b, u);
a = fitness(t, c);
if (a < f) {
f = a;
n = c;
}
}
b = n;
}
o_winteger(-4, f);
o_text(b_string(b));
o_newline();
return 0;
}