From 7abac5e995ed313c1032c0304614e47a958fbe88 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Fri, 7 Mar 2025 01:14:12 -0600 Subject: [PATCH] special int and float instructions --- src/HushGP/Instructions/FloatInstructions.hs | 41 ++++++++++++++-- src/HushGP/Instructions/IntInstructions.hs | 50 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index cbfd4b3..4d439d0 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -164,6 +164,10 @@ instructionFloatShove = instructionShove float instructionFloatIsStackEmpty :: State -> State instructionFloatIsStackEmpty = instructionIsStackEmpty float +-- |Duplicate the top N items from the float stack based on the top int from the int stack. +instructionFloatDupItems :: State -> State +instructionFloatDupItems = instructionDupItems float + -- |Pushes the sin of the top float to the float stack. instructionFloatSin :: State -> State instructionFloatSin state@(State {_float = f1 : fs}) = state {_float = sin f1 : fs} @@ -179,9 +183,40 @@ instructionFloatTan :: State -> State instructionFloatTan state@(State {_float = f1 : fs}) = state {_float = tan f1 : fs} instructionFloatTan state = state --- |Duplicate the top N items from the float stack based on the top int from the int stack. -instructionFloatDupItems :: State -> State -instructionFloatDupItems = instructionDupItems float +-- |Pushes the absolute value of the top float to the float stack. +instructionFloatAbs :: State -> State +instructionFloatAbs state@(State {_float = f1 : fs}) = state {_float = abs f1 : fs} +instructionFloatAbs state = state + +-- |Pushes the exponential of the top float to the float stack. +instructionFloatExp :: State -> State +instructionFloatExp state@(State {_float = f1 : fs}) = state {_float = exp f1 : fs} +instructionFloatExp state = state + +-- |Pushes the log of the top float to the float stack. +instructionFloatLog :: State -> State +instructionFloatLog state@(State {_float = f1 : fs}) = state {_float = log f1 : fs} +instructionFloatLog state = state + +-- |Pushes the squared value of the top float to the float stack. +instructionFloatSquare :: State -> State +instructionFloatSquare state@(State {_float = f1 : fs}) = state {_float = f1 ^ (2 :: Int) : fs} +instructionFloatSquare state = state + +-- |Pushes the cubed value of the top float to the float stack. +instructionFloatCube :: State -> State +instructionFloatCube state@(State {_float = f1 : fs}) = state {_float = f1 ^ (3 :: Int) : fs} +instructionFloatCube state = state + +-- |Pushes the square rooted value of the top float to the float stack. +instructionFloatSqrt :: State -> State +instructionFloatSqrt state@(State {_float = f1 : fs}) = state {_float = sqrt f1 : fs} +instructionFloatSqrt state = state + +-- |Pushes the top float with its sign reversed to the top of the float stack. +instructionFloatReverseSign :: State -> State +instructionFloatReverseSign state@(State {_float = f1 : fs}) = state {_float = (-1) * f1 : fs} +instructionFloatReverseSign state = state allFloatInstructions :: [Gene] allFloatInstructions = map StateFunc ($(functionExtractor "instruction")) diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 5d49a6f..5eb0bcb 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -168,5 +168,55 @@ instructionIntIsStackEmpty = instructionIsStackEmpty int instructionIntDupItems :: State -> State instructionIntDupItems = instructionDupItems int +-- |Pushes the sin of the top int to the int stack. Rounding if needed. +instructionIntSin :: State -> State +instructionIntSin state@(State {_int = i1 : is}) = state {_int = round (sin (fromIntegral @Integer @Double i1)) : is} +instructionIntSin state = state + +-- |Pushes the cos of the top int to the int stack. Rounding if needed. +instructionIntCos :: State -> State +instructionIntCos state@(State {_int = i1 : is}) = state {_int = round (cos (fromIntegral @Integer @Double i1)) : is} +instructionIntCos state = state + +-- |Pushes the tan of the top int to the int stack. Rounding if needed. +instructionIntTan :: State -> State +instructionIntTan state@(State {_int = i1 : is}) = state {_int = round (tan (fromIntegral @Integer @Double i1)) : is} +instructionIntTan state = state + +-- |Pushes the absolute value of the top int to the int stack. +instructionIntAbs :: State -> State +instructionIntAbs state@(State {_int = i1 : is}) = state {_int = abs i1 : is} +instructionIntAbs state = state + +-- |Pushes the exponential of the top int to the int stack. Rounding if needed. +instructionIntExp :: State -> State +instructionIntExp state@(State {_int = i1 : is}) = state {_int = round (exp (fromIntegral @Integer @Double i1)) : is} +instructionIntExp state = state + +-- |Pushes the log of the top int to the int stack. Rounding if needed. +instructionIntLog :: State -> State +instructionIntLog state@(State {_int = i1 : is}) = state {_int = round (log (fromIntegral @Integer @Double i1)) : is} +instructionIntLog state = state + +-- |Pushes the squared value of the top int to the int stack. +instructionIntSquare :: State -> State +instructionIntSquare state@(State {_int = i1 : is}) = state {_int = i1 ^ (2 :: Int) : is} +instructionIntSquare state = state + +-- |Pushes the cubed value of the top int to the int stack. +instructionIntCube :: State -> State +instructionIntCube state@(State {_int = i1 : is}) = state {_int = i1 ^ (3 :: Int) : is} +instructionIntCube state = state + +-- |Pushes the square rooted value of the top int to the int stack. Rounding if needed. +instructionIntSqrt :: State -> State +instructionIntSqrt state@(State {_int = i1 : is}) = state {_int = round (sqrt (fromIntegral @Integer @Double i1)) : is} +instructionIntSqrt state = state + +-- |Pushes the top int with its sign reversed to the top of the int stack. +instructionIntReverseSign :: State -> State +instructionIntReverseSign state@(State {_int = i1 : is}) = state {_int = (-1) * i1 : is} +instructionIntReverseSign state = state + allIntInstructions :: [Gene] allIntInstructions = map StateFunc ($(functionExtractor "instruction"))