Permalink
Browse files

breeding the population works

  • Loading branch information...
1 parent 3ed947e commit 97ce9e749fefec56e5ea18b7bb02843aaae2d022 @Swizec committed Aug 11, 2012
Showing with 21 additions and 3 deletions.
  1. +21 −3 code/evolution.hs
View
@@ -5,6 +5,7 @@ import qualified Operators.Basic as Operators
import qualified Selectors.Basic as Selector
import System.Random
+import Data.List
evaluate :: (Num a) => [[Char]] -> [(a, [Char])]
evaluate [x] = [Evaluator.evaluate x]
@@ -21,17 +22,34 @@ mutate' gen xs =
let mutated = mutate gen xs
in (snd $ last mutated, map (fst) mutated)
+breed'::(RandomGen g) => g -> ([Char], [Char]) -> (g, ([Char], [Char]))
+breed' gen (a,b) =
+ let (a', b', gen') = Operators.breed a b gen
+ in (gen', (a',b'))
+
+breed::(RandomGen g, Num a, Ord a) => g -> [(a, [Char])] -> [[Char]]
+breed gen xs =
+ let pairs = map (\ [a,b] -> (a,b)) $ filter (\a -> length a == 2) . (!!0) $
+ map (subsequences) $ permutations $
+ map (snd) $ Selector.select xs
+ (gen', bred) = mapAccumL breed' gen pairs
+ in (map (snd) xs) ++ foldr (\ (a,b) acc -> a:b:acc) [] bred
+
+
-- main loop
population :: (RandomGen g) => Int -> g -> (g, [[Char]])
-population 0 gen = mutate' gen $ take 30 $ Initiator.start_population gen
+population 0 gen = mutate' gen $ take 6 $ Initiator.start_population gen
population n gen =
let (gen', xs) = population (n-1) gen
- in (gen', map (snd) $! Selector.select $ evaluate $ xs)
+-- (new_pop, gen'') = breed gen' $ Selector.select . evaluate xs
+ in (gen', map (snd) $! Selector.select $ evaluate xs)
main = do
randomGen <- newStdGen
- print $ Operators.breed "Hello World" "This is silly" randomGen
+-- print $ Operators.breed "Hello World" "This is silly" randomGen
+
+ print $ length $ breed randomGen $ evaluate $ snd $ population 0 randomGen
-- print $ evaluate $ snd $ population 0 randomGen
-- print $ evaluate $ snd $ population 1 randomGen

0 comments on commit 97ce9e7

Please sign in to comment.