module GEP.Random (
randomSymbol,
randomSymbolList,
newIndividual,
newPopulation,
mutate
) where
import GEP.Types
import GEP.Params
import GEP.Rmonad
randomSymbol :: [a]
-> GEPMonad a
randomSymbol syms =
do index <- nextR (length syms)
return (syms !! (index1))
randomSymbolList :: [a]
-> Int
-> GEPMonad [a]
randomSymbolList _ 0 = do return []
randomSymbolList syms n =
do current <- randomSymbol syms
rest <- randomSymbolList syms (n1)
return ([current]++rest)
newIndividual :: Genome
-> Int
-> GEPMonad Chromosome
newIndividual _ 0 = do return []
newIndividual g n =
do hI <- randomSymbolList (allsymbols g) head_len
tI <- randomSymbolList (terminals g) tail_len
otherGenes <- newIndividual g (n1)
return (hI++tI++otherGenes)
where
head_len = headLength g
tail_len = tailLength g
newPopulation :: Genome
-> Int
-> GEPMonad [Chromosome]
newPopulation _ 0 = do return []
newPopulation g n =
do p <- newPopulation g (n1)
i <- newIndividual g (numGenes g)
return ([i]++p)
mutateGene :: Genome -> Rates -> Gene -> GEPMonad Gene
mutateGene g r gene = do
let (h, t) = splitAt (headLength g) gene
hMutated <- mapM mutateHeadSymbol h
tMutated <- mapM mutateTailSymbol t
return $ hMutated ++ tMutated
where
mutateTailSymbol :: Symbol -> GEPMonad Symbol
mutateTailSymbol s = mutateSymbol r s $ terminals g
mutateHeadSymbol :: Symbol -> GEPMonad Symbol
mutateHeadSymbol s = mutateSymbol r s $ allsymbols g
mutateSymbol :: Rates -> Symbol -> [Symbol] -> GEPMonad Symbol
mutateSymbol r s ss =
nextF 1.0 >>= \prob ->
if prob < pMutate r
then randomSymbol ss
else return s
mutate :: Genome -> Rates -> Chromosome -> GEPMonad Chromosome
mutate g r s =
do
genes' <- mapM (\i -> mutateGene g r i) genes
return $ genesToChrom genes'
where
genes = chromToGenes s (geneLength g)