module Neet.Species (
Species(..)
, SpecScore(..)
, newSpec
, runFitTest
, updateSpec
) where
import Neet.Genome
import Data.MultiMap (MultiMap)
import qualified Data.MultiMap as MM
import Data.List (foldl')
data Species =
Species { specSize :: Int
, specOrgs :: [Genome]
, specScore :: SpecScore
, lastImprovement :: Int
}
data SpecScore = SpecScore { bestScore :: Double, bestGen :: Genome }
instance Show Species where
show (Species siz _ (SpecScore scr _) lastImprov) =
"Species {specSize = " ++ show siz ++
", specOrgs = <...>, bestScore = " ++ show scr ++
", bestGen = <...>" ++
", lastImprovement = " ++ show lastImprov ++ "}"
newSpec :: Genome -> [Genome] -> Species
newSpec gen gens = Species (length gens + 1) (gen:gens) (SpecScore 0 gen) 0
runFitTest :: (Genome -> Double) -> Species -> (MultiMap Double Genome, SpecScore, Double)
runFitTest f Species{..} = (mmap, ss, totF / dubSize)
where dubSize = fromIntegral specSize :: Double
(mmap, totF) = foldl' accumOne (MM.empty, 0) $ map calcOne specOrgs
calcOne g = let fitness = f g in (fitness, g)
accumOne (accM, accA) (fit, g) = (MM.insert fit g accM, accA + fit)
ss = case MM.findMaxWithValues mmap of
Nothing -> error "(runFitTest) folding fitness resulted in empty map!"
Just (scr, (x:_)) -> SpecScore scr x
updateSpec :: SpecScore -> Species -> Species
updateSpec ss spec = spec { specScore = newScr
, lastImprovement = li
}
where oldScr = specScore spec
(newScr, li)
| bestScore ss > bestScore oldScr = (ss, 0)
| otherwise = (oldScr, lastImprovement spec + 1)