def step(self, action): # execute the chosen action given the ocular motor noise move_dis = calc_dis(self.fixate, action) ocular_noise = np.random.normal(0, self.ocular_std * move_dis, action.shape) self.fixate = action + ocular_noise self.fixate = np.clip(self.fixate, -1, 1) others = { 'n_fixation': self.n_fixation, 'target': self.state, 'belief': self.belief, 'aim': action, 'fixate': self.fixate } self.n_fixation += 1 # check if the eye is within the target region dis_to_target = calc_dis(self.state, self.fixate) if dis_to_target < self.fitts_W / 2: done = True reward = 0 else: done = False reward = -1 # has not reached the target, get new obs at the new fixation location self.obs, self.obs_uncertainty = self._get_obs() self.belief, self.belief_uncertainty = self._get_belief() if self.n_fixation > self.max_fixation: done = True return self.belief, reward, done, others
def step(self, a): self.n_steps += 1 self.chosen_action = self.actions[a] self._state_transit(self.chosen_action[self.EYE], self.eta[self.EYE], self.EYE) self._state_transit(self.chosen_action[self.HAND], self.eta[self.HAND], self.HAND) self._get_state_observation() # check if the hand is within the target region dis_to_target = calc_dis(self.target_pos, self.fixate[self.HAND]) if dis_to_target < self.fitts_W / 2: done = True reward = 0 else: done = False reward = -1 if self.n_steps > self.max_steps: done = True info = self._save_data() return self.observation, reward, done, info
def _get_obs(self): eccentricity=calc_dis(self.state,self.fixate) obs_uncertainty=eccentricity spatial_noise=np.random.normal(0, self.swapping_std*eccentricity, self.state.shape) obs=self.state + spatial_noise # the obs should rarely be outside of -1 and 1, just in case obs=np.clip(obs,-1,1) return obs,obs_uncertainty
def _get_tgt_obs(self): eccentricity = calc_dis(self.target_pos, self.fixate[self.EYE]) spatial_noise = np.random.normal(0, self.swapping_std * eccentricity, self.target_pos.shape) obs = self.target_pos + spatial_noise obs = np.clip(obs, -1, 1) obs_uncertainty = eccentricity return obs, obs_uncertainty
def _get_obs(self): eccentricity = calc_dis(self.state, self.fixate) obs_uncertainty = eccentricity spatial_noise = np.random.normal(0, self.swapping_std * eccentricity, self.state.shape) obs = self.state + spatial_noise # the obs should rarely be outside of -1 and 1, just in case ''' if obs[0]>1 or obs[0]<-1 or obs[1]>1 or obs[0]<-1: print(obs) print('obs is out of the range!!!!!') ''' obs = np.clip(obs, -1, 1) return obs, obs_uncertainty
def _state_transit(self, action, eta, mode): if action == 1: # new command self.stage[mode] = self.PREP self.prep_step[mode] = 0 self.aim_at[mode] = self.observation[0:2] if self.stage[mode] == self.PREP: self.prep_step[mode] += self.time_step if self.prep_step[mode] > self.prep_duration[mode]: # done with the prep, ready to move self.stage[mode] = self.MOV self.mov_step[mode] = 0 # generate the trajectory move_dis = calc_dis(self.current_pos[mode], self.aim_at[mode]) # motor noise is dependent on the moving distance noise = np.random.normal( 0, self.motor_noise_params[mode] * move_dis, (2, )) actual_pos = self.aim_at[mode] + noise amp = calc_dis(self.current_pos[mode], actual_pos) * self.scale_deg pos, vel = get_trajectory(eta, amp, self.current_pos[mode], actual_pos, self.time_step) self.n_move_steps[mode] = len(pos) if mode == self.EYE: self.pos_e = pos self.vel_e = vel else: self.pos_h = pos self.vel_h = vel if self.stage[mode] == self.MOV: if self.mov_step[mode] < self.n_move_steps[mode]: # update pos if mode == self.EYE: self.current_pos[mode] = self.pos_e[self.mov_step[mode]] self.current_vel[mode] = self.vel_e[self.mov_step[mode]] else: self.current_pos[mode] = self.pos_h[self.mov_step[mode]] self.current_vel[mode] = self.vel_h[self.mov_step[mode]] else: # finish moving, start fixate self.stage[mode] = self.FIX self.fixate_step[mode] = 0 self.mov_step[mode] += 1 if self.stage[mode] == self.FIX: self.fixate_step[mode] += self.time_step if self.fixate_step[mode] >= self.fixation_duration[mode]: self.fixate[mode] = self.current_pos[mode] if mode == self.EYE: self.tgt_obs, self.tgt_obs_uncertainty = self._get_tgt_obs( ) self.tgt_belief, self.tgt_belief_uncertainty = self._get_tgt_belief( )
fix1 = True prep2 = True move2 = True fix2 = True env = EyeHandEnv() test_actions = [ 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] obs = env.reset() for i in range(len(test_actions)): action = test_actions[i] observation, reward, done, info = env.step(action) dis = calc_dis(info['target_pos'], info['pos_eye']) dis_hand = calc_dis(info['target_pos'], info['pos_hand']) print(info) stage_eye = info['stage_eye'] stage_hand = info['stage_hand'] plt.subplot(1, 2, 1) plt.plot(0, 0.5, 'k+', markersize=15) if stage_eye == -1: plt.plot(i + 1, dis, 'ko', markersize=15) if prep1: plt.plot(i + 1, dis, 'ko', label='eye_prep', markersize=15)
model = PPO.load(f'{log_dir}savedmodel/model_ppo_2000000_steps') # Test the trained agent n_eps=5000 eps=0 while eps<n_eps: done=False step=0 obs= env.reset() fixate=np.array([0,0]) movement_time=0 while not done: step+=1 action, _ = model.predict(obs,deterministic = True) obs, reward, done, info = env.step(action) move_dis=calc_dis(info['fixate'],fixate) fixate=info['fixate'] amp=move_dis/(unit) movement_time+=saccade_time(amp)+latency if done: if step==1: first_time.append(movement_time) elif step==2: second_time.append(movement_time) else: third_time.append(movement_time) all_time.append(movement_time) eps+=1 break all_time_mean[0,i]=np.mean(first_time)
plt.figure(figsize=(8,8)) data=my_data[:,-1]==e data_episode=my_data[data,:] n_steps=len(data_episode) for step in range(n_steps): target_pos=data_episode[step,1:3] eye_stage=data_episode[step,5:6] eye_pos=data_episode[step,6:8] hand_stage=data_episode[step,10:11] hand_pos=data_episode[step,11:13] dis_eye=0.5-calc_dis(target_pos,eye_pos) dis_hand=0.5-calc_dis(target_pos,hand_pos) s=14 ss=10 if step==0: #Eye plt.plot(-50,0,'ko',markersize=s,label='Eye Prep') plt.plot(-50,0,'k<',label='Eye Moving',markersize=ss) plt.plot(0,0,'k*',markersize=s,label='Eye Fixate') #Eye plt.plot(-50,0,'ro',markersize=s,label='Hand Prep') plt.plot(-50,0,'r<',label='Hand Moving',markersize=ss) plt.plot(5,0,'r*',markersize=s,label='Hand Fixate')