diff options
author | sanine <sanine.not@pm.me> | 2023-11-29 02:59:51 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-29 02:59:51 -0600 |
commit | a506d45e16922bbda123f92dad55959694ccc2df (patch) | |
tree | 22df35c21398f907f0018c1452d0ffaa00f45cae /test | |
parent | e8d7a5237c666a40e14f3709289329fd8c2cb7d2 (diff) |
implement individual gene mutation functions
Diffstat (limited to 'test')
-rw-r--r-- | test/GenomeTest.hs | 72 | ||||
-rw-r--r-- | test/Main.hs | 2 |
2 files changed, 74 insertions, 0 deletions
diff --git a/test/GenomeTest.hs b/test/GenomeTest.hs new file mode 100644 index 0000000..0a27a94 --- /dev/null +++ b/test/GenomeTest.hs @@ -0,0 +1,72 @@ +module GenomeTest (suite) where + +import Test.Tasty +import Test.Tasty.HUnit +import qualified Mind as M +import Genome +import System.Random + +suite :: TestTree +suite = testGroup "genome tests" $ + [ mutationTests + ] + + +mutationTests :: TestTree +mutationTests = testGroup "mutations" $ + [ testCase "mutating the source of a gene" $ + let + rand = mkStdGen 1 -- randomR generates sequence (1, 2, 0, ...) + genome = Genome { numInput = 2, numInternal = 1, numOutput = 5, genes = [] } + sourceGene = Gene { source = M.Input 0, sink = M.Output 1, weight = 4 } + mutatedGenes = fst $ foldl + (\(list, r) g -> + let (g', r') = mutateGeneSource genome g r + in (list ++ [g'], r') + ) + ([], rand) + (replicate 3 sourceGene) + in + mutatedGenes @?= + [ Gene { source = M.Input 1, sink = M.Output 1, weight = 4 } + , Gene { source = M.Internal 0, sink = M.Output 1, weight = 4 } + , Gene { source = M.Input 0, sink = M.Output 1, weight = 4 } + ] + , testCase "mutating the sink of a gene" $ + let + rand = mkStdGen 1 -- randomR generates sequence (1, 2, 0, ...) + genome = Genome { numInput = 2, numInternal = 1, numOutput = 2, genes = [] } + sourceGene = Gene { source = M.Input 0, sink = M.Output 1, weight = 4 } + mutatedGenes = fst $ foldl + (\(list, r) g -> + let (g', r') = mutateGeneSink genome g r + in (list ++ [g'], r') + ) + ([], rand) + (replicate 3 sourceGene) + in + mutatedGenes @?= + [ Gene { source = M.Input 0, sink = M.Output 0, weight = 4 } + , Gene { source = M.Input 0, sink = M.Output 1, weight = 4 } + , Gene { source = M.Input 0, sink = M.Internal 0, weight = 4 } + ] + , testCase "mutating the weight of a gene" $ + let + rand = mkStdGen 0 -- randomR generates sequence (7.572357,-1.4116564,-7.2413177, ...) + genome = Genome { numInput = 2, numInternal = 1, numOutput = 2, genes = [] } + sourceGene = Gene { source = M.Input 0, sink = M.Output 1, weight = 4 } + mutatedGenes = fst $ foldl + (\(list, r) g -> + let (g', r') = mutateGeneWeight genome g r + in (list ++ [g'], r') + ) + ([], rand) + (replicate 3 sourceGene) + in + mutatedGenes @?= + [ Gene { source = M.Input 0, sink = M.Output 1, weight = 7.572357} + , Gene { source = M.Input 0, sink = M.Output 1, weight = -1.4116564 } + , Gene { source = M.Input 0, sink = M.Internal 1, weight = -7.2413177 } + ] + + ] diff --git a/test/Main.hs b/test/Main.hs index 8847a07..907bbdd 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -3,8 +3,10 @@ module Main (main) where import Test.Tasty import qualified MindTest +import qualified GenomeTest main :: IO () main = defaultMain $ testGroup "all tests" $ [ MindTest.suite + , GenomeTest.suite ] |