diff --git a/src/HushGP/Genome.hs b/src/HushGP/Genome.hs index 7fb90e8..f9a027a 100644 --- a/src/HushGP/Genome.hs +++ b/src/HushGP/Genome.hs @@ -6,6 +6,21 @@ import Data.Map qualified as Map import HushGP.Instructions.Opens import HushGP.State import HushGP.Utility +import HushGP.Instructions + +tempPlushy :: [Gene] +tempPlushy = [ + StateFunc (instructionIntDiv, "instructionIntDiv"), + StateFunc (instructionExecDup, "instructionExecDup"), + GeneInt 1, + GeneInt 0, + StateFunc (instructionIntDiv, "instructionIntDiv"), + GeneInt (-15), + StateFunc (instructionIntSub, "instructionIntSub"), + StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), + Close, + Close + ] -- | Makes a random plushy from variables in a passed argMap and -- a passed list of instructions. @@ -50,31 +65,26 @@ plushyToPush plushy = plushyToPush' (concatMap (\x -> if isOpenerList x then x < -- | Internal function used to convert a plushy genome with opens in it into a push genome. plushyToPush' :: [Gene] -> [Gene] -> [Gene] -plushyToPush' openPlushy push = - if null openPlushy - then - if any isOpen push +plushyToPush' openPlushy push + | null openPlushy = if any isOpen push then plushyToPush' [Close] push else push - else - if firstPlushy == Close - then - if any isOpen push - then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> postOpen else preOpen <> postOpen <> [decOpen (Open (numOpen (push !! openIndex)))]) + | firstPlushy == Close = if any isOpen push + then plushyToPush' (drop 1 openPlushy) (if numOpen (push !! openIndex) == 1 then preOpen <> [Block postOpen] else preOpen <> [Block (postOpen <> [decOpen (Open (numOpen (push !! openIndex)))])]) else plushyToPush' (drop 1 openPlushy) push - else plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) + | otherwise = plushyToPush' (drop 1 openPlushy) (push <> [firstPlushy]) where - firstPlushy :: Gene - firstPlushy = - case uncons openPlushy of - Just (g, _) -> g - _ -> error "This shouldn't happen" - postOpen :: [Gene] - postOpen = reverse (takeWhile (not . isOpen) (reverse push)) - openIndex :: Int - openIndex = length push - length postOpen - 1 - numOpen :: Gene -> Int - numOpen (Open n) = n - numOpen _ = 0 - preOpen :: [Gene] - preOpen = take openIndex push + firstPlushy :: Gene + firstPlushy + = case uncons openPlushy of + Just (g, _) -> g + _ -> error "This shouldn't happen" + postOpen :: [Gene] + postOpen = reverse (takeWhile (not . isOpen) (reverse push)) + openIndex :: Int + openIndex = length push - length postOpen - 1 + numOpen :: Gene -> Int + numOpen (Open n) = n + numOpen _ = 0 + preOpen :: [Gene] + preOpen = take openIndex push diff --git a/src/HushGP/Instructions.hs b/src/HushGP/Instructions.hs index 6a8859d..ab701c7 100644 --- a/src/HushGP/Instructions.hs +++ b/src/HushGP/Instructions.hs @@ -31,10 +31,16 @@ import HushGP.Instructions.VectorIntInstructions import HushGP.Instructions.VectorStringInstructions import HushGP.State +noOpStateFunc :: Gene +noOpStateFunc = StateFunc (instructionNoOp, "instructionNoOp") + +noOpStateFuncBlock :: Gene +noOpStateFuncBlock = StateFunc (instructionNoOpBlock, "instructionNoOpBlock") + -- | All of the instructions declared in all the instruction submodules allInstructions :: [Gene] allInstructions = - allIntInstructions + noOpStateFunc : noOpStateFuncBlock : allIntInstructions <> allFloatInstructions <> allBoolInstructions <> allCharInstructions diff --git a/src/HushGP/Instructions/GenericInstructions.hs b/src/HushGP/Instructions/GenericInstructions.hs index 5a557a5..4d43fa9 100644 --- a/src/HushGP/Instructions/GenericInstructions.hs +++ b/src/HushGP/Instructions/GenericInstructions.hs @@ -9,6 +9,14 @@ import Data.List.Split -- import Debug.Trace +-- |Does No Operation. Useful for genome stuff :) +instructionNoOpBlock :: State -> State +instructionNoOpBlock state = state + +-- |Does No Operation. Just evolve fodder. +instructionNoOp :: State -> State +instructionNoOp state = state + -- |Duplicates the top of a stack based on a lens. instructionDup :: Lens' State [a] -> State -> State instructionDup accessor state = diff --git a/src/HushGP/Instructions/Opens.hs b/src/HushGP/Instructions/Opens.hs index d4334d5..ab8c837 100644 --- a/src/HushGP/Instructions/Opens.hs +++ b/src/HushGP/Instructions/Opens.hs @@ -2,6 +2,7 @@ module HushGP.Instructions.Opens where import HushGP.State import Data.Map qualified as Map +import HushGP.Instructions.GenericInstructions import HushGP.Instructions.ExecInstructions import HushGP.Instructions.StringInstructions import HushGP.Instructions.VectorIntInstructions @@ -36,5 +37,6 @@ instructionOpens = Map.fromList [ (StateFunc (instructionVectorFloatIterate, "instructionVectorFloatIterate"), 1), (StateFunc (instructionVectorStringIterate, "instructionVectorStringIterate"), 1), (StateFunc (instructionVectorBoolIterate, "instructionVectorBoolIterate"), 1), - (StateFunc (instructionVectorCharIterate, "instructionVectorCharIterate"), 1) + (StateFunc (instructionVectorCharIterate, "instructionVectorCharIterate"), 1), + (StateFunc (instructionNoOpBlock, "instructionNoOpBlock"), 1) ]