Costs¶
vertax.cost
Cost functions collection, used for outer optimization.
A cost function can be any user-defined function but it has to respect a strict signature.
For a PbcMesh and PbcBilevelOptimizer, the cost function must have the following signature:
vertTable: Array,
heTable: Array,
faceTable: Array,
width: float,
height: float,
vertTable_target: Array,
heTable_target: Array,
faceTable_target: Array,
selected_verts: Array | None,
selected_hes: Array | None,
selected_faces: Array | None,
image_target: Array | None,
and return an Array (or float).
The names can vary and you can give default parameters. But the number and type of parameters is important. You don't have to use every parameters but they all have to be here. An unused parameters can of course also have the type None.
Same for BoundedMesh and BoundedBilevelOptimizer, but with a slightly different signature:
vertTable: Array,
angTable: Array,
heTable: Array,
faceTable: Array,
vertTable_target: Array,
angTable_target: Array,
heTable_target: Array,
faceTable_target: Array,
selected_verts: Array | None,
selected_hes: Array | None,
selected_faces: Array | None,
image_target: Array | None,
and return an Array (or float).
Hopefully the variable names are self-explanatory.
You can create a function with this signature exactly that uses also locally-accessible external variable if you want.
cost_v2v ¶
cost_v2v(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, selected_verts: Array | None = None, selected_hes: Array | None = None, selected_faces: Array | None = None, _image_target: Array | None = None) -> Array
Cost vertex to vertex. Compare the positions of given vertices to target vertices (PbcMesh).
cost_areas ¶
cost_areas(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, _selected_verts: Array | None = None, _selected_hes: Array | None = None, selected_faces: Array | None = None, _image_target: Array | None = None) -> Array
Cost areas : compare the areas of the given mesh versus the areas of the target mesh (PbcMesh).
Mean over the selected faces (all faces if selected_faces is None) of the squared difference
between the area of a face and the area of the same face in the target mesh, which must have
the same faces.
cost_ratio ¶
cost_ratio(vertTable: Array, _angTable: Array | None = None, _heTable: Array | None = None, _faceTable: Array | None = None, _vertTable_target: Array | None = None, _angTable_target: Array | None = None, _heTable_target: Array | None = None, _faceTable_target: Array | None = None, _selected_verts: Array | None = None, _selected_hes: Array | None = None, _selected_faces: Array | None = None, _image_target: Array | None = None) -> Array
Cost that nudges a BoundedMesh to elongate along one axis while narrowing along the orthogonal axis.
The cost is the squared distance of the aspect ratio to a fixed target,
with r_target = TARGET_RATIO (a module constant, 2.0). L is the longest distance between
two vertices, and S is the extent of the vertices perpendicular to the axis through those two
vertices (the largest minus the smallest signed distance to that axis).
This cost is not smooth. The two vertices that define the long axis are selected with
argmax, so the axis, and hence S, jump when another pair of vertices becomes the farthest
pair, and S is a maximum minus a minimum over vertices. The gradient is that of the current
selection and is discontinuous at such changes. For a smooth aspect-ratio cost, built from the
second moments of the region the cluster covers, use make_cost_aspect_ratio.
aspect_ratio_along_x ¶
aspect_ratio_along_x(vertTable: Array, angTable: Array, heTable: Array) -> Array
Aspect ratio of a bounded cluster along the fixed x axis, \(r_x=\sqrt{S_{xx}/S_{yy}}\).
\(S_{xx}\) and \(S_{yy}\) are the central second moments of the region the cluster
covers, cells and circular caps of the free interface included
(vertax.geo.get_cluster_moments_bounded). For an ellipse of semi-axes a along x and b
along y, \(r_x=a/b\). The axis is fixed: the same ellipse turned by an angle \(\phi\)
has \(r_x^2=(a^2\cos^2\phi+b^2\sin^2\phi)/(a^2\sin^2\phi+b^2\cos^2\phi)\), which is
largest at \(\phi=0\), so elongating the cluster along any other direction gives a smaller
\(r_x\). Smooth in the vertex positions and arc angles.
aspect_ratio_principal ¶
aspect_ratio_principal(vertTable: Array, angTable: Array, heTable: Array) -> Array
Aspect ratio of a bounded cluster along its own principal axes, \(\sqrt{\lambda_1/\lambda_2}\).
\(\lambda_1\ge\lambda_2\) are the eigenvalues of the central second-moment matrix of the
region the cluster covers (vertax.geo.get_cluster_moments_bounded), so the value does not
depend on the orientation of the cluster, and the direction of elongation is left free. For an
ellipse of semi-axes a >= b it is a/b at any orientation. Smooth except at an exactly
isotropic cluster, where the two eigenvalues cross; a term of relative size 1e-12 under the
square root keeps the gradient finite there.
make_cost_aspect_ratio_along_x ¶
make_cost_aspect_ratio_along_x(target_ratio: float) -> Callable
Outer cost \(\mathcal C=(r_x-r_\mathrm{target})^2\) for a BoundedMesh, with the axis of elongation fixed to x.
r_x is aspect_ratio_along_x. Use this cost when the direction of elongation is part of the
design; with make_cost_aspect_ratio the direction is free and can drift during the design.
The returned function has the bounded outer-cost signature.
make_cost_aspect_ratio ¶
make_cost_aspect_ratio(target_ratio: float) -> Callable
Outer cost \(\mathcal C=(r-r_\mathrm{target})^2\) for a BoundedMesh, invariant to rotation.
r is aspect_ratio_principal, so the cluster may elongate along any direction. Unlike
cost_ratio, which measures the largest vertex distance and changes non-smoothly when the
farthest pair of vertices changes, this cost is smooth. The returned function has the bounded
outer-cost signature.
cost_checkerboard ¶
cost_checkerboard(vertTable: Array, _angTable: Array, heTable: Array, faceTable: Array, _vertTable_target: Array | None = None, _angTable_target: Array | None = None, _heTable_target: Array | None = None, _faceTable_target: Array | None = None, _selected_verts: Array | None = None, _selected_hes: Array | None = None, _selected_faces: Array | None = None, _image_target: Array | None = None) -> Array
Cost that nudges a BoundedMesh with different fated cells to avoid having neighboring cells with same fate.
This leads to a checkerboard pattern when there is 2 fates.
topological_distance ¶
topological_distance(vertTable: Array, heTable: Array, faceTable: Array, width: float, height: float, vertTable_target: Array, heTable_target: Array, faceTable_target: Array, selected_verts: Array | None = None, selected_hes: Array | None = None, selected_faces: Array | None = None, _image_target: Array | None = None) -> int
Discrete topological distance d_topo between two meshes: differing cell contacts.
Builds the dual (cell) adjacency matrix of each mesh, A[a, b] = 1 iff cells a and b
share a junction, puts the two cell labellings into correspondence with the Hungarian
algorithm (cell labels are arbitrary), and counts the cell--cell contacts on which the two
meshes then disagree, each unordered pair once:
An elementary T1 transition destroys exactly one contact and creates exactly one other, so it
changes two upper-triangle entries. Therefore d_topo is always even, and
d_topo / 2 is a lower bound on the number of T1 transitions that separate the two
topologies. The bound need not be reached: two tissues can differ by more T1 transitions than
their contacts show.
This is a measurement, not an objective. It returns a Python int and provides no
gradient, so it cannot be assigned to loss_function_outer. Use it to quantify topological
distance and to time phase switches in a staged schedule. Separately, any purely topological or
edge-level cost is a strict information loss for edge-tension inference relative to a
vertex-level cost such as cost_v2v -- the cell-adjacency Laplacian has a large null
space -- so topology should not be used to infer tensions in the first place.
hessian_at_target ¶
hessian_at_target(target: PbcMesh, energy: Callable, edges_params: Array | None = None) -> Array
Hessian of the inner energy at the target geometry, divided by its spectral norm.
The Hessian \(\mathbf H=\nabla^2_{\mathbf X}\mathcal E\) is taken with respect to the flattened
vertex positions \(\mathbf X=(x_1,y_1,x_2,y_2,\dots)\) of target, at its own topology and box,
with the target's vertices_params and faces_params (for \(\mathcal E_2\), the target
areas \(A^0\)). It is symmetrized, \((\mathbf H+\mathbf H^{\mathsf T})/2\), and divided by its
spectral norm \(\max_i|\lambda_i(\mathbf H)|\), so that its largest eigenvalue in magnitude is 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
PbcMesh
|
The target mesh. Its vertex positions are converted to JAX's default float dtype;
use |
required |
energy
|
Callable
|
The inner energy with the signature of |
required |
edges_params
|
Array | None
|
The edge parameters at which the Hessian is evaluated. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The normalized Hessian, shape |
make_cost_hessian_weighted ¶
make_cost_hessian_weighted(target: PbcMesh, energy: Callable, edges_params: Array | None = None) -> Callable
Hessian-weighted vertex cost \(\mathcal C_W\) for a PbcMesh, in the periodic outer-cost signature.
where \(\widehat{\mathbf H}\) is hessian_at_target (by default with every edge parameter set to
1) and each vertex displacement \(\mathbf X-\mathbf X_{\text{target}}\) is taken to the nearest
periodic image of the target vertex, as in cost_v2v. The vertex cost cost_v2v is
\(\tfrac12\lVert\mathbf H^{+}\mathbf M\,\delta\boldsymbol\theta\rVert^2\) to first order in the
parameter error \(\delta\boldsymbol\theta\) (\(\mathbf M\), the derivative of the forces with respect
to the parameters), so the soft modes of the tissue, with small Hessian eigenvalues, amplify any
geometric error. With the Hessian \(\mathbf H\) at the true parameters in place of
\(\widehat{\mathbf H}\), the same expansion gives \(\tfrac12\lVert\mathbf M\,\delta\boldsymbol\theta\rVert^2\),
the force residual, which the soft modes do not amplify; \(\widehat{\mathbf H}\) approximates
\(\mathbf H\) up to its normalization, without using the parameters being inferred. The
normalization multiplies the cost by a constant and keeps its scale comparable to that of
cost_v2v.
\(\widehat{\mathbf H}\) is computed once, from target, and belongs to its topology: the cost
compares vertex \(i\) of the mesh with vertex \(i\) of the target, and is meant for a mesh with the
target topology, kept fixed (update_T1 = False). From another topology, use the staged
schedule of switch_cost_at_target_topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
PbcMesh
|
The target mesh; the cost compares with the target vertices the optimizer passes,
which are normally |
required |
energy
|
Callable
|
The inner energy, in the signature of |
required |
edges_params
|
Array | None
|
The edge parameters of the Hessian; |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
|
Example
optimizer.loss_function_outer = make_cost_hessian_weighted(target, optimizer.loss_function_inner)
optimizer.update_T1 = False
switch_cost_at_target_topology ¶
switch_cost_at_target_topology(optimizer: PbcBilevelOptimizer, mesh: PbcMesh, cost: Callable) -> bool
Second stage of the staged schedule: at the first match with the target topology, freeze it and change cost.
The staged schedule infers parameters from a mesh whose topology differs from the target's. It
minimizes a cost without a fixed metric, normally cost_v2v, with T1 transitions active, until
the topology of the mesh first matches that of the target (\(d_{\text{topo}}=0\),
topological_distance); it then disables T1 transitions and minimizes cost, normally
make_cost_hessian_weighted, whose Hessian belongs to the target topology and changes
discontinuously at a T1 transition. Switching at the first match matters: under cost_v2v the
topology can match and unmatch again, so a rule that waits for several matching epochs switches
late or never.
Call it after every epoch. It compares mesh with the optimizer's target
(vertices_target, edges_target, faces_target); once the switch has been made, it
leaves the optimizer as it is and returns True without computing the distance again. To cap
the first stage at a number of epochs, make the switch yourself when the cap is reached
(optimizer.update_T1 = False; optimizer.loss_function_outer = cost).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
PbcBilevelOptimizer
|
The |
required |
mesh
|
PbcMesh
|
The mesh being optimized, after the epoch. |
required |
cost
|
Callable
|
The cost of the second stage. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
optimizer.loss_function_outer = cost_v2v
cost_w = make_cost_hessian_weighted(target, optimizer.loss_function_inner)
for epoch in range(n_epochs):
optimizer.bilevel_optimization(mesh)
switch_cost_at_target_topology(optimizer, mesh, cost_w)