June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,24 @@
|
|||
fitness(a::AbstractString, b::AbstractString) = count(l == t for (l, t) in zip(a, b))
|
||||
function mutate(str::AbstractString, rate::Float64)
|
||||
L = collect(Char, " ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
return map(str) do c
|
||||
if rand() < rate rand(L) else c end
|
||||
end
|
||||
end
|
||||
|
||||
function evolve(parent::String, target::String, mutrate::Float64, nchild::Int)
|
||||
println("Initial parent is $parent, its fitness is $(fitness(parent, target))")
|
||||
gens = 0
|
||||
while parent != target
|
||||
children = collect(mutate(parent, mutrate) for i in 1:nchild)
|
||||
bestfit, best = findmax(fitness.(children, target))
|
||||
parent = children[best]
|
||||
gens += 1
|
||||
if gens % 10 == 0
|
||||
println("After $gens generations, the new parent is $parent and its fitness is $(fitness(parent, target))")
|
||||
end
|
||||
end
|
||||
println("After $gens generations, the parent evolved into the target $target")
|
||||
end
|
||||
|
||||
evolve("IU RFSGJABGOLYWF XSMFXNIABKT", "METHINKS IT IS LIKE A WEASEL", 0.08998, 100)
|
||||
Loading…
Add table
Add a link
Reference in a new issue