diff options
-rw-r--r-- | src/Genome.hs | 11 | ||||
-rw-r--r-- | test/GenomeTest.hs | 34 |
2 files changed, 38 insertions, 7 deletions
diff --git a/src/Genome.hs b/src/Genome.hs index 4fb063c..c3fc361 100644 --- a/src/Genome.hs +++ b/src/Genome.hs @@ -5,6 +5,8 @@ module Genome , mutateGeneSink , mutateGeneWeight , mutateGene + + , mutateGenomeAddInternal ) where @@ -67,3 +69,12 @@ mutateGene :: RandomGen a => Genome -> Gene -> a -> (Gene, a) mutateGene genome g r = let (f, r') = randomChoice [mutateGeneSource, mutateGeneSink, mutateGeneWeight] r in f genome g r' + +-- add new internal neuron +mutateGenomeAddInternal :: RandomGen a => Genome -> a -> (Genome, a) +mutateGenomeAddInternal genome r = (genome { numInternal = 1 + numInternal genome }, r) + + +-- remove an internal neuron, decrementing sources and incrementing sinks +mutateGenomeRemoveInternal :: RandomGen a => Genome -> a -> (Genome, a) +mutateGenomeRemoveInternal = undefined diff --git a/test/GenomeTest.hs b/test/GenomeTest.hs index a09e742..56499a1 100644 --- a/test/GenomeTest.hs +++ b/test/GenomeTest.hs @@ -63,16 +63,36 @@ 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 = 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) - ) + (map weight mutatedGenes) + (map weight expected) + ) + , testCase "insert new internal neuron" $ + let + genome = Genome + { numInput = 1 + , 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 } + ] + } + r = mkStdGen 5 + (genome', r') = mutateGenomeAddInternal genome r + in do + r' @?= r + (numInput genome') @?= (numInput genome) + (numInternal genome') @?= (1 + numInternal genome) + (numOutput genome') @?= (numOutput genome) + (genes genome') @?= (genes genome) ] |