variation done

This commit is contained in:
Rowan Torbitzky-Lane 2025-03-04 16:11:37 -06:00
parent b002d571a3
commit d1d36eb3aa
3 changed files with 39 additions and 10 deletions

View File

@ -46,7 +46,7 @@ gpLoop pushArgs@(PushArgs {trainingData = tData}) = do
-- the training data (possibly downsampled). -- the training data (possibly downsampled).
gpLoop' :: PushArgs -> Int -> Int -> [Individual] -> [PushData] -> IO () gpLoop' :: PushArgs -> Int -> Int -> [Individual] -> [PushData] -> IO ()
gpLoop' gpLoop'
pushArgs@(PushArgs {enableDownsampling = enableDS, solutionErrorThreshold = seThresh, downsampleParentsGens = dsParentGens, downsampleParentRate = dsParentRate, trainingData = tData}) pushArgs@(PushArgs {enableDownsampling = enableDS, solutionErrorThreshold = seThresh, downsampleParentsGens = dsParentGens, downsampleParentRate = dsParentRate, trainingData = tData, elitism = isElite, populationSize = popSize})
generation generation
evaluations evaluations
population population
@ -78,7 +78,8 @@ gpLoop'
| (not (enableDownsampling epsilonPushArgs) && (generation >= maxGenerations epsilonPushArgs)) | (not (enableDownsampling epsilonPushArgs) && (generation >= maxGenerations epsilonPushArgs))
|| (enableDownsampling epsilonPushArgs && (evaluations >= (maxGenerations epsilonPushArgs * length population * length indexedTrainingData))) = || (enableDownsampling epsilonPushArgs && (evaluations >= (maxGenerations epsilonPushArgs * length population * length indexedTrainingData))) =
print $ "Best individual: " <> show (plushy bestInd) print $ "Best individual: " <> show (plushy bestInd)
| otherwise = | otherwise = do
newPop <- if isElite then replicateM (popSize - 1) (newIndividual epsilonPushArgs evaledPop) else replicateM popSize (newIndividual epsilonPushArgs evaledPop)
gpLoop' gpLoop'
pushArgs pushArgs
(succ generation) (succ generation)
@ -87,9 +88,9 @@ gpLoop'
+ (if generation `mod` downsampleParentsGens pushArgs == 0 then length parentReps * (length indexedTrainingData - length (trainingData pushArgs)) else 0) + (if generation `mod` downsampleParentsGens pushArgs == 0 then length parentReps * (length indexedTrainingData - length (trainingData pushArgs)) else 0)
+ (if bestIndPassesDownsample then length indexedTrainingData - length tData else 0) + (if bestIndPassesDownsample then length indexedTrainingData - length tData else 0)
) )
( if elitism pushArgs ( if isElite
then bestInd : replicate (populationSize epsilonPushArgs - 1) (newIndividual epsilonPushArgs evaledPop) then bestInd : newPop
else replicate (populationSize epsilonPushArgs) (newIndividual epsilonPushArgs evaledPop) else newPop
) )
( if enableDS && ((generation `mod` dsParentGens) == 0) ( if enableDS && ((generation `mod` dsParentGens) == 0)
then updateCaseDistances repEvaluatedPop indexedTrainingData indexedTrainingData (informedDownsamplingType pushArgs) (solutionErrorThreshold pushArgs / fromIntegral @Int @Double (length indexedTrainingData)) then updateCaseDistances repEvaluatedPop indexedTrainingData indexedTrainingData (informedDownsamplingType pushArgs) (solutionErrorThreshold pushArgs / fromIntegral @Int @Double (length indexedTrainingData))

View File

@ -3,7 +3,6 @@ module HushGP.GP.PushArgs where
import HushGP.State import HushGP.State
import HushGP.Instructions import HushGP.Instructions
import HushGP.GP.PushData import HushGP.GP.PushData
import Data.Map qualified as Map
-- | The structure holding the arguments for the various aspects -- | The structure holding the arguments for the various aspects
-- of the evolutionary run in Hush. -- of the evolutionary run in Hush.

View File

@ -2,6 +2,7 @@ module HushGP.GP.Variation where
import Data.List import Data.List
import Control.Monad import Control.Monad
import System.Random
import HushGP.State import HushGP.State
import HushGP.GP.PushArgs import HushGP.GP.PushArgs
import HushGP.GP.Individual import HushGP.GP.Individual
@ -119,7 +120,7 @@ uniformDeletion' (old:oldList) !newList adjustRate = do
-- |Creates a new individual based on the probabilities of the desired -- |Creates a new individual based on the probabilities of the desired
-- crossover methods. -- crossover methods.
newIndividual :: PushArgs -> [Individual] -> IO Individual newIndividual :: PushArgs -> [Individual] -> IO Individual
newIndividual pushArgs@PushArgs{variation = var} population = do newIndividual pushArgs@PushArgs{variation = var, umadRate = uRate} population = do
randOp <- randomOperation var 0.0 randOp <- randomOperation var 0.0
case randOp of case randOp of
"reproduction" -> selectParent pushArgs population "reproduction" -> selectParent pushArgs population
@ -135,9 +136,37 @@ newIndividual pushArgs@PushArgs{variation = var} population = do
pure $ postVariationInd childPlushy pure $ postVariationInd childPlushy
"umad" -> do "umad" -> do
parent <- selectParent pushArgs population parent <- selectParent pushArgs population
child <- uniformAddition pushArgs (plushy parent) >>= uniformDeletion pushArgs childPlushy <- uniformAddition pushArgs (plushy parent) >>= uniformDeletion pushArgs
pure $ postVariationInd child pure $ postVariationInd childPlushy
"rumad" -> undefined -- TODO: this tomorrow! "alternation" -> do
parent0 <- selectParent pushArgs population
parent1 <- selectParent pushArgs population
childPlushy <- alternation pushArgs (plushy parent0) (plushy parent1)
pure $ postVariationInd childPlushy
"rumad" -> do -- Responsive umad, deletion rate from computed amount of additions.
parent <- selectParent pushArgs population
addedChildPlushy <- uniformAddition pushArgs (plushy parent)
let effectiveAdditionRate = fromIntegral @Int @Double (length addedChildPlushy - length (plushy parent)) / fromIntegral @Int @Double (length (plushy parent))
finalChild <- uniformDeletion pushArgs{umadRate = effectiveAdditionRate} addedChildPlushy
pure $ postVariationInd finalChild
"vumad" -> do -- variable umad, umad rate chosen randomly from [0, umadRate]
rate <- fst . uniformR (0.0 :: Double, uRate) <$> initStdGen
parent <- selectParent pushArgs population
addedChildPlushy <- uniformAddition pushArgs{umadRate = rate} (plushy parent)
deletedChildPlushy <- uniformDeletion pushArgs{umadRate = rate} addedChildPlushy
pure $ postVariationInd deletedChildPlushy
"uniformAddition" -> do
parent <- selectParent pushArgs population
childPlushy <- uniformAddition pushArgs (plushy parent)
pure $ postVariationInd childPlushy
"uniformReplacement" -> do
parent <- selectParent pushArgs population
childPlushy <- uniformReplacement pushArgs (plushy parent)
pure $ postVariationInd childPlushy
"uniformDeletion" -> do
parent <- selectParent pushArgs population
childPlushy <- uniformDeletion pushArgs (plushy parent)
pure $ postVariationInd childPlushy
_ -> error ("Error: No match for selection operation: " <> randOp) _ -> error ("Error: No match for selection operation: " <> randOp)
where where
randDecimal :: IO Double randDecimal :: IO Double