A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
14
Task/Evolutionary-algorithm/J/evolutionary-algorithm-1.j
Normal file
14
Task/Evolutionary-algorithm/J/evolutionary-algorithm-1.j
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
CHARSET=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
|
||||
NPROG=: 100 NB. number of progeny (C)
|
||||
MRATE=: 0.05 NB. mutation rate
|
||||
|
||||
create =: (?@$&$ { ])&CHARSET NB. creates random list from charset of same shape as y
|
||||
fitness =: +/@:~:"1
|
||||
copy =: # ,:
|
||||
mutate =: &(>: $ ?@$ 0:)(`(,: create))} NB. adverb
|
||||
select =: ] {~ (i. <./)@:fitness NB. select fittest member of population
|
||||
|
||||
nextgen =: select ] , [: MRATE mutate NPROG copy ]
|
||||
while =: conjunction def '(] , (u {:))^:(v {:)^:_ ,:'
|
||||
|
||||
evolve=: nextgen while (0 < fitness) create
|
||||
6
Task/Evolutionary-algorithm/J/evolutionary-algorithm-2.j
Normal file
6
Task/Evolutionary-algorithm/J/evolutionary-algorithm-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
filter=: {: ,~ ({~ i.@>.&.(%&20)@#) NB. take every 20th and last item
|
||||
filter evolve 'METHINKS IT IS LIKE A WEASEL'
|
||||
XXURVQXKQXDLCGFVICCUA NUQPND
|
||||
MEFHINVQQXT IW LIKEUA WEAPEL
|
||||
METHINVS IT IW LIKEUA WEAPEL
|
||||
METHINKS IT IS LIKE A WEASEL
|
||||
27
Task/Evolutionary-algorithm/J/evolutionary-algorithm-3.j
Normal file
27
Task/Evolutionary-algorithm/J/evolutionary-algorithm-3.j
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
CHARSET=: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
|
||||
NPROG=: 100 NB. "C" from specification
|
||||
|
||||
fitness=: +/@:~:"1
|
||||
select=: ] {~ (i. <./)@:fitness NB. select fittest member of population
|
||||
populate=: (?@$&# { ])&CHARSET NB. get random list from charset of same length as y
|
||||
log=: [: smoutput [: ;:inv (('#';'fitness: ';'; ') ,&.> ":&.>)
|
||||
|
||||
mutate=: dyad define
|
||||
idxmut=. I. x >: (*/$y) ?@$ 0
|
||||
(populate idxmut) idxmut"_} y
|
||||
)
|
||||
|
||||
evolve=: monad define
|
||||
target=. y
|
||||
parent=. populate y
|
||||
iter=. 0
|
||||
mrate=. %#y
|
||||
while. 0 < val=. target fitness parent do.
|
||||
if. 0 = 50|iter do. log iter;val;parent end.
|
||||
iter=. iter + 1
|
||||
progeny=. mrate mutate NPROG # ,: parent NB. create progeny by mutating parent copies
|
||||
parent=. target select parent,progeny NB. select fittest parent for next generation
|
||||
end.
|
||||
log iter;val;parent
|
||||
parent
|
||||
)
|
||||
5
Task/Evolutionary-algorithm/J/evolutionary-algorithm-4.j
Normal file
5
Task/Evolutionary-algorithm/J/evolutionary-algorithm-4.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
evolve 'METHINKS IT IS LIKE A WEASEL'
|
||||
#0 fitness: 27 ; YGFDJFTBEDB FAIJJGMFKDPYELOA
|
||||
#50 fitness: 2 ; MEVHINKS IT IS LIKE ADWEASEL
|
||||
#76 fitness: 0 ; METHINKS IT IS LIKE A WEASEL
|
||||
METHINKS IT IS LIKE A WEASEL
|
||||
Loading…
Add table
Add a link
Reference in a new issue