File size: 3,511 Bytes
37d21bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Public self-check for a MuJoCo submission. Uses only the submission.

Usage: python self_check.py /work/output
Reports manifest errors (missing labels, references), contacts deeper than 5 mm at the authored
state, and the time above 5 mm, peak body speed and finite state over a 3 s passive rollout.
The limits are the prompt's construction rules.
"""
import json
import sys
from pathlib import Path

import mujoco
import numpy as np

LIMIT_M, LIMIT_S, SPEED = .005, .02, 5.


def body(model, geom):
    return mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_BODY, int(model.geom_bodyid[geom])) or f'body{int(model.geom_bodyid[geom])}'


def main(output):
    output = Path(output)
    report = {'manifest_errors': []}
    model = mujoco.MjModel.from_xml_path(str(output / 'scene.xml'))
    data = mujoco.MjData(model)
    manifest = json.loads((output / 'objects.json').read_text())['objects']
    kinds = {'body_names': mujoco.mjtObj.mjOBJ_BODY, 'geom_names': mujoco.mjtObj.mjOBJ_GEOM,
             'joint_names': mujoco.mjtObj.mjOBJ_JOINT, 'flex_names': mujoco.mjtObj.mjOBJ_FLEX}
    ids = [o.get('id') for o in manifest]
    if len(ids) != len(set(ids)):
        report['manifest_errors'].append('duplicate object ids')
    for obj in manifest:
        if not isinstance(obj.get('label'), str) or not obj['label'].strip():
            report['manifest_errors'].append(f"{obj.get('id')}: no label (say plainly what the object is)")
        for key, kind in kinds.items():
            for name in obj.get(key, []):
                if mujoco.mj_name2id(model, kind, name) < 0:
                    report['manifest_errors'].append(f"{obj.get('id')}: {key} '{name}' does not resolve")
        sites = obj.get('sites', {})
        for name in (sites.values() if isinstance(sites, dict) else sites):
            if mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_SITE, name) < 0:
                report['manifest_errors'].append(f"{obj.get('id')}: site '{name}' does not resolve")
    mujoco.mj_forward(model, data)
    initial = sorted(((-float(c.dist), body(model, c.geom1), body(model, c.geom2)) for c in data.contact[:data.ncon]
                      if -c.dist > LIMIT_M), reverse=True)
    report['initial_contacts_deeper_than_5mm'] = [{'depth_m': d, 'bodies': [a, b]} for d, a, b in initial[:20]]
    report['initial_contact_count_deeper_than_5mm'] = len(initial)
    above, peak, finite = 0., 0., True
    for _ in range(int(np.ceil(3/model.opt.timestep))):
        mujoco.mj_step(model, data)
        deepest = max([-float(c.dist) for c in data.contact[:data.ncon]] or [0.])
        above += model.opt.timestep if deepest > LIMIT_M else 0.
        peak = max(peak, float(np.linalg.norm(data.cvel[:, 3:], axis=1).max()) if data.cvel.size else 0.)
        finite = bool(np.isfinite(data.qpos).all() and np.isfinite(data.qvel).all())
        if not finite:
            break
    report.update(time_above_5mm_s=above, peak_body_speed_mps=peak, finite=finite,
                  timestep_s=float(model.opt.timestep),
                  passive_ok=bool(finite and above <= LIMIT_S and peak <= SPEED and not report['manifest_errors']))
    print(json.dumps(report, indent=2))
    print('\nPASSIVE CHECK', 'PASSED' if report['passive_ok'] else 'FAILED',
          f"- {len(initial)} initial contacts > 5 mm, {above:.3f} s above 5 mm, peak speed {peak:.2f} m/s,"
          f" {len(report['manifest_errors'])} manifest errors")


if __name__ == '__main__':
    main(sys.argv[1] if len(sys.argv) > 1 else '/work/output')