Add applicative CLI option parsing

This commit is contained in:
David Pätzel 2020-01-07 08:45:50 +01:00
parent 483e1707c8
commit bbddd62c70
3 changed files with 50 additions and 15 deletions

View File

@ -1,20 +1,24 @@
{ mkDerivation, base, Cabal, extra, monad-loops, MonadRandom, pipes { mkDerivation, base, Cabal, extra, monad-loops, MonadRandom
, protolude, QuickCheck, quickcheck-instances, random, random-fu , optparse-applicative, pipes, protolude, QuickCheck
, random-shuffle, stdenv, text , quickcheck-instances, random, random-fu, random-shuffle, stdenv
, text
}: }:
mkDerivation { mkDerivation {
pname = "ga"; pname = "haga";
version = "0.1.0.0"; version = "0.1.0.0";
src = ./.; src = ./.;
isLibrary = true; isLibrary = true;
isExecutable = true; isExecutable = true;
libraryHaskellDepends = [ libraryHaskellDepends = [
base extra monad-loops MonadRandom pipes protolude QuickCheck base extra monad-loops MonadRandom optparse-applicative pipes
quickcheck-instances random random-fu random-shuffle text protolude QuickCheck quickcheck-instances random random-fu
random-shuffle text
]; ];
executableHaskellDepends = [ executableHaskellDepends = [
base Cabal extra monad-loops MonadRandom pipes protolude QuickCheck base Cabal extra monad-loops MonadRandom optparse-applicative pipes
quickcheck-instances random random-fu random-shuffle text protolude QuickCheck quickcheck-instances random random-fu
random-shuffle text
]; ];
description = "Simplistic genetic algorithms library";
license = stdenv.lib.licenses.gpl3; license = stdenv.lib.licenses.gpl3;
} }

View File

@ -24,6 +24,7 @@ library
, extra , extra
, MonadRandom , MonadRandom
, monad-loops , monad-loops
, optparse-applicative
, pipes , pipes
, protolude , protolude
, QuickCheck , QuickCheck
@ -44,6 +45,7 @@ executable haga
, extra , extra
, MonadRandom , MonadRandom
, monad-loops , monad-loops
, optparse-applicative
, pipes , pipes
, protolude , protolude
, QuickCheck , QuickCheck
@ -66,6 +68,7 @@ executable haga-tests
, extra , extra
, MonadRandom , MonadRandom
, monad-loops , monad-loops
, optparse-applicative
, pipes , pipes
, protolude , protolude
, QuickCheck , QuickCheck

View File

@ -2,21 +2,49 @@
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
import Options.Applicative
import Pipes import Pipes
import Pretty import Pretty
import Protolude hiding (for) import Protolude hiding (for, option)
import System.IO import System.IO
import Szenario191 import Szenario191
mkPop = population 100 (I prios []) data Options
= Options
{ iterations :: N,
populationSize :: N
}
options :: Parser Options
options =
Options
<$> option auto
( long "iterations"
<> short 'i'
<> metavar "N"
<> value 1000
<> help "Number of iterations"
)
<*> option auto
( long "population-size"
<> short 'p'
<> metavar "N"
<> value 100
<> help "Population size"
)
optionsWithHelp =
info (helper <*> options)
( fullDesc
<> progDesc "Run a GA"
<> header "haga - Haskell implementations of EAs"
)
main :: IO () main :: IO ()
main = do main = execParser optionsWithHelp >>= \opts -> do
args <- getArgs
let t = fromMaybe 100 $ headMay args >>= readMaybe
hSetBuffering stdout NoBuffering hSetBuffering stdout NoBuffering
pop <- mkPop pop <- population (populationSize opts) (I prios [])
pop' <- runEffect $ for (run 2 1 (5 / 100) pop (steps t)) log pop' <- runEffect $ for (run 2 1 (5 / 100) pop (steps $ iterations opts)) log
(res, _) <- bests 5 pop' (res, _) <- bests 5 pop'
sequence_ $ format <$> res sequence_ $ format <$> res
where where