Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
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
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue