int regression start

This commit is contained in:
Rowan Torbitzky-Lane 2025-02-15 01:24:40 -06:00
parent feddc3cbfe
commit b88a4944f9
4 changed files with 68 additions and 17 deletions

View File

@ -36,7 +36,9 @@ library
-- Modules exported by the library. -- Modules exported by the library.
exposed-modules: HushGP.Push exposed-modules: HushGP.Push
, HushGP.GP , HushGP.TH
, HushGP.Utility
, HushGP.Genome
, HushGP.State , HushGP.State
, HushGP.Instructions , HushGP.Instructions
, HushGP.Instructions.IntInstructions , HushGP.Instructions.IntInstructions
@ -58,10 +60,10 @@ library
, HushGP.PushTests.IntTests , HushGP.PushTests.IntTests
, HushGP.PushTests.GenericTests , HushGP.PushTests.GenericTests
, HushGP.PushTests.UtilTests , HushGP.PushTests.UtilTests
, HushGP.GP
, HushGP.GP.PushArgs , HushGP.GP.PushArgs
, HushGP.TH , HushGP.Problems.IntegerRegression
, HushGP.Utility
, HushGP.Genome
-- Modules included in this library but not exported. -- Modules included in this library but not exported.
-- other-modules: -- other-modules:

View File

@ -4,6 +4,8 @@ import HushGP.State
import Data.Map qualified as Map import Data.Map qualified as Map
import HushGP.Instructions import HushGP.Instructions
-- | The structure holding the arguments for the various aspects
-- of the evolutionary run in Hush.
data PushArgs = PushArgs data PushArgs = PushArgs
{ {
-- | For alternation, std deviation fo index when alternating. -- | For alternation, std deviation fo index when alternating.
@ -42,8 +44,12 @@ data PushArgs = PushArgs
downsampleParentsGens :: Int, downsampleParentsGens :: Int,
-- | Whether or not to add the best individual to the next generation. -- | Whether or not to add the best individual to the next generation.
elitism :: Bool, elitism :: Bool,
-- User must provide their own error function. TODO: This -- | User must provide their own error function.
-- errorFunction :: (PushArgs -> ) -- 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". -- | Type of informed downsampling. "solved", "elite", "soft".
informedDownsamplingType :: String, informedDownsamplingType :: String,
-- | List of instructions to use in the evolutionary run. -- | List of instructions to use in the evolutionary run.
@ -76,10 +82,10 @@ data PushArgs = PushArgs
stepLimit :: Int, stepLimit :: Int,
-- | For tournament selection, amount of individuals in each tournament. -- | For tournament selection, amount of individuals in each tournament.
tournamentSize :: Int, tournamentSize :: Int,
-- Training data for the gp, must be provided. -- | Training data for the gp, must be provided.
-- trainingData :: something trainingData :: [[Gene]],
-- Testing data for the gp, must be provided if there is any. -- | Testing data for the gp, must be provided if there is any.
-- testingData :: something testingData :: [[Gene]],
-- | Addition rate for UMAD (deletion rate derived from this). -- | Addition rate for UMAD (deletion rate derived from this).
umadRate :: Float, umadRate :: Float,
-- | Genetic operators and probabilities for their use, should sum to one -- | Genetic operators and probabilities for their use, should sum to one
@ -87,6 +93,8 @@ data PushArgs = PushArgs
variation :: Map.Map String Float variation :: Map.Map String Float
} }
-- | The default values for which all runs of Hush derive
-- their args from.
defaultPushArgs :: PushArgs defaultPushArgs :: PushArgs
defaultPushArgs = PushArgs { defaultPushArgs = PushArgs {
alignmentDeviation = 2, alignmentDeviation = 2,
@ -106,7 +114,7 @@ defaultPushArgs = PushArgs {
downsampleParentRate = 0.01, downsampleParentRate = 0.01,
downsampleParentsGens = 10, downsampleParentsGens = 10,
elitism = False, elitism = False,
-- errorFunction = something, errorFunction = error "Must supply the error function yourself",
informedDownsamplingType = "solved", informedDownsamplingType = "solved",
instructionList = allInstructions, instructionList = allInstructions,
maxMotelyBatchSize = 10, maxMotelyBatchSize = 10,
@ -124,8 +132,8 @@ defaultPushArgs = PushArgs {
ssxNotBmx = False, ssxNotBmx = False,
stepLimit = 1000, stepLimit = 1000,
tournamentSize = 5, tournamentSize = 5,
-- testingData = [], testingData = [],
-- trainingData = [], trainingData = [],
umadRate = 0.1, umadRate = 0.1,
variation = Map.fromList [("umad", 1.0)] variation = Map.fromList [("umad", 1.0)]
} }

View File

@ -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)}

View File

@ -21,9 +21,9 @@ data Gene
| GeneVectorBool [Bool] | GeneVectorBool [Bool]
| GeneVectorString [String] | GeneVectorString [String]
| GeneVectorChar [Char] | GeneVectorChar [Char]
| -- | State -> State is the function itself. String stores the name of the function. -- | State -> State is the function itself. String stores the name of the function.
StateFunc (State -> State, String) | StateFunc (State -> State, String)
| PlaceInput String | PlaceInput Int
| Close | Close
| Open Int | Open Int
| Skip | Skip
@ -160,7 +160,7 @@ data State = State
_vectorString :: [[String]], _vectorString :: [[String]],
_vectorChar :: [[Char]], _vectorChar :: [[Char]],
_parameter :: [Gene], _parameter :: [Gene],
_input :: Map.Map String Gene _input :: Map.Map Int Gene
} }
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)