You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add logging file
* add logging statements
* some progress
* some actual progress
* small fixes
* fix Printf compat
* minor optimizations
* one more optimization attempt
* various improvements and rework interface.md
* small docstring typos
* stopping criterion docs and small fixes
* WIP
* collapse docstrings by default
* write logging docs
* clean up and finish logging docs
* fix test
* add iteration test
* add error test
* Add test for `IfAction`
* Add GroupAction test
* add global toggle test
* `GroupAction` -> `ActionGroup`
* Update interface.md
Co-authored-by: Ronny Bergmann <git@ronnybergmann.net>
---------
Co-authored-by: Ronny Bergmann <git@ronnybergmann.net>
To make everything tangible, we will work through a concrete example to illustrate the library's goals and concepts.
37
+
Our running example is Heron's / Babylonian method for estimating $\sqrt{S}$.
38
+
(see also the concise background on Wikipedia: [Babylonian method (Heron's method)](https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)):
39
+
Starting from an initial guess $x_0$, we may converge to the solution by iterating:
Algorithms define a mutable step via [`step!`](@ref). For Heron's method:
101
+
102
+
```@example Heron
103
+
function AlgorithmsInterface.step!(problem::SqrtProblem, algorithm::HeronAlgorithm, state::HeronState)
104
+
S = problem.S
105
+
x = state.iterate
106
+
state.iterate = 0.5 * (x + S / x)
107
+
return state
108
+
end
109
+
```
110
+
111
+
Note that we are only focussing on the actual algorithm, and *not* incrementing the iteration counter.
112
+
These kinds of bookkeeping should be handled by the [`AlgorithmsInterface.increment!`](@ref) function, which will by default already increment the iteration counter.
113
+
The following generic functionality is therefore enough for our purposes, and does *not* need to be defined.
114
+
Nevertheless, if additional bookkeeping would be desired, this can be achieved by overloading that function:
115
+
116
+
```julia
117
+
function AlgorithmsInterface.increment!(state::State)
118
+
state.iteration +=1
119
+
return state
120
+
end
121
+
```
122
+
123
+
### Running the algorithm
124
+
125
+
With these definitions in place you can already run (assuming you also choose a stopping criterion – added in the next section):
126
+
127
+
```@example Heron
128
+
function heron_sqrt(x; maxiter = 10)
129
+
prob = SqrtProblem(x)
130
+
alg = HeronAlgorithm(StopAfterIteration(maxiter))
131
+
state = solve(prob, alg) # allocates & runs
132
+
return state.iterate
133
+
end
134
+
135
+
println("Approximate sqrt: ", heron_sqrt(16.0))
136
+
```
137
+
138
+
We will refine this example with better halting logic and logging shortly.
139
+
140
+
## Reference: Core interface types & functions
141
+
142
+
Below are the automatic API docs for the core interface pieces. Read them after grasping the example above – the intent should now be clearer.
26
143
27
144
```@autodocs
28
145
Modules = [AlgorithmsInterface]
@@ -31,7 +148,7 @@ Order = [:type, :function]
31
148
Private = true
32
149
```
33
150
34
-
## Algorithm
151
+
###Algorithm
35
152
36
153
```@autodocs
37
154
Modules = [AlgorithmsInterface]
@@ -40,7 +157,7 @@ Order = [:type, :function]
40
157
Private = true
41
158
```
42
159
43
-
## Problem
160
+
###Problem
44
161
45
162
```@autodocs
46
163
Modules = [AlgorithmsInterface]
@@ -49,11 +166,15 @@ Order = [:type, :function]
49
166
Private = true
50
167
```
51
168
52
-
## State
169
+
###State
53
170
54
171
```@autodocs
55
172
Modules = [AlgorithmsInterface]
56
173
Pages = ["interface/state.jl"]
57
174
Order = [:type, :function]
58
175
Private = true
59
-
```
176
+
```
177
+
178
+
### Next: Stopping criteria
179
+
180
+
Proceed to the stopping criteria section to add robust halting logic (iteration caps, time limits, tolerance on successive iterates, and combinations) to this square‑root example.
0 commit comments