Copyright | (c) Justin Le 2019 |
---|---|
License | BSD3 |
Maintainer | justin@jle.im |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Functions to run optimiziers.
Synopsis
- data RunOpts m a = RO {}
- hoistRunOpts :: (forall x. m x -> n x) -> RunOpts m a -> RunOpts n a
- data ParallelOpts m a = PO {}
- hoistParallelOpts :: (forall x. m x -> n x) -> ParallelOpts m a -> ParallelOpts n a
- opto :: Monad m => RunOpts m a -> m (Maybe r) -> a -> Opto m r a -> m a
- opto' :: Monad m => RunOpts m a -> m (Maybe r) -> a -> Opto m r a -> m (a, Opto m r a)
- optoNonSampling' :: Monad m => RunOpts m a -> a -> Opto m () a -> m (a, Opto m () a)
- optoNonSampling :: Monad m => RunOpts m a -> a -> Opto m () a -> m a
- optoConduit :: Monad m => RunOpts m a -> a -> Opto (ConduitT r a m) r a -> ConduitT r a m ()
- optoConduit' :: Monad m => RunOpts m a -> a -> Opto (ConduitT r a m) r a -> ConduitT r a m (Opto (ConduitT r a m) r a)
- optoFold :: Monad m => RunOpts m a -> a -> Opto (StateT [r] m) r a -> [r] -> m (a, [r])
- optoFold' :: Monad m => RunOpts m a -> a -> Opto (StateT [r] m) r a -> [r] -> m (a, [r], Opto (StateT [r] m) r a)
- optoPar :: forall m r a. MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> m (Maybe r) -> a -> Opto m r a -> m a
- optoParChunk :: forall m r a. MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> (Int -> m [r]) -> a -> Opto (StateT [r] m) r a -> m a
- optoParNonSampling :: MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> a -> Opto m () a -> m a
- optoConduitPar :: forall m r a. MonadUnliftIO m => RunOpts m a -> ParallelOpts m a -> a -> Opto m r a -> ConduitT () r m () -> ConduitT () a m ()
- 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 ()
- mean :: (Foldable1 t, Fractional a) => t a -> a
Options
Options for running an optimizer.
RO | |
|
Instances
Contravariant (RunOpts m) Source # | |
Invariant (RunOpts m) Source # | |
Defined in Numeric.Opto.Run | |
Applicative m => Default (RunOpts m a) Source # | |
Defined in Numeric.Opto.Run |
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.
PO | |
|
Instances
Functor m => Invariant (ParallelOpts m) Source # | |
Defined in Numeric.Opto.Run invmap :: (a -> b) -> (b -> a) -> ParallelOpts m a -> ParallelOpts m b # | |
(Applicative m, Fractional a) => Default (ParallelOpts m a) Source # | |
Defined in Numeric.Opto.Run 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
:: 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.
:: 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.
:: 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.
:: 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
:: 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
.
:: 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.
:: 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.
:: 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
:: 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:
- Splitting into multiple threads (based on
poThreads
) - Running
opto
(single-threaded optimiztion) on each thread, independently, from the same initial value. - 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 usingpoCombine
(by default, it takes the mean). - 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.
:: 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.
:: 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.