opto-0.1.0.0: General-purpose performant numeric optimization library

Copyright(c) Justin Le 2019
LicenseBSD3
Maintainerjustin@jle.im
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Numeric.Opto.Run

Contents

Description

Functions to run optimiziers.

Synopsis

Options

data RunOpts m a Source #

Options for running an optimizer.

Constructors

RO 

Fields

  • roStopCond :: Diff a -> a -> m Bool

    Stop condition; will stop when True (default = never stop)

  • roReport :: a -> m ()

    Reporting function (default = no report)

  • roLimit :: Maybe Int

    Number of batches to run (Nothing = run forever) (default = Nothing).

  • roBatch :: Int

    Size of batching updates (1 = no batching) (default = 1)

  • roFreq :: Maybe Int

    batches per report (Nothing = never report) (default = Just 1).

Instances
Contravariant (RunOpts m) Source # 
Instance details

Defined in Numeric.Opto.Run

Methods

contramap :: (a -> b) -> RunOpts m b -> RunOpts m a #

(>$) :: b -> RunOpts m b -> RunOpts m a #

Invariant (RunOpts m) Source # 
Instance details

Defined in Numeric.Opto.Run

Methods

invmap :: (a -> b) -> (b -> a) -> RunOpts m a -> RunOpts m b #

Applicative m => Default (RunOpts m a) Source # 
Instance details

Defined in Numeric.Opto.Run

Methods

def :: RunOpts m a #

hoistRunOpts :: (forall x. m x -> n x) -> RunOpts m a -> RunOpts n a Source #

Map over the underlying monad of a RunOpts.

data ParallelOpts m a Source #

Options for running an optimizer in a concurrent setting.

Constructors

PO 

Fields

  • poThreads :: Maybe Int

    Number of threads (Nothing = max capacity) (default = Nothing)

  • poSplit :: Int

    How many batches thread will process before regrouping (default = 1000)

  • poCombine :: NonEmpty a -> m a

    How to recombine a pool of updated results into a single result (default = pure . mean)

  • poPull :: Bool

    For conduit runners, whether or not conduit is in "pull-based" mode, where optimization doesn't happen until requested downstream. This is ignored if not running via conduit (default = True)

Instances
Functor m => Invariant (ParallelOpts m) Source # 
Instance details

Defined in Numeric.Opto.Run

Methods

invmap :: (a -> b) -> (b -> a) -> ParallelOpts m a -> ParallelOpts m b #

(Applicative m, Fractional a) => Default (ParallelOpts m a) Source # 
Instance details

Defined in Numeric.Opto.Run

Methods

def :: ParallelOpts m a #

hoistParallelOpts :: (forall x. m x -> n x) -> ParallelOpts m a -> ParallelOpts n a Source #

Map over the underlying monad of a ParallelOpts.

Single-threaded

opto Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> m (Maybe r)

Produce new sample.

-> a

Value to optimize

-> Opto m r a

Optimizer

-> m a 

Run an optimizer on some input, given a monadic action to produce each new sample. When the action produces Nothing, the running immediately terminates even if the stop condition has not yet been met.

opto' Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> m (Maybe r)

Produce new sample.

-> a

Value to optimize

-> Opto m r a

Optimizer

-> m (a, Opto m r a) 

A version of opto that also returns an updated optimizer state that can be resumed.

optoNonSampling' Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto m () a

Non-sampling optimizer

-> m (a, Opto m () a) 

A version of optoNonSampling that also returns an updated optimizer state that can be resumed.

optoNonSampling Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto m () a

Non-sampling optimizer

-> m a 

Run a non-sampling optimizer on some input until the stop condition is met.

Sampling methods

optoConduit Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto (ConduitT r a m) r a

Optimizer

-> ConduitT r a m () 

Given an optimizer and some initial value, produce a ConduitT that takes in samples and outputs each successively optimized versions of the value. This essentially is a convenient wrapper over opto.

To get the final optimized result after a stream has terminated, compose this with a sink like last.

optoConduit ro x0 o .| last
  :: ConduitT r o m (Maybe a)

optoConduit ro x0 o .| lastDef x0
  :: ConduitT r o m a

Note that this emits every single updated version of the value, but still only runs roReport at the frequency of roFreq.

optoConduit' Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto (ConduitT r a m) r a

Optimizer

-> ConduitT r a m (Opto (ConduitT r a m) r a) 

A version of optoConduit that also returns an updated optimizer state that can be resumed.

optoFold Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto (StateT [r] m) r a

Optimizer

-> [r]

List of samples to optimize over

-> m (a, [r]) 

Convenient wrapper over opto to allow consumption over a list of samples.

optoFold' Source #

Arguments

:: Monad m 
=> RunOpts m a

Runner options

-> a

Value to optimize

-> Opto (StateT [r] m) r a

Optimizer

-> [r]

List of samples to optimize over

-> m (a, [r], Opto (StateT [r] m) r a) 

A version of optoFold' that also returns an updated optimizer state that can be resumed.

Parallel

optoPar Source #

Arguments

:: MonadUnliftIO m 
=> RunOpts m a

Runner options

-> ParallelOpts m a

Parallelization options

-> m (Maybe r)

Produce new sample (should be thread-safe)

-> a

Value to optimize

-> Opto m r a

Optimizer

-> m a 

Run an optimizer in parallel on multiple threads on some value, given a (thread-safe) monadic action to produce each new sample.

It does this by repeatedly:

  1. Splitting into multiple threads (based on poThreads)
  2. Running opto (single-threaded optimiztion) on each thread, independently, from the same initial value.
  3. After poSplit items have been processed, all threads wait on each other to stop. After each thread is done, each thread's optimized value is then aggregated using poCombine (by default, it takes the mean).
  4. This new optimized combined value is then used to begin the cycle again.

When action produces Nothing for all threads, the running immediately terminates on all threads and returns even if the stop condition has not yet been met. If the stop condition is met, the value given to the stop condition will be used as the final result, ignoring all other thread pools.

optoParChunk Source #

Arguments

:: MonadUnliftIO m 
=> RunOpts m a

Runner options

-> ParallelOpts m a

Parallelization options

-> (Int -> m [r])

Batched fetch of samples. Input is how many samples the action expects to receive, although it is okay if a lower amount is given due to an exhausted sample pool.

-> a

Value to optimize

-> Opto (StateT [r] m) r a

Optimizer

-> m a 

A version of optoPar that performs a batch fetch for each thread's entire sample pool before beginning parallel optimization. This can be useful if the sampling is faster in batch amounts.

optoParNonSampling Source #

Arguments

:: MonadUnliftIO m 
=> RunOpts m a

Runner options

-> ParallelOpts m a

Parallelization options

-> a

Value to optimize

-> Opto m () a

Non-sampling optimizer

-> m a 

Run a non-sampling optimizer in parallel on multiple threads on some value until the stop condition is met.

See optoPar for a detailed description of how parallel optimization is implemented.

Sampling Methods

optoConduitPar :: forall m r a. MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> a -> Opto m r a -> ConduitT () r m () -> ConduitT () a m () Source #

Given an optimizer, some initial value, and a conduit source, returns a conduit sorce that outputs succesively optimized versions of the value after each thread recombination, where each version is optimized using parallel multi-threaded optimization.

See optoPar for a detailed description on how parallel optimization is implemented.

Note that, unlike optoConduit, which is a conduit, this is a conduit (source) transformer. It takes a source outputting samples and returns a new source of optimized values.

A value is emitted after every thread recombination/call of poCombine.

optoConduitParChunk :: forall m r a. MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> a -> Opto (StateT [r] m) r a -> ConduitT () r m () -> ConduitT () a m () Source #

A version of optoConduitPar that performs a batch fetch from the input source for each thread's entire sample pool before beginning parallel optimization. This can be useful if the source can produce values faster in batch amounts.

Util

mean :: (Foldable1 t, Fractional a) => t a -> a Source #

The mean of the values in a non-empty container.