Files
Robot_Arm/ml/mining/tasks.py
T
Hark 2a0a2e55ad ml: 数据生成 + 可微运动学对拍 + MeanFlow/WTA 策略与闭环仿真 + APG/挖错/ONNX 导出与运行手册
- M1 数据生成器:采样/老师/距离下界认证/时间律/统计/一致性门禁;确定性老师(碰撞次数计时 + resetSession)、pos 任务冻结 J6、--consensus 邻域共识过滤、Dr 类。
- 全流程代码与运行手册:数据打包/可微运动学与对拍/MeanFlow+WTA 模型/闭环仿真/APG/挖错循环/ONNX 导出;webapp 策略运行时 ik-policy.js、?policy=1 接入、tests/policy_bench.cjs;ik-solver 增 pairList/clusterExport/criticalPairs/pairGap/leverTable。
2026-10-18 06:40:23 +08:00

80 lines
3.1 KiB
Python

"""无老师出题(PyTorch):A 均匀 / B 直线被挡 / C 贴立柱 / D 工作空间外;pos/pose 各半。"""
import math
import torch
from ml.common.features import wrap180
def rand_q(kin, n, g):
u = torch.rand(n, 6, device=kin.lo.device, generator=g)
return kin.lo + u * (kin.hi - kin.lo)
def free_q(kin, n, g, chunk=4096):
out = []
while sum(x.shape[0] for x in out) < n:
q = rand_q(kin, chunk, g)
ok = kin.gaps(q, 0.5)['gm'].min(-1).values > 0
out.append(q[ok])
return torch.cat(out)[:n]
def straight_blocked(kin, a, b, n=24):
d = torch.where(kin.cont, wrap180(b - a), b - a)
s = torch.linspace(0, 1, n, device=a.device)
pts = (a[:, None] + s[None, :, None] * d[:, None]).reshape(-1, 6)
return (kin.gaps(pts, 0.5)['gm'].min(-1).values <= 0).view(a.shape[0], n).any(-1)
def column_gap(kin, q):
gm = kin.gaps(q, 40)['gm']
return torch.stack([gm[:, g] for g in (2, 3, 4, 5, 6)], -1).min(-1).values # 组 0(立柱/底座)× 组 2..6
@torch.no_grad()
def sample_tasks(kin, n, seed, mix=(0.3, 0.3, 0.3, 0.1)):
g = torch.Generator(device=kin.lo.device); g.manual_seed(seed)
want = [int(round(n * m)) for m in mix]; want[0] += n - sum(want)
tasks = []
def emit(cat, q0, b, T=None):
pose = torch.rand(q0.shape[0], device=q0.device, generator=g) < 0.5
if cat == 'D': pose[:] = False
xf = kin.cum_xf(b)
Tb = kin.fk(b, xf) if T is None else T
Rb = kin.orient(b, xf).reshape(-1, 9)
for i in range(q0.shape[0]):
tasks.append({'cat': cat, 'mode': 'pose' if pose[i] else 'pos', 'q0': q0[i].tolist(), 'T': Tb[i].tolist(),
'R': Rb[i].tolist() if pose[i] else None, 'exec_s': 3.0, 'travel': 0.0})
if want[0]:
emit('A', free_q(kin, want[0], g), free_q(kin, want[0], g))
for cat, k in (('B', want[1]), ('C', want[2])):
got_a, got_b, have = [], [], 0
while have < k:
a, b = free_q(kin, 2048, g), free_q(kin, 2048, g)
m = straight_blocked(kin, a, b)
if cat == 'C': m &= column_gap(kin, b) < 8
got_a.append(a[m]); got_b.append(b[m]); have += int(m.sum())
if k: emit(cat, torch.cat(got_a)[:k], torch.cat(got_b)[:k])
if want[3]:
a, b = free_q(kin, want[3], g), rand_q(kin, want[3], g)
o = kin.fk(torch.zeros(1, 6, device=a.device))
T = o + (kin.fk(b) - o) * 1.6
emit('D', a, b, T)
return tasks
def neighbours(t, k, g):
"""±5 mm / ±3° 邻居"""
out = []
for _ in range(k):
u = torch.randn(3, generator=g); u = u / u.norm() * 5.0
nt = dict(t); nt['T'] = [x + float(d) for x, d in zip(t['T'], u)]
if t.get('R'):
ax = torch.randn(3, generator=g); ax = ax / ax.norm(); ang = math.radians(3.0) * float(torch.rand(1, generator=g))
K = torch.tensor([[0, -ax[2], ax[1]], [ax[2], 0, -ax[0]], [-ax[1], ax[0], 0]])
Rr = torch.eye(3) + math.sin(ang) * K + (1 - math.cos(ang)) * K @ K
nt['R'] = (Rr @ torch.tensor(t['R']).view(3, 3)).flatten().tolist()
nt['hard'] = True
out.append(nt)
return out