From 8fb358e84770f69606f7f27c40cfdf0ce57cd026 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 21 Nov 2023 23:48:32 -0600 Subject: fix warnings and bug in connectNeurons --- src/Mind.hs | 19 ++++++++++--------- test/MindTest.hs | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Mind.hs b/src/Mind.hs index 70b7494..359293c 100644 --- a/src/Mind.hs +++ b/src/Mind.hs @@ -70,7 +70,7 @@ validSource (Network _ h _) (Internal x) = -- insert a new edge into a neuron list, possibly failing insertEdge :: [[Edge]] -> Int -> Edge -> Maybe [[Edge]] insertEdge ns i e - | (inRange (0, length ns) i) = let (front, es:back) = splitAt i ns + | (inRange (0, (length ns)-1) i) = let (front, es:back) = splitAt i ns in Just $ front ++ [e:es] ++ back | otherwise = Nothing @@ -88,6 +88,7 @@ type InputState = ([Float], [Float]) type NewState = [Maybe Float] +output :: Network -> [Float] -> [Float] -> NewState -> [Float] output net input state state'= let numOutput = length $ outputNeurons net @@ -109,8 +110,8 @@ newState net input state = updateValue :: NewState -> Int -> Float -> NewState -updateValue state' index value = - let (front, _:back) = splitAt index state' +updateValue state' i value = + let (front, _:back) = splitAt i state' in front ++ (Just value):back @@ -132,15 +133,15 @@ getValue net inputState state' (Output x) = foldEdges:: Network -> InputState -> NewState -> NeuronIndex -> [Edge] -> (Float, NewState) foldEdges net (input, state) state' sink edges = let - (total, ns) = foldl - (\(total, ns) (Edge (source, w)) -> + (t, ns) = foldl + (\(total, nss) (Edge (source, w)) -> let - (value, ns') = if (sink == source) + (value, nss') = if (sink == source) then (state !! (getNeuronIndex source), ns) - else getValue net (input, state) ns source + else getValue net (input, state) nss source total' = (w * value) + total - in (total', ns') + in (total', nss') ) (0, state') edges - in (tanh total, ns) + in (tanh t, ns) diff --git a/test/MindTest.hs b/test/MindTest.hs index efa79ef..d8b63cf 100644 --- a/test/MindTest.hs +++ b/test/MindTest.hs @@ -57,7 +57,7 @@ networkTests = testGroup "network tests" $ @?= Nothing , testCase "output sink out of range" $ let network = Network 3 [[], []] [[]] - in (connectNeurons network (Input 0) (Output 4) (negate 1.0)) + in (connectNeurons network (Input 0) (Output 1) (negate 1.0)) @?= Nothing -- network computations -- cgit v1.2.1