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