Tue, 07 Dec 2021 11:41:07 +0200
New logger and iteration interface
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 | |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
7 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
8 | `module AlgTools.iterate` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
9 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
10 | This module implements looping and status display utilities for iterate algorithms. It includes: |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
11 | - `make_iterate` and `make_iterate_log` for constructing functions for iterating |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
12 | - `simple_iterate` and `simple_iterage_log` for iteration |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
13 | - `default_iterate = make_iterate()` default implementation |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
14 | - `logrepr` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
15 | """ |
0 | 16 | module Iterate |
17 | ||
18 | using Printf | |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
19 | using ..Logger |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
20 | using ..FunctionalProgramming |
0 | 21 | |
22 | ############## | |
23 | # Our exports | |
24 | ############## | |
25 | ||
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
26 | export make_iterate, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
27 | make_iterate_log, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
28 | simple_iterate, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
29 | simple_iterate_log, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
30 | default_iterate, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
31 | logrepr |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
32 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
33 | ########################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
34 | # Internal helper routines |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
35 | ########################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
36 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
37 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
38 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
39 | `logrepr(v)` |
0 | 40 | |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
41 | Create the displayed presentation for log items. Override for nice (condensed) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
42 | presentation of rich log items, or define `show` if it is not defined for your type. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
43 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
44 | function logrepr(v :: Any) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
45 | return "«", v, "»" |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
46 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
47 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
48 | function logrepr(v :: Number) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
49 | return "J=", v |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
50 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
51 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
52 | @inline function calculate_and_report(calc_objective, iter, maxiter) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
53 | v = calc_objective() |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
54 | s = @sprintf("%d/%d J=%f%s\n", iter, params.maxiter, v, extra) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
55 | print_styled(iter, "/", maxiter, logrepr(v)...; color=:light_black) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
56 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
57 | return v |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
58 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
59 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
60 | ######################################################################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
61 | # Simple iteration function, calling `step()` `params.maxiter` times and |
0 | 62 | # reporting objective value every `params.verbose_iter` iterations. |
63 | # The function `step` should take as its argument a function that itself | |
64 | # takes as its argument a function that calculates the objective value | |
65 | # on demand. | |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
66 | ######################################################################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
67 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
68 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
69 | `simple_iterate(step :: Function; maxiter = 1000, verbose_iter=100)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
70 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
71 | Iterate the function `step` for at most `maxiter` iterations, displaying |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
72 | status every `verbose_iter` iterations. The function `step` should accept one parameter, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
73 | called `verbose` below, and return `true` if the iterations are to continue, `false` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
74 | otherwise. The `verbose` parameter of `step` is itself a function that should be |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
75 | called with a function that will on demand, as determined by `verbose`, calculate |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
76 | the function value or other reported data. For example: |
0 | 77 | |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
78 | ```julia |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
79 | simple_iterate() do verbose |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
80 | # perform iterations |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
81 | verbose() do |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
82 | # calculate function value or other displayed data v |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
83 | return |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
84 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
85 | return true |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
86 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
87 | ``` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
88 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
89 | function simple_iterate(step :: Function ; maxiter=1000, verbose_iter=100) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
90 | for iter=1:maxiter |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
91 | res = step() do calc_objective |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
92 | if verbose_iter!=0 && mod(iter, verbose_iter) == 0 |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
93 | calculate_and_report(calc_objective, iter, params.maxiter) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
94 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
95 | true |
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
96 | end |
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
97 | if !res |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
98 | break |
0 | 99 | end |
100 | end | |
101 | end | |
102 | ||
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
103 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
104 | `simple_iterate(step :: Function, ch :: Channel{D}; ...)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
105 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
106 | Version of `simple_iterate` that on each step reads a new value of type `D` from the channel `ch`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
107 | These are passed as the second parameter of `step`. The behaviour is otherwise as with |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
108 | the basic version of `simple_iterate`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
109 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
110 | function simple_iterate(step :: Function, ch :: Channel{D}; kwargs...) where D |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
111 | simple_iterate(kwargs...) do calc_objective |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
112 | step(calc_objective, take!(ch)) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
113 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
114 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
115 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
116 | # constructor interface |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
117 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
118 | `make_iterate(; ...)` and `make_iterate(ch :: Channel{D}; ...)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
119 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
120 | These are constructor versions of `simple_iterate`, each returning a function |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
121 | that takes as its sole parameter the `step` parameter of `simple_iterate`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
122 | For example, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
123 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
124 | ```julia |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
125 | iterate = make_iterate(verbose_iter=10, maxiter=10000) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
126 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
127 | iterate() do verbose |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
128 | # use as the example in the documentation of `simple_iterate` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
129 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
130 | ``` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
131 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
132 | make_iterate = curryflip(simple_iterate) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
133 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
134 | # backwards-compatibility |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
135 | simple_iterate(step :: Function, params :: NamedTuple) = |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
136 | simple_iterate(step; params...) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
137 | simple_iterate(step :: Function, ch :: Channel{D}, params :: NamedTuple) where D = |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
138 | simple_iterate(step, ch; params...) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
139 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
140 | # Default parameterisation for use as default keyword argument in algorithm |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
141 | default_iterate = make_iterate() |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
142 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
143 | ######################################################################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
144 | # Logging iteration function. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
145 | ######################################################################### |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
146 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
147 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
148 | `simple_iterate_log(step :: Function, log :: Log{T}; maxiter = 1000, verbose_iter=100)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
149 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
150 | Iterate the function `step` for at most `maxiter` iterations, logging and displaying |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
151 | results as determined by `verbose_iter`. The function `step` should accept one parameter, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
152 | called `verbose` below, and return `true` if the iterations are to continue, `false` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
153 | otherwise. The `verbose` parameter of `step` is itself a function that should be |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
154 | called with a function that will on demand, as determined by the `log`, calculate |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
155 | the function value or other logged and displayed data. For example: |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
156 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
157 | ```julia |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
158 | log = Log{Float64} |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
159 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
160 | simple_iterate_log(log) do verbose |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
161 | # perform iterations |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
162 | verbose() do |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
163 | # calculate function value or other logged data v |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
164 | return v |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
165 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
166 | return true |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
167 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
168 | ``` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
169 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
170 | function simple_iterate_log(step :: Function, log :: Log{T}; maxiter=1000) where T |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
171 | for iter=1:maxiter |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
172 | res = step() do calc_objective |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
173 | if_log!(log, iter) do |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
174 | calculate_and_report(calc_objective, iter, params.maxiter) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
175 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
176 | true |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
177 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
178 | if !res |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
179 | break |
0 | 180 | end |
181 | end | |
182 | end | |
183 | ||
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
184 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
185 | `simple_iterate_log(step :: Function, log :: Log{T}, ch :: Channel{D}; ...)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
186 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
187 | Version of `simple_iterate_log` that on each step reads a new value of type `D` from the channel `ch`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
188 | These are passed as the second parameter of `step`. The behaviour is otherwise as with |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
189 | the basic version of `simple_iterate_log`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
190 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
191 | function simple_iterate_log(step :: Function, log :: Log{T}, ch :: Channel{D}; kwargs...) where {T, D} |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
192 | simple_iterate_log(log; kwargs...) do calc_objective |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
193 | step(calc_objective, take!(ch)) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
194 | end |
0 | 195 | end |
196 | ||
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
197 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
198 | `make_iterate_log(log :: log{T}; ...)` and `make_iterate_log(log :: Log{T}, ch :: Channel{D}; ...)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
199 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
200 | These are constructor versions of `simple_iterate_log`, each returning a function |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
201 | that takes as its sole parameter the `step` parameter of `simple_iterate_log`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
202 | For example, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
203 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
204 | ```julia |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
205 | log = Log{Float64} |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
206 | iterate = make_iterate_log(log, verbose_iter=10, maxiter=10000) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
207 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
208 | iterate() do verbose |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
209 | # proceed as in the example in the documentation of `simple_iterate` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
210 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
211 | ``` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
212 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
213 | make_iterate_log = curryflip(simple_iterate_log) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
214 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
215 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
22
diff
changeset
|
216 |