packages feed

genetics (empty) → 0.0.2

raw patch · 4 files changed

+93/−0 lines, 4 filesdep +basedep +random-fusetup-changed

Dependencies added: base, random-fu

Files

+ LICENSE view
@@ -0,0 +1,23 @@+FreeBSD License++Copyright 2011 Andrew Pennebaker. All rights reserved.++Redistribution and use in source and binary forms, with or without modification,+are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this+list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright notice,+this list of conditions and the following disclaimer in the documentation and/or+other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT+SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ genetics.cabal view
@@ -0,0 +1,19 @@+Name:           genetics+Version:        0.0.2+Category:       Machine Learning+Synopsis:       A Genetic Algorithm library+Description:    A Gene typeclass for genetic algorithms+License:        BSD3+License-file:   LICENSE+Author:         Andrew Pennebaker+Maintainer:     andrew.pennebaker@gmail.com+Build-Type:     Simple+Cabal-Version:  >=1.6++source-repository head+  type:     git+  location: https://github.com/mcandre/genetics++Executable hellogenetics+    Main-is:        hellogenetics.hs+    Build-Depends:  base >= 4.3.1.0 && < 5, random-fu >= 0.1.4 && < 0.3
+ hellogenetics.hs view
@@ -0,0 +1,49 @@+#!/usr/bin/env runhaskell++{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}++import Data.Random (RVar, runRVar)+import Data.Random.List (randomElement)+import Data.Random.Source.DevRandom++import Genetics+import Control.Monad (replicateM)+import Data.Char (ord, chr)++target :: String+target = "Hello World!"++generations :: Int+generations = 2 ^ 16++indexSpace :: [Int]+indexSpace = [0 .. length target - 1]++randomIndex :: IO Int+randomIndex = runRVar (randomElement indexSpace) DevRandom++charSpace :: String+charSpace = [' ' .. '~']++randomChar :: IO Char+randomChar = runRVar (randomElement charSpace) DevRandom++randomGene :: IO String+randomGene = replicateM (length target) randomChar++instance Gene String where+  fitness gene = sum (zipWith (\t g -> if t == g then 1 else 0) target gene) / fromIntegral (length target)++  mutate gene = do+    i <- randomIndex+    c <- randomChar+    return $ take i gene ++ [c] ++ drop (i + 1) gene++  species _ = 8++main :: IO ()+main = do+  pool <- replicateM (species [""]) randomGene+  pool' <- evolve generations pool+  putStrLn $ best pool'