2019-10-17 17:25:25 +02:00
|
|
|
{-# LANGUAGE DeriveFoldable #-}
|
2019-10-17 18:23:19 +02:00
|
|
|
{-# LANGUAGE DeriveFunctor #-}
|
2019-10-17 17:25:25 +02:00
|
|
|
{-# LANGUAGE DeriveTraversable #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2019-10-18 09:10:11 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-10-22 08:14:16 +02:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2019-10-18 09:57:43 +02:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2023-04-26 15:46:30 +02:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Module : GA
|
|
|
|
-- Description : Abstract genetic algorithm
|
|
|
|
-- Copyright : David Pätzel, 2019
|
|
|
|
-- License : GPL-3
|
|
|
|
-- Maintainer : David Pätzel <david.paetzel@posteo.de>
|
|
|
|
-- Stability : experimental
|
|
|
|
--
|
|
|
|
-- Simplistic abstract definition of a genetic algorithm.
|
|
|
|
--
|
|
|
|
-- In order to use it for a certain problem, basically, you have to make your
|
|
|
|
-- solution type an instance of 'Individual' and then simply call the 'run'
|
|
|
|
-- function.
|
2019-10-17 17:25:25 +02:00
|
|
|
module GA where
|
|
|
|
|
2020-05-02 16:10:24 +02:00
|
|
|
import Control.Arrow hiding (first, second)
|
2019-10-17 17:25:25 +02:00
|
|
|
import Data.List.NonEmpty ((<|))
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
2019-10-22 14:32:36 +02:00
|
|
|
import qualified Data.List.NonEmpty.Extra as NE (appendl, sortOn)
|
2019-10-17 17:25:25 +02:00
|
|
|
import Data.Random
|
2024-02-11 21:25:15 +01:00
|
|
|
import System.Random.MWC (create)
|
2019-10-18 09:57:43 +02:00
|
|
|
import Pipes
|
2019-10-17 18:23:19 +02:00
|
|
|
import Protolude
|
2019-10-17 17:25:25 +02:00
|
|
|
import Test.QuickCheck hiding (sample, shuffle)
|
2020-05-02 17:42:04 +02:00
|
|
|
import Test.QuickCheck.Instances ()
|
2019-10-22 08:14:16 +02:00
|
|
|
import Test.QuickCheck.Monadic
|
2019-10-22 06:53:53 +02:00
|
|
|
|
2019-10-22 17:02:04 +02:00
|
|
|
-- TODO there should be a few 'shuffle's here
|
2019-10-22 14:32:36 +02:00
|
|
|
|
2019-10-22 07:10:28 +02:00
|
|
|
-- TODO enforce this being > 0
|
2019-10-17 17:25:25 +02:00
|
|
|
type N = Int
|
|
|
|
|
2019-10-17 18:23:19 +02:00
|
|
|
type R = Double
|
2019-10-17 17:25:25 +02:00
|
|
|
|
|
|
|
class Eq i => Individual i where
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Generates a completely random individual given an existing individual.
|
|
|
|
--
|
|
|
|
-- We have to add @i@ here as a parameter in order to be able to inject stuff.
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2019-10-22 17:02:04 +02:00
|
|
|
-- TODO This (and also, Seminar.I, which contains an ugly parameter @p@) has
|
|
|
|
-- to be done nicer!
|
2024-02-11 21:25:15 +01:00
|
|
|
new :: i -> RVar i
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Generates a random population of the given size.
|
2024-02-11 21:25:15 +01:00
|
|
|
population :: N -> i -> RVar (Population i)
|
2019-10-22 14:32:36 +02:00
|
|
|
population n i
|
|
|
|
| n <= 0 = undefined
|
|
|
|
| otherwise = NE.fromList <$> replicateM n (new i)
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2024-02-11 21:25:15 +01:00
|
|
|
mutate :: i -> RVar i
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2024-02-11 21:25:15 +01:00
|
|
|
crossover1 :: i -> i -> RVar (Maybe (i, i))
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- An individual's fitness. Higher values are considered “better”.
|
|
|
|
--
|
|
|
|
-- We explicitely allow fitness values to be have any sign (see, for example,
|
|
|
|
-- 'proportionate1').
|
2019-10-17 17:25:25 +02:00
|
|
|
fitness :: (Monad m) => i -> m R
|
2019-10-17 18:23:19 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Performs an n-point crossover.
|
|
|
|
--
|
|
|
|
-- Given the function for single-point crossover, 'crossover1', this function can
|
|
|
|
-- be derived through recursion and a monad combinator (which is also the default
|
|
|
|
-- implementation).
|
2024-02-11 21:25:15 +01:00
|
|
|
crossover :: N -> i -> i -> RVar (Maybe (i, i))
|
2019-10-17 17:25:25 +02:00
|
|
|
crossover n i1 i2
|
2019-10-17 18:23:19 +02:00
|
|
|
| n <= 0 = return $ Just (i1, i2)
|
2019-10-17 17:25:25 +02:00
|
|
|
| otherwise = do
|
2023-04-26 15:46:30 +02:00
|
|
|
isM <- crossover1 i1 i2
|
|
|
|
maybe (return Nothing) (uncurry (crossover (n - 1))) isM
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Needed for QuickCheck tests, for now, a very simplistic implementation should
|
|
|
|
-- suffice.
|
2019-10-22 14:33:19 +02:00
|
|
|
instance Individual Integer where
|
2024-02-11 21:25:15 +01:00
|
|
|
new _ = uniform 0 (0 + 100000)
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2024-02-11 21:25:15 +01:00
|
|
|
mutate i = uniform (i - 10) (i + 10)
|
2019-10-22 14:33:19 +02:00
|
|
|
|
|
|
|
crossover1 i1 i2 = return $ Just (i1 - i2, i2 - i1)
|
|
|
|
|
|
|
|
fitness = return . fromIntegral . negate
|
2019-10-22 14:32:36 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Populations are just basic non-empty lists.
|
2019-10-22 14:32:36 +02:00
|
|
|
type Population i = NonEmpty i
|
2019-10-22 08:14:16 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Produces offspring circularly from the given list of parents.
|
|
|
|
children ::
|
2024-02-11 21:25:15 +01:00
|
|
|
(Individual i) =>
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | The @nX@ of the @nX@-point crossover operator
|
|
|
|
N ->
|
|
|
|
NonEmpty i ->
|
2024-02-11 21:25:15 +01:00
|
|
|
RVar (NonEmpty i)
|
2019-10-17 17:25:25 +02:00
|
|
|
children _ (i :| []) = (:| []) <$> mutate i
|
|
|
|
children nX (i1 :| [i2]) = children2 nX i1 i2
|
|
|
|
children nX (i1 :| i2 : is') =
|
|
|
|
(<>) <$> children2 nX i1 i2 <*> children nX (NE.fromList is')
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
prop_children_asManyAsParents ::
|
|
|
|
(Individual a, Show a) => N -> NonEmpty a -> Property
|
2020-05-02 16:10:24 +02:00
|
|
|
prop_children_asManyAsParents nX is =
|
2023-04-26 15:46:30 +02:00
|
|
|
again $
|
|
|
|
monadicIO $
|
|
|
|
do
|
2024-02-11 21:25:15 +01:00
|
|
|
mwc <- Test.QuickCheck.Monadic.run create
|
|
|
|
is' <- Test.QuickCheck.Monadic.run $ sampleFrom mwc (children nX is)
|
2023-04-26 15:46:30 +02:00
|
|
|
return $ counterexample (show is') $ length is' == length is
|
2020-05-02 16:10:24 +02:00
|
|
|
|
2024-02-11 21:25:15 +01:00
|
|
|
children2 :: (Individual i) => N -> i -> i -> RVar (NonEmpty i)
|
2019-10-17 17:25:25 +02:00
|
|
|
children2 nX i1 i2 = do
|
|
|
|
-- TODO Add crossover probability?
|
|
|
|
(i3, i4) <- fromMaybe (i1, i2) <$> crossover nX i1 i2
|
|
|
|
i5 <- mutate i3
|
|
|
|
i6 <- mutate i4
|
|
|
|
return $ i5 :| [i6]
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- The best according to a function; returns up to @k@ results and the remaining
|
|
|
|
-- population.
|
|
|
|
--
|
|
|
|
-- If @k <= 0@, this returns the best one anyway (as if @k == 1@).
|
|
|
|
bestsBy ::
|
|
|
|
(Individual i, Monad m) =>
|
|
|
|
N ->
|
|
|
|
(i -> m R) ->
|
|
|
|
Population i ->
|
|
|
|
m (NonEmpty i, [i])
|
2019-10-22 14:33:19 +02:00
|
|
|
bestsBy k f pop@(i :| pop')
|
|
|
|
| k <= 0 = bestsBy 1 f pop
|
|
|
|
| otherwise = foldM run (i :| [], []) pop'
|
|
|
|
where
|
|
|
|
run (bests, rest) i =
|
|
|
|
((NE.fromList . NE.take k) &&& (rest <>) . NE.drop k)
|
|
|
|
<$> sorted (i <| bests)
|
|
|
|
sorted =
|
|
|
|
fmap (fmap fst . NE.sortOn (Down . snd)) . traverse (\i -> (i,) <$> f i)
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- The @k@ best individuals in the population when comparing using the supplied
|
|
|
|
-- function.
|
2019-10-22 14:33:19 +02:00
|
|
|
bestsBy' :: (Individual i, Monad m) => N -> (i -> m R) -> Population i -> m [i]
|
|
|
|
bestsBy' k f =
|
2019-10-17 18:23:19 +02:00
|
|
|
fmap (NE.take k . fmap fst . NE.sortBy (comparing (Down . snd)))
|
|
|
|
. traverse (\i -> (i,) <$> f i)
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
prop_bestsBy_isBestsBy' :: Individual a => Int -> Population a -> Property
|
2019-10-22 14:33:19 +02:00
|
|
|
prop_bestsBy_isBestsBy' k pop =
|
2023-04-26 15:46:30 +02:00
|
|
|
k > 0 ==>
|
|
|
|
monadicIO $
|
|
|
|
do
|
|
|
|
a <- fst <$> bestsBy k fitness pop
|
|
|
|
b <- bestsBy' k fitness pop
|
|
|
|
assert $ NE.toList a == b
|
2019-10-22 14:33:19 +02:00
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
prop_bestsBy_lengths :: Individual a => Int -> Population a -> Property
|
2020-01-07 09:17:39 +01:00
|
|
|
prop_bestsBy_lengths k pop =
|
|
|
|
k > 0 ==> monadicIO $ do
|
|
|
|
(bests, rest) <- bestsBy k fitness pop
|
2023-04-26 15:46:30 +02:00
|
|
|
assert $
|
|
|
|
length bests == min k (length pop) && length bests + length rest == length pop
|
2020-01-07 09:17:39 +01:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- The @k@ worst individuals in the population (and the rest of the population).
|
2019-10-22 14:33:19 +02:00
|
|
|
worst :: (Individual i, Monad m) => N -> Population i -> m (NonEmpty i, [i])
|
2019-10-22 07:08:37 +02:00
|
|
|
worst = flip bestsBy (fmap negate . fitness)
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- The @k@ best individuals in the population (and the rest of the population).
|
2019-10-22 14:33:19 +02:00
|
|
|
bests :: (Individual i, Monad m) => N -> Population i -> m (NonEmpty i, [i])
|
2019-10-18 09:57:43 +02:00
|
|
|
bests = flip bestsBy fitness
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2019-10-22 14:33:19 +02:00
|
|
|
-- TODO add top x percent parent selection (select n guys, sort by fitness first)
|
2023-04-26 15:46:30 +02:00
|
|
|
|
|
|
|
-- |
|
|
|
|
-- Performs one iteration of a steady state genetic algorithm that in each
|
|
|
|
-- iteration that creates @k@ offspring simply deletes the worst @k@ individuals
|
|
|
|
-- while making sure that the given percentage of elitists survive (at least 1
|
|
|
|
-- elitist, even if the percentage is 0 or low enough for rounding to result in 0
|
|
|
|
-- elitists).
|
|
|
|
stepSteady ::
|
2024-02-11 21:25:15 +01:00
|
|
|
(Individual i) =>
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | Mechanism for selecting parents
|
2024-02-11 21:25:15 +01:00
|
|
|
Selection RVar i ->
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | Number of parents @nParents@ for creating @nParents@ children
|
|
|
|
N ->
|
|
|
|
-- | How many crossover points (the @nX@ in @nX@-point crossover)
|
|
|
|
N ->
|
|
|
|
-- | Elitism ratio @pElite@
|
|
|
|
R ->
|
|
|
|
Population i ->
|
2024-02-11 21:25:15 +01:00
|
|
|
RVar (Population i)
|
2020-05-02 16:12:31 +02:00
|
|
|
stepSteady select nParents nX pElite pop = do
|
|
|
|
-- TODO Consider keeping the fitness evaluations already done for pop (so we
|
|
|
|
-- only reevaluate iChildren)
|
|
|
|
iParents <- select nParents pop
|
2019-10-22 14:33:19 +02:00
|
|
|
iChildren <- NE.filter (`notElem` pop) <$> children nX iParents
|
|
|
|
let pop' = pop `NE.appendl` iChildren
|
2020-05-02 16:10:24 +02:00
|
|
|
(elitists, rest) <- bests nBest pop'
|
|
|
|
case rest of
|
|
|
|
[] -> return elitists
|
|
|
|
(i : is) ->
|
|
|
|
-- NOTE 'bests' always returns at least one individual, thus we need this
|
|
|
|
-- slightly ugly branching
|
|
|
|
if length elitists == length pop
|
|
|
|
then return elitists
|
|
|
|
else
|
|
|
|
(elitists <>)
|
2023-04-26 15:46:30 +02:00
|
|
|
. fst
|
|
|
|
<$> bests (length pop - length elitists) (i :| is)
|
2019-10-22 14:33:19 +02:00
|
|
|
where
|
2020-05-02 16:10:24 +02:00
|
|
|
nBest = floor . (pElite *) . fromIntegral $ NE.length pop
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
prop_stepSteady_constantPopSize ::
|
|
|
|
(Individual a, Show a) => NonEmpty a -> Property
|
2020-05-02 16:10:24 +02:00
|
|
|
prop_stepSteady_constantPopSize pop =
|
|
|
|
forAll
|
|
|
|
( (,)
|
|
|
|
<$> choose (1, length pop)
|
|
|
|
<*> choose (1, length pop)
|
|
|
|
)
|
|
|
|
$ \(nParents, nX) -> monadicIO $ do
|
|
|
|
let pElite = 0.1
|
2024-02-11 21:25:15 +01:00
|
|
|
mwc <- Test.QuickCheck.Monadic.run create
|
|
|
|
pop' <- Test.QuickCheck.Monadic.run $ sampleFrom mwc (stepSteady (tournament 4) nParents nX pElite pop)
|
2020-05-02 16:10:24 +02:00
|
|
|
return . counterexample (show pop') $ length pop' == length pop
|
2019-10-18 09:57:43 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Given an initial population, runs the GA until the termination criterion is
|
|
|
|
-- fulfilled.
|
|
|
|
--
|
|
|
|
-- Uses the pipes library to, in each step, 'Pipes.yield' the currently best known
|
|
|
|
-- solution.
|
|
|
|
run ::
|
2024-02-11 21:25:15 +01:00
|
|
|
(Individual i) =>
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | Mechanism for selecting parents
|
2024-02-11 21:25:15 +01:00
|
|
|
Selection RVar i ->
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | Number of parents @nParents@ for creating @nParents@ children
|
|
|
|
N ->
|
|
|
|
-- | How many crossover points (the @nX@ in @nX@-point crossover)
|
|
|
|
N ->
|
|
|
|
-- | Elitism ratio @pElite@
|
|
|
|
R ->
|
2024-02-11 21:25:15 +01:00
|
|
|
RVar (Population i) ->
|
2023-04-26 15:46:30 +02:00
|
|
|
Termination i ->
|
2024-02-11 21:25:15 +01:00
|
|
|
Producer (Int, R) IO (Population i)
|
|
|
|
run select nParents nX pElite pop term = do
|
|
|
|
mwc <- lift create
|
|
|
|
let x = \currPop generation -> do
|
|
|
|
currPop' <- lift $ sampleFrom mwc $ currPop
|
|
|
|
if term currPop' generation
|
|
|
|
then return currPop'
|
|
|
|
else do
|
|
|
|
let nextPop = stepSteady select nParents nX pElite currPop'
|
|
|
|
nextPop' <- lift $ sampleFrom mwc $ nextPop
|
|
|
|
(iBests, _) <- lift $ bests 1 nextPop'
|
|
|
|
fs <- lift . sequence $ fitness <$> iBests
|
|
|
|
let fBest = NE.head fs
|
|
|
|
Pipes.yield (generation, fBest)
|
|
|
|
x nextPop (generation + 1)
|
|
|
|
x pop 0
|
|
|
|
|
2019-10-17 17:25:25 +02:00
|
|
|
|
2020-05-02 16:12:31 +02:00
|
|
|
-- * Selection mechanisms
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- A function generating a monadic action which selects a given number of
|
|
|
|
-- individuals from the given population.
|
2020-05-02 16:12:31 +02:00
|
|
|
type Selection m i = N -> Population i -> m (NonEmpty i)
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Selects @n@ individuals from the population the given mechanism by repeatedly
|
|
|
|
-- selecting a single individual using the given selection mechanism (with
|
|
|
|
-- replacement, so the same individual can be selected multiple times).
|
|
|
|
chain ::
|
2024-02-11 21:25:15 +01:00
|
|
|
(Individual i) =>
|
|
|
|
(Population i -> RVar i) ->
|
|
|
|
Selection RVar i
|
2020-05-02 16:12:31 +02:00
|
|
|
-- TODO Ensure that the same individual is not selected multiple times
|
|
|
|
-- (require Selections to partition)
|
|
|
|
chain select1 n pop
|
|
|
|
| n > 1 = (<|) <$> select1 pop <*> chain select1 (n - 1) pop
|
|
|
|
| otherwise = (:|) <$> select1 pop <*> return []
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Selects @n@ individuals from the population by repeatedly selecting a single
|
|
|
|
-- indidual using a tournament of the given size (the same individual can be
|
|
|
|
-- selected multiple times, see 'chain').
|
2024-02-11 21:25:15 +01:00
|
|
|
tournament :: (Individual i) => N -> Selection RVar i
|
2020-05-02 16:12:31 +02:00
|
|
|
tournament nTrnmnt = chain (tournament1 nTrnmnt)
|
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
prop_tournament_selectsN :: Individual a => Int -> Int -> NonEmpty a -> Property
|
2020-05-02 16:12:31 +02:00
|
|
|
prop_tournament_selectsN nTrnmnt n pop =
|
2023-04-26 15:46:30 +02:00
|
|
|
0 < nTrnmnt
|
|
|
|
&& nTrnmnt < length pop
|
|
|
|
&& 0 < n
|
|
|
|
==> monadicIO
|
2020-05-02 16:12:31 +02:00
|
|
|
$ do
|
2024-02-11 21:25:15 +01:00
|
|
|
mwc <- Test.QuickCheck.Monadic.run create
|
|
|
|
pop' <- Test.QuickCheck.Monadic.run $ sampleFrom mwc (tournament 2 n pop)
|
2020-05-02 16:12:31 +02:00
|
|
|
assert $ length pop' == n
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Selects one individual from the population using tournament selection.
|
|
|
|
tournament1 ::
|
2024-02-11 21:25:15 +01:00
|
|
|
(Individual i) =>
|
2023-04-26 15:46:30 +02:00
|
|
|
-- | Tournament size
|
|
|
|
N ->
|
|
|
|
Population i ->
|
2024-02-11 21:25:15 +01:00
|
|
|
RVar i
|
2020-05-02 16:12:31 +02:00
|
|
|
tournament1 nTrnmnt pop
|
|
|
|
-- TODO Use Positive for this constraint
|
|
|
|
| nTrnmnt <= 0 = undefined
|
|
|
|
| otherwise = trnmnt >>= fmap (NE.head . fst) . bests 1
|
|
|
|
where
|
|
|
|
trnmnt = withoutReplacement nTrnmnt pop
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Selects @n@ individuals uniformly at random from the population (without
|
|
|
|
-- replacement, so if @n >= length pop@, simply returns @pop@).
|
|
|
|
withoutReplacement ::
|
|
|
|
-- | How many individuals to select
|
|
|
|
N ->
|
|
|
|
Population i ->
|
2024-02-11 21:25:15 +01:00
|
|
|
RVar (NonEmpty i)
|
2020-05-02 16:12:31 +02:00
|
|
|
withoutReplacement 0 _ = undefined
|
|
|
|
withoutReplacement n pop
|
|
|
|
| n >= length pop = return pop
|
2024-02-11 21:25:15 +01:00
|
|
|
| otherwise = fmap (NE.fromList) (shuffleNofM n (length pop) (NE.toList pop))
|
2020-05-02 16:12:31 +02:00
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
prop_withoutReplacement_selectsN :: Int -> NonEmpty a -> Property
|
2020-05-02 16:12:31 +02:00
|
|
|
prop_withoutReplacement_selectsN n pop =
|
2024-02-11 21:25:15 +01:00
|
|
|
0 < n && n <= length pop ==> monadicIO (do
|
|
|
|
mwc <- Test.QuickCheck.Monadic.run create
|
|
|
|
pop' <- Test.QuickCheck.Monadic.run $ sampleFrom mwc (withoutReplacement n pop)
|
|
|
|
assert $ length pop' == n)
|
2020-05-02 16:12:31 +02:00
|
|
|
|
2019-10-17 17:25:25 +02:00
|
|
|
-- * Termination criteria
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Termination decisions may take into account the current population and the
|
|
|
|
-- current iteration number.
|
2019-10-17 17:25:25 +02:00
|
|
|
type Termination i = Population i -> N -> Bool
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Termination after a number of steps.
|
2019-10-17 17:25:25 +02:00
|
|
|
steps :: N -> Termination i
|
|
|
|
steps tEnd _ t = t >= tEnd
|
2019-10-22 08:14:16 +02:00
|
|
|
|
2019-10-22 14:32:36 +02:00
|
|
|
-- * Helper functions
|
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
-- |
|
|
|
|
-- Shuffles a non-empty list.
|
2024-02-11 21:25:15 +01:00
|
|
|
shuffle' :: NonEmpty a -> RVar (NonEmpty a)
|
2020-05-02 17:42:04 +02:00
|
|
|
shuffle' xs@(_ :| []) = return xs
|
2024-02-11 21:25:15 +01:00
|
|
|
shuffle' xs = fmap (NE.fromList) (shuffle (toList xs))
|
2019-10-22 14:32:36 +02:00
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
prop_shuffle_length :: NonEmpty a -> Property
|
2024-02-11 21:25:15 +01:00
|
|
|
prop_shuffle_length xs = monadicIO(do
|
|
|
|
mwc <- Test.QuickCheck.Monadic.run create
|
|
|
|
xs' <- Test.QuickCheck.Monadic.run $ sampleFrom mwc (shuffle' xs)
|
|
|
|
assert $ length xs' == length xs)
|
2019-10-22 14:32:36 +02:00
|
|
|
|
2019-10-22 08:14:16 +02:00
|
|
|
return []
|
|
|
|
|
2020-05-02 17:42:04 +02:00
|
|
|
runTests :: IO Bool
|
2019-10-22 08:14:16 +02:00
|
|
|
runTests = $quickCheckAll
|