Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
83
Task/Evolutionary-algorithm/Aime/evolutionary-algorithm.aime
Normal file
83
Task/Evolutionary-algorithm/Aime/evolutionary-algorithm.aime
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue