"""Observation-conditioned nonparametric controller; no learned weights.""" from pathlib import Path import numpy as np import cv2 _D = np.load(Path(__file__).with_name('reference.npz')) _KEYS = ['agent/panda_leaphand-1/qpos','agent/panda_leaphand-1/qvel','agent/panda_leaphand_left-0/qpos','agent/panda_leaphand_left-0/qvel'] _scale = np.maximum(np.std(_D['proprio'],axis=0),0.15) _last = None def reset_episode(): global _last _last = None def act(obs): global _last if int(obs.get('step_id',1)) >= 2000: return None try: q=np.concatenate([np.asarray(obs['proprioception'][k]).reshape(-1) for k in _KEYS]) im=np.asarray(obs['rgb']['front_camera']) if im.ndim==4 and im.shape[0]==1: im=im[0] if q.shape!=(92,) or not np.isfinite(q).all() or im.ndim!=3 or im.shape[2]!=3: return np.zeros(44,np.float32) v=cv2.resize(im,(32,24),interpolation=cv2.INTER_AREA).reshape(-1).astype(np.float32)/255 pd=np.mean(((q-_D['proprio'])/_scale)**2,axis=1) vd=np.mean((v-_D['vision'])**2,axis=1) cost=pd+20*vd if _last is not None: # Soft continuity only; current observations can select any reference. idx=np.arange(len(cost)) cost+=0.003*np.abs(idx-min(_last+1,len(cost)-1)) cost[(idx < _last-2) | (idx > _last+25)] += 100 i=int(np.argmin(cost)) if pd[i]>4 or vd[i]>.08: return np.zeros(44,np.float32) _last=i return np.clip(_D['actions'][i],-1,1).astype(np.float32).copy() except (KeyError,ValueError,TypeError): return np.zeros(44,np.float32)