From bbddd62c7071b8cb2f4abcc47c613c61f3bf7801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20P=C3=A4tzel?= Date: Tue, 7 Jan 2020 08:45:50 +0100 Subject: [PATCH] Add applicative CLI option parsing --- default.nix | 20 ++++++++++++-------- haga.cabal | 3 +++ src/Main.hs | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/default.nix b/default.nix index b02396b..a849063 100644 --- a/default.nix +++ b/default.nix @@ -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; } diff --git a/haga.cabal b/haga.cabal index 8ab8596..9d2a470 100644 --- a/haga.cabal +++ b/haga.cabal @@ -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 diff --git a/src/Main.hs b/src/Main.hs index dd13cf1..b15b70d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -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