summaryrefslogtreecommitdiff
path: root/test/GenomeTest.hs
blob: a09e742afa3988b525f994f937cb6a01041d978d (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)
      expected =
      	[ 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.Output 1, weight = -7.2413177  }
      	]
      approxEqual a b = abs (a-b) < 0.0001
    in do
      (map source mutatedGenes) @?= (map source expected)
      (map sink mutatedGenes) @?= (map sink expected)
      True @?= foldl (&&) True (zipWith approxEqual
      	(map weight mutatedGenes)
      	(map weight expected)
      	)
  ]