diff options
author | sanine <sanine.not@pm.me> | 2023-11-29 16:45:14 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-29 16:45:14 -0600 |
commit | d28e8e243d3d6563c0f1036aa0b74e0d0c77c80a (patch) | |
tree | c09770815f64468c65bfb99823bb223879a3b5bb | |
parent | 4c63f3d4dba0c7164cce9559f09dc82612cdee36 (diff) |
implement mutateGenomeRemoveGene
-rw-r--r-- | src/Genome.hs | 12 | ||||
-rw-r--r-- | test/GenomeTest.hs | 29 |
2 files changed, 39 insertions, 2 deletions
diff --git a/src/Genome.hs b/src/Genome.hs index 927005e..e0091b9 100644 --- a/src/Genome.hs +++ b/src/Genome.hs @@ -9,6 +9,7 @@ module Genome , mutateGenomeAddInternal , mutateGenomeRemoveInternal , mutateGenomeAddGene + , mutateGenomeRemoveGene ) where @@ -149,8 +150,6 @@ mutateGenomeAddGene g r = gene = Gene so si w in (g {genes = gene:(genes g)}, r''') - - randomSource :: RandomGen a => Genome -> a -> (NeuronIndex, a) randomSource g r = let @@ -164,3 +163,12 @@ randomSink g r = (idx, r') = randomR (0, numInternal g + numOutput g - 1) r result = if idx < numInternal g then Internal idx else Output $ idx - numInput g in (result, r') + + +-- remove a random gene +mutateGenomeRemoveGene :: RandomGen a => Genome -> a -> (Genome, a) +mutateGenomeRemoveGene g r = + let + (idx, r') = randomR (0, (length $ genes g) - 1) r + (front, _:back) = splitAt idx (genes g) + in (g {genes = front ++ back}, r') diff --git a/test/GenomeTest.hs b/test/GenomeTest.hs index d775467..aa6d310 100644 --- a/test/GenomeTest.hs +++ b/test/GenomeTest.hs @@ -175,4 +175,33 @@ mutationTests = testGroup "mutations" $ validSource (source new) @?= True validSink (sink new) @?= True (w >= -4) && (w <= 4) @?= True + , testCase "remove a gene" $ + let + r = mkStdGen 1 -- randomR (0, 2) produces (1, 2, 0, ...) + genome = Genome + { numInput = 1 + , numInternal = 3 + , numOutput = 1 + , genes = + [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 } + , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 } + , Gene { source = Internal 2, sink = Output 0, weight = 1.0 } + ] + } + (genome', r') = mutateGenomeRemoveGene genome r + (genome'', r'') = mutateGenomeRemoveGene genome r' + (genome''', _) = mutateGenomeRemoveGene genome r'' + in do + genes genome' @?= + [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 } + , Gene { source = Internal 2, sink = Output 0, weight = 1.0 } + ] + genes genome'' @?= + [ Gene { source = Input 0, sink = Internal 0, weight = 1.0 } + , Gene { source = Internal 0, sink = Internal 1, weight = 1.0 } + ] + genes genome''' @?= + [ Gene { source = Internal 0, sink = Internal 1, weight = 1.0 } + , Gene { source = Internal 2, sink = Output 0, weight = 1.0 } + ] ] |