Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -8,7 +8,7 @@ fitness(data t, data b)
|
|||
i = b_length(t);
|
||||
while (i) {
|
||||
i -= 1;
|
||||
f += sign(b_character(t, i) ^ b_character(b, i));
|
||||
f += sign(t[i] ^ b[i]);
|
||||
}
|
||||
|
||||
return f;
|
||||
|
|
@ -23,9 +23,9 @@ mutate(data c, data b, data u)
|
|||
i = 0;
|
||||
while (i < l) {
|
||||
if (drand(15)) {
|
||||
b_append(c, b_character(b, i));
|
||||
b_append(c, b[i]);
|
||||
} else {
|
||||
b_append(c, b_character(u, drand(26)));
|
||||
b_append(c, u[drand(26)]);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ main(void)
|
|||
i = l;
|
||||
while (i) {
|
||||
i -= 1;
|
||||
b_append(b, b_character(u, drand(26)));
|
||||
b_append(b, u[drand(26)]);
|
||||
}
|
||||
|
||||
f = fitness(t, b);
|
||||
|
|
@ -53,9 +53,7 @@ main(void)
|
|||
data n;
|
||||
integer a;
|
||||
|
||||
o_winteger(-4, f);
|
||||
o_text(b_string(b));
|
||||
o_newline();
|
||||
o_form("/lw4/~\n", f, b_string(b));
|
||||
|
||||
n = b;
|
||||
|
||||
|
|
@ -75,9 +73,7 @@ main(void)
|
|||
b = n;
|
||||
}
|
||||
|
||||
o_winteger(-4, f);
|
||||
o_text(b_string(b));
|
||||
o_newline();
|
||||
o_form("/lw4/~\n", f, b_string(b));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
<Cfset theString = 'METHINKS IT IS LIKE A WEASEL'>
|
||||
<cfparam name="parent" default="">
|
||||
<Cfset theAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ">
|
||||
<Cfset fitness = 0>
|
||||
<Cfset children = 3>
|
||||
<Cfset counter = 0>
|
||||
|
||||
<Cfloop from="1" to="#children#" index="child">
|
||||
<Cfparam name="child#child#" default="">
|
||||
<Cfparam name="fitness#child#" default=0>
|
||||
</Cfloop>
|
||||
|
||||
<Cfloop condition="fitness lt 1">
|
||||
|
||||
<Cfset oldparent = parent>
|
||||
<Cfset counter = counter + 1>
|
||||
|
||||
<cfloop from="1" to="#children#" index="child">
|
||||
<Cfset thischild = ''>
|
||||
|
||||
<Cfloop from="1" to="#len(theString)#" index="i">
|
||||
<cfset Mutate = Mid(theAlphabet, RandRange(1, 28), 1)>
|
||||
<cfif fitness eq 0>
|
||||
<Cfset thischild = thischild & mutate>
|
||||
<Cfelse>
|
||||
|
||||
<Cfif Mid(theString, i, 1) eq Mid(variables["child" & child], i, 1)>
|
||||
<Cfset thischild = thischild & Mid(variables["child" & child], i, 1)>
|
||||
<Cfelse>
|
||||
<cfset MutateChance = 1/fitness>
|
||||
<Cfset MutateChanceRand = rand()>
|
||||
<Cfif MutateChanceRand lte MutateChance>
|
||||
<Cfset thischild = thischild & mutate>
|
||||
<Cfelse>
|
||||
<Cfset thischild = thischild & Mid(variables["child" & child], i, 1)>
|
||||
</Cfif>
|
||||
</Cfif>
|
||||
|
||||
</cfif>
|
||||
</Cfloop>
|
||||
|
||||
<Cfset variables["child" & child] = thischild>
|
||||
|
||||
</cfloop>
|
||||
|
||||
<cfloop from="1" to="#children#" index="child">
|
||||
<Cfset thisChildFitness = 0>
|
||||
<Cfloop from="1" to="#len(theString)#" index="i">
|
||||
<Cfif Mid(variables["child" & child], i, 1) eq Mid(theString, i, 1)>
|
||||
<Cfset thisChildFitness = thisChildFitness + 1>
|
||||
</Cfif>
|
||||
</Cfloop>
|
||||
|
||||
<Cfset variables["fitness" & child] = (thisChildFitness)/len(theString)>
|
||||
|
||||
<Cfif variables["fitness" & child] gt fitness>
|
||||
<Cfset fitness = variables["fitness" & child]>
|
||||
<Cfset parent = variables["child" & child]>
|
||||
</Cfif>
|
||||
|
||||
</cfloop>
|
||||
|
||||
<Cfif parent neq oldparent>
|
||||
<Cfoutput>###counter# #numberformat(fitness*100, 99)#% fit: #parent#<br></Cfoutput><cfflush>
|
||||
</Cfif>
|
||||
|
||||
</Cfloop>
|
||||
|
|
@ -5,7 +5,7 @@ enum C = 100; // Number of children in each generation.
|
|||
enum P = 0.05; // Mutation probability.
|
||||
enum fitness = (dchar[] s) => target.zip(s).count!q{ a[0] != a[1] };
|
||||
dchar rnd() { return (uppercase ~ " ")[uniform(0, $)]; }
|
||||
enum mut= (dchar[] s) => s.map!(a=> uniform(0,1.) < P ? rnd : a).array;
|
||||
enum mut = (dchar[] s) => s.map!(a => uniform01 < P ? rnd : a).array;
|
||||
|
||||
void main() {
|
||||
auto parent = target.length.iota.map!(_ => rnd).array;
|
||||
|
|
|
|||
|
|
@ -2,27 +2,26 @@ global target;
|
|||
target = split("METHINKS IT IS LIKE A WEASEL", "");
|
||||
charset = ["A":"Z", " "];
|
||||
p = ones(length(charset), 1) ./ length(charset);
|
||||
parent = discrete_rnd(length(target), charset, p)';
|
||||
mutaterate = 0.01;
|
||||
parent = discrete_rnd(charset, p, length(target), 1);
|
||||
mutaterate = 0.1;
|
||||
|
||||
C = 100;
|
||||
C = 1000;
|
||||
|
||||
function r = fitness(parent, thetarget)
|
||||
r = sum(parent == thetarget) ./ length(thetarget);
|
||||
function r = fitness(parent, target)
|
||||
r = sum(parent == target) ./ length(target);
|
||||
endfunction
|
||||
|
||||
function r = mutate(parent, therate, charset)
|
||||
function r = mutate(parent, mutaterate, charset)
|
||||
r = parent;
|
||||
p = unifrnd(0, 1, length(parent), 1);
|
||||
nmutants = sum( p < therate );
|
||||
nmutants = sum( p < mutaterate );
|
||||
if (nmutants)
|
||||
s = discrete_rnd(nmutants, charset, ones(length(charset), 1) ./ length(charset))';
|
||||
r( p < therate ) = s;
|
||||
s = discrete_rnd(charset, ones(length(charset), 1) ./ length(charset),nmutants,1);
|
||||
r( p < mutaterate ) = s;
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function r = evolve(parent, mutatefunc, fitnessfunc, C, mutaterate, \
|
||||
charset)
|
||||
function r = evolve(parent, mutatefunc, fitnessfunc, C, mutaterate, charset)
|
||||
global target;
|
||||
children = [];
|
||||
for i = 1:C
|
||||
|
|
@ -42,10 +41,11 @@ function printgen(p, t, i)
|
|||
endfunction
|
||||
|
||||
i = 0;
|
||||
|
||||
while( !all(parent == target) )
|
||||
i++;
|
||||
parent = evolve(parent, @mutate, @fitness, C, mutaterate, charset);
|
||||
if ( mod(i, 20) == 0 )
|
||||
if ( mod(i, 1) == 0 )
|
||||
printgen(parent, target, i);
|
||||
endif
|
||||
endwhile
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
/* Weasel.rex - Me thinks thou art a weasel. - G,M.D. - 2/25/2011 */
|
||||
arg C M
|
||||
/* C is the number of children parent produces each generation. */
|
||||
/* M is the mutation rate of each gene (character) */
|
||||
|
||||
call initialize
|
||||
generation = 0
|
||||
do until parent = target
|
||||
most_fitness = fitness(parent)
|
||||
most_fit = parent
|
||||
do C
|
||||
child = mutate(parent, M)
|
||||
child_fitness = fitness(child)
|
||||
if child_fitness > most_fitness then
|
||||
do
|
||||
most_fitness = child_fitness
|
||||
most_fit = child
|
||||
say "Generation" generation": most fit='"most_fit"', fitness="left(most_fitness,4)
|
||||
end
|
||||
end
|
||||
parent = most_fit
|
||||
generation = generation + 1
|
||||
end
|
||||
exit
|
||||
|
||||
initialize:
|
||||
target = "METHINKS IT IS LIKE A WEASEL"
|
||||
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
|
||||
c_length_target = length(target)
|
||||
parent = mutate(copies(" ", c_length_target), 1.0)
|
||||
do i = 1 to c_length_target
|
||||
target_ch.i = substr(target,i,1)
|
||||
end
|
||||
return
|
||||
|
||||
fitness: procedure expose target_ch. c_length_target
|
||||
arg parm_string
|
||||
fitness = 0
|
||||
do i_target = 1 to c_length_target
|
||||
if substr(parm_string,i_target,1) = target_ch.i_target then
|
||||
fitness = fitness + 1
|
||||
end
|
||||
return fitness
|
||||
|
||||
mutate:procedure expose alphabet
|
||||
arg string, parm_mutation_rate
|
||||
result = ""
|
||||
do istr = 1 to length(string)
|
||||
if random(1,1000)/1000 <= parm_mutation_rate then
|
||||
result = result || substr(alphabet,random(1,length(alphabet)),1)
|
||||
else
|
||||
result = result || substr(string,istr,1)
|
||||
end
|
||||
return result
|
||||
|
|
@ -10,4 +10,4 @@ loop (
|
|||
my $parent = alphabet.roll(target.chars).join;
|
||||
$parent ne target;
|
||||
$parent = max :by(&fitness), mutate($parent) xx C
|
||||
) { printf "%6d: '%s'\n", (state $)++, $parent }
|
||||
) { printf "%6d: '%s'\n", $++, $parent }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue