Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
56
Task/Evolutionary-algorithm/Red/evolutionary-algorithm.red
Normal file
56
Task/Evolutionary-algorithm/Red/evolutionary-algorithm.red
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Red[]
|
||||
|
||||
; allowed characters
|
||||
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
|
||||
; target string
|
||||
target: "METHINKS IT IS LIKE A WEASEL"
|
||||
; parameter controlling the number of children
|
||||
C: 10
|
||||
; parameter controlling the evolution rate
|
||||
RATE: 0.05
|
||||
|
||||
; compute closeness of 'string' to 'target'
|
||||
fitness: function [string] [
|
||||
sum: 0
|
||||
|
||||
repeat i length? string [
|
||||
if not-equal? pick string i pick target i [
|
||||
sum: sum + 1
|
||||
]
|
||||
]
|
||||
|
||||
sum
|
||||
]
|
||||
|
||||
; return copy of 'string' with mutations, frequency based on given 'rate'
|
||||
mutate: function [string rate] [
|
||||
result: copy string
|
||||
|
||||
repeat i length? result [
|
||||
if rate > random 1.0 [
|
||||
poke result i random/only alphabet
|
||||
]
|
||||
]
|
||||
|
||||
result
|
||||
]
|
||||
|
||||
; create initial random parent
|
||||
parent: ""
|
||||
repeat i length? target [
|
||||
append parent random/only alphabet
|
||||
]
|
||||
|
||||
; main loop, displaying progress
|
||||
while [not-equal? parent target] [
|
||||
print parent
|
||||
children: copy []
|
||||
|
||||
repeat i C [
|
||||
append children mutate parent RATE
|
||||
]
|
||||
sort/compare children function [a b] [lesser? fitness a fitness b]
|
||||
|
||||
parent: pick children 1
|
||||
]
|
||||
print parent
|
||||
Loading…
Add table
Add a link
Reference in a new issue