From b88a4944f969d717b05ab17e107b9ff7ee6201ec Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Sat, 15 Feb 2025 01:24:40 -0600 Subject: [PATCH] int regression start --- HushGP.cabal | 10 +++--- src/HushGP/GP/PushArgs.hs | 26 +++++++++------ src/HushGP/Problems/IntegerRegression.hs | 41 ++++++++++++++++++++++++ src/HushGP/State.hs | 8 ++--- 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 src/HushGP/Problems/IntegerRegression.hs diff --git a/HushGP.cabal b/HushGP.cabal index 8e34eab..ab43c47 100644 --- a/HushGP.cabal +++ b/HushGP.cabal @@ -36,7 +36,9 @@ library -- Modules exported by the library. exposed-modules: HushGP.Push - , HushGP.GP + , HushGP.TH + , HushGP.Utility + , HushGP.Genome , HushGP.State , HushGP.Instructions , HushGP.Instructions.IntInstructions @@ -58,10 +60,10 @@ library , HushGP.PushTests.IntTests , HushGP.PushTests.GenericTests , HushGP.PushTests.UtilTests + , HushGP.GP , HushGP.GP.PushArgs - , HushGP.TH - , HushGP.Utility - , HushGP.Genome + , HushGP.Problems.IntegerRegression + -- Modules included in this library but not exported. -- other-modules: diff --git a/src/HushGP/GP/PushArgs.hs b/src/HushGP/GP/PushArgs.hs index 7388a6b..a8b8536 100644 --- a/src/HushGP/GP/PushArgs.hs +++ b/src/HushGP/GP/PushArgs.hs @@ -4,6 +4,8 @@ import HushGP.State import Data.Map qualified as Map import HushGP.Instructions +-- | The structure holding the arguments for the various aspects +-- of the evolutionary run in Hush. data PushArgs = PushArgs { -- | For alternation, std deviation fo index when alternating. @@ -42,8 +44,12 @@ data PushArgs = PushArgs downsampleParentsGens :: Int, -- | Whether or not to add the best individual to the next generation. elitism :: Bool, - -- User must provide their own error function. TODO: This - -- errorFunction :: (PushArgs -> ) + -- | User must provide their own error function. + -- Arg 1: PushArgs for the current set of arguments. + -- Arg 2: ([[Gene]], [Gene]) is the input data. Input is the first index and output is the second index. + -- Arg 3: [Gene] is the plushy representation of a program. + -- Returns the error list for a given set of inputs of type [Double]. + errorFunction :: PushArgs -> [[Gene]] -> [Gene] -> [Double], -- | Type of informed downsampling. "solved", "elite", "soft". informedDownsamplingType :: String, -- | List of instructions to use in the evolutionary run. @@ -76,10 +82,10 @@ data PushArgs = PushArgs stepLimit :: Int, -- | For tournament selection, amount of individuals in each tournament. tournamentSize :: Int, - -- Training data for the gp, must be provided. - -- trainingData :: something - -- Testing data for the gp, must be provided if there is any. - -- testingData :: something + -- | Training data for the gp, must be provided. + trainingData :: [[Gene]], + -- | Testing data for the gp, must be provided if there is any. + testingData :: [[Gene]], -- | Addition rate for UMAD (deletion rate derived from this). umadRate :: Float, -- | Genetic operators and probabilities for their use, should sum to one @@ -87,6 +93,8 @@ data PushArgs = PushArgs variation :: Map.Map String Float } +-- | The default values for which all runs of Hush derive +-- their args from. defaultPushArgs :: PushArgs defaultPushArgs = PushArgs { alignmentDeviation = 2, @@ -106,7 +114,7 @@ defaultPushArgs = PushArgs { downsampleParentRate = 0.01, downsampleParentsGens = 10, elitism = False, - -- errorFunction = something, + errorFunction = error "Must supply the error function yourself", informedDownsamplingType = "solved", instructionList = allInstructions, maxMotelyBatchSize = 10, @@ -124,8 +132,8 @@ defaultPushArgs = PushArgs { ssxNotBmx = False, stepLimit = 1000, tournamentSize = 5, - -- testingData = [], - -- trainingData = [], + testingData = [], + trainingData = [], umadRate = 0.1, variation = Map.fromList [("umad", 1.0)] } diff --git a/src/HushGP/Problems/IntegerRegression.hs b/src/HushGP/Problems/IntegerRegression.hs new file mode 100644 index 0000000..b85b7f0 --- /dev/null +++ b/src/HushGP/Problems/IntegerRegression.hs @@ -0,0 +1,41 @@ +module HushGP.Problems.IntegerRegression where + +import HushGP.State +import HushGP.Instructions +import Data.List.Split +import HushGP.GP.PushArgs +import HushGP.Genome +import HushGP.Push +import Data.Map qualified as Map + +-- | The target function for this run. The function the gp +-- is trying to evolve. +targetFunction :: Integer -> Integer +targetFunction x = (x * x * x) + (2 * x) + +-- | The training data for the model. +trainData :: ([[Gene]], [Gene]) +trainData = (chunksOf 1 $ map GeneInt [-10..10], map (GeneInt . targetFunction) [-10..11]) + +-- | The testing data for the model. +testData :: ([[Gene]], [Gene]) +testData = (chunksOf 1 $ map GeneInt $ [-20..(-11)] <> [11..21], map (GeneInt . targetFunction) ([-20..(-11)] <> [11..21])) + +-- | The instructions used to +runInstructions :: [Gene] +runInstructions = + [ + PlaceInput 0, + Close, + GeneInt 1, + GeneInt 0 + ] + <> allIntInstructions + +-- |The error function for a single set of inputs and outputs. +intErrorFunction :: PushArgs -> ([Gene], Gene) -> [Gene] -> [Double] +intErrorFunction args (inputData, outputData) plushy = + head $ _int $ interpretExec loadedState + where + loadedState :: State + loadedState = (loadProgram (plushyToPush plushy) emptyState){_input = Map.fromList (zip [0..] inputData)} diff --git a/src/HushGP/State.hs b/src/HushGP/State.hs index 5dad5a7..87a1d92 100644 --- a/src/HushGP/State.hs +++ b/src/HushGP/State.hs @@ -21,9 +21,9 @@ data Gene | GeneVectorBool [Bool] | GeneVectorString [String] | GeneVectorChar [Char] - | -- | State -> State is the function itself. String stores the name of the function. - StateFunc (State -> State, String) - | PlaceInput String + -- | State -> State is the function itself. String stores the name of the function. + | StateFunc (State -> State, String) + | PlaceInput Int | Close | Open Int | Skip @@ -160,7 +160,7 @@ data State = State _vectorString :: [[String]], _vectorChar :: [[Char]], _parameter :: [Gene], - _input :: Map.Map String Gene + _input :: Map.Map Int Gene } deriving (Show, Eq, Ord)