forked from stinaq/WEASEL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaskell.hs
44 lines (35 loc) · 1.18 KB
/
haskell.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
import Control.Monad (liftM, replicateM)
import System.Random (newStdGen, randomRIO)
import Data.List (sortBy)
import Data.Function (on)
generationSize = 100
mutationRate = 0.04
goal = "METHINKS IT IS LIKE A WEASEL"
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
randomString :: IO String
randomString = replicateM (length goal) randomCharacter
randomCharacter :: IO Char
randomCharacter = newStdGen
>> randomRIO (0, length alphabet - 1)
>>= return . (!!) alphabet
mutate :: Char -> IO Char
mutate c = newStdGen
>> (randomRIO (0.0, 1.0) :: IO Float)
>>= \r -> if r < mutationRate then randomCharacter else return c
procreate :: String -> IO [String]
procreate parent = mapM (mapM mutate) (replicate (length goal) parent)
findFittest :: [String] -> String
findFittest = fst . head . reverse . sortBy (compare `on` snd) . map fitness
fitness :: String -> (String, Int)
fitness s = (,) s . length . filter (\(x,y) -> x == y) . zip goal $ s
evolve :: String -> IO String
evolve current
| goal == current = return current
| otherwise = do
progeny <- procreate current
let fittest = findFittest progeny
putStrLn fittest
evolve fittest
main = do
current <- randomString
evolve current