Spaces:
Running
Running
Commit ·
9aba971
1
Parent(s): e9b7141
Add 3D shape comparison reward module (AlphaFold-inspired)
Browse filesNew env/shape_reward.py with:
- Chamfer Distance (primary reward, scipy KD-tree, <0.1ms)
- Hausdorff Distance (worst-case misalignment)
- lDDT-like local distance score (superposition-free, per-fold accuracy)
- GDT-TS threshold scores (% vertices within distance thresholds)
- Bounding box IoU
Wired into env/rewards.py as LEVEL 5 (15% weight) alongside existing
2D crease pattern matching. Activates when target has 'vertices_coords_folded'
field with 3D vertex data. Gracefully inactive for 2D-only targets.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- env/rewards.py +33 -6
- env/shape_reward.py +223 -0
env/rewards.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
from .verifier import check_all_vertices, check_degree_sanity, geometric_crease_coverage
|
| 3 |
from .paper_state import PaperState
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def load_target(target_path: str) -> dict:
|
|
@@ -81,7 +83,28 @@ def compute_reward(
|
|
| 81 |
r['delta'] = max(0.0, new_coverage - old_coverage)
|
| 82 |
r['regression'] = min(0.0, new_coverage - old_coverage)
|
| 83 |
|
| 84 |
-
# LEVEL 5:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
all_valid = (
|
| 86 |
r['kawasaki'] == 1.0
|
| 87 |
and r['maekawa'] == 1.0
|
|
@@ -89,22 +112,26 @@ def compute_reward(
|
|
| 89 |
)
|
| 90 |
r['completion'] = 10.0 if (r['progress'] > 0.9 and all_valid) else 0.0
|
| 91 |
|
| 92 |
-
# LEVEL
|
| 93 |
r['efficiency'] = -0.01 * (1 + step / max_steps)
|
| 94 |
|
| 95 |
-
# Weighted total
|
| 96 |
r['total'] = (
|
|
|
|
| 97 |
0.05 * r['anchored']
|
| 98 |
+ 0.05 * r['novelty']
|
| 99 |
+ 0.06 * r['kawasaki']
|
| 100 |
+ 0.06 * r['maekawa']
|
| 101 |
+ 0.04 * r['blb']
|
| 102 |
+ 0.04 * r['degree_sanity']
|
| 103 |
-
+ 0.
|
| 104 |
+ 0.05 * r['economy']
|
| 105 |
+ 0.05 * r['assignment_accuracy']
|
| 106 |
-
+ 0.
|
| 107 |
-
+ 0.
|
|
|
|
|
|
|
|
|
|
| 108 |
+ r['completion']
|
| 109 |
+ r['efficiency']
|
| 110 |
)
|
|
|
|
| 1 |
import json
|
| 2 |
+
import numpy as np
|
| 3 |
from .verifier import check_all_vertices, check_degree_sanity, geometric_crease_coverage
|
| 4 |
from .paper_state import PaperState
|
| 5 |
+
from .shape_reward import compute_3d_shape_reward
|
| 6 |
|
| 7 |
|
| 8 |
def load_target(target_path: str) -> dict:
|
|
|
|
| 83 |
r['delta'] = max(0.0, new_coverage - old_coverage)
|
| 84 |
r['regression'] = min(0.0, new_coverage - old_coverage)
|
| 85 |
|
| 86 |
+
# LEVEL 5: 3D Shape comparison (AlphaFold-inspired)
|
| 87 |
+
# If the target has 3D vertex data, compare the current fold state's
|
| 88 |
+
# vertex positions against the target's folded shape.
|
| 89 |
+
r['shape_score'] = 0.0
|
| 90 |
+
target_3d = target.get('vertices_coords_folded') # 3D target shape
|
| 91 |
+
if target_3d is not None:
|
| 92 |
+
# Current state vertices (2D for now; z=0 for flat creases)
|
| 93 |
+
current_verts = []
|
| 94 |
+
for vid, (x, y) in new_state.graph.vertices.items():
|
| 95 |
+
current_verts.append([x, y, 0.0])
|
| 96 |
+
|
| 97 |
+
if current_verts:
|
| 98 |
+
shape_result = compute_3d_shape_reward(current_verts, target_3d)
|
| 99 |
+
r['chamfer'] = shape_result['chamfer']
|
| 100 |
+
r['chamfer_score'] = shape_result['chamfer_score']
|
| 101 |
+
r['hausdorff'] = shape_result['hausdorff']
|
| 102 |
+
r['bbox_iou'] = shape_result['bbox_iou']
|
| 103 |
+
r['lddt'] = shape_result['lddt']
|
| 104 |
+
r['shape_score'] = shape_result['shape_total']
|
| 105 |
+
r.update({k: v for k, v in shape_result.items() if k.startswith('gdt_')})
|
| 106 |
+
|
| 107 |
+
# LEVEL 6: Completion bonus
|
| 108 |
all_valid = (
|
| 109 |
r['kawasaki'] == 1.0
|
| 110 |
and r['maekawa'] == 1.0
|
|
|
|
| 112 |
)
|
| 113 |
r['completion'] = 10.0 if (r['progress'] > 0.9 and all_valid) else 0.0
|
| 114 |
|
| 115 |
+
# LEVEL 7: Efficiency — escalating step cost
|
| 116 |
r['efficiency'] = -0.01 * (1 + step / max_steps)
|
| 117 |
|
| 118 |
+
# Weighted total (2D crease matching + 3D shape comparison)
|
| 119 |
r['total'] = (
|
| 120 |
+
# 2D crease pattern matching (existing)
|
| 121 |
0.05 * r['anchored']
|
| 122 |
+ 0.05 * r['novelty']
|
| 123 |
+ 0.06 * r['kawasaki']
|
| 124 |
+ 0.06 * r['maekawa']
|
| 125 |
+ 0.04 * r['blb']
|
| 126 |
+ 0.04 * r['degree_sanity']
|
| 127 |
+
+ 0.15 * r['progress']
|
| 128 |
+ 0.05 * r['economy']
|
| 129 |
+ 0.05 * r['assignment_accuracy']
|
| 130 |
+
+ 0.10 * r['delta']
|
| 131 |
+
+ 0.05 * r['regression']
|
| 132 |
+
# 3D shape comparison (new — AlphaFold-inspired)
|
| 133 |
+
+ 0.15 * r['shape_score']
|
| 134 |
+
# Bonuses and penalties
|
| 135 |
+ r['completion']
|
| 136 |
+ r['efficiency']
|
| 137 |
)
|
env/shape_reward.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
3D Shape Comparison Rewards (AlphaFold-inspired)
|
| 3 |
+
|
| 4 |
+
Computes how close a folded origami shape is to a target 3D shape using:
|
| 5 |
+
- Chamfer Distance: average nearest-neighbor distance between point clouds
|
| 6 |
+
- Hausdorff Distance: worst-case misalignment
|
| 7 |
+
- GDT-TS-like score: % of vertices within distance thresholds (for logging)
|
| 8 |
+
- Bounding box IoU: does the folded shape fit the target dimensions?
|
| 9 |
+
|
| 10 |
+
These metrics are fast (<1ms for typical origami meshes with 10-100 vertices)
|
| 11 |
+
and can be computed per-step or at episode end.
|
| 12 |
+
|
| 13 |
+
Usage:
|
| 14 |
+
from env.shape_reward import compute_3d_shape_reward
|
| 15 |
+
|
| 16 |
+
reward = compute_3d_shape_reward(
|
| 17 |
+
predicted_vertices=[[0,0,0], [1,0,0], [1,1,0], [0,1,0.5]],
|
| 18 |
+
target_vertices=[[0,0,0], [1,0,0], [1,1,0], [0,1,0]],
|
| 19 |
+
)
|
| 20 |
+
# reward = {'chamfer': 0.03, 'hausdorff': 0.5, 'gdt_1': 0.75, ...}
|
| 21 |
+
"""
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 25 |
+
from scipy.spatial import cKDTree
|
| 26 |
+
from scipy.spatial.distance import directed_hausdorff
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def chamfer_distance(P: np.ndarray, Q: np.ndarray) -> float:
|
| 30 |
+
"""
|
| 31 |
+
Symmetric Chamfer Distance between two point clouds.
|
| 32 |
+
|
| 33 |
+
CD(P,Q) = (1/|P|) * sum_p(min_q ||p-q||^2) + (1/|Q|) * sum_q(min_p ||q-p||^2)
|
| 34 |
+
|
| 35 |
+
Lower = better. 0 = identical shapes.
|
| 36 |
+
"""
|
| 37 |
+
if len(P) == 0 or len(Q) == 0:
|
| 38 |
+
return float('inf')
|
| 39 |
+
|
| 40 |
+
tree_P = cKDTree(P)
|
| 41 |
+
tree_Q = cKDTree(Q)
|
| 42 |
+
|
| 43 |
+
# P -> Q distances
|
| 44 |
+
d_pq, _ = tree_Q.query(P)
|
| 45 |
+
# Q -> P distances
|
| 46 |
+
d_qp, _ = tree_P.query(Q)
|
| 47 |
+
|
| 48 |
+
return float(np.mean(d_pq ** 2) + np.mean(d_qp ** 2))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def hausdorff_dist(P: np.ndarray, Q: np.ndarray) -> float:
|
| 52 |
+
"""
|
| 53 |
+
Symmetric Hausdorff Distance — max of min distances.
|
| 54 |
+
Captures worst-case misalignment.
|
| 55 |
+
"""
|
| 56 |
+
if len(P) == 0 or len(Q) == 0:
|
| 57 |
+
return float('inf')
|
| 58 |
+
|
| 59 |
+
d_forward = directed_hausdorff(P, Q)[0]
|
| 60 |
+
d_backward = directed_hausdorff(Q, P)[0]
|
| 61 |
+
return float(max(d_forward, d_backward))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def gdt_ts_score(P: np.ndarray, Q: np.ndarray, thresholds: tuple = (0.01, 0.02, 0.05, 0.10)) -> dict:
|
| 65 |
+
"""
|
| 66 |
+
GDT-TS-like score: fraction of predicted vertices within distance thresholds of target.
|
| 67 |
+
|
| 68 |
+
Inspired by protein structure prediction metrics. For each threshold t,
|
| 69 |
+
compute the fraction of vertices in P that have a nearest neighbor in Q
|
| 70 |
+
within distance t.
|
| 71 |
+
|
| 72 |
+
Returns dict like: {'gdt_1': 0.8, 'gdt_2': 0.9, 'gdt_5': 1.0, 'gdt_10': 1.0, 'gdt_avg': 0.925}
|
| 73 |
+
"""
|
| 74 |
+
if len(P) == 0 or len(Q) == 0:
|
| 75 |
+
return {f'gdt_{int(t*100)}': 0.0 for t in thresholds}
|
| 76 |
+
|
| 77 |
+
tree_Q = cKDTree(Q)
|
| 78 |
+
distances, _ = tree_Q.query(P)
|
| 79 |
+
|
| 80 |
+
scores = {}
|
| 81 |
+
for t in thresholds:
|
| 82 |
+
key = f'gdt_{int(t * 100)}'
|
| 83 |
+
scores[key] = float(np.mean(distances <= t))
|
| 84 |
+
|
| 85 |
+
scores['gdt_avg'] = float(np.mean(list(scores.values())))
|
| 86 |
+
return scores
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def bounding_box_iou(P: np.ndarray, Q: np.ndarray) -> float:
|
| 90 |
+
"""
|
| 91 |
+
3D bounding box Intersection over Union.
|
| 92 |
+
|
| 93 |
+
Computes axis-aligned bounding boxes of both point clouds
|
| 94 |
+
and returns their volumetric IoU [0, 1].
|
| 95 |
+
"""
|
| 96 |
+
if len(P) == 0 or len(Q) == 0:
|
| 97 |
+
return 0.0
|
| 98 |
+
|
| 99 |
+
# Ensure 3D
|
| 100 |
+
if P.shape[1] == 2:
|
| 101 |
+
P = np.column_stack([P, np.zeros(len(P))])
|
| 102 |
+
if Q.shape[1] == 2:
|
| 103 |
+
Q = np.column_stack([Q, np.zeros(len(Q))])
|
| 104 |
+
|
| 105 |
+
p_min, p_max = P.min(axis=0), P.max(axis=0)
|
| 106 |
+
q_min, q_max = Q.min(axis=0), Q.max(axis=0)
|
| 107 |
+
|
| 108 |
+
# Intersection
|
| 109 |
+
inter_min = np.maximum(p_min, q_min)
|
| 110 |
+
inter_max = np.minimum(p_max, q_max)
|
| 111 |
+
inter_dims = np.maximum(0, inter_max - inter_min)
|
| 112 |
+
inter_vol = float(np.prod(inter_dims))
|
| 113 |
+
|
| 114 |
+
# Union
|
| 115 |
+
p_vol = float(np.prod(np.maximum(1e-10, p_max - p_min)))
|
| 116 |
+
q_vol = float(np.prod(np.maximum(1e-10, q_max - q_min)))
|
| 117 |
+
union_vol = p_vol + q_vol - inter_vol
|
| 118 |
+
|
| 119 |
+
if union_vol < 1e-15:
|
| 120 |
+
return 0.0
|
| 121 |
+
|
| 122 |
+
return inter_vol / union_vol
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def lddt_like_score(P: np.ndarray, Q: np.ndarray, cutoff: float = 0.15, thresholds: tuple = (0.005, 0.01, 0.02, 0.04)) -> float:
|
| 126 |
+
"""
|
| 127 |
+
lDDT-like (Local Distance Difference Test) score for origami.
|
| 128 |
+
|
| 129 |
+
Inspired by AlphaFold's lDDT metric. For each pair of vertices that are
|
| 130 |
+
within `cutoff` distance in the target shape Q, check if their pairwise
|
| 131 |
+
distance is preserved in the predicted shape P within various thresholds.
|
| 132 |
+
|
| 133 |
+
This is superposition-free — it doesn't require alignment.
|
| 134 |
+
Measures local fold accuracy: are nearby vertices still in the right relative positions?
|
| 135 |
+
|
| 136 |
+
Returns score in [0, 1]. Higher = better.
|
| 137 |
+
"""
|
| 138 |
+
n = min(len(P), len(Q))
|
| 139 |
+
if n < 2:
|
| 140 |
+
return 1.0
|
| 141 |
+
|
| 142 |
+
P_n = P[:n]
|
| 143 |
+
Q_n = Q[:n]
|
| 144 |
+
|
| 145 |
+
# Compute pairwise distances in both shapes
|
| 146 |
+
# Only consider pairs within cutoff in the target
|
| 147 |
+
Q_dists = np.linalg.norm(Q_n[:, None, :] - Q_n[None, :, :], axis=-1)
|
| 148 |
+
P_dists = np.linalg.norm(P_n[:, None, :] - P_n[None, :, :], axis=-1)
|
| 149 |
+
|
| 150 |
+
mask = (Q_dists < cutoff) & (Q_dists > 1e-10) # exclude self-pairs
|
| 151 |
+
if not np.any(mask):
|
| 152 |
+
return 1.0
|
| 153 |
+
|
| 154 |
+
dist_diffs = np.abs(P_dists[mask] - Q_dists[mask])
|
| 155 |
+
|
| 156 |
+
# For each threshold, fraction of pairs preserved
|
| 157 |
+
scores = [float(np.mean(dist_diffs < t)) for t in thresholds]
|
| 158 |
+
return float(np.mean(scores))
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def compute_3d_shape_reward(
|
| 162 |
+
predicted_vertices: list | np.ndarray,
|
| 163 |
+
target_vertices: list | np.ndarray,
|
| 164 |
+
weights: dict | None = None,
|
| 165 |
+
) -> dict:
|
| 166 |
+
"""
|
| 167 |
+
Compute all 3D shape comparison metrics between predicted and target shapes.
|
| 168 |
+
|
| 169 |
+
Args:
|
| 170 |
+
predicted_vertices: Nx2 or Nx3 array of vertex positions (current fold state)
|
| 171 |
+
target_vertices: Mx2 or Mx3 array of vertex positions (target shape)
|
| 172 |
+
weights: optional weight dict for composite score
|
| 173 |
+
|
| 174 |
+
Returns dict with all metrics + weighted 'shape_total' score.
|
| 175 |
+
"""
|
| 176 |
+
P = np.asarray(predicted_vertices, dtype=np.float64)
|
| 177 |
+
Q = np.asarray(target_vertices, dtype=np.float64)
|
| 178 |
+
|
| 179 |
+
# Ensure 3D
|
| 180 |
+
if P.ndim == 1:
|
| 181 |
+
P = P.reshape(-1, 2 if len(P) % 2 == 0 else 3)
|
| 182 |
+
if Q.ndim == 1:
|
| 183 |
+
Q = Q.reshape(-1, 2 if len(Q) % 2 == 0 else 3)
|
| 184 |
+
if P.shape[1] == 2:
|
| 185 |
+
P = np.column_stack([P, np.zeros(len(P))])
|
| 186 |
+
if Q.shape[1] == 2:
|
| 187 |
+
Q = np.column_stack([Q, np.zeros(len(Q))])
|
| 188 |
+
|
| 189 |
+
w = weights or {
|
| 190 |
+
'chamfer': 5.0,
|
| 191 |
+
'hausdorff': 1.0,
|
| 192 |
+
'bbox_iou': 3.0,
|
| 193 |
+
'lddt': 2.0,
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
result = {}
|
| 197 |
+
|
| 198 |
+
# Core metrics
|
| 199 |
+
cd = chamfer_distance(P, Q)
|
| 200 |
+
result['chamfer'] = cd
|
| 201 |
+
result['chamfer_score'] = max(0.0, 1.0 - cd * 10.0) # normalized to ~[0,1]
|
| 202 |
+
|
| 203 |
+
hd = hausdorff_dist(P, Q)
|
| 204 |
+
result['hausdorff'] = hd
|
| 205 |
+
result['hausdorff_score'] = max(0.0, 1.0 - hd * 2.0)
|
| 206 |
+
|
| 207 |
+
result['bbox_iou'] = bounding_box_iou(P, Q)
|
| 208 |
+
|
| 209 |
+
result['lddt'] = lddt_like_score(P, Q)
|
| 210 |
+
|
| 211 |
+
# GDT-TS scores for logging
|
| 212 |
+
gdt = gdt_ts_score(P, Q)
|
| 213 |
+
result.update(gdt)
|
| 214 |
+
|
| 215 |
+
# Composite score
|
| 216 |
+
result['shape_total'] = (
|
| 217 |
+
w.get('chamfer', 5.0) * result['chamfer_score']
|
| 218 |
+
+ w.get('hausdorff', 1.0) * result['hausdorff_score']
|
| 219 |
+
+ w.get('bbox_iou', 3.0) * result['bbox_iou']
|
| 220 |
+
+ w.get('lddt', 2.0) * result['lddt']
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
return result
|