RosettaCodeData/Task/Evolutionary-algorithm/11l/evolutionary-algorithm.11l

20 lines
604 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
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++