Skip to content

Commit

Permalink
Added more operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
groscoe committed Oct 21, 2018
1 parent be5d870 commit 93fe590
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Algorithm/Evolutionary/Operators/Mutation.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Algorithm.Evolutionary.Operators.Mutation (
swapAlleles
swapAlleles,
flipBit
) where

import Control.Monad.Random.Class
Expand All @@ -18,3 +19,10 @@ swap i j = swap' (i-1) (j-1)
zipWith (\x y -> if x == f then xs !! s
else if x == s then xs !! f
else y) [0..] xs


flipBit :: MonadRandom m => Double -> [Bool] -> m [Bool]
flipBit mutationProbability = mapM (flipIfSmallerThan mutationProbability)
where flipIfSmallerThan x bit = do
r <- getRandomR (0, 0.1)
pure $ if r <= x then not bit else bit
9 changes: 8 additions & 1 deletion src/Algorithm/Evolutionary/Operators/Recombination.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Algorithm.Evolutionary.Operators.Recombination (
cutAndCrossFill
cutAndCrossFill,
crossover
) where

cutAndCrossFill :: Eq a => Int -> [a] -> [a] -> ([a], [a])
Expand All @@ -10,3 +11,9 @@ cutAndCrossFill n i1 i2 =
r1 = take m $ filter (not . (`elem` l1)) i2
r2 = take m $ filter (not . (`elem` l2)) i1
in (l1 ++ r1, l2 ++ r2)

crossover :: Int -> [a] -> [a] -> ([a], [a])
crossover point xs ys =
let (x1, x2) = splitAt point xs
(y1, y2) = splitAt point ys
in (x1 ++ y2, y1 ++ x2)

0 comments on commit 93fe590

Please sign in to comment.