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 _test(self, track, max_frames): config = pystk.RaceConfig(num_kart=1, laps=1) config.track = track config.players[ 0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL config.render = self.P is not None k = pystk.Race(config) try: state = pystk.WorldState() track = pystk.Track() k.start() k.step() last_rescue = 0 for t in range(max_frames): state.update() track.update() kart = state.players[0].kart if kart.race_result: break if self.P is None: aim_point_world = self._point_on_track( kart.distance_down_track + TRACK_OFFSET, track) else: image = np.array(k.render_data[0].image) aim_point_image = self.P( TF.to_tensor(image)[None]).squeeze(0) proj = np.array(state.players[0].camera.projection).T view = np.array(state.players[0].camera.view).T aim_point_world = self._to_world(aim_point_image, proj, view, kart.location[1]) aim_point_car = self._to_kart(aim_point_world, kart) current_vel = np.linalg.norm(kart.velocity) action = self.C(aim_point_car, current_vel) if current_vel <= 1.0 and t - last_rescue > RESCUE_TIMEOUT: action.rescue = True last_rescue = t k.step(action) finally: k.stop() del k if kart.race_result: return 1, '%0.1f s' % kart.finish_time return kart.overall_distance / track.length, '%0.1f%% done' % ( 100 * kart.overall_distance / track.length)
def _race(self, drive, track_name, time_limit=1200): distance = 0. total_length = 1.0 try: config = pystk.RaceConfig(laps=1) config.players[0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL config.track = track_name config.step_size = 0.1 race = pystk.Race(config) race.start() race.step() track = pystk.Track() track.update() total_length = track.length for t in range(time_limit): state = pystk.WorldState() state.update() kart = state.players[0].kart if kart.race_result: distance = total_length break distance = state.karts[0].distance_down_track image = 1*np.asarray(race.render_data[0].image) action = drive(image) race.step(action) finally: race.stop() del race return distance / total_length
def start(self, config: pystk.RaceConfig = None, track_name='lighthouse'): if config is None: config = pystk.RaceConfig() config.players[ 0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL config.track = track_name config.step_size = 0.1 if self.race is not None: self.race.stop() del self.race self.config = config self.race = pystk.Race(config) self.race.start() self.track = pystk.Track() self.track.update()
def __init__(self, track): config = pystk.GraphicsConfig.ld() config.screen_width = 64 config.screen_height = 64 config.render_window = False pystk.init(config) race_config = pystk.RaceConfig() race_config.players[0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL race_config.track = track race_config.step_size = 0.1 race_config.render = True self.race = pystk.Race(race_config) self.race.start() self.race.step() self.track = pystk.Track() self.track.update()
def __init__(self, track: str): config = pystk.GraphicsConfig.ld() config.screen_width = 64 config.screen_height = 64 config.render_window = False pystk.init(config) race_config = pystk.RaceConfig() race_config.num_kart = 4 race_config.players[ 0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL race_config.players[0].team = 0 race_config.players[ 1].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL race_config.players[1].team = 0 race_config.players[ 2].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL race_config.players[2].team = 1 race_config.players[ 3].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL race_config.players[2].team = 1 race_config.track = track race_config.step_size = 0.1 race_config.render = False race_config.mode = race_config.RaceMode.THREE_STRIKES self.race = pystk.Race(race_config) self.race.start() self.race.step() self.track = pystk.Track() self.track.update()
def rollout(self, track, controller, planner=None, max_frames=1000, verbose=False, data_callback=None): """ Play a level (track) for a single round. :param track: Name of the track :param controller: low-level controller, see controller.py :param planner: high-level planner, see planner.py :param max_frames: Maximum number of frames to play for :param verbose: Should we use matplotlib to show the agent drive? :param data_callback: Rollout calls data_callback(time_step, image, 2d_aim_point) every step, used to store the data :return: Number of steps played """ do_render = verbose or planner is not None or data_callback is not None if self.k is not None and self.k.config.track == track and do_render == self.k.config.render: self.k.restart() self.k.step() else: if self.k is not None: self.k.stop() del self.k config = pystk.RaceConfig(num_kart=1, laps=1, render=do_render, track=track) config.players[ 0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL self.k = pystk.Race(config) self.k.start() self.k.step() state = pystk.WorldState() track = pystk.Track() last_rescue = 0 if verbose: import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) for t in range(max_frames): state.update() track.update() kart = state.players[0].kart if np.isclose(kart.overall_distance / track.length, 1.0, atol=2e-3): if verbose: print("Finished at t=%d" % t) break proj = np.array(state.players[0].camera.projection).T view = np.array(state.players[0].camera.view).T if data_callback is not None: aim_point_world = self._point_on_track( kart.distance_down_track + 20, track) ap = self._to_image(aim_point_world, proj, view) if 0 <= ap[0] < self.config.screen_width and 0 <= ap[ 1] < self.config.screen_height: data_callback(t, np.array(self.k.render_data[0].image), ap) if planner: image = np.array(self.k.render_data[0].image) aim_point_image = planner(TF.to_tensor(image)[None]).squeeze(0) aim_point_world = self._to_world(aim_point_image, proj, view, kart.location[1]) else: aim_point_world = self._point_on_track( kart.distance_down_track + TRACK_OFFSET, track) aim_point_car = self._to_kart(aim_point_world, kart) current_vel = np.linalg.norm(kart.velocity) action = controller(aim_point_car, current_vel) if current_vel < 1.0 and t - last_rescue > RESCUE_TIMEOUT: last_rescue = t action.rescue = True if verbose: ax.clear() ax.imshow(self.k.render_data[0].image) ax.add_artist( plt.Circle(self._to_image(kart.location, proj, view), 2, ec='b', fill=False, lw=1.5)) ax.add_artist( plt.Circle(self._to_image(aim_point_world, proj, view), 2, ec='r', fill=False, lw=1.5)) if planner: ap = self._point_on_track( kart.distance_down_track + TRACK_OFFSET, track) ax.add_artist( plt.Circle(self._to_image(ap, proj, view), 2, ec='g', fill=False, lw=1.5)) plt.pause(1e-3) self.k.step(action) t += 1 return t
print(o) s = dumps(o) print(len(s)) print(loads(s)) test(pystk.GraphicsConfig.ld()) test(pystk.RaceConfig()) pystk.init(pystk.GraphicsConfig.ld()) config = pystk.RaceConfig() k = pystk.Race(config) k.start() state = pystk.WorldState() track = pystk.Track() for it in range(10): k.step() state.update() track.update() test(state) test(track) test(k.last_action) test(k.render_data) k.stop() del k pystk.clean()
def rollout(self, track, controller, planner=None, max_frames=1000, verbose=False, data_callback=None): """ Play a level (track) for a single round. :param track: Name of the track :param controller: low-level controller, see controller.py :param planner: high-level planner, see planner.py :param max_frames: Maximum number of frames to play for :param verbose: Should we use matplotlib to show the agent drive? :param data_callback: Rollout calls data_callback(time_step, image, 2d_aim_point) every step, used to store the data :return: Number of steps played """ if self.k is not None and self.k.config.track == track: self.k.restart() self.k.step() else: if self.k is not None: self.k.stop() del self.k config = pystk.RaceConfig(num_kart=1, laps=1, render=True, track=track) config.players[ 0].controller = pystk.PlayerConfig.Controller.PLAYER_CONTROL self.k = pystk.Race(config) self.k.start() self.k.step() state = pystk.WorldState() track = pystk.Track() last_rescue = 0 if verbose and not ON_COLAB: import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) elif verbose and ON_COLAB: global COLAB_IMAGES COLAB_IMAGES = list() for t in range(max_frames): state.update() track.update() kart = state.players[0].kart if np.isclose(kart.overall_distance / track.length, 1.0, atol=2e-3): if verbose: print("Finished at t=%d" % t) break proj = np.array(state.players[0].camera.projection).T view = np.array(state.players[0].camera.view).T aim_point_world = self._point_on_track( kart.distance_down_track + TRACK_OFFSET, track) aim_point_image = self._to_image(aim_point_world, proj, view) aim_point = aim_point_image if data_callback is not None: data_callback(t, np.array(self.k.render_data[0].image), aim_point_image) if planner: image = np.array(self.k.render_data[0].image) aim_point = planner(TF.to_tensor(image)[None]).squeeze( 0).cpu().detach().numpy() current_vel = np.linalg.norm(kart.velocity) action = controller(aim_point, current_vel) if current_vel < 1.0 and t - last_rescue > RESCUE_TIMEOUT: last_rescue = t action.rescue = True if verbose and not ON_COLAB: ax.clear() ax.imshow(self.k.render_data[0].image) WH2 = np.array( [self.config.screen_width, self.config.screen_height]) / 2 ax.add_artist( plt.Circle(WH2 * (1 + self._to_image(kart.location, proj, view)), 2, ec='b', fill=False, lw=1.5)) ax.add_artist( plt.Circle( WH2 * (1 + self._to_image(aim_point_world, proj, view)), 2, ec='r', fill=False, lw=1.5)) if planner: ap = self._point_on_track( kart.distance_down_track + TRACK_OFFSET, track) ax.add_artist( plt.Circle(WH2 * (1 + aim_point_image), 2, ec='g', fill=False, lw=1.5)) plt.pause(1e-3) elif verbose and ON_COLAB: from PIL import Image, ImageDraw image = Image.fromarray(self.k.render_data[0].image) draw = ImageDraw.Draw(image) WH2 = np.array( [self.config.screen_width, self.config.screen_height]) / 2 p = (aim_point_image + 1) * WH2 draw.ellipse((p[0] - 2, p[1] - 2, p[0] + 2, p[1] + 2), fill=(255, 0, 0)) if planner: p = (aim_point + 1) * WH2 draw.ellipse((p[0] - 2, p[1] - 2, p[0] + 2, p[1] + 2), fill=(0, 255, 0)) COLAB_IMAGES.append(np.array(image)) self.k.step(action) t += 1 if verbose and ON_COLAB: show_on_colab() return t, kart.overall_distance / track.length