20 lines
604 B
Text
20 lines
604 B
Text
|
|
V target = Array(‘METHINKS IT IS LIKE A WEASEL’)
|
|||
|
|
V alphabet = ‘ ABCDEFGHIJLKLMNOPQRSTUVWXYZ’
|
|||
|
|
V p = 0.05
|
|||
|
|
V c = 100
|
|||
|
|
|
|||
|
|
F neg_fitness(trial)
|
|||
|
|
R sum(zip(trial, :target).map((t, h) -> Int(t != h)))
|
|||
|
|
|
|||
|
|
F mutate(parent)
|
|||
|
|
R parent.map(ch -> (I random:() < :p {random:choice(:alphabet)} E ch))
|
|||
|
|
|
|||
|
|
V parent = (0 .< target.len).map(_ -> random:choice(:alphabet))
|
|||
|
|
V i = 0
|
|||
|
|
print((‘#3’.format(i))‘ ’parent.join(‘’))
|
|||
|
|
L parent != target
|
|||
|
|
V copies = ((0 .< c).map(_ -> mutate(:parent)))
|
|||
|
|
parent = min(copies, key' x -> neg_fitness(x))
|
|||
|
|
print((‘#3’.format(i))‘ ’parent.join(‘’))
|
|||
|
|
i++
|