Пример #1
0
def check_map():
    from PIL import Image
    from pathlib import Path
    home = str(Path.home())
    game = DeepSeaTreasure(width=5,
                           render=True,
                           graphical_state=True,
                           speed=10)
    num_of_actions = game.get_num_of_actions()
    game.reset()
    full_path = home + '/Desktop/Images/'
    count = 0

    while True:
        random_action = np.random.randint(0, num_of_actions)
        reward = game.step(random_action)
        print(reward)
        next_state = Utils.process_state(game.get_state())
        is_terminal = game.is_terminal()

        # save state and map
        #count = count + 1
        #img = Image.fromarray(next_state, 'L')
        #img.save(full_path + str(count) +'.png')

        if is_terminal:
            print("Total Score", game.total_score)
            game.reset()
Пример #2
0
 def __draw_score(self):
     total_score = self.font.render(
         str(self.total_score) + " " + str(self.total_score_2), False,
         Utils.get_color(Utils.WHITE))
     self.screen.blit(total_score,
                      (self.width / 2 - total_score.get_width() / 2,
                       self.height - total_score.get_height() * 2))
Пример #3
0
    def __draw_score(self):

        total_score = self.font.render(
            str(self.total_score) + " " + str(self.total_score_2) + " " +
            str(self.total_score_3), False, Utils.get_color(Utils.BLACK))
        self.screen.blit(total_score,
                         (self.screen_size / 2 - total_score.get_width() / 2,
                          self.screen_size - total_score.get_height() * 2))
Пример #4
0
 def __calculate_fps(self):
     self.frames_count = self.frames_count + 1
     if self.max_frames > 0:
         if self.frames_count > self.max_frames:
             self.end_of_game = True
     current_time = Utils.get_current_time()
     if current_time > self.started_time:
         self.frame_speed = self.frames_count / (current_time -
                                                 self.started_time)
     else:
         self.frame_speed = 0
Пример #5
0
    def reset(self):
        for sprite in self.sprites:
            sprite.kill()

        self.pos = self.default_init_pos
        self.vel = self.default_init_vel
        if self.rand_starts:
            rng = np.random.RandomState()
            rand_start_pos = rng.uniform(low=-0.6, high=-0.4)
            self.pos = rand_start_pos
            self.vel = 0

        self.__generate_car()

        self.__generate_goal()

        if self.is_debug:
            interval = Utils.get_current_time() - self.started_time
            print("#################  RESET GAME  ##################")
            print("Episode terminated after:", interval, "(s)")
            print("Total score:", self.total_score)
            print("#################################################")

        self.rewards_2.clear()
        self.rewards_3.clear()
        self.total_score = 0
        self.total_score_2 = 0
        self.total_score_3 = 0
        self.step_count = 0
        self.__render()

        if self.graphical_state:
            for _ in range(self.frame_stack):
                self.frames.append(self.get_state())
            return LazyFrames(list(self.frames), False)
        else:
            return self.get_state()
Пример #6
0
    def __init__(self,
                 min_pos=-1.2,
                 max_pos=0.6,
                 min_vel=-0.07,
                 max_vel=0.07,
                 goal_pos=0.5,
                 acceleration=0.001,
                 gravity_factor=-0.0025,
                 hill_peak_freq=3.0,
                 default_init_pos=-0.5,
                 default_init_vel=0.0,
                 reward_per_step=-1,
                 reward_at_goal=0,
                 random_starts=False,
                 transition_noise=0.,
                 seed=None,
                 render=True,
                 speed=60,
                 is_debug=False,
                 frame_skip=1,
                 friction=0,
                 graphical_state=False,
                 discrete_states=6,
                 episode_limit=200,
                 frame_stack=4,
                 screen_width=84,
                 screen_height=84):
        self.vel = default_init_vel
        self.pos = default_init_pos
        self.min_pos = min_pos
        self.max_pos = max_pos
        self.min_vel = min_vel
        self.max_vel = max_vel
        self.goal_pos = goal_pos
        self.acc = acceleration
        self.gra = gravity_factor
        self.hill_peak_freq = hill_peak_freq
        self.default_init_pos = default_init_pos
        self.default_init_vel = default_init_vel
        self.reward_per_step = reward_per_step
        self.reward_at_goal = reward_at_goal
        self.rand_starts = random_starts
        self.trans_noise = transition_noise
        self.last_action = 0
        self.rd = render
        self.speed = speed

        self.frame_stack = frame_stack
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.frames = deque(maxlen=self.frame_stack)

        self.num_of_actions = 3
        self.num_of_objs = 3
        self.screen_size = 500
        self.current_path = os.path.dirname(os.path.abspath(__file__))
        self.car_width = 60
        self.car_height = 30
        self.sprites = pygame.sprite.Group()
        self.frames_count = 0
        self.step_count = 0
        self.max_frames = 10000
        self.is_debug = is_debug
        self.frame_speed = 0
        self.log_freq = 500
        self.started_time = Utils.get_current_time()
        self.frame_skip = frame_skip
        self.current_buffer = np.array([[[0, 0, 0]
                                         for _ in range(self.screen_size)]
                                        for _ in range(self.screen_size)])
        self.mountain = []
        self.surface = pygame.Surface((self.screen_size, self.screen_size))
        self.friction = friction
        self.rewards_2 = cl.deque(maxlen=100)
        self.rewards_3 = cl.deque(maxlen=100)
        self.total_score = 0
        self.total_score_2 = 0
        self.total_score_3 = 0
        self.graphical_state = graphical_state
        self.max_states_per_dim = discrete_states
        self.episode_limit = episode_limit

        if seed is None or seed < 0 or seed >= 9999:
            if seed is not None and (seed < 0 or seed >= 9999):
                print("Invalid seed ! Default seed = randint(0, 9999")
            self.seed = np.random.randint(0, 9999)
            self.random_seed = True
        else:
            self.random_seed = False
            self.seed = seed
            np.random.seed(seed)

        # Initialize
        self.__init_pygame_engine()

        # Create mountain
        self.__draw_sine_wave()

        # Create player
        self.__generate_car()

        # Create goal
        self.__generate_goal()

        # Render the first frame
        self.__render()