summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-29 16:25:38 -0600
committersanine <sanine.not@pm.me>2023-11-29 16:25:38 -0600
commit4c63f3d4dba0c7164cce9559f09dc82612cdee36 (patch)
tree46204da85448e7ca97cb5ac2aaa90134c9c7e23c /test
parent3047e46f3478e20250556de833c1b6dd1b1a31f0 (diff)
implement mutateGenomeAddGene
Diffstat (limited to 'test')
-rw-r--r--test/GenomeTest.hs94
1 files changed, 62 insertions, 32 deletions
diff --git a/test/GenomeTest.hs b/test/GenomeTest.hs
index 8448133..d775467 100644
--- a/test/GenomeTest.hs
+++ b/test/GenomeTest.hs
@@ -2,9 +2,11 @@ module GenomeTest (suite) where
import Test.Tasty
import Test.Tasty.HUnit
-import qualified Mind as M
+import Mind (NeuronIndex (..))
import Genome
import System.Random
+import Data.Ix
+
suite :: TestTree
suite = testGroup "genome tests" $
@@ -18,7 +20,7 @@ mutationTests = testGroup "mutations" $
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 }
+ sourceGene = Gene { source = Input 0, sink = Output 1, weight = 4 }
mutatedGenes = fst $ foldl
(\(list, r) g ->
let (g', r') = mutateGeneSource genome g r
@@ -28,15 +30,15 @@ mutationTests = testGroup "mutations" $
(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 }
+ [ Gene { source = Input 1, sink = Output 1, weight = 4 }
+ , Gene { source = Internal 0, sink = Output 1, weight = 4 }
+ , Gene { source = Input 0, sink = 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 }
+ sourceGene = Gene { source = Input 0, sink = Output 1, weight = 4 }
mutatedGenes = fst $ foldl
(\(list, r) g ->
let (g', r') = mutateGeneSink genome g r
@@ -46,15 +48,15 @@ mutationTests = testGroup "mutations" $
(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 }
+ [ Gene { source = Input 0, sink = Output 0, weight = 4 }
+ , Gene { source = Input 0, sink = Output 1, weight = 4 }
+ , Gene { source = Input 0, sink = 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 }
+ sourceGene = Gene { source = Input 0, sink = Output 1, weight = 4 }
mutatedGenes = fst $ foldl
(\(list, r) g ->
let (g', r') = mutateGeneWeight genome g r
@@ -63,9 +65,9 @@ mutationTests = testGroup "mutations" $
([], 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 }
+ [ Gene { source = Input 0, sink = Output 1, weight = 7.572357}
+ , Gene { source = Input 0, sink = Output 1, weight = -1.4116564 }
+ , Gene { source = Input 0, sink = Output 1, weight = -7.2413177 }
]
approxEqual a b = abs (a-b) < 0.0001
in do
@@ -82,9 +84,9 @@ mutationTests = testGroup "mutations" $
, numInternal = 2
, numOutput = 1
, genes =
- [ Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Output 0, weight = 1.0 }
+ [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Output 0, weight = 1.0 }
]
}
r = mkStdGen 5
@@ -103,10 +105,10 @@ mutationTests = testGroup "mutations" $
, numInternal = 3
, numOutput = 1
, genes =
- [ Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Internal 2, weight = 1.0 }
- , Gene { source = M.Internal 2, sink = M.Output 0, weight = 1.0 }
+ [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Internal 2, weight = 1.0 }
+ , Gene { source = Internal 2, sink = Output 0, weight = 1.0 }
]
}
(genome', r') = mutateGenomeRemoveInternal genome r
@@ -126,23 +128,51 @@ mutationTests = testGroup "mutations" $
(numOutput genome''') @?= (numOutput genome)
(genes genome') @?=
- [ Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Output 0, weight = 1.0 }
+ [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Output 0, weight = 1.0 }
]
(genes genome'') @?=
- [ Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Output 0, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Output 0, weight = 1.0 }
+ [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Output 0, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Output 0, weight = 1.0 }
]
(genes genome''') @?=
- [ Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Input 0, sink = M.Internal 0, weight = 1.0 }
- , Gene { source = M.Internal 0, sink = M.Internal 1, weight = 1.0 }
- , Gene { source = M.Internal 1, sink = M.Output 0, weight = 1.0 }
+ [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Input 0, sink = Internal 0, weight = 1.0 }
+ , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 }
+ , Gene { source = Internal 1, sink = Output 0, weight = 1.0 }
]
+ , testCase "add new gene" $
+ let
+ genome = Genome
+ { numInput = 1
+ , numInternal = 2
+ , numOutput = 1
+ , genes = []
+ }
+ r = mkStdGen 5
+ (genome', _) = mutateGenomeAddGene genome r
+ new = last $ genes genome'
+ -- checking sources
+ validSource (Input x) = inRange (0, numInput genome') x
+ validSource (Internal x) = inRange (0, numInternal genome') x
+ validSource (Output _) = False
+ -- checking sinks
+ validSink (Input _) = False
+ validSink (Internal x) = inRange(0, numInternal genome') x
+ validSink (Output x) = inRange(0, numOutput genome') x
+ w = weight new
+ in do
+ (numInput genome') @?= (numInput genome)
+ (numInternal genome') @?= (numInternal genome)
+ (numOutput genome') @?= (numOutput genome)
+ (length $ genes genome') @?= 1 + (length $ genes genome)
+ validSource (source new) @?= True
+ validSink (sink new) @?= True
+ (w >= -4) && (w <= 4) @?= True
]