Fri, 03 May 2024 13:08:41 -0500
Update manifest
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 | ||
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
25 | function simple_verbosity(iter, params, calc_objective) |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
26 | if params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0 |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
27 | v, extra₀ = calc_objective() |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
28 | if isa(extra₀, AbstractString) |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
29 | extra = " [$extra₀]" |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
30 | else |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
31 | extra = "" |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
32 | end |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
33 | @printf("%d/%d J=%f%s\n", iter, params.maxiter, v, extra) |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
34 | return true |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
35 | end |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
36 | end |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
37 | |
0 | 38 | function simple_iterate(step :: Function, |
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
39 | params :: NamedTuple) |
0 | 40 | for iter=1:params.maxiter |
41 | step() do calc_objective | |
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
42 | simple_verbosity(iter, params, calc_objective) |
0 | 43 | end |
44 | end | |
45 | end | |
46 | ||
47 | function simple_iterate(step :: Function, | |
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
48 | datachannel :: Channel{T}, |
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
49 | params :: NamedTuple) where T |
0 | 50 | for iter=1:params.maxiter |
51 | d = take!(datachannel) | |
52 | step(d) do calc_objective | |
22
d5e10d963303
Add "extra" string output to simple_iterate.
Tuomo Valkonen <tuomov@iki.fi>
parents:
4
diff
changeset
|
53 | simple_verbosity(iter, params, calc_objective) |
0 | 54 | end |
55 | end | |
56 | end | |
57 | ||
58 | end | |
59 |