Skip to content

Commit 4688695

Browse files
committed
add resolution_loss_function for Learner1D
1 parent ae2ddf4 commit 4688695

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

adaptive/learner/learner1D.py

+36
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ def triangle_loss(xs, ys):
7777
return sum(vol(pts[i : i + 3]) for i in range(N)) / N
7878

7979

80+
def resolution_loss_function(min_length=0, max_length=1):
81+
"""Loss function that is similar to the `default_loss` function, but you
82+
can set the maximum and minimum size of an interval.
83+
84+
Works with `~adaptive.Learner1D` only.
85+
86+
The arguments `min_length` and `max_length` should be in between 0 and 1
87+
because the total size is normalized to 1.
88+
89+
Returns
90+
-------
91+
loss_function : callable
92+
93+
Examples
94+
--------
95+
>>> def f(x):
96+
... return x**2
97+
>>>
98+
>>> loss = resolution_loss_function(min_length=0.01, max_length=1)
99+
>>> learner = adaptive.Learner1D(f, bounds=[(-1, -1), (1, 1)], loss_per_triangle=loss)
100+
"""
101+
102+
@uses_nth_neighbors(0)
103+
def resolution_loss(xs, ys):
104+
loss = default_loss(xs, ys)
105+
if loss < min_length:
106+
# Return zero such that they won't be chosen again
107+
return 0
108+
if loss > max_length:
109+
# Return infinite such that this interval will be picked
110+
return np.inf
111+
return loss
112+
113+
return resolution_loss
114+
115+
80116
def curvature_loss_function(area_factor=1, euclid_factor=0.02, horizontal_factor=0.02):
81117
# XXX: add a doc-string
82118
@uses_nth_neighbors(1)

0 commit comments

Comments
 (0)