RosettaCodeData/Task/Evolutionary-algorithm/EasyLang/evolutionary-algorithm.easy

38 lines
729 B
Text
Raw Normal View History

2024-04-19 16:56:29 -07:00
target$ = "METHINKS IT IS LIKE A WEASEL"
abc$[] = strchars " ABCDEFGHIJLKLMNOPQRSTUVWXYZ"
P = 0.05
C = 100
func fitness trial$ .
for i to len trial$
res += if substr trial$ i 1 <> substr target$ i 1
.
return res
.
func$ mutate parent$ .
for c$ in strchars parent$
if randomf < P
2024-10-16 18:07:41 -07:00
res$ &= abc$[random len abc$[]]
2024-04-19 16:56:29 -07:00
else
res$ &= c$
.
.
return res$
.
for i to len target$
2024-10-16 18:07:41 -07:00
parent$ &= abc$[random len abc$[]]
2024-04-19 16:56:29 -07:00
.
while fitness parent$ > 0
copies$[] = [ ]
for i to C
copies$[] &= mutate parent$
.
parent$ = copies$[1]
for s$ in copies$[]
if fitness s$ < fitness parent$
parent$ = s$
.
.
step += 1
print step & " " & parent$
.