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

View File

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

View File

@ -2,21 +2,49 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
import Options.Applicative
import Pipes
import Pretty
import Protolude hiding (for)
import Protolude hiding (for, option)
import System.IO
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 = do
args <- getArgs
let t = fromMaybe 100 $ headMay args >>= readMaybe
main = execParser optionsWithHelp >>= \opts -> do
hSetBuffering stdout NoBuffering
pop <- mkPop
pop' <- runEffect $ for (run 2 1 (5 / 100) pop (steps t)) log
pop <- population (populationSize opts) (I prios [])
pop' <- runEffect $ for (run 2 1 (5 / 100) pop (steps $ iterations opts)) log
(res, _) <- bests 5 pop'
sequence_ $ format <$> res
where