September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,27 +1,22 @@
|
|||
function evolve(parent,target,mutation_rate,num_children)
|
||||
println("Initial parent is $parent, its fitness is $(fitness(parent,target))")
|
||||
gens=0
|
||||
while parent!=target
|
||||
children=[mutate(parent,mutation_rate) for i=1:num_children]
|
||||
bestfit,best=findmax(map(child->fitness(child,target),children))
|
||||
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")
|
||||
function evolve(parent::String, target::String, mutrate::Float64, nchild::Int)
|
||||
fitness(a, b::String=target) = count(l == t for (l, t) in zip(a, b))
|
||||
function mutate(str::String, rate::Float64=mutrate)
|
||||
L = split(" ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
|
||||
r = collect(rand() < rate ? rand(L) : c for c in str)
|
||||
return string(r...)
|
||||
end
|
||||
println("Initial parent is $parent, its fitness is $(fitness(parent))")
|
||||
gens = 0
|
||||
while parent != target
|
||||
children = collect(mutate(parent, mutrate) for i in 1:nchild)
|
||||
bestfit, best = findmax(fitness.(children))
|
||||
parent = children[best]
|
||||
gens += 1
|
||||
if gens % 10 == 0
|
||||
println("After $gens generations, the new parent is $parent and its fitness is $(fitness(parent))")
|
||||
end
|
||||
end
|
||||
println("After $gens generations, the parent evolved into the target $target")
|
||||
end
|
||||
|
||||
fitness(s1,s2)=count(x->x,convert(Array{Char,1},s1).==convert(Array{Char,1},s2))
|
||||
|
||||
function mutate(s,rate)
|
||||
new_s=""
|
||||
for c in s
|
||||
new_s*=string(rand()<rate?
|
||||
" ABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand(1:27)]:c)
|
||||
end
|
||||
return new_s
|
||||
end
|
||||
|
||||
evolve("IU RFSGJABGOLYWF XSMFXNIABKT","METHINKS IT IS LIKE A WEASEL",0.08998,100)
|
||||
evolve("IU RFSGJABGOLYWF XSMFXNIABKT", "METHINKS IT IS LIKE A WEASEL", 0.08998, 100)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue