2024-04-29 10:41:01 +02:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2019-10-18 09:57:43 +02:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2019-10-18 09:10:11 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2023-04-26 15:46:30 +02:00
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2019-05-30 12:40:32 +02:00
|
|
|
|
2020-01-07 08:45:50 +01:00
|
|
|
import Options.Applicative
|
2019-10-18 09:57:43 +02:00
|
|
|
import Pipes
|
2019-10-22 16:42:16 +02:00
|
|
|
import Pretty
|
2024-02-11 21:25:15 +01:00
|
|
|
import Protolude hiding (for)
|
2019-10-18 09:57:43 +02:00
|
|
|
import System.IO
|
2024-04-22 14:33:40 +02:00
|
|
|
-- import LambdaDatasets.IrisDataset
|
2024-05-09 08:48:00 +02:00
|
|
|
-- import LambdaDatasets.NurseryDataset
|
|
|
|
import LambdaDatasets.GermanDataset
|
2024-03-17 18:14:52 +01:00
|
|
|
import Debug.Trace as DB
|
|
|
|
import qualified Data.Map.Strict as Map
|
2019-10-17 19:50:19 +02:00
|
|
|
|
2023-04-26 15:46:30 +02:00
|
|
|
data Options = Options
|
2024-02-19 21:56:28 +01:00
|
|
|
{ iterations :: !N,
|
|
|
|
populationSize :: !N
|
2023-04-26 15:46:30 +02:00
|
|
|
}
|
2020-01-07 08:45:50 +01:00
|
|
|
|
|
|
|
options :: Parser Options
|
|
|
|
options =
|
|
|
|
Options
|
2023-04-26 15:46:30 +02:00
|
|
|
<$> option
|
|
|
|
auto
|
|
|
|
( long "iterations"
|
|
|
|
<> short 'i'
|
|
|
|
<> metavar "N"
|
2024-04-22 14:33:40 +02:00
|
|
|
<> value 1500
|
2023-04-26 15:46:30 +02:00
|
|
|
<> help "Number of iterations"
|
|
|
|
)
|
|
|
|
<*> option
|
|
|
|
auto
|
|
|
|
( long "population-size"
|
|
|
|
<> short 'p'
|
|
|
|
<> metavar "N"
|
2024-04-21 19:20:02 +02:00
|
|
|
<> value 400
|
2023-04-26 15:46:30 +02:00
|
|
|
<> help "Population size"
|
|
|
|
)
|
2020-01-07 08:45:50 +01:00
|
|
|
|
2020-05-02 17:47:43 +02:00
|
|
|
optionsWithHelp :: ParserInfo Options
|
2020-01-07 08:45:50 +01:00
|
|
|
optionsWithHelp =
|
2023-04-26 15:46:30 +02:00
|
|
|
info
|
|
|
|
(helper <*> options)
|
2020-01-07 08:45:50 +01:00
|
|
|
( fullDesc
|
|
|
|
<> progDesc "Run a GA"
|
|
|
|
<> header "haga - Haskell implementations of EAs"
|
|
|
|
)
|
2019-10-18 09:57:43 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
2023-04-26 15:46:30 +02:00
|
|
|
main =
|
|
|
|
execParser optionsWithHelp >>= \opts -> do
|
|
|
|
hSetBuffering stdout NoBuffering
|
2024-04-22 14:33:40 +02:00
|
|
|
lEE <- shuffledLEE
|
2024-04-29 10:41:01 +02:00
|
|
|
let cfg = GaRunConfig {
|
|
|
|
enviroment = lE,
|
|
|
|
initialEvaluator = lEE,
|
|
|
|
selectionType = Tournament 3,
|
|
|
|
termination = (steps (iterations opts)),
|
|
|
|
poulationSize = (populationSize opts),
|
|
|
|
stepSize = 120,
|
|
|
|
elitismRatio = 5/100
|
|
|
|
}
|
|
|
|
pop' <- runEffect (for (run cfg) logCsv)
|
2024-04-22 14:33:40 +02:00
|
|
|
lEE' <- calc lEE pop'
|
|
|
|
let (res, _) = bests lEE' 5 pop'
|
|
|
|
let lEE' = lEE {training = False}
|
|
|
|
lEE' <- calc lEE' res
|
|
|
|
mapM_ (format lEE') res
|
2019-10-18 09:57:43 +02:00
|
|
|
where
|
2024-04-22 14:33:40 +02:00
|
|
|
format l s = do
|
|
|
|
let f = fitness' l s
|
2019-10-18 09:57:43 +02:00
|
|
|
putErrText $ show f <> "\n" <> pretty s
|
2024-02-11 21:25:15 +01:00
|
|
|
logCsv = putText . csv
|
2019-10-22 14:33:19 +02:00
|
|
|
csv (t, f) = show t <> " " <> show f
|