Compare commits
3 Commits
5383356791
...
76493bc362
Author | SHA1 | Date | |
---|---|---|---|
76493bc362 | |||
058bbbfd94 | |||
4b10281941 |
@ -64,6 +64,7 @@ library
|
||||
, HushGP.GP.PushArgs
|
||||
, HushGP.GP.Variation
|
||||
, HushGP.GP.Downsample
|
||||
, HushGP.GP.PushData
|
||||
, HushGP.Problems.IntegerRegression
|
||||
|
||||
|
||||
|
@ -9,6 +9,7 @@ import HushGP.Genome
|
||||
import HushGP.State
|
||||
import HushGP.GP.Variation
|
||||
import HushGP.GP.Downsample
|
||||
import HushGP.GP.PushData
|
||||
import HushGP.Utility
|
||||
|
||||
-- import Debug.Trace (trace, traceStack)
|
||||
@ -22,7 +23,7 @@ generatePopulation pushArgs = do
|
||||
|
||||
-- | Evaluates a population of plushies with the error function passed in via PushArgs and sorts them.
|
||||
-- TODO: Need to make this runnable in parallel too.
|
||||
evaluatePopulation :: PushArgs -> ([[Gene]], [Gene], [Int]) -> [Individual] -> [Individual]
|
||||
evaluatePopulation :: PushArgs -> [PushData] -> [Individual] -> [Individual]
|
||||
evaluatePopulation pushArgs passedTrainingData population = sort $ zipWith updateIndividual (map (errorFunction pushArgs pushArgs passedTrainingData . plushy) population) population
|
||||
|
||||
-- | A helper function used in evaluatePopulation. Takes a [Double] as the error scores and an individual.
|
||||
|
@ -1,8 +1,9 @@
|
||||
module HushGP.GP.PushArgs where
|
||||
|
||||
import HushGP.State
|
||||
import Data.Map qualified as Map
|
||||
import HushGP.Instructions
|
||||
import HushGP.GP.PushData
|
||||
import Data.Map qualified as Map
|
||||
|
||||
-- | The structure holding the arguments for the various aspects
|
||||
-- of the evolutionary run in Hush.
|
||||
@ -46,10 +47,10 @@ data PushArgs = PushArgs
|
||||
elitism :: Bool,
|
||||
-- | 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 2: [PushData] is the input data.
|
||||
-- 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], [Int]) -> [Gene] -> [Double],
|
||||
errorFunction :: PushArgs -> [PushData] -> [Gene] -> [Double],
|
||||
-- | Type of informed downsampling. "solved", "elite", "soft".
|
||||
informedDownsamplingType :: String,
|
||||
-- | List of instructions to use in the evolutionary run.
|
||||
@ -83,9 +84,9 @@ data PushArgs = PushArgs
|
||||
-- | For tournament selection, amount of individuals in each tournament.
|
||||
tournamentSize :: Int,
|
||||
-- | Training data for the gp, must be provided.
|
||||
trainingData :: ([[Gene]], [Gene]),
|
||||
trainingData :: [PushData],
|
||||
-- | Testing data for the gp, must be provided if there is any.
|
||||
testingData :: ([[Gene]], [Gene]),
|
||||
testingData :: [PushData],
|
||||
-- | Addition rate for UMAD (deletion rate derived from this).
|
||||
umadRate :: Float,
|
||||
-- | Genetic operators and probabilities for their use, should sum to one
|
||||
@ -134,8 +135,8 @@ defaultPushArgs = PushArgs {
|
||||
ssxNotBmx = False,
|
||||
stepLimit = 1000,
|
||||
tournamentSize = 5,
|
||||
testingData = ([], []),
|
||||
trainingData = ([], []),
|
||||
testingData = error "Must supply the testingData yourself",
|
||||
trainingData = error "Must supply the trainingData yourself",
|
||||
umadRate = 0.1,
|
||||
variation = Map.fromList [("umad", 1.0)],
|
||||
epsilons = Nothing
|
||||
|
14
src/HushGP/GP/PushData.hs
Normal file
14
src/HushGP/GP/PushData.hs
Normal file
@ -0,0 +1,14 @@
|
||||
module HushGP.GP.PushData where
|
||||
|
||||
import HushGP.State
|
||||
|
||||
data PushData = PushData {
|
||||
inputData :: [Gene],
|
||||
outputData :: Gene,
|
||||
downsampleIndex :: Maybe Int,
|
||||
caseDistances :: Maybe [Double]
|
||||
}
|
||||
|
||||
-- |Utility function: Sets the index of the passed training data.
|
||||
makeIndexedTrainingData :: [PushData] -> [PushData]
|
||||
makeIndexedTrainingData oldData = zipWith (\dat idx -> dat{downsampleIndex = Just idx}) oldData [0..]
|
@ -6,6 +6,7 @@ import Data.Map qualified as Map
|
||||
import HushGP.State
|
||||
import HushGP.Instructions
|
||||
import HushGP.GP.PushArgs
|
||||
import HushGP.GP.PushData
|
||||
import HushGP.Genome
|
||||
import HushGP.Push
|
||||
import HushGP.Instructions.Utility
|
||||
@ -25,12 +26,23 @@ 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])
|
||||
trainData :: [PushData]
|
||||
trainData = map (\num -> PushData {
|
||||
inputData = [GeneInt num],
|
||||
outputData = (GeneInt . targetFunction) num,
|
||||
downsampleIndex = Nothing,
|
||||
caseDistances = Nothing})
|
||||
[-10..10]
|
||||
|
||||
-- | 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]))
|
||||
testData :: [PushData]
|
||||
-- testData = (chunksOf 1 $ map GeneInt $ [-20..(-11)] <> [11..21], map (GeneInt . targetFunction) ([-20..(-11)] <> [11..21]))
|
||||
testData = map (\num -> PushData {
|
||||
inputData = [GeneInt num],
|
||||
outputData = (GeneInt . targetFunction) num,
|
||||
downsampleIndex = Nothing,
|
||||
caseDistances = Nothing})
|
||||
[-20..(-11)] <> [11..21]
|
||||
|
||||
-- | The instructions used in the evolutionary run.
|
||||
runInstructions :: [Gene]
|
||||
@ -57,9 +69,9 @@ loadState plushy vals =
|
||||
(loadProgram (plushyToPush plushy) emptyState){_input = Map.fromList (zip [0..] vals)}
|
||||
|
||||
-- | The error function for a single set of inputs and outputs.
|
||||
intErrorFunction :: PushArgs -> ([[Gene]], [Gene], [Int]) -> [Gene] -> [Double]
|
||||
intErrorFunction _args (inputData, outputData, _) plushy =
|
||||
map abs $ zipWith (-) (map ((fromIntegral @Integer @Double . (errorHead . _int) . interpretExec) . loadState plushy) inputData) (map (fromIntegral @Integer @Double . extractGeneInt) outputData)
|
||||
intErrorFunction :: PushArgs -> [PushData] -> [Gene] -> [Double]
|
||||
intErrorFunction _args pushData plushy =
|
||||
map abs $ zipWith (-) (map ((fromIntegral @Integer @Double . (errorHead . _int) . interpretExec) . loadState plushy) pushData) (map (fromIntegral @Integer @Double . extractGeneInt) outputData)
|
||||
|
||||
intPushArgs :: PushArgs
|
||||
intPushArgs = defaultPushArgs
|
||||
|
@ -30,7 +30,3 @@ thrd (_, _, x) = x
|
||||
-- |Utility function: Converts a tuple to a triple with a passed value.
|
||||
tupleToTriple :: (a, b) -> c -> (a, b, c)
|
||||
tupleToTriple (x, y) z = (x, y, z)
|
||||
|
||||
-- |Utility function: Converts the training data passed in to an indexed representation
|
||||
makeIndexedTrainingData :: ([[Gene]], [Gene]) -> ([[Gene]], [Gene], [Int])
|
||||
makeIndexedTrainingData (inputs, outputs) = (inputs, outputs, [0..(length inputs)])
|
||||
|
Loading…
x
Reference in New Issue
Block a user