-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconvergence_loop_example.py
More file actions
44 lines (34 loc) · 1.29 KB
/
convergence_loop_example.py
File metadata and controls
44 lines (34 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from flowcept import Flowcept, FlowceptLoop
THRESHOLD = 0.05
MAX_ITERS = 50
def update_error(e: float) -> float:
"""Dummy update rule that shrinks error each step."""
return e * 0.7 # replace with your real update
def finalize_loop(loop):
"""Flush the last iteration so it’s captured even if we break early."""
if getattr(loop, "enabled", False) and getattr(loop, "_last_iteration_task", None) is not None:
try:
loop._end_iteration_task(loop._last_iteration_task) # uses FlowceptLoop internals
except Exception:
pass
# --- Convergence with FlowceptLoop ---
error = 1.0
loop = FlowceptLoop(
items=range(MAX_ITERS), # upper bound; we’ll break early on convergence
loop_name="convergence",
item_name="iter",
)
for it in loop:
# --- your loop body ---
error = update_error(error)
print(f"iter={it} error={error:.4f}")
# Record per-iteration outputs (only once per iteration)
loop.end_iter({
"iter": it,
"error": error,
"status": "converged" if error <= THRESHOLD else "continuing",
})
# Convergence check — emulate: while error > THRESHOLD: update error
if error <= THRESHOLD:
finalize_loop(loop) # ensure the last iteration is closed before breaking
break