Skip to content

Instantly share code, notes, and snippets.

@JPMoresmau
Created January 21, 2011 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPMoresmau/789711 to your computer and use it in GitHub Desktop.
Save JPMoresmau/789711 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable, FlexibleInstances, MultiParamTypeClasses #-}
module Gen where
import GenProg
import Data.Generics
import Control.Monad
import Control.Monad.Random
import Logic
data E=Nand E E
| Const Bit
| Param
deriving (Show,Eq,Data,Typeable)
eval :: E -> Bit -> Bit
eval (Nand e1 e2) p=nand (eval e1 p,eval e2 p)
eval (Const b) _=b
eval Param p=p
notFitness :: E -> Double
notFitness e=let
resF=eval e F
resT=eval e T
in if resF==T && resT==F
then ((realToFrac $ nodes e)*100)
else 100000000
instance GenProg (Rand StdGen) E where
terminal = do
r<-getRandomR (0,2)
return $ [Const T,Const F,Param] !! r
nonterminal = liftM2 Nand terminal terminal
run=let
params = defaultEvolParams { fitness = notFitness }
g = mkStdGen 0
in cachedBest $ evalRand (evolve params) g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment