Wed, 18 Dec 2019 21:32:27 +0200
put_unless_closed!
| 0 | 1 | ################################# |
| 2 | # Tools for iterative algorithms | |
| 3 | ################################# | |
| 4 | ||
|
4
59fd17a3cea0
Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
5 | __precompile__() |
|
59fd17a3cea0
Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
6 | |
| 0 | 7 | module Iterate |
| 8 | ||
| 9 | using Printf | |
| 10 | ||
| 11 | ############## | |
| 12 | # Our exports | |
| 13 | ############## | |
| 14 | ||
| 15 | export simple_iterate | |
| 16 | ||
| 17 | ######################################################################## | |
| 18 | # Simple itertion function, calling `step()` `params.maxiter` times and | |
| 19 | # reporting objective value every `params.verbose_iter` iterations. | |
| 20 | # The function `step` should take as its argument a function that itself | |
| 21 | # takes as its argument a function that calculates the objective value | |
| 22 | # on demand. | |
| 23 | ######################################################################## | |
| 24 | ||
| 25 | function simple_iterate(step :: Function, | |
| 26 | params::NamedTuple) | |
| 27 | for iter=1:params.maxiter | |
| 28 | step() do calc_objective | |
| 29 | if params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0 | |
| 30 | v, _ = calc_objective() | |
| 31 | @printf("%d/%d J=%f\n", iter, params.maxiter, v) | |
| 32 | return true | |
| 33 | end | |
| 34 | end | |
| 35 | end | |
| 36 | end | |
| 37 | ||
| 38 | function simple_iterate(step :: Function, | |
| 39 | datachannel::Channel{T}, | |
| 40 | params::NamedTuple) where T | |
| 41 | for iter=1:params.maxiter | |
| 42 | d = take!(datachannel) | |
| 43 | step(d) do calc_objective | |
| 44 | if params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0 | |
| 45 | v, _ = calc_objective() | |
| 46 | @printf("%d/%d J=%f\n", iter, params.maxiter, v) | |
| 47 | return true | |
| 48 | end | |
| 49 | end | |
| 50 | end | |
| 51 | end | |
| 52 | ||
| 53 | end | |
| 54 |