Created
January 21, 2011 14:02
-
-
Save JPMoresmau/789711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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