I have a generator that implements evaluation of function f()
over a grid of parameters:
def gen():
for a in vec_a:
for b in vec_b:
for c in vec_c:
for ...
res = f(a, b, c, ...)
yield (a, b, c, ...), res
vec_* are sorted such that f()
is an increasing function of corresponding parameter given all others fixed. More precisely:
if (a2 >= a1) and (b2 >= b1) and (c2 >= c1) and ...:
assert f(a2, b2, c2, ...) >= f(a1, b1, c1, ...)
Thus, for example, if f(a0, b0, c0, ...)
== np.inf, then:
f(a, b0, c0, ...) == np.inf
for every a >= a0f(a0, b, c0, ...) == np.inf
for every b >= b0f(a0, b0, c, ...) == np.inf
for every c >= c0
Now I want to write a generic generator that accepts gen
and skips unnecessary evaluations of f
according to the following rules:
- if
f(.) == np.inf
at some point then I break the innermost loop - if the innermost loop was interrupted at the very first iteration, I should break the penultimate loop level
- rule #3 applies to all other levels of the nested loop
Example: If I get np.inf
at the very first iteration of the grid, I should skip the entire grid and not perform any more evaluations of f.
Example: If I have a grid [0,1,2] x [0,1,2] x [0,1,2]
and f(0,1,0) == np.inf
, then I jump to evaluation f(1, 0, 0)
.
How would I implement such a generator?