Compare commits

..

No commits in common. "76493bc3621d8311816f950fdc71d7c7d27e298e" and "5383356791286fd127346b66831e8c83c73877fa" have entirely different histories.

6 changed files with 19 additions and 44 deletions

View File

@ -64,7 +64,6 @@ library
, HushGP.GP.PushArgs
, HushGP.GP.Variation
, HushGP.GP.Downsample
, HushGP.GP.PushData
, HushGP.Problems.IntegerRegression

View File

@ -9,7 +9,6 @@ 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)
@ -23,7 +22,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 -> [PushData] -> [Individual] -> [Individual]
evaluatePopulation :: PushArgs -> ([[Gene]], [Gene], [Int]) -> [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.

View File

@ -1,9 +1,8 @@
module HushGP.GP.PushArgs where
import HushGP.State
import HushGP.Instructions
import HushGP.GP.PushData
import Data.Map qualified as Map
import HushGP.Instructions
-- | The structure holding the arguments for the various aspects
-- of the evolutionary run in Hush.
@ -47,10 +46,10 @@ data PushArgs = PushArgs
elitism :: Bool,
-- | User must provide their own error function.
-- Arg 1: PushArgs for the current set of arguments.
-- Arg 2: [PushData] is the input data.
-- 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 -> [PushData] -> [Gene] -> [Double],
errorFunction :: PushArgs -> ([[Gene]], [Gene], [Int]) -> [Gene] -> [Double],
-- | Type of informed downsampling. "solved", "elite", "soft".
informedDownsamplingType :: String,
-- | List of instructions to use in the evolutionary run.
@ -84,9 +83,9 @@ data PushArgs = PushArgs
-- | For tournament selection, amount of individuals in each tournament.
tournamentSize :: Int,
-- | Training data for the gp, must be provided.
trainingData :: [PushData],
trainingData :: ([[Gene]], [Gene]),
-- | Testing data for the gp, must be provided if there is any.
testingData :: [PushData],
testingData :: ([[Gene]], [Gene]),
-- | Addition rate for UMAD (deletion rate derived from this).
umadRate :: Float,
-- | Genetic operators and probabilities for their use, should sum to one
@ -135,8 +134,8 @@ defaultPushArgs = PushArgs {
ssxNotBmx = False,
stepLimit = 1000,
tournamentSize = 5,
testingData = error "Must supply the testingData yourself",
trainingData = error "Must supply the trainingData yourself",
testingData = ([], []),
trainingData = ([], []),
umadRate = 0.1,
variation = Map.fromList [("umad", 1.0)],
epsilons = Nothing

View File

@ -1,14 +0,0 @@
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..]

View File

@ -6,7 +6,6 @@ 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
@ -26,23 +25,12 @@ targetFunction :: Integer -> Integer
targetFunction x = (x * x * x) + (2 * x)
-- | The training data for the model.
trainData :: [PushData]
trainData = map (\num -> PushData {
inputData = [GeneInt num],
outputData = (GeneInt . targetFunction) num,
downsampleIndex = Nothing,
caseDistances = Nothing})
[-10..10]
trainData :: ([[Gene]], [Gene])
trainData = (chunksOf 1 $ map GeneInt [-10..10], map (GeneInt . targetFunction) [-10..11])
-- | The testing data for the model.
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]
testData :: ([[Gene]], [Gene])
testData = (chunksOf 1 $ map GeneInt $ [-20..(-11)] <> [11..21], map (GeneInt . targetFunction) ([-20..(-11)] <> [11..21]))
-- | The instructions used in the evolutionary run.
runInstructions :: [Gene]
@ -69,9 +57,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 -> [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)
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)
intPushArgs :: PushArgs
intPushArgs = defaultPushArgs

View File

@ -30,3 +30,7 @@ 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)])