Skip to content
Snippets Groups Projects

2D: add loss that minimizes the area of the triangle in 3D

Merged Bas Nijholt requested to merge loss_like_1d_in_2d into master
1 file
+ 38
0
Compare changes
  • Side-by-side
  • Inline
@@ -99,6 +99,44 @@ def resolution_loss(ip, min_distance=0, max_distance=1):
return loss
def minimize_triangle_surface_loss(ip):
"""Loss function that is similar to the default loss function in the
`Learner1D`. The loss is the area spanned by the 3D vectors of the
vertices.
Works with `~adaptive.Learner2D` only.
Examples
--------
>>> from adaptive.learner.learner2D import minimize_triangle_surface_loss
>>> def f(xy):
... x, y = xy
... return x**2 + y**2
>>>
>>> learner = adaptive.Learner2D(f, bounds=[(-1, -1), (1, 1)],
... loss_per_triangle=minimize_triangle_surface_loss)
>>>
"""
tri = ip.tri
points = tri.points[tri.vertices]
values = ip.values[tri.vertices]
delta_pts = points - points[:, -1, :][:, None, :]
vectors = delta_pts[:, :2, :]
a_xy = vectors[:, 0, :]
b_xy = vectors[:, 1, :]
delta_vals = values - values[:, -1, :][:, None, :]
vectors = delta_vals[:, :2, :]
a_z = vectors[:, 0, :]
b_z = vectors[:, 1, :]
a = np.hstack([a_xy, a_z])
b = np.hstack([b_xy, b_z])
return np.linalg.norm(np.cross(a, b), axis=1)
def default_loss(ip):
dev = np.sum(deviations(ip), axis=0)
A = areas(ip)
Loading