module IT.FI2POP where
import IT.Algorithms
import IT.Random
import IT.ITEA
import Control.Monad.Extra (iterateM)
fi2pop :: Mutation -> Fitness -> (Population, Population) -> Rnd [(Population, Population)]
fi2pop :: Mutation
-> Fitness
-> (Population, Population)
-> Rnd [(Population, Population)]
fi2pop Mutation
f Fitness
g (Population
feas0, Population
infeas0) = let n :: Int
n = Population -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Population
feas0
in ((Population, Population)
-> StateT StdGen Identity (Population, Population))
-> (Population, Population) -> Rnd [(Population, Population)]
forall (m :: * -> *) a. Monad m => (a -> m a) -> a -> m [a]
iterateM (Mutation
-> Fitness
-> Int
-> (Population, Population)
-> StateT StdGen Identity (Population, Population)
step2pop Mutation
f Fitness
g Int
n) (Population
feas0, Population
infeas0)
splitPop :: Population -> (Population, Population)
splitPop :: Population -> (Population, Population)
splitPop Population
pop = Population -> Population -> Population -> (Population, Population)
go Population
pop [] []
where
go :: Population -> Population -> Population -> (Population, Population)
go [] Population
feas Population
infeas = (Population
feas, Population
infeas)
go (Solution
p:Population
ps) Population
feas Population
infeas
| Solution -> Double
_constr Solution
p Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0 = Population -> Population -> Population -> (Population, Population)
go Population
ps (Solution
pSolution -> Population -> Population
forall a. a -> [a] -> [a]
:Population
feas) Population
infeas
| Bool
otherwise = Population -> Population -> Population -> (Population, Population)
go Population
ps Population
feas (Solution
pSolution -> Population -> Population
forall a. a -> [a] -> [a]
:Population
infeas)
step2pop :: Mutation -> Fitness -> Int -> (Population, Population) -> Rnd (Population, Population)
step2pop :: Mutation
-> Fitness
-> Int
-> (Population, Population)
-> StateT StdGen Identity (Population, Population)
step2pop Mutation
mutFun Fitness
fitFun Int
nPop (Population
feas, Population
infeas) = do
let tourn :: Population -> Int -> Rnd Population
tourn = if Int
nPop Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
1000 then Population -> Int -> Rnd Population
tournamentSeq else Population -> Int -> Rnd Population
tournament
Population
childrenF <- Int
-> (Solution -> Rnd Expr)
-> Fitness
-> Population
-> Rnd Population
parRndMap Int
nPop (Mutation
mutFun Mutation -> (Solution -> Expr) -> Solution -> Rnd Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> Expr
_expr) Fitness
fitFun Population
feas
Population
childrenI <- Int
-> (Solution -> Rnd Expr)
-> Fitness
-> Population
-> Rnd Population
parRndMap Int
nPop (Mutation
mutFun Mutation -> (Solution -> Expr) -> Solution -> Rnd Expr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Solution -> Expr
_expr) Fitness
fitFun Population
infeas
let (Population
feas', Population
infeas') = Population -> (Population, Population)
splitPop (Population
childrenF Population -> Population -> Population
forall a. [a] -> [a] -> [a]
++ Population
childrenI)
Population
nextFeas <- Population -> Int -> Rnd Population
tourn (Population
feas Population -> Population -> Population
forall a. [a] -> [a] -> [a]
++ Population
feas') Int
nPop
Population
nextInfeas <- Population -> Int -> Rnd Population
tourn (Population
infeas Population -> Population -> Population
forall a. [a] -> [a] -> [a]
++ Population
infeas') Int
nPop
(Population, Population)
-> StateT StdGen Identity (Population, Population)
forall (m :: * -> *) a. Monad m => a -> m a
return (Population
nextFeas, Population
nextInfeas)