From 2ba43396d7536b430562e1e182b8da512fac657d Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Wed, 22 Jan 2025 13:23:42 -0600 Subject: [PATCH] more instructions, need to test instructionCodeN --- src/Instructions/CodeInstructions.hs | 24 ++++++++++++++++++++++++ test/Main.hs | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Instructions/CodeInstructions.hs b/src/Instructions/CodeInstructions.hs index b01f257..363673c 100644 --- a/src/Instructions/CodeInstructions.hs +++ b/src/Instructions/CodeInstructions.hs @@ -12,6 +12,10 @@ blockLength :: Gene -> Int blockLength (Block xs) = length xs blockLength _ = 1 +blockIsNull :: Gene -> Bool +blockIsNull (Block xs) = null xs +blockIsNull _ = False + -- I think I can abstract the boilerplate base case check for a lot of these -- with a different function @@ -40,6 +44,11 @@ codeCombine (Block xs) ygene = Block (xs <> [ygene]) codeCombine xgene (Block ys) = Block (xgene : ys) codeCombine xgene ygene = Block [xgene, ygene] +codeMember :: Gene -> Gene -> Bool +codeMember (Block _) (Block _) = False -- Can't compare two lists with `elem` +codeMember (Block xs) ygene = ygene `elem` xs +codeMember _ _ = False + instructionCodePop :: State -> State instructionCodePop state = instructionPop state code @@ -136,3 +145,18 @@ instructionCodeIf state = state instructionCodeWhen :: State -> State instructionCodeWhen state@(State {_code = (c1 : cs), _bool = (b1 : bs), _exec = es}) = state{_code = cs, _bool = bs, _exec = if b1 then c1 : es else es} instructionCodeWhen state = state + +instructionCodeMember :: State -> State +instructionCodeMember state@(State {_code = (c1 : c2 : cs), _bool = bs}) = state{_code = cs, _bool = codeMember c1 c2 : bs} +instructionCodeMember state = state + +-- https://erp12.github.io/pyshgp/html/core_instructions.html#code-nth +instructionCodeN :: State -> State +instructionCodeN state@(State {_code = (c1 : cs), _int = (_ : is)}) = + if not $ blockIsNull c1Block + then state {_code = c1Block : cs, _int = is} + else state + where + c1Block :: Gene + c1Block = if not $ isBlock c1 then Block [c1] else c1 +instructionCodeN state = state diff --git a/test/Main.hs b/test/Main.hs index 425ae96..79026f3 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -107,4 +107,7 @@ main = do intTestFunc "instructionCodeIfTrue" [6] [GeneBool True, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeIfFalse" [3] [GeneBool False, StateFunc instructionCodeFromExec, GeneInt 3, StateFunc instructionCodeFromExec, GeneInt 6, StateFunc instructionCodeIf] emptyState intTestFunc "instructionCodeWhen" [6, 3, 6] [GeneInt 6, GeneInt 3, GeneInt 4, GeneInt 2, GeneBool True, StateFunc instructionCodeFromExec, StateFunc instructionIntAdd, StateFunc instructionCodeWhen] emptyState - -- stopped for the night at https://erp12.github.io/pyshgp/html/core_instructions.html#code-member + boolTestFunc "instructionCodeMemberTrue" [True] [StateFunc instructionCodeFromExec, GeneInt 2, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMemberFalse" [False] [StateFunc instructionCodeFromExec, GeneInt 7, StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState + boolTestFunc "instructionCodeMember2Blocks" [False] [StateFunc instructionCodeFromExec, Block [GeneInt 7, GeneInt 0], StateFunc instructionCodeFromExec, Block [GeneFloat 3.6, GeneInt 2, GeneIntVector [8, 9]], StateFunc instructionCodeMember] emptyState +