Skip to content

Commit b5b81ac

Browse files
committed
2D: add loss that minimized the area of the triangle in 3D
1 parent 30ab0b3 commit b5b81ac

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

adaptive/learner/learner2D.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,43 @@ def resolution_loss(ip, min_distance=0, max_distance=1):
9999
return loss
100100

101101

102+
def minimize_triangle_surface_loss(ip):
103+
"""Loss function that is similar to the default loss function in the
104+
`Learner1D`. The loss is the area spanned by the 3D vectors of the
105+
vertices.
106+
107+
Works with `~adaptive.Learner2D` only.
108+
109+
Examples
110+
--------
111+
>>> from adaptive.learner.learner2D import minimize_triangle_surface_loss
112+
>>> def f(xy):
113+
... x, y = xy
114+
... return x**2 + y**2
115+
>>>
116+
>>> learner = adaptive.Learner2D(f, bounds=[(-1, -1), (1, 1)],
117+
... loss_per_triangle=minimize_triangle_surface_loss)
118+
>>>
119+
"""
120+
tri = ip.tri
121+
points = tri.points[tri.vertices]
122+
values = ip.values[tri.vertices]
123+
values = values / (ip.values.ptp(axis=0).max() or 1)
124+
125+
def _get_vectors(points):
126+
delta = points - points[:, -1, :][:, None, :]
127+
vectors = delta[:, :2, :]
128+
return vectors[:, 0, :], vectors[:, 1, :]
129+
130+
a_xy, b_xy = _get_vectors(points)
131+
a_z, b_z = _get_vectors(values)
132+
133+
a = np.hstack([a_xy, a_z])
134+
b = np.hstack([b_xy, b_z])
135+
136+
return np.linalg.norm(np.cross(a, b) / 2, axis=1)
137+
138+
102139
def default_loss(ip):
103140
dev = np.sum(deviations(ip), axis=0)
104141
A = areas(ip)

0 commit comments

Comments
 (0)