forked from nsmryan/HEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEA.hs
45 lines (37 loc) · 1.04 KB
/
EA.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module EA(
ea,
ga,
evaluate,
maxGens,
fitnessCap,
rndPopFrom,
module EAMonad
) where
import EAMonad
import Selection
import Randomly
import qualified Data.Traversable as T
import Data.Sequence as S
ea :: EAMonad p e -> (p -> EAMonad p e) -> (p -> EAMonad Bool e) -> EAMonad p e
ea init gen pred = init >>= loopM pred gen' where
gen' p = incGen >> gen p
loopM pred f p = do
b <- pred p
if b then return p else f p >>= loopM pred f
ga init eval select recombine elit pred = let
gen p = recordFitness p >>= select >>= recombine >>= eval
gen' = if elit then elitism gen else gen in
ea (init >>= eval) gen' pred
maxGens gens p = do
curgen <- getGens
return $ gens <= curgen
fitnessCap maxfit p = return $ maxfit <= bestFit p
evaluate eval p = T.mapM eval' p where
eval' i = do
fit <- eval i
return $ (i, fit)
recordFitness p = do
let fit = bestFit p
record $! "BestFit: " ++ show fit ++ "\n"
return (fit `seq` p) --prevents memory leaks
rndPopFrom ps is a = S.replicateM ps $ S.replicateM is $ generateFrom a