def drive(img, kart: pystk.Kart): """ @img: (120,160,3) RGB image return: pystk.Action """ img = np.asarray(img) / 255.0 i, old_action = ray.get(count.increment.remote()) print(i, old_action) prev = ray.get(count.get_img.remote()) if type(old_action) == int: old_action = [old_action, old_action] if i % 5 == 0: action = agent.compute_action(img) else: action = old_action steer_dir = action[0] #gas = action[1] #brake = action[2] fire = action[1] setter = count.set_img.remote(img) count.set_action.remote(action) if i > 10: if np.sum(np.abs(img - prev)) < 50: return pystk.Action(rescue=True) return pystk.Action(steer=steer_dir, acceleration=1.0, fire=fire)
def control(aim_point, current_vel, steer_gain=6, skid_thresh=0.2, target_vel=25): import numpy as np #this seems to initialize an object action = pystk.Action() #compute acceleration action.acceleration = np.clip(target_vel - current_vel, 0, 1) if current_vel > target_vel: action.brake = True action.nitro = False else: action.brake = False action.nitro = True # Compute steering action.steer = np.clip(steer_gain * aim_point[0], -1, 1) # Compute skidding if abs(aim_point[0]) > skid_thresh: action.drift = True else: action.drift = False return action
def play(self, save=None, max_frames=50): state = pystk.WorldState() track = pystk.Track() if save is not None: import PIL.Image import os if not os.path.exists(save): os.makedirs(save) for t in range(max_frames): print('\rframe %d' % t, end='\r') state.update() #print(state.soccer.ball.location) #print(state.soccer.goal_line) #goal_line = False #opposing_goal = state.soccer.goal_line[:-1] #goal_target = [] #if goal_line == True: # goal_target = [statistics.mean([opposing_goal[0][0][0], opposing_goal[0][1][0]]), statistics.mean([opposing_goal[0][0][1], opposing_goal[0][1][1]]),statistics.mean([opposing_goal[0][0][2], opposing_goal[0][1][2]]), ] list_actions = [] for i, p in enumerate(self.active_players): kart = state.players[i].kart aim_point_world = state.soccer.ball.location proj = np.array(state.players[i].camera.projection).T view = np.array(state.players[i].camera.view).T aim_point_image = self._to_image(aim_point_world, proj, view) player = state.players[i] image = np.array(self.k.render_data[i].image) action = pystk.Action() player_action = p(image, player) for a in player_action: setattr(action, a, player_action[a]) list_actions.append(action) if save is not None: PIL.Image.fromarray(image).save(os.path.join(save, 'player%02d_%05d.png' % (i, t))) dest = os.path.join(save, 'player%02d_%05d' % (i, t)) with open(dest + '.csv', 'w') as f: f.write('%0.1f,%-0.1f' % tuple(aim_point_image)) s = self.k.step(list_actions) if not s: # Game over break if save is not None: import subprocess for i, p in enumerate(self.active_players): dest = os.path.join(save, 'player%02d' % i) output = save + '_player%02d.mp4' % i subprocess.call(['ffmpeg', '-y', '-framerate', '10', '-i', dest + '_%05d.png', output]) if hasattr(state, 'soccer'): return state.soccer.score return state.soccer_score
def control(aim_point, current_vel): action = pystk.Action() """ Your code here Hint: Use action.acceleration (0..1) to change the velocity. Try targeting a target_velocity (e.g. 20). Hint: Use action.brake to True/False to brake (optionally) Hint: Use action.steer to turn the kart towards the aim_point, clip the steer angle to -1..1 Hint: You may want to use action.drift=True for wide turns (it will turn faster) """ #print(aim_point) steer_factor = 1.2 steer_x_threshold = 0.1 drift_x_threshold = 3 drift_vel_threshold = 17 x, y, z = aim_point if current_vel < target_velocity: action.acceleration = 1 if abs(x) >= steer_x_threshold: action.steer = steer_factor * x if abs(x) >= drift_x_threshold: action.acceleration = max(0.1, 1 - current_vel / target_velocity) if current_vel > drift_vel_threshold: action.drift = True return action
def __call__(self, s, v): """ s -> image v -> velocity """ with torch.no_grad(): s = s.transpose(2, 0, 1) s = torch.FloatTensor(s).unsqueeze(0).cpu() m = torch.distributions.Categorical(logits=self.net(s)) if np.random.rand() < self.eps: action_index = np.random.choice(list(range(self.n_actions))) else: action_index = m.sample().item() p = m.probs.squeeze()[action_index] p_action = (1 - self.eps) * p + self.eps / N_ACTIONS binary = bin(action_index).lstrip('0b').rjust(4, '0') action = pystk.Action() action.steer = int(binary[0] == '1') * -1.0 + int( binary[1] == '1') * 1.0 action.acceleration = np.clip(5 + int(binary[2] == '1') * 20.0 - v, 0, 0.5) action.drift = binary[3] == '1' return action, action_index, p_action
def rollout_agent(device, vision, action, n_steps=200): race_config = pystk.RaceConfig(num_kart=1, track='icy_soccer_field', mode=pystk.RaceConfig.RaceMode.SOCCER) k = pystk.Race(race_config) k.start() for i in range(5): #Skip the first 5 steps since its the game starting k.step() try: data = [] for n in range(n_steps): img = torch.tensor(np.array(k.render_data[0].image), dtype=torch.float).to(device).permute(2, 0, 1) # heatmap = vision(img) # p = action(torch.sigmoid(heatmap))[0] p = action(img)[0] # print(p[0]) k.step( pystk.Action(steer=float(p[0]), acceleration=float(p[1]), brake=float(p[2]) > 0.5)) #TODO: remove /10 later # print(pystk.Action(acceleration=float(p[0]), steer=float(p[1]), brake=float(p[2])>0.5)) la = k.last_action[0] # print((la.acceleration, la.steer, la.brake)) # print('end') data.append((np.array(k.render_data[0].image), (la.steer, la.acceleration, la.brake))) #TODO: remove /10 later finally: k.stop() del k return data
def drive(img): """ @img: (96,128,3) RGB image return: pystk.Action """ action = pystk.Action() raise NotImplementedError('drive')
def control(aim_point, current_vel): """ Set the Action for the low-level controller :param aim_point: Aim point, in screen coordinate frame [-1..1] :param current_vel: Current velocity of the kart :return: a pystk.Action (set acceleration, brake, steer, drift) """ action = pystk.Action() """ Your code here Hint: Use action.acceleration (0..1) to change the velocity. Try targeting a target_velocity (e.g. 20). Hint: Use action.brake to True/False to brake (optionally) Hint: Use action.steer to turn the kart towards the aim_point, clip the steer angle to -1..1 Hint: You may want to use action.drift=True for wide turns (it will turn faster) """ # Direction of the red circle direction = aim_point[0] scalingFactor = 4 # How much do we steer towards the red circle if (direction > 0): steering = min(1, direction * scalingFactor) elif (direction < 0): steering = max(-1, direction * scalingFactor) else: steering = 0 # Try to maintain a constant velocity if current_vel < 21: action.acceleration = 1 else: action.acceleration = 0 # Skid, if the turn is sharp if (direction < -0.2 or 0.2 < direction): if direction < 0: steering = -1 elif direction > 0: steering = 1 action.drift = True else: action.drift = False # If the red circle is too far left or right, we slow down and readjust the direction of travel if direction < -0.9 or 0.9 < direction: action.drift = False action.brake = True # Activate Nitro action.nitro = True # Save the steering angle action.steer = steering return action
def play(self, save=None, max_frames=50): state = pystk.WorldState() # soccer = pystk.Soccer() # soccer_ball = pystk.SoccerBall() if save is not None: import PIL.Image import os if not os.path.exists(save): os.makedirs(save) time = datetime.now() for t in range(max_frames): # print('\rframe %d' % t, end='\r') print('\rframe {} time {}'.format(t, (datetime.now() - time).seconds), end='\r') state.update() list_actions = [] for i, p in enumerate(self.active_players): player = state.players[i] image = np.array(self.k.render_data[i].image) action = pystk.Action() player_action = p(image, player) for a in player_action: setattr(action, a, player_action[a]) list_actions.append(action) if save is not None: PIL.Image.fromarray(image).save( os.path.join(save, 'player%02d_%05d.png' % (i, t))) s = self.k.step(list_actions) if not s: # Game over break # print(state.soccer.score) # print(state.soccer.ball.location) # print(state.soccer.goal_line) # print(state.karts[0].location) # print(dir(state.karts[0])) # print(t) # print('end') if save is not None: import subprocess for i, p in enumerate(self.active_players): dest = os.path.join(save, 'player%02d' % i) output = save + '_player%02d.mp4' % i subprocess.call([ 'ffmpeg', '-y', '-framerate', '10', '-i', dest + '_%05d.png', output ]) if hasattr(state, 'soccer'): return state.soccer.score return state.soccer_score
def rollout(self, drive, reward_func, max_step: float = 1200, restart: bool = True): """ :param return_data: what data should we return? 'action', 'image', 'next_image', 'done', 'reward' :return: """ import collections Data = collections.namedtuple('Data', 'action image reward done next_image') assert self.race is not None, "You need to start the case before the rollout" if restart: self.race.restart() self.race.step() action = pystk.Action() result = [] state = pystk.WorldState() state.update() i = 1 * np.asarray(self.race.render_data[0].image) total_reward = 0. for it in range(max_step): state = pystk.WorldState() state.update() a = drive(i) self.race.step(a) next_i = 1 * np.asarray(self.race.render_data[0].image) d = (it + 1 == max_step) # Feel free to experiment with this r = reward_func(state) total_reward += r result.append( Data(image=i, action=a, reward=r, next_image=next_i, done=d)) i = next_i if d: break return result, total_reward
def __call__(self, s, v): cv2.imshow('s', cv2.cvtColor(s, cv2.COLOR_BGR2RGB)) key = cv2.waitKey(1) import time time.sleep(1.0 / 10.0) action = pystk.Action() action.steer = int(key == 97) * -1.0 + int(key == 100) * 1.0 action.acceleration = np.clip(5 + int(action.steer == 0) * 90.0 - v, 0, 0.5) return action, 0, 1.0
def play(self, save=None, max_frames=50): state = pystk.WorldState() if save is not None: import PIL.Image import os if not os.path.exists(save): os.makedirs(save) for t in range(max_frames): print('\rframe %d' % t, end='\r') state.update() list_info = [] list_actions = [] for i, p in enumerate(self.active_players): player = state.players[i] image = np.array(self.k.render_data[i].image) action = pystk.Action() player_action = p(image, player) for a in player_action: setattr(action, a, player_action[a]) list_actions.append(action) if save is not None: PIL.Image.fromarray(image).save( os.path.join(save, 'player%02d_%05d.png' % (i, t))) # self.uis[players.kart.player_id].show(self.k.render_data[players.kart.player_id]) # list_info = [] s = self.k.step(list_actions) if not s: # Game over break # step 2 viz # for ui, d in zip(self.uis, self.k.render_data): # ui.show(d) if save is not None: import subprocess for i, p in enumerate(self.active_players): dest = os.path.join(save, 'player%02d' % i) output = save + '_player%02d.mp4' % i subprocess.call([ 'ffmpeg', '-y', '-framerate', '10', '-i', dest + '_%05d.png', output ]) if hasattr(state, 'soccer'): return state.soccer.score return state.soccer_score
def step(self, action): self.curr_iter += 1 steer_dir = action[0] gas = action[1] brake = action[2] fire = action[3] #rescue = action[1] action = pystk.Action(steer=steer_dir, acceleration=gas, brake=brake, fire=fire) # , rescue=rescue) self.race.step(action) self.race.step(action) self.race.step(action) self.race.step(action) self.race.step(action) self.race.step(action) self.race.step(action) state = pystk.WorldState() state.update() scores = state.ffa.scores kart = state.players[0].kart rank = sorted(scores, reverse=True).index(scores[kart.id]) score = {0: 10, 1: 8, 2: 6}.get(rank, 7 - rank) reward = score / 10 inst = np.asarray(self.race.render_data[0].instance) >> 24 img = np.asarray(self.race.render_data[0].image) / 255 #i = np.concatenate((img,inst[..., np.newaxis]), axis=2) i = img new_distance = state.karts[0].distance_down_track delta = new_distance - self.prev_distance is_stuck = (self.curr_iter > 10) and delta < 0.001 done = (self.curr_iter == self.max_step) or is_stuck # if self.curr_iter % 6 == 0: self.prev_distance = new_distance # return <obs>, <reward: float>, <done: bool>, <info: dict> return i, reward, done, {}
def control(aim_point, current_vel): """ Set the Action for the low-level controller :param aim_point: Aim point, in screen coordinate frame [-1..1] :param current_vel: Current velocity of the kart :return: a pystk.Action (set acceleration, brake, steer, drift) """ current_y_location = 0 current_x_location = 0 aim_x_location = aim_point[0] aim_y_location = aim_point[1] action = pystk.Action() action.acceleration = 1 * (1 - abs(aim_x_location)) if aim_x_location < 0: action.steer = -1 elif aim_x_location > 0: action.steer = 1 else: action.steer = 0 if aim_x_location < -0.4: action.drift = True elif aim_x_location > 0.4: action.drift = True else: action.drift = False if current_vel > 18 and abs(aim_x_location) > abs(0.5): action.acceleration = 0 action.brake = True if current_vel > 20: action.acceleration = 0 """ Your code here Hint: Use action.acceleration (0..1) to change the velocity. Try targeting a target_velocity (e.g. 20). Hint: Use action.brake to True/False to brake (optionally) Hint: Use action.steer to turn the kart towards the aim_point, clip the steer angle to -1..1 Hint: You may want to use action.drift=True for wide turns (it will turn faster) """ return action
def control(aim_point, current_vel): """ Set the Action for the low-level controller :param aim_point: Aim point, in local coordinate frame :param current_vel: Current velocity of the kart :return: a pystk.Action (set acceleration, brake, steer, drift) """ action = pystk.Action() action.acceleration = 1 direction = aim_point[0] height = aim_point[1] distance = aim_point[2] """ Your code here Hint: Use action.acceleration (0..1) to change the velocity. Try targeting a target_velocity (e.g. 20). Hint: Use action.brake to True/False to brake (optionally) Hint: Use action.steer to turn the kart towards the aim_point, clip the steer angle to -1..1 Hint: You may want to use action.drift=True for wide turns (it will turn faster) """ direction = aim_point[0].item() if (-1 < direction > 1): action.drift = True if (current_vel > 20): action.brake = True action.acceleration = 0 else: action.brake = False if (direction > 1): direction = 1 if (direction < -1): direction = -1 if (.01 < direction > -.01) and distance > 15: action.nitro = True else: action.nitro = False # print() action.steer = direction return action
def control(aim_point, current_vel): """ Set the Action for the low-level controller :param aim_point: Aim point, in screen coordinate frame [-1..1] :param current_vel: Current velocity of the kart :return: a pystk.Action (set acceleration, brake, steer, drift) """ action = pystk.Action() """ Your code here Hint: Use action.acceleration (0..1) to change the velocity. Try targeting a target_velocity (e.g. 20). Hint: Use action.brake to True/False to brake (optionally) Hint: Use action.steer to turn the kart towards the aim_point, clip the steer angle to -1..1 Hint: You may want to use action.drift=True for wide turns (it will turn faster) """ action.nitro = (aim_point[1] < -0.1) action.steer = 1 if (aim_point[0] > 0) else -1 action.acceleration = 1 if (current_vel < 20) else 0 action.brake = (NP.abs(aim_point[0]) > 0.80) action.drift = (NP.abs(aim_point[0]) > 0.80) return action
def play(self, save=None, max_frames=50, save_callback=None): global SAVE_COUNT, SAVE_AT state = pystk.WorldState() if save is not None: import PIL.Image if not os.path.exists(save): os.makedirs(save) # turn on interactive mode for controller visualization plt.ion() for t in range(max_frames): print('\rframe %d' % t, end='\r') state.update() list_actions = [] for i, p in enumerate(self.active_players): HACK_DICT['race'] = self.k HACK_DICT['render_data'] = self.k.render_data[i] HACK_DICT['kart'] = state.karts[i] HACK_DICT['state'] = state #print('kart: ', state.karts[i].name, '\t\t max_steer: ', "{:.4f}".format(state.karts[i].max_steer_angle)) player = state.players[i] image = np.array(self.k.render_data[i].image) action = pystk.Action() player_action = p(image, player) for a in player_action: setattr(action, a, player_action[a]) list_actions.append(action) if save is not None: im = PIL.Image.fromarray(image) #draw = ImageDraw.Draw(im) #draw.text((0, 0),"Sample Text",(255,255,255)) im.save(os.path.join(save, 'player%02d_%05d.png' % (i, t))) for i, action in enumerate(list_actions): HACK_DICT['player_%d' % i] = { 'steer': action.steer, 'acceleration': action.acceleration } if save_callback is not None: save_callback(self.k, state, t, HACK_DICT) SAVE_COUNT += 1 s = self.k.step(list_actions) if not s: # Game over break if save is not None: import subprocess for i, p in enumerate(self.active_players): dest = os.path.join(save, 'player%02d' % i) output = 'videos/_player%02d.mp4' % i subprocess.call([ 'ffmpeg', '-y', '-framerate', '10', '-i', dest + '_%05d.png', output ]) if hasattr(state, 'soccer'): return state.soccer.score return state.soccer_score
def __init__(self, visualization_type: VT): self.visualization_type = visualization_type self.current_action = pystk.Action() self.visible = False self.pause = False
def play(self, save=None, max_frames=50): state = pystk.WorldState() import PIL.Image import os prev_score = [0, 0] for t in range(max_frames): #print('\rframe %d' % t, end='\r') state.update() #print some info test = True if test: #print('soccer ball location', state.soccer.ball.location) #print('soccer ball location', state.soccer.ball.location[1]) #print('soccer ball location', state.soccer.ball.location[2]) #print('kart 1 location', state.karts[0].location) #print('kart 2 location', state.karts[1].location) #print('kart 3 location', state.karts[2].location) #print('kart 4 location', state.karts[3].location) #print('score', state.soccer.score) if prev_score != state.soccer.score: print("Scored: ", state.soccer.score, " at frame ", t) prev_score = state.soccer.score """ goal = state.soccer.goal_line[0][0] print('Goal Poistion: ', goal[0],",", goal[1], ",",goal[2]) goal1 = state.soccer.goal_line[0][1] print('Goal Poistion: ', goal1[0],",", goal1[1], ",",goal1[2]) """ #for kart in state.karts: #print(kart.location) #print('goal line', state.soccer.goal_line[0][0]) #print('goal line', state.soccer.goal_line[0][0][0]) #print('goal line', state.soccer.goal_line[0][0][1]) #print('goal line', state.soccer.goal_line[0][0][2]) #print("Soccer location: ", state.soccer.ball.location) #aim_point_car = self._to_kart(np.array(state.soccer.ball.location), state.karts[0]) #print("Soccer location to car: ", aim_point_car ) list_actions = [] for i, p in enumerate(self.active_players): player = state.players[i] image = np.array(self.k.render_data[i].image) action = pystk.Action() #print('soccer ball location', state.soccer.ball.location) #player_action = p( state.soccer, player) player_action = p(image, player, state.soccer.ball.location) for a in player_action: setattr(action, a, player_action[a]) list_actions.append(action) proj = np.array(player.camera.projection).T view = np.array(player.camera.view).T # print(state.players) #Generate labels for training sets aim_point_world_soccer = state.soccer.ball.location aps = self._to_image(aim_point_world_soccer, proj, view) #print(state.soccer.goal_line ) list = [[ state.soccer.ball.size / 2, 0.18, state.soccer.ball.size / 2 ], [ state.soccer.ball.size / 2, 0.18, -state.soccer.ball.size / 2 ], [ state.soccer.ball.size / 2, -0.18, state.soccer.ball.size / 2 ], [ state.soccer.ball.size / 2, -0.18, -state.soccer.ball.size / 2 ], [ -state.soccer.ball.size / 2, 0.18, state.soccer.ball.size / 2 ], [ -state.soccer.ball.size / 2, 0.18, -state.soccer.ball.size / 2 ], [ -state.soccer.ball.size / 2, -0.18, state.soccer.ball.size / 2 ], [ -state.soccer.ball.size / 2, -0.18, -state.soccer.ball.size / 2 ]] width1 = 400 width2 = 0 height1 = 300 height2 = 0 for item in list: aps_flex = self._to_image([ aim_point_world_soccer[0] + item[0], aim_point_world_soccer[1] + item[1], aim_point_world_soccer[2] + item[2] ], proj, view) if width1 > aps_flex[0]: width1 = aps_flex[0] if width2 < aps_flex[0]: width2 = aps_flex[0] if height1 > aps_flex[1]: height1 = aps_flex[1] if height2 < aps_flex[1]: height2 = aps_flex[1] player_x = player.kart.location[0] player_y = player.kart.location[2] front_x = player.kart.front[0] front_y = player.kart.front[2] ball_x = aim_point_world_soccer[0] ball_y = aim_point_world_soccer[2] vector_self = [front_x - player_x, front_y - player_y] vector_ball = [ball_x - player_x, ball_y - player_y] dot_prod = vector_self[0] * vector_ball[0] + vector_self[ 1] * vector_ball[1] if dot_prod > 0 and 0 <= aps[ 0] < self.graphics_config.screen_width and 0 <= aps[ 1] < self.graphics_config.screen_height and -30 < player.kart.location[ 0] < 30 and -55 < player.kart.location[2] < 55: if (width2 - width1) * (height2 - height1) > 40: # print([[width1, height1, width2, height2]]) PIL.Image.fromarray(image).save( os.path.join('dense_data/train/', 'player%02d_%05d.png' % (i, t))) np.savez(os.path.join('dense_data/train/', 'player%02d_%05d' % (i, t)) + '.npz', puck=[[width1, height1, width2, height2]]) ''' # with open(os.path.join(save, 'player%02d_%05d' % (i, t)) + '.csv', 'w') as f: # f.write('%0.3f,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f' % ( # state.soccer.ball.location[0],state.soccer.ball.location[1], state.soccer.ball.location[2], # state.soccer.goal_line[0][0][0], state.soccer.goal_line[0][0][1], state.soccer.goal_line[0][0][2] # )) ''' ''' f.write('%0.3f %0.3f %0.3f %0.3f %0.3f %0.3f' %tuple(state.soccer.ball.location), tuple(state.soccer.goal_line[0][0]))) ''' s = self.k.step(list_actions) if not s: # Game over break if save is not None: import subprocess for i, p in enumerate(self.active_players): dest = os.path.join(save, 'player%02d' % i) output = save + '_player%02d.mp4' % i subprocess.call([ 'ffmpeg', '-y', '-framerate', '10', '-i', dest + '_%05d.png', output ]) if hasattr(state, 'soccer'): return state.soccer.score return state.soccer_score
reward_func, max_step=1000, )) ray.get(starts) trajectories = [] rewards = [] for _ in range(iterations): ro = ray.get(rollouts) for traj, reward in ro: trajectories.extend(traj) rewards.append(reward) return trajectories, rewards if __name__ == '__main__': reward_func = lambda s: np.linalg.norm(s.karts[0].velocity) agent = lambda i: pystk.Action(steer=np.random.uniform(low=-1, high=1), acceleration=1) ray.init(logging_level=40) trajs, rewards = get_rollouts(4, agent, reward_func) print('Average episode reward: %.2f ± %.2f' % (np.mean(rewards), np.std(rewards)))
def rollout( self, policy: policy.BasePolicy, max_step: float = 100, frame_skip: int = 0, gamma: float = 1.0): self.race.restart() self.race.step(pystk.Action()) self.track.update() result = list() state = pystk.WorldState() state.update() r_total = 0 d = state.karts[0].distance_down_track s = np.array(self.race.render_data[0].image) off_track = deque(maxlen=20) traveled = deque(maxlen=50) for it in range(max_step): # Early termination. if it > 20 and (np.median(traveled) < 0.05 or all(off_track)): break v = np.linalg.norm(state.karts[0].velocity) action, action_i, p_action = policy(s, v) if isinstance(action, pystk.Action): action_raw = [action.steer, action.acceleration, action.drift] else: action_raw = action action = pystk.Action() action.steer = action_raw[0] action.acceleration = np.clip(action_raw[1] - v, 0, np.inf) action.drift = action_raw[2] > 0.5 for _ in range(1 + frame_skip): self.race.step(action) self.track.update() state = pystk.WorldState() state.update() s_p = np.array(self.race.render_data[0].image) d_new = min(state.karts[0].distance_down_track, d + 5.0) node_idx = np.searchsorted( self.track.path_distance[:, 1], d_new % self.track.path_distance[-1, 1]) % len(self.track.path_nodes) a_b = self.track.path_nodes[node_idx] distance = point_from_line(state.karts[0].location, a_b[0], a_b[1]) distance_traveled = get_distance(d_new, d, self.track.path_distance[-1, 1]) gain = distance_traveled if distance_traveled > 0 else 0 mult = int(distance < 6.0) traveled.append(gain) off_track.append(distance > 6.0) r_total = max(r_total, d_new * mult) r = np.clip(0.5 * max(mult * gain, 0) + 0.5 * mult, -1.0, 1.0) result.append( Data( s.copy(), np.float32(action_raw), np.uint8([action_i]), np.float32([p_action]), np.float32([r]), s_p.copy(), np.float32([np.nan]), np.float32([0]))) d = d_new s = s_p G = 0 # Ugly. for i, data in enumerate(reversed(result)): G = data.r + gamma * G result[-(i + 1)] = Data( data.s, data.a, data.a_i, data.p_a, data.r, data.sp, np.float32([G]), np.float32([i == 0])) # HACK PLEASE REMEMBER THIS return result[4:], r_total / self.track.path_distance[-1, 1]
if not race_config.render: config = pystk.GraphicsConfig.ld() config.screen_width = 320 config.screen_height = 240 pystk.init(config) race = pystk.Race(race_config) race.start() race.step() for a, s in random_sa: w = pystk.WorldState() w.update() s_vec.append(w) race.step(pystk.Action(acceleration=a, steer=s)) race.stop() del race pystk.clean() state_names = list(states) for i in range(len(states[state_names[0]])): mismatch = False for k, n in enumerate(state_names): for m in state_names[k + 1:]: try: recursive_cmp(states[n][i], states[m][i], 'state[%d]' % i) except AssertionError as e: print('State mismatch between %s and %s' % (n, m)) print(e.args) mismatch = True
uis = [ gui.UI([gui.VT[x] for x in args.visualization]) for i in range(num_player) ] save_depth = "DEPTH" in args.visualization save_labels = "SEMANTIC" in args.visualization or "INSTANCE" in args.visualization state = pystk.WorldState() t0 = time() n = 0 while (n < args.steps) and ((not args.display) or all(ui.visible for ui in uis)): if (not args.display) or (not all(ui.pause for ui in uis)): #race.step(uis[0].current_action) action = pystk.Action() action.acceleration = 1.0 action.steer = np.random.uniform(-1, 1) race.step(action) state.update() if args.verbose and config.mode == config.RaceMode.SOCCER: print('Score ', state.soccer.score) print(' ', state.soccer.ball) print(' ', state.soccer.goal_line) if (args.display): for ui, d in zip(uis, race.render_data): ui.show(d) if args.save_dir: